Raise an IOError when a file is read in a write-only mode.

This commit is contained in:
Scott Taylor 2009-09-30 03:28:33 -04:00
parent 0b7d45b985
commit 3a5098092c
2 changed files with 26 additions and 1 deletions

View File

@ -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

View File

@ -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|