From bf6f12160192bd0ba67610831c248cf4e836970d Mon Sep 17 00:00:00 2001 From: Pat Nakajima Date: Thu, 11 Jun 2009 18:11:44 -0400 Subject: [PATCH 1/4] Fix FileUtils.cp behavior. --- lib/fakefs.rb | 11 ++++++----- test/fakefs_test.rb | 15 +++++++++++---- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/fakefs.rb b/lib/fakefs.rb index dab6eed..b834af1 100644 --- a/lib/fakefs.rb +++ b/lib/fakefs.rb @@ -29,10 +29,6 @@ module FakeFS dst_file = FileSystem.find(dest) src_file = FileSystem.find(src) - if dst_file - raise Errno::EEXIST, dest - end - if !src_file raise Errno::ENOENT, src end @@ -41,7 +37,12 @@ module FakeFS raise Errno::EISDIR, src end - FileSystem.add(dest, src_file.entry.clone) + if dst_file and File.directory?(dst_file) + FileSystem.add(File.join(dest, src), src_file.entry.clone) + else + FileSystem.delete(dest) + FileSystem.add(dest, src_file.entry.clone) + end end def cp_r(src, dest) diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index 4301572..26a9038 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -304,13 +304,20 @@ class FakeFSTest < Test::Unit::TestCase assert_equal 'bar', File.read('baz') end - def test_cp_fails_on_dest_exists + def test_cp_file_into_dir File.open('foo', 'w') {|f| f.write 'bar' } FileUtils.mkdir_p 'baz' - assert_raise Errno::EEXIST do - FileUtils.cp('foo', 'baz') - end + FileUtils.cp('foo', 'baz') + assert_equal 'bar', File.read('baz/foo') + end + + def test_cp_overwrites_dest_file + File.open('foo', 'w') {|f| f.write 'FOO' } + File.open('bar', 'w') {|f| f.write 'BAR' } + + FileUtils.cp('foo', 'bar') + assert_equal 'FOO', File.read('bar') end def test_cp_fails_on_no_source From 94afbcabc14bd4d747e4101ea9e31f1ae4948c06 Mon Sep 17 00:00:00 2001 From: Pat Nakajima Date: Fri, 12 Jun 2009 17:54:32 -0400 Subject: [PATCH 2/4] Fixed File.file? for non-existent files --- lib/fakefs.rb | 3 ++- test/fakefs_test.rb | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/fakefs.rb b/lib/fakefs.rb index b834af1..54913b0 100644 --- a/lib/fakefs.rb +++ b/lib/fakefs.rb @@ -128,7 +128,8 @@ module FakeFS if path.respond_to? :entry path.entry.is_a? MockFile else - FileSystem.find(path).entry.is_a? MockFile + result = FileSystem.find(path) + result && result.entry.is_a?(MockFile) end end diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index 26a9038..36abfd7 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -133,6 +133,10 @@ class FakeFSTest < Test::Unit::TestCase assert File.file?(sympath) end + def test_knows_non_existent_files_arent_files + assert ! File.file?('does/not/exist.txt') + end + def test_can_chown_files good = 'file.txt' bad = 'nofile.txt' From 57d02ab8b2a8674045855926a5933e01f944ebd5 Mon Sep 17 00:00:00 2001 From: Pat Nakajima Date: Fri, 12 Jun 2009 18:16:16 -0400 Subject: [PATCH 3/4] More accurate test for File.file? --- lib/fakefs.rb | 2 +- test/fakefs_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/fakefs.rb b/lib/fakefs.rb index 54913b0..11f5e36 100644 --- a/lib/fakefs.rb +++ b/lib/fakefs.rb @@ -129,7 +129,7 @@ module FakeFS path.entry.is_a? MockFile else result = FileSystem.find(path) - result && result.entry.is_a?(MockFile) + result ? result.entry.is_a?(MockFile) : false end end diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index 36abfd7..058df0d 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -134,7 +134,7 @@ class FakeFSTest < Test::Unit::TestCase end def test_knows_non_existent_files_arent_files - assert ! File.file?('does/not/exist.txt') + assert_equal RealFile.file?('does/not/exist.txt'), File.file?('does/not/exist.txt') end def test_can_chown_files From d7870563aa17926960778cd157b8d94780015d11 Mon Sep 17 00:00:00 2001 From: Pat Nakajima Date: Fri, 12 Jun 2009 18:22:48 -0400 Subject: [PATCH 4/4] Fixed inconsistent File.directory? behavior --- lib/fakefs.rb | 3 ++- test/fakefs_test.rb | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/fakefs.rb b/lib/fakefs.rb index 11f5e36..550ef74 100644 --- a/lib/fakefs.rb +++ b/lib/fakefs.rb @@ -112,7 +112,8 @@ module FakeFS if path.respond_to? :entry path.entry.is_a? MockDir else - FileSystem.find(path).entry.is_a? MockDir + result = FileSystem.find(path) + result ? result.entry.is_a?(MockDir) : false end end diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index 058df0d..c32a657 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -42,6 +42,11 @@ class FakeFSTest < Test::Unit::TestCase assert File.directory?(sympath) end + def test_knows_non_existent_directories_arent_directories + path = 'does/not/exist/' + assert_equal RealFile.directory?(path), File.directory?(path) + end + def test_doesnt_overwrite_existing_directories FileUtils.mkdir_p(path = "/path/to/dir") assert File.exists?(path)