FileUtils.ln_s should raise an error if symlinking to a subpath

which does not exist (and :force => true is not provided)
This commit is contained in:
Scott Taylor 2011-09-05 17:18:29 -04:00
parent 6a2a821c8a
commit 973a2ae1f3
3 changed files with 28 additions and 1 deletions

View File

@ -61,6 +61,10 @@ module FakeFS
glob(pattern) glob(pattern)
end end
def self.exists?(path)
File.exists?(path) && File.directory?(path)
end
def self.chdir(dir, &blk) def self.chdir(dir, &blk)
FileSystem.chdir(dir, &blk) FileSystem.chdir(dir, &blk)
end end

View File

@ -40,9 +40,17 @@ module FakeFS
def ln_s(target, path, options = {}) def ln_s(target, path, options = {})
options = { :force => false }.merge(options) options = { :force => false }.merge(options)
(FileSystem.find(path) and !options[:force]) ? raise(Errno::EEXIST, path) : FileSystem.delete(path) (FileSystem.find(path) && !options[:force]) ?
raise(Errno::EEXIST, path) :
FileSystem.delete(path)
if !options[:force] && !Dir.exists?(File.dirname(path))
raise Errno::ENOENT, path
end
FileSystem.add(path, FakeSymlink.new(target)) FileSystem.add(path, FakeSymlink.new(target))
end end
def ln_sf(target, path) def ln_sf(target, path)
ln_s(target, path, { :force => true }) ln_s(target, path, { :force => true })
end end

View File

@ -142,18 +142,33 @@ class FakeFSTest < Test::Unit::TestCase
end end
def test_symlinks_in_different_directories def test_symlinks_in_different_directories
FileUtils.mkdir_p("/path/to/bar")
FileUtils.mkdir_p(target = "/path/to/foo/target") FileUtils.mkdir_p(target = "/path/to/foo/target")
FileUtils.ln_s(target, link = "/path/to/bar/symlink") FileUtils.ln_s(target, link = "/path/to/bar/symlink")
assert_equal target, File.readlink(link) assert_equal target, File.readlink(link)
end end
def test_symlinks_to_symlinks def test_symlinks_to_symlinks
FileUtils.mkdir_p(target = "/path/to/foo/target") FileUtils.mkdir_p(target = "/path/to/foo/target")
FileUtils.mkdir_p("/path/to/bar")
FileUtils.mkdir_p("/path/to/bar2")
FileUtils.ln_s(target, link1 = "/path/to/bar/symlink") FileUtils.ln_s(target, link1 = "/path/to/bar/symlink")
FileUtils.ln_s(link1, link2 = "/path/to/bar2/symlink") FileUtils.ln_s(link1, link2 = "/path/to/bar2/symlink")
assert_equal link1, File.readlink(link2) assert_equal link1, File.readlink(link2)
end end
def test_symlink_to_symlinks_should_raise_error_if_dir_doesnt_exist
FileUtils.mkdir_p(target = "/path/to/foo/target")
assert !Dir.exists?("/path/to/bar")
assert_raise Errno::ENOENT do
FileUtils.ln_s(target, "/path/to/bar/symlink")
end
end
def test_knows_symlinks_are_symlinks def test_knows_symlinks_are_symlinks
FileUtils.mkdir_p(target = "/path/to/target") FileUtils.mkdir_p(target = "/path/to/target")
FileUtils.ln_s(target, link = "/path/to/symlink") FileUtils.ln_s(target, link = "/path/to/symlink")