diff --git a/lib/fakefs/file.rb b/lib/fakefs/file.rb index db14ef3..c608000 100644 --- a/lib/fakefs/file.rb +++ b/lib/fakefs/file.rb @@ -107,6 +107,8 @@ module FakeFS @mode = mode @file = FileSystem.find(path) @open = true + + check_file_existence! if read_only? end def close @@ -145,6 +147,12 @@ module FakeFS private + def check_file_existence! + unless @file + raise Errno::ENOENT, "No such file or directory - #{@file}" + end + end + def read_only? @mode == READ_ONLY end diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index 22b08cc..c562cc0 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -114,6 +114,12 @@ class FakeFSTest < Test::Unit::TestCase end end + def test_raises_error_when_cannot_find_file_in_read_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|