Merge commit 'defunkt/master'

Conflicts:
	test/fakefs_test.rb
This commit is contained in:
Pat Nakajima 2009-07-22 04:24:34 -04:00
commit bc5dbd3a01
4 changed files with 66 additions and 6 deletions

View File

@ -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 test-time dependency: your actual library does not need to use or know about
FakeFS. FakeFS.
Speed?
------
http://gist.github.com/150348
Authors Authors
------- -------

3
Rakefile Normal file
View File

@ -0,0 +1,3 @@
task :default do
exec "ruby test/fakefs_test.rb"
end

View File

@ -19,6 +19,7 @@ module FakeFS
FileSystem.delete(path) FileSystem.delete(path)
end end
alias_method :rm_rf, :rm alias_method :rm_rf, :rm
alias_method :rm_r, :rm
def ln_s(target, path) def ln_s(target, path)
raise Errno::EEXIST, path if FileSystem.find(path) raise Errno::EEXIST, path if FileSystem.find(path)
@ -38,7 +39,7 @@ module FakeFS
end end
if dst_file and File.directory?(dst_file) 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 else
FileSystem.delete(dest) FileSystem.delete(dest)
FileSystem.add(dest, src_file.entry.clone) FileSystem.add(dest, src_file.entry.clone)
@ -64,9 +65,9 @@ module FakeFS
# about and cleaned up. # about and cleaned up.
if new_dir if new_dir
if src[-2..-1] == '/.' 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 else
new_dir[dir.name] = dir.entry.clone new_dir[dir.name] = dir.entry.clone(new_dir)
end end
else else
FileSystem.add(dest, dir.entry.clone) FileSystem.add(dest, dir.entry.clone)
@ -107,6 +108,11 @@ module FakeFS
end end
end end
end end
def cd(dir)
FileSystem.chdir(dir)
end
alias_method :chdir, :cd
end end
class File class File
@ -117,7 +123,7 @@ module FakeFS
end end
def self.exist?(path) def self.exist?(path)
FileSystem.find(path) || false !!FileSystem.find(path)
end end
class << self class << self
@ -353,16 +359,27 @@ module FakeFS
class MockFile class MockFile
attr_accessor :name, :parent, :content attr_accessor :name, :parent, :content
def initialize(name = nil, parent = nil) def initialize(name = nil, parent = nil)
@name = name @name = name
@parent = parent @parent = parent
@content = '' @content = ''
end end
def clone(parent = nil)
clone = super()
clone.parent = parent if parent
clone
end
def entry def entry
self self
end end
def inspect
"(MockFile name:#{name.inspect} parent:#{parent.to_s.inspect} size:#{content.size})"
end
def to_s def to_s
File.join(parent.to_s, name) File.join(parent.to_s, name)
end end
@ -380,6 +397,19 @@ module FakeFS
self self
end 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 def to_s
if parent && parent.to_s != '.' if parent && parent.to_s != '.'
File.join(parent.to_s, name) File.join(parent.to_s, name)

View File

@ -121,7 +121,7 @@ class FakeFSTest < Test::Unit::TestCase
file.read file.read
end end
end end
def test_can_read_from_file_objects def test_can_read_from_file_objects
path = '/path/to/file.txt' path = '/path/to/file.txt'
File.open(path, 'w') do |f| File.open(path, 'w') do |f|
@ -193,8 +193,17 @@ class FakeFSTest < Test::Unit::TestCase
FileUtils.mkdir_p '/path' FileUtils.mkdir_p '/path'
File.open('/path/foo', 'w'){|f| f.write 'foo' } File.open('/path/foo', 'w'){|f| f.write 'foo' }
File.open('/path/foobar', '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'], 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 # Unsupported so far. More hackery than I want to work on right now
# assert_equal ['/path'], Dir['/path*'] # assert_equal ['/path'], Dir['/path*']
end end
@ -413,6 +422,18 @@ class FakeFSTest < Test::Unit::TestCase
assert_equal 'footext', File.open('symdir/subdir/foo'){|f| f.read } assert_equal 'footext', File.open('symdir/subdir/foo'){|f| f.read }
end 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 def test_clone_clones_normal_files
RealFile.open(here('foo'), 'w'){|f| f.write 'bar' } RealFile.open(here('foo'), 'w'){|f| f.write 'bar' }
assert !File.exists?(here('foo')) assert !File.exists?(here('foo'))