Permalink
Browse files

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

which does not exist (and :force => true is not provided)
  • Loading branch information...
smtlaissezfaire committed Sep 5, 2011
1 parent 6a2a821 commit 973a2ae1f3c80ed0ac10ae62578431be8c9a2587
Showing with 28 additions and 1 deletion.
  1. +4 −0 lib/fakefs/dir.rb
  2. +9 −1 lib/fakefs/fileutils.rb
  3. +15 −0 test/fakefs_test.rb
View
@@ -61,6 +61,10 @@ def self.[](pattern)
glob(pattern)
end
+ def self.exists?(path)
+ File.exists?(path) && File.directory?(path)
+ end
+
def self.chdir(dir, &blk)
FileSystem.chdir(dir, &blk)
end
View
@@ -40,9 +40,17 @@ def rm(list, options = {})
def ln_s(target, path, 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))
end
+
def ln_sf(target, path)
ln_s(target, path, { :force => true })
end
View
@@ -142,18 +142,33 @@ def test_can_follow_symlinks
end
def test_symlinks_in_different_directories
+ FileUtils.mkdir_p("/path/to/bar")
FileUtils.mkdir_p(target = "/path/to/foo/target")
+
FileUtils.ln_s(target, link = "/path/to/bar/symlink")
assert_equal target, File.readlink(link)
end
def test_symlinks_to_symlinks
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(link1, link2 = "/path/to/bar2/symlink")
assert_equal link1, File.readlink(link2)
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
FileUtils.mkdir_p(target = "/path/to/target")
FileUtils.ln_s(target, link = "/path/to/symlink")

0 comments on commit 973a2ae

Please sign in to comment.