diff --git a/lib/fakefs/file.rb b/lib/fakefs/file.rb index c608000..0d81d0b 100644 --- a/lib/fakefs/file.rb +++ b/lib/fakefs/file.rb @@ -108,7 +108,7 @@ module FakeFS @file = FileSystem.find(path) @open = true - check_file_existence! if read_only? + check_file_existence! if !file_creation_mode? end def close @@ -157,6 +157,14 @@ module FakeFS @mode == READ_ONLY end + def file_creation_modes + MODES - [READ_ONLY, READ_WRITE] + end + + def file_creation_mode? + file_creation_modes.include?(@mode) + end + def check_mode(mode) if !MODES.include?(mode) raise ArgumentError, "illegal access mode #{mode}" diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index c562cc0..fd444ef 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -120,6 +120,12 @@ class FakeFSTest < Test::Unit::TestCase end end + def test_raises_error_when_cannot_find_file_in_read_write_mode + assert_raises(Errno::ENOENT) do + File.open("does_not_exist", "r+") + end + end + def test_can_read_files_once_written path = '/path/to/file.txt' File.open(path, 'w') do |f|