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:
parent
ddcdfcd04f
commit
da576d4208
@ -19,24 +19,29 @@ module FakeFS
|
|||||||
alias_method :rm_rf, :rm
|
alias_method :rm_rf, :rm
|
||||||
|
|
||||||
def ln_s(target, path)
|
def ln_s(target, path)
|
||||||
|
raise Errno::EEXIST, path if FileSystem.find(path)
|
||||||
FileSystem.add(path, MockSymlink.new(target))
|
FileSystem.add(path, MockSymlink.new(target))
|
||||||
end
|
end
|
||||||
|
|
||||||
def cp_r(src, dest)
|
def cp_r(src, dest)
|
||||||
if dir = FileSystem.find(src)
|
# This error sucks, but it conforms to the original Ruby
|
||||||
new_dir = FileSystem.find(dest)
|
# method.
|
||||||
if !new_dir
|
raise "unknown file type: #{src}" unless dir = FileSystem.find(src)
|
||||||
if !FileSystem.find(dest+'/../')
|
|
||||||
raise Errno::ENOENT, dest
|
new_dir = FileSystem.find(dest)
|
||||||
end
|
|
||||||
FileSystem.add(dest, dir.entry.clone)
|
if new_dir && !File.directory?(dest)
|
||||||
else
|
raise Errno::EEXIST, dest
|
||||||
new_dir[dir.name] = dir.entry.clone
|
end
|
||||||
end
|
|
||||||
|
if !new_dir && !FileSystem.find(dest+'/../')
|
||||||
|
raise Errno::ENOENT, dest
|
||||||
|
end
|
||||||
|
|
||||||
|
if new_dir
|
||||||
|
new_dir[dir.name] = dir.entry.clone
|
||||||
else
|
else
|
||||||
# This error sucks, but it conforms to the original Ruby
|
FileSystem.add(dest, dir.entry.clone)
|
||||||
# method.
|
|
||||||
raise "unknown file type: #{src}"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -76,7 +81,7 @@ module FakeFS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.directory?(path)
|
def self.directory?(path)
|
||||||
FileSystem.find(path).is_a? MockDir
|
FileSystem.find(path).entry.is_a? MockDir
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.symlink?(path)
|
def self.symlink?(path)
|
||||||
@ -84,7 +89,7 @@ module FakeFS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.file?(path)
|
def self.file?(path)
|
||||||
FileSystem.find(path).is_a? MockFile
|
FileSystem.find(path).entry.is_a? MockFile
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.expand_path(*args)
|
def self.expand_path(*args)
|
||||||
|
@ -36,6 +36,12 @@ class FakeFSTest < Test::Unit::TestCase
|
|||||||
assert File.directory?(path)
|
assert File.directory?(path)
|
||||||
end
|
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
|
def test_doesnt_overwrite_existing_directories
|
||||||
FileUtils.mkdir_p(path = "/path/to/dir")
|
FileUtils.mkdir_p(path = "/path/to/dir")
|
||||||
assert File.exists?(path)
|
assert File.exists?(path)
|
||||||
@ -47,6 +53,10 @@ class FakeFSTest < Test::Unit::TestCase
|
|||||||
FileUtils.mkdir_p(target = "/path/to/target")
|
FileUtils.mkdir_p(target = "/path/to/target")
|
||||||
FileUtils.ln_s(target, "/path/to/link")
|
FileUtils.ln_s(target, "/path/to/link")
|
||||||
assert_kind_of MockSymlink, FileSystem.fs['path']['to']['link']
|
assert_kind_of MockSymlink, FileSystem.fs['path']['to']['link']
|
||||||
|
|
||||||
|
assert_raises(Errno::EEXIST) {
|
||||||
|
FileUtils.ln_s(target, '/path/to/link')
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_can_follow_symlinks
|
def test_can_follow_symlinks
|
||||||
@ -107,6 +117,16 @@ class FakeFSTest < Test::Unit::TestCase
|
|||||||
assert File.file?(path)
|
assert File.file?(path)
|
||||||
end
|
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
|
def test_can_chown_files
|
||||||
good = 'file.txt'
|
good = 'file.txt'
|
||||||
bad = 'nofile.txt'
|
bad = 'nofile.txt'
|
||||||
@ -307,6 +327,23 @@ class FakeFSTest < Test::Unit::TestCase
|
|||||||
}
|
}
|
||||||
end
|
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 test_clone_clones_normal_files
|
||||||
def here(fname); File.expand_path(File.dirname(__FILE__)+'/'+fname); end
|
def here(fname); File.expand_path(File.dirname(__FILE__)+'/'+fname); end
|
||||||
RealFile.open(here('foo'), 'w'){|f| f.write 'bar' }
|
RealFile.open(here('foo'), 'w'){|f| f.write 'bar' }
|
||||||
@ -316,4 +353,16 @@ class FakeFSTest < Test::Unit::TestCase
|
|||||||
ensure
|
ensure
|
||||||
RealFile.unlink(here('foo')) if RealFile.exists?(here('foo'))
|
RealFile.unlink(here('foo')) if RealFile.exists?(here('foo'))
|
||||||
end
|
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
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user