Raise an error when giving File.open an illegal access mode. Set default mode for File.open to READ_ONLY

This commit is contained in:
Scott Taylor 2009-09-30 02:57:10 -04:00
parent 6068034c29
commit a8245db145
2 changed files with 17 additions and 1 deletions

View File

@ -100,7 +100,9 @@ module FakeFS
end end
attr_reader :path attr_reader :path
def initialize(path, mode = nil, perm = nil) def initialize(path, mode = READ_ONLY, perm = nil)
check_mode(mode)
@path = path @path = path
@mode = mode @mode = mode
@file = FileSystem.find(path) @file = FileSystem.find(path)
@ -146,5 +148,11 @@ module FakeFS
def read_only? def read_only?
@mode == READ_ONLY @mode == READ_ONLY
end end
def check_mode(mode)
if !MODES.include?(mode)
raise ArgumentError, "illegal access mode #{mode}"
end
end
end end
end end

View File

@ -106,6 +106,14 @@ class FakeFSTest < Test::Unit::TestCase
end end
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 def test_can_read_files_once_written
path = '/path/to/file.txt' path = '/path/to/file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|