Fix FileUtils.cp behavior.

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

View File

@ -29,10 +29,6 @@ module FakeFS
dst_file = FileSystem.find(dest) dst_file = FileSystem.find(dest)
src_file = FileSystem.find(src) src_file = FileSystem.find(src)
if dst_file
raise Errno::EEXIST, dest
end
if !src_file if !src_file
raise Errno::ENOENT, src raise Errno::ENOENT, src
end end
@ -41,7 +37,12 @@ module FakeFS
raise Errno::EISDIR, src raise Errno::EISDIR, src
end 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 end
def cp_r(src, dest) def cp_r(src, dest)

View File

@ -304,13 +304,20 @@ class FakeFSTest < Test::Unit::TestCase
assert_equal 'bar', File.read('baz') assert_equal 'bar', File.read('baz')
end end
def test_cp_fails_on_dest_exists def test_cp_file_into_dir
File.open('foo', 'w') {|f| f.write 'bar' } File.open('foo', 'w') {|f| f.write 'bar' }
FileUtils.mkdir_p 'baz' FileUtils.mkdir_p 'baz'
assert_raise Errno::EEXIST do FileUtils.cp('foo', 'baz')
FileUtils.cp('foo', 'baz') assert_equal 'bar', File.read('baz/foo')
end 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 end
def test_cp_fails_on_no_source def test_cp_fails_on_no_source