diff --git a/lib/fakefs.rb b/lib/fakefs.rb index 52ab82b..00a16c9 100644 --- a/lib/fakefs.rb +++ b/lib/fakefs.rb @@ -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) + FileSystem.add(File.join(dest, src), src_file.entry.clone(dest)) else FileSystem.delete(dest) - FileSystem.add(dest, src_file.entry.clone) + FileSystem.add(dest, src_file.entry.clone(dest)) end end @@ -66,16 +66,16 @@ module FakeFS if src[-2..-1] == '/.' dir.values.each{|f| new_dir[f.name] = f } else - new_dir[dir.name] = dir.entry.clone + new_dir[dir.name] = dir.entry.clone(new_dir) end else - FileSystem.add(dest, dir.entry.clone) + FileSystem.add(dest, dir.entry.clone(dest)) end end def mv(src, dest) if target = FileSystem.find(src) - FileSystem.add(dest, target.entry.clone) + FileSystem.add(dest, target.entry.clone(dest)) FileSystem.delete(src) else raise Errno::ENOENT, src @@ -350,6 +350,12 @@ module FakeFS @content = '' end + def clone(parent) + clone = super() + clone.parent = parent + clone + end + def entry self end @@ -371,6 +377,14 @@ module FakeFS self end + def clone(parent) + clone = super() + clone.each do |key, value| + value.parent = parent + end + clone + end + def to_s if parent && parent.to_s != '.' File.join(parent.to_s, name) diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index dc20127..17e36d5 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -175,8 +175,15 @@ class FakeFSTest < Test::Unit::TestCase FileUtils.mkdir_p '/path' File.open('/path/foo', 'w'){|f| f.write 'foo' } File.open('/path/foobar', 'w'){|f| f.write 'foo' } + + FileUtils.mkdir_p '/path/bar' + File.open('/path/bar/baz', 'w'){|f| f.write 'foo' } + assert_equal ['/path'], Dir['/path'] - assert_equal ['/path/foo', '/path/foobar'], Dir['/path/*'] + assert_equal ['/path/bar', '/path/foo', '/path/foobar'], Dir['/path/*'] + + assert_equal ['/path/bar/baz'], Dir['/path/bar/*'] + # Unsupported so far. More hackery than I want to work on right now # assert_equal ['/path'], Dir['/path*'] end @@ -445,7 +452,7 @@ class FakeFSTest < Test::Unit::TestCase FileUtils.ln_s 'subdir', 'new' assert_equal 'works', File.open('new/nother'){|f| f.read } end - + def test_files_can_be_touched FileUtils.touch('touched_file') assert File.exists?('touched_file')