From 95c86560c0ee3d82c4c789372d0a28966af16859 Mon Sep 17 00:00:00 2001 From: Pat Nakajima Date: Fri, 17 Jul 2009 04:18:43 +0800 Subject: [PATCH 1/2] Added verify.rb to find gaps in FakeFS Signed-off-by: Chris Wanstrath --- test/verify.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 test/verify.rb diff --git a/test/verify.rb b/test/verify.rb new file mode 100644 index 0000000..9fa1ff7 --- /dev/null +++ b/test/verify.rb @@ -0,0 +1,27 @@ +# Figure out what's missing from fakefs +# +# USAGE +# +# $ ruby test/verify.rb | grep "not implemented" +require 'fakefs' +require 'test/unit' + +class FakeFSVerifierTest < Test::Unit::TestCase + (RealFile.methods - Class.new.methods).each do |name| + define_method("test #{name} class method") do + assert File.respond_to?(name), "File.#{name} not implemented" + end + end + + (RealFile.instance_methods - Enumerable.instance_methods).each do |name| + define_method("test #{name} instance method") do + assert File.instance_methods.include?(name), "File##{name} not implemented" + end + end + + (RealFileUtils.methods - Class.new.methods).each do |name| + define_method("test #{name} module method") do + assert FileUtils.respond_to?(name), "FileUtils.#{name} not implemented" + end + end +end From c93867a2fd9d1cc7dd09fc3bbe18a38e5031cb77 Mon Sep 17 00:00:00 2001 From: Pat Nakajima Date: Fri, 17 Jul 2009 04:19:03 +0800 Subject: [PATCH 2/2] Implemented File#<< and File#close Signed-off-by: Chris Wanstrath --- lib/fakefs.rb | 9 +++++++++ test/fakefs_test.rb | 20 +++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) 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')