From 973a2ae1f3c80ed0ac10ae62578431be8c9a2587 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Mon, 5 Sep 2011 17:18:29 -0400 Subject: [PATCH] FileUtils.ln_s should raise an error if symlinking to a subpath which does not exist (and :force => true is not provided) --- lib/fakefs/dir.rb | 4 ++++ lib/fakefs/fileutils.rb | 10 +++++++++- test/fakefs_test.rb | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/fakefs/dir.rb b/lib/fakefs/dir.rb index 379db08..5d96a7d 100644 --- a/lib/fakefs/dir.rb +++ b/lib/fakefs/dir.rb @@ -61,6 +61,10 @@ module FakeFS glob(pattern) end + def self.exists?(path) + File.exists?(path) && File.directory?(path) + end + def self.chdir(dir, &blk) FileSystem.chdir(dir, &blk) end diff --git a/lib/fakefs/fileutils.rb b/lib/fakefs/fileutils.rb index 8a47856..d5a904a 100644 --- a/lib/fakefs/fileutils.rb +++ b/lib/fakefs/fileutils.rb @@ -40,9 +40,17 @@ module FakeFS 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 diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index 9384e59..445fc75 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -142,18 +142,33 @@ class FakeFSTest < Test::Unit::TestCase 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")