diff --git a/lib/fakefs/file.rb b/lib/fakefs/file.rb index a14c6b9..db14ef3 100644 --- a/lib/fakefs/file.rb +++ b/lib/fakefs/file.rb @@ -100,7 +100,9 @@ module FakeFS end attr_reader :path - def initialize(path, mode = nil, perm = nil) + def initialize(path, mode = READ_ONLY, perm = nil) + check_mode(mode) + @path = path @mode = mode @file = FileSystem.find(path) @@ -146,5 +148,11 @@ module FakeFS def read_only? @mode == READ_ONLY end + + def check_mode(mode) + if !MODES.include?(mode) + raise ArgumentError, "illegal access mode #{mode}" + end + end end end diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index 5b9d69d..22b08cc 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -106,6 +106,14 @@ class FakeFSTest < Test::Unit::TestCase end end + def test_file_opens_in_invalid_mode + FileUtils.touch("foo") + + assert_raises(ArgumentError) do + File.open("foo", "an_illegal_mode") + end + end + def test_can_read_files_once_written path = '/path/to/file.txt' File.open(path, 'w') do |f|