Fix FileUtils.cp behavior.

This commit is contained in:
Pat Nakajima 2009-06-11 18:11:44 -04:00 committed by Chris Wanstrath
parent 3537c43cbe
commit 6a2c03aa84
2 changed files with 17 additions and 9 deletions

View File

@ -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)

View File

@ -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