From a8245db14599eccdc106d6bf17d40a53deed2783 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Wed, 30 Sep 2009 02:57:10 -0400 Subject: [PATCH] Raise an error when giving File.open an illegal access mode. Set default mode for File.open to READ_ONLY --- lib/fakefs/file.rb | 10 +++++++++- test/fakefs_test.rb | 8 ++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) 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|