diff --git a/lib/fakefs.rb b/lib/fakefs.rb index 52ab82b..f1ffebd 100644 --- a/lib/fakefs.rb +++ b/lib/fakefs.rb @@ -193,9 +193,15 @@ module FakeFS @path = path @mode = mode @file = FileSystem.find(path) + @open = true + end + + def close + @open = false end def read + raise IOError.new('closed stream') unless @open @file.content end @@ -208,6 +214,8 @@ module FakeFS end def write(content) + raise IOError.new('closed stream') unless @open + if !File.exists?(@path) @file = FileSystem.add(path, MockFile.new) end @@ -215,6 +223,7 @@ module FakeFS @file.content += content end alias_method :print, :write + alias_method :<<, :write def flush; self; end end diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index dc20127..bc4c8f3 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -94,6 +94,14 @@ class FakeFSTest < Test::Unit::TestCase assert_equal "Yatta!", File.read(path) end + def test_can_write_to_files + path = '/path/to/file.txt' + File.open(path, 'w') do |f| + f << 'Yada Yada' + end + assert_equal 'Yada Yada', File.read(path) + end + def test_can_read_with_File_readlines path = '/path/to/file.txt' File.open(path, 'w') do |f| @@ -104,6 +112,16 @@ class FakeFSTest < Test::Unit::TestCase assert_equal ["Yatta!", "woot"], File.readlines(path) end + def test_File_close_disallows_further_access + path = '/path/to/file.txt' + file = File.open(path, 'w') + file.write 'Yada' + file.close + assert_raise IOError do + file.read + end + end + def test_can_read_from_file_objects path = '/path/to/file.txt' File.open(path, 'w') do |f| @@ -445,7 +463,7 @@ class FakeFSTest < Test::Unit::TestCase FileUtils.ln_s 'subdir', 'new' assert_equal 'works', File.open('new/nother'){|f| f.read } end - + def test_files_can_be_touched FileUtils.touch('touched_file') assert File.exists?('touched_file')