Fixes to cp_r, ln_s, directory? and file?. Raise an error if we try to cp_r into

something that isn't a directory. Do not allow symlinking if the path
the symlink would be at already exists. directory? and file? now
correctly pass if called on a symlink path that points to a path of
the corresponding type.
This commit is contained in:
Jeff Hodges 2009-06-01 03:52:08 -07:00
parent ddcdfcd04f
commit da576d4208
2 changed files with 69 additions and 15 deletions

View File

@ -19,24 +19,29 @@ module FakeFS
alias_method :rm_rf, :rm
def ln_s(target, path)
raise Errno::EEXIST, path if FileSystem.find(path)
FileSystem.add(path, MockSymlink.new(target))
end
def cp_r(src, dest)
if dir = FileSystem.find(src)
new_dir = FileSystem.find(dest)
if !new_dir
if !FileSystem.find(dest+'/../')
raise Errno::ENOENT, dest
end
FileSystem.add(dest, dir.entry.clone)
else
new_dir[dir.name] = dir.entry.clone
end
else
# This error sucks, but it conforms to the original Ruby
# method.
raise "unknown file type: #{src}"
raise "unknown file type: #{src}" unless dir = FileSystem.find(src)
new_dir = FileSystem.find(dest)
if new_dir && !File.directory?(dest)
raise Errno::EEXIST, dest
end
if !new_dir && !FileSystem.find(dest+'/../')
raise Errno::ENOENT, dest
end
if new_dir
new_dir[dir.name] = dir.entry.clone
else
FileSystem.add(dest, dir.entry.clone)
end
end
@ -76,7 +81,7 @@ module FakeFS
end
def self.directory?(path)
FileSystem.find(path).is_a? MockDir
FileSystem.find(path).entry.is_a? MockDir
end
def self.symlink?(path)
@ -84,7 +89,7 @@ module FakeFS
end
def self.file?(path)
FileSystem.find(path).is_a? MockFile
FileSystem.find(path).entry.is_a? MockFile
end
def self.expand_path(*args)

View File

@ -36,6 +36,12 @@ class FakeFSTest < Test::Unit::TestCase
assert File.directory?(path)
end
def test_knows_symlink_directories_are_directories
FileUtils.mkdir_p(path = "/path/to/dir")
FileUtils.ln_s path, sympath = '/sympath'
assert File.directory?(sympath)
end
def test_doesnt_overwrite_existing_directories
FileUtils.mkdir_p(path = "/path/to/dir")
assert File.exists?(path)
@ -47,6 +53,10 @@ class FakeFSTest < Test::Unit::TestCase
FileUtils.mkdir_p(target = "/path/to/target")
FileUtils.ln_s(target, "/path/to/link")
assert_kind_of MockSymlink, FileSystem.fs['path']['to']['link']
assert_raises(Errno::EEXIST) {
FileUtils.ln_s(target, '/path/to/link')
}
end
def test_can_follow_symlinks
@ -107,6 +117,16 @@ class FakeFSTest < Test::Unit::TestCase
assert File.file?(path)
end
def test_knows_symlink_files_are_files
path = '/path/to/file.txt'
File.open(path, 'w') do |f|
f.write "Yatta!"
end
FileUtils.ln_s path, sympath='/sympath'
assert File.file?(sympath)
end
def test_can_chown_files
good = 'file.txt'
bad = 'nofile.txt'
@ -307,6 +327,23 @@ class FakeFSTest < Test::Unit::TestCase
}
end
def test_cp_r_only_copies_into_directories
FileUtils.mkdir_p 'subdir'
Dir.chdir('subdir'){ File.open('foo', 'w'){|f| f.write 'footext' } }
File.open('bar', 'w') {|f| f.write 'bartext' }
assert_raises(Errno::EEXIST) do
FileUtils.cp_r 'subdir', 'bar'
end
FileUtils.mkdir_p 'otherdir'
FileUtils.ln_s 'otherdir', 'symdir'
FileUtils.cp_r 'subdir', 'symdir'
assert_equal 'footext', File.open('symdir/subdir/foo'){|f| f.read }
end
def test_clone_clones_normal_files
def here(fname); File.expand_path(File.dirname(__FILE__)+'/'+fname); end
RealFile.open(here('foo'), 'w'){|f| f.write 'bar' }
@ -316,4 +353,16 @@ class FakeFSTest < Test::Unit::TestCase
ensure
RealFile.unlink(here('foo')) if RealFile.exists?(here('foo'))
end
def test_file_can_read_from_symlinks
File.open('first', 'w'){|f| f.write '1'}
FileUtils.ln_s 'first', 'one'
assert_equal '1', File.open('one'){|f| f.read }
FileUtils.mkdir_p 'subdir'
File.open('subdir/nother','w'){|f| f.write 'works' }
FileUtils.ln_s 'subdir', 'new'
assert_equal 'works', File.open('new/nother'){|f| f.read }
end
end