Raise an IOError when a file is read in a write-only mode.
This commit is contained in:
parent
0b7d45b985
commit
3a5098092c
@ -116,7 +116,8 @@ module FakeFS
|
||||
end
|
||||
|
||||
def read
|
||||
raise IOError.new('closed stream') unless @open
|
||||
raise IOError, 'closed stream' unless @open
|
||||
raise IOError, 'not opened for reading' if write_only?
|
||||
@file.content
|
||||
end
|
||||
|
||||
@ -172,5 +173,9 @@ module FakeFS
|
||||
@file = FileSystem.add(path, FakeFile.new)
|
||||
end
|
||||
end
|
||||
|
||||
def write_only?
|
||||
@mode == WRITE_ONLY || @mode == APPEND_WRITE_ONLY
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -146,6 +146,26 @@ class FakeFSTest < Test::Unit::TestCase
|
||||
assert File.exists?("foo")
|
||||
end
|
||||
|
||||
def test_file_in_write_only_raises_error_when_reading
|
||||
FileUtils.touch("foo")
|
||||
|
||||
f = File.open("foo", "w")
|
||||
|
||||
assert_raises(IOError) do
|
||||
f.read
|
||||
end
|
||||
end
|
||||
|
||||
def test_file_in_append_write_only_raises_error_when_reading
|
||||
FileUtils.touch("foo")
|
||||
|
||||
f = File.open("foo", "a")
|
||||
|
||||
assert_raises(IOError) do
|
||||
f.read
|
||||
end
|
||||
end
|
||||
|
||||
def test_can_read_files_once_written
|
||||
path = '/path/to/file.txt'
|
||||
File.open(path, 'w') do |f|
|
||||
|
Loading…
Reference in New Issue
Block a user