fix random cloning bugs

This commit is contained in:
Chris Wanstrath 2009-07-19 15:23:42 -07:00
parent eb634265e6
commit a7f20095f0
2 changed files with 31 additions and 9 deletions

View File

@ -38,10 +38,10 @@ module FakeFS
end
if dst_file and File.directory?(dst_file)
FileSystem.add(File.join(dest, src), src_file.entry.clone(dest))
FileSystem.add(File.join(dest, src), src_file.entry.clone(dst_file))
else
FileSystem.delete(dest)
FileSystem.add(dest, src_file.entry.clone(dest))
FileSystem.add(dest, src_file.entry.clone)
end
end
@ -64,18 +64,18 @@ module FakeFS
# about and cleaned up.
if new_dir
if src[-2..-1] == '/.'
dir.values.each{|f| new_dir[f.name] = f }
dir.values.each{|f| new_dir[f.name] = f.clone(new_dir) }
else
new_dir[dir.name] = dir.entry.clone(new_dir)
end
else
FileSystem.add(dest, dir.entry.clone(dest))
FileSystem.add(dest, dir.entry.clone)
end
end
def mv(src, dest)
if target = FileSystem.find(src)
FileSystem.add(dest, target.entry.clone(dest))
FileSystem.add(dest, target.entry.clone)
FileSystem.delete(src)
else
raise Errno::ENOENT, src
@ -353,15 +353,16 @@ module FakeFS
class MockFile
attr_accessor :name, :parent, :content
def initialize(name = nil, parent = nil)
@name = name
@parent = parent
@content = ''
end
def clone(parent)
def clone(parent = nil)
clone = super()
clone.parent = parent
clone.parent = parent if parent
clone
end
@ -369,6 +370,10 @@ module FakeFS
self
end
def inspect
"(MockFile name:#{name.inspect} parent:#{parent.to_s.inspect} size:#{content.size})"
end
def to_s
File.join(parent.to_s, name)
end
@ -386,11 +391,16 @@ module FakeFS
self
end
def clone(parent)
def inspect
"(MockDir name:#{name.inspect} parent:#{parent.to_s.inspect} size:#{size})"
end
def clone(parent = nil)
clone = Marshal.load(Marshal.dump(self))
clone.each do |key, value|
value.parent = parent
value.parent = clone
end
clone.parent = parent if parent
clone
end

View File

@ -422,6 +422,18 @@ class FakeFSTest < Test::Unit::TestCase
assert_equal 'footext', File.open('symdir/subdir/foo'){|f| f.read }
end
def test_cp_r_sets_parent_correctly
FileUtils.mkdir_p '/path/foo'
File.open('/path/foo/bar', 'w'){|f| f.write 'foo' }
File.open('/path/foo/baz', 'w'){|f| f.write 'foo' }
FileUtils.cp_r '/path/foo', '/path/bar'
assert File.exists?('/path/bar/baz')
FileUtils.rm_rf '/path/bar/baz'
assert_equal %w( /path/bar/bar ), Dir['/path/bar/*']
end
def test_clone_clones_normal_files
RealFile.open(here('foo'), 'w'){|f| f.write 'bar' }
assert !File.exists?(here('foo'))