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
FakeFS.
Speed?
------
http://gist.github.com/150348
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)
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)

View File

@ -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'))