diff --git a/README.markdown b/README.markdown index c4dbf91..52acf1a 100644 --- a/README.markdown +++ b/README.markdown @@ -31,6 +31,12 @@ FakeFS provides a test suite and works with symlinks. It's also strictly a test-time dependency: your actual library does not need to use or know about FakeFS. + +Speed? +------ +http://gist.github.com/150348 + + Authors ------- diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..c96fc5a --- /dev/null +++ b/Rakefile @@ -0,0 +1,3 @@ +task :default do + exec "ruby test/fakefs_test.rb" +end diff --git a/lib/fakefs.rb b/lib/fakefs.rb index f1ffebd..0d85386 100644 --- a/lib/fakefs.rb +++ b/lib/fakefs.rb @@ -19,6 +19,7 @@ module FakeFS FileSystem.delete(path) end alias_method :rm_rf, :rm + alias_method :rm_r, :rm def ln_s(target, path) raise Errno::EEXIST, path if FileSystem.find(path) @@ -38,7 +39,7 @@ 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(dst_file)) else FileSystem.delete(dest) FileSystem.add(dest, src_file.entry.clone) @@ -64,9 +65,9 @@ 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[dir.name] = dir.entry.clone(new_dir) end else FileSystem.add(dest, dir.entry.clone) @@ -107,6 +108,11 @@ module FakeFS end end end + + def cd(dir) + FileSystem.chdir(dir) + end + alias_method :chdir, :cd end class File @@ -117,7 +123,7 @@ module FakeFS end def self.exist?(path) - FileSystem.find(path) || false + !!FileSystem.find(path) end class << self @@ -353,16 +359,27 @@ module FakeFS class MockFile attr_accessor :name, :parent, :content + def initialize(name = nil, parent = nil) @name = name @parent = parent @content = '' end + def clone(parent = nil) + clone = super() + clone.parent = parent if parent + clone + end + def entry 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 @@ -380,6 +397,19 @@ module FakeFS self end + 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 = clone + end + clone.parent = parent if parent + 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 bc4c8f3..aee941f 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -121,7 +121,7 @@ class FakeFSTest < Test::Unit::TestCase file.read end end - + def test_can_read_from_file_objects path = '/path/to/file.txt' File.open(path, 'w') do |f| @@ -193,8 +193,17 @@ 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' } + + FileUtils.cp_r '/path/bar', '/path/bar2' + assert_equal ['/path'], Dir['/path'] - assert_equal ['/path/foo', '/path/foobar'], Dir['/path/*'] + assert_equal %w( /path/bar /path/bar2 /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 @@ -413,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'))