diff --git a/lib/fakefs/fileutils.rb b/lib/fakefs/fileutils.rb index 6c00888..8b62905 100644 --- a/lib/fakefs/fileutils.rb +++ b/lib/fakefs/fileutils.rb @@ -26,13 +26,13 @@ module FakeFS alias_method :rm_rf, :rm alias_method :rm_r, :rm - def ln_s(target, path) - raise Errno::EEXIST, path if FileSystem.find(path) + 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.add(path, FakeSymlink.new(target)) end def ln_sf(target, path) - FileSystem.delete(path) - FileSystem.add(path, FakeSymlink.new(target)) + ln_s(target, path, { :force => true }) end diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index a52a12c..f1f6712 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -74,6 +74,20 @@ class FakeFSTest < Test::Unit::TestCase FileUtils.ln_s(target, '/path/to/link') end end + + def test_can_force_creation_of_symlinks + FileUtils.mkdir_p(target = "/path/to/first/target") + FileUtils.ln_s(target, "/path/to/link") + assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link'] + FileUtils.ln_s(target, '/path/to/link', :force => true) + end + + def test_create_symlink_using_ln_sf + FileUtils.mkdir_p(target = "/path/to/first/target") + FileUtils.ln_s(target, "/path/to/link") + assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link'] + FileUtils.ln_sf(target, '/path/to/link') + end def test_can_follow_symlinks FileUtils.mkdir_p(target = "/path/to/target")