Suddenly, cp_r works beautifully! This commit now lets us handle the

case where a directory is meant to be copied as a subdirectory to the
destination path when that path already exists. We also do the
appropriate error message when the dest path given is non-existent and
so is its parent.

Oh, crap, we should probably check that it's a directory that we are
merging into.
This commit is contained in:
Jeff Hodges 2009-06-01 03:25:12 -07:00
parent 4de717a976
commit ddcdfcd04f
2 changed files with 30 additions and 2 deletions

View File

@ -24,7 +24,15 @@ module FakeFS
def cp_r(src, dest)
if dir = FileSystem.find(src)
FileSystem.add(dest, dir.entry.clone)
new_dir = FileSystem.find(dest)
if !new_dir
if !FileSystem.find(dest+'/../')
raise Errno::ENOENT, dest
end
FileSystem.add(dest, dir.entry.clone)
else
new_dir[dir.name] = dir.entry.clone
end
else
# This error sucks, but it conforms to the original Ruby
# method.
@ -170,7 +178,7 @@ module FakeFS
end
def clear
@dir_levels = []
@dir_levels = nil
@fs = nil
end

View File

@ -287,6 +287,26 @@ class FakeFSTest < Test::Unit::TestCase
end
end
def test_cp_r_handles_copying_directories
FileUtils.mkdir_p 'subdir'
Dir.chdir('subdir'){ File.open('foo', 'w'){|f| f.write 'footext' } }
FileUtils.mkdir_p 'baz'
# To a previously uncreated directory
FileUtils.cp_r('subdir', 'quux')
assert_equal 'footext', File.open('quux/foo'){|f| f.read }
# To a directory that already exists
FileUtils.cp_r('subdir', 'baz')
assert_equal 'footext', File.open('baz/subdir/foo'){|f| f.read }
# To a subdirectory of a directory that does not exist
assert_raises(Errno::ENOENT) {
FileUtils.cp_r('subdir', 'nope/something')
}
end
def test_clone_clones_normal_files
def here(fname); File.expand_path(File.dirname(__FILE__)+'/'+fname); end
RealFile.open(here('foo'), 'w'){|f| f.write 'bar' }