From 6a2c03aa84d799a41d2acf0a59e1c5505fdd5a01 Mon Sep 17 00:00:00 2001 From: Pat Nakajima Date: Thu, 11 Jun 2009 18:11:44 -0400 Subject: [PATCH] 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