diff --git a/lib/fakefs/file.rb b/lib/fakefs/file.rb index 04e30c7..2ed69e2 100644 --- a/lib/fakefs/file.rb +++ b/lib/fakefs/file.rb @@ -37,7 +37,12 @@ module FakeFS end def self.exist?(path) - !!FileSystem.find(path) + if(File.symlink?(path)) then + referent = File.expand_path(File.readlink(path), File.dirname(path)) + exist?(referent) + else + !!FileSystem.find(path) + end end class << self @@ -132,7 +137,7 @@ module FakeFS def self.readlink(path) symlink = FileSystem.find(path) - FileSystem.find(symlink.target).to_s + symlink.target end def self.read(path) diff --git a/lib/fakefs/file_system.rb b/lib/fakefs/file_system.rb index 1c6f851..a5c8087 100644 --- a/lib/fakefs/file_system.rb +++ b/lib/fakefs/file_system.rb @@ -7,7 +7,7 @@ module FakeFS end def fs - @fs ||= FakeDir.new('.') + @fs ||= FakeDir.new('/') end def clear diff --git a/test/fakefs_test.rb b/test/fakefs_test.rb index eda6ea2..ffb1169 100644 --- a/test/fakefs_test.rb +++ b/test/fakefs_test.rb @@ -117,6 +117,11 @@ class FakeFSTest < Test::Unit::TestCase assert File.exists?(path) end + def test_symlink_with_missing_refferent_does_not_exist + File.symlink('/foo', '/bar') + assert !File.exists?('/bar') + end + def test_can_create_symlinks FileUtils.mkdir_p(target = "/path/to/target") FileUtils.ln_s(target, "/path/to/link") @@ -155,6 +160,27 @@ class FakeFSTest < Test::Unit::TestCase assert_equal target, File.readlink(link) end + def test_symlink_with_relative_path_exists + FileUtils.touch("/file") + FileUtils.mkdir_p("/a/b") + FileUtils.ln_s("../../file", link = "/a/b/symlink") + assert File.exist?('/a/b/symlink') + end + + def test_symlink_with_relative_path_and_nonexistant_file_does_not_exist + FileUtils.touch("/file") + FileUtils.mkdir_p("/a/b") + FileUtils.ln_s("../../file_foo", link = "/a/b/symlink") + assert !File.exist?('/a/b/symlink') + end + + def test_symlink_with_relative_path_has_correct_target + FileUtils.touch("/file") + FileUtils.mkdir_p("/a/b") + FileUtils.ln_s("../../file", link = "/a/b/symlink") + assert_equal "../../file", File.readlink(link) + end + def test_symlinks_to_symlinks FileUtils.mkdir_p(target = "/path/to/foo/target") FileUtils.mkdir_p("/path/to/bar") @@ -685,7 +711,7 @@ class FakeFSTest < Test::Unit::TestCase FileUtils.mkdir_p '/path' # this fails. the root dir should be named '/' but it is '.' - #assert_equal ['/'], Dir['/'] + assert_equal ['/'], Dir['/'] end def test_dir_glob_handles_recursive_globs @@ -781,14 +807,14 @@ class FakeFSTest < Test::Unit::TestCase def test_chdir_changes_directories_like_a_boss # I know memes! FileUtils.mkdir_p '/path' - assert_equal '.', FileSystem.fs.name + assert_equal '/', FileSystem.fs.name assert_equal({}, FileSystem.fs['path']) Dir.chdir '/path' do File.open('foo', 'w') { |f| f.write 'foo'} File.open('foobar', 'w') { |f| f.write 'foo'} end - assert_equal '.', FileSystem.fs.name + assert_equal '/', FileSystem.fs.name assert_equal(['foo', 'foobar'], FileSystem.fs['path'].keys.sort) c = nil