* fixing File#exist? for symlinks (should return false when target of symlink does not exist)

* changing name of root directory to '/'
* support for relative targets for symlinks
    - File#readlink should return the target of the symlink
(Closes #71)
This commit is contained in:
Jared Luxenberg 2011-03-23 17:33:25 -07:00 committed by Scott Taylor
parent 4da1d58d66
commit 600b1150ef
3 changed files with 37 additions and 6 deletions

View File

@ -37,8 +37,13 @@ module FakeFS
end end
def self.exist?(path) def self.exist?(path)
if(File.symlink?(path)) then
referent = File.expand_path(File.readlink(path), File.dirname(path))
exist?(referent)
else
!!FileSystem.find(path) !!FileSystem.find(path)
end end
end
class << self class << self
alias_method :exists?, :exist? alias_method :exists?, :exist?
@ -132,7 +137,7 @@ module FakeFS
def self.readlink(path) def self.readlink(path)
symlink = FileSystem.find(path) symlink = FileSystem.find(path)
FileSystem.find(symlink.target).to_s symlink.target
end end
def self.read(path) def self.read(path)

View File

@ -7,7 +7,7 @@ module FakeFS
end end
def fs def fs
@fs ||= FakeDir.new('.') @fs ||= FakeDir.new('/')
end end
def clear def clear

View File

@ -117,6 +117,11 @@ class FakeFSTest < Test::Unit::TestCase
assert File.exists?(path) assert File.exists?(path)
end end
def test_symlink_with_missing_refferent_does_not_exist
File.symlink('/foo', '/bar')
assert !File.exists?('/bar')
end
def test_can_create_symlinks def test_can_create_symlinks
FileUtils.mkdir_p(target = "/path/to/target") FileUtils.mkdir_p(target = "/path/to/target")
FileUtils.ln_s(target, "/path/to/link") FileUtils.ln_s(target, "/path/to/link")
@ -155,6 +160,27 @@ class FakeFSTest < Test::Unit::TestCase
assert_equal target, File.readlink(link) assert_equal target, File.readlink(link)
end 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 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/bar")
@ -685,7 +711,7 @@ class FakeFSTest < Test::Unit::TestCase
FileUtils.mkdir_p '/path' FileUtils.mkdir_p '/path'
# this fails. the root dir should be named '/' but it is '.' # this fails. the root dir should be named '/' but it is '.'
#assert_equal ['/'], Dir['/'] assert_equal ['/'], Dir['/']
end end
def test_dir_glob_handles_recursive_globs def test_dir_glob_handles_recursive_globs
@ -781,14 +807,14 @@ class FakeFSTest < Test::Unit::TestCase
def test_chdir_changes_directories_like_a_boss def test_chdir_changes_directories_like_a_boss
# I know memes! # I know memes!
FileUtils.mkdir_p '/path' FileUtils.mkdir_p '/path'
assert_equal '.', FileSystem.fs.name assert_equal '/', FileSystem.fs.name
assert_equal({}, FileSystem.fs['path']) assert_equal({}, FileSystem.fs['path'])
Dir.chdir '/path' do Dir.chdir '/path' do
File.open('foo', 'w') { |f| f.write 'foo'} File.open('foo', 'w') { |f| f.write 'foo'}
File.open('foobar', 'w') { |f| f.write 'foo'} File.open('foobar', 'w') { |f| f.write 'foo'}
end end
assert_equal '.', FileSystem.fs.name assert_equal '/', FileSystem.fs.name
assert_equal(['foo', 'foobar'], FileSystem.fs['path'].keys.sort) assert_equal(['foo', 'foobar'], FileSystem.fs['path'].keys.sort)
c = nil c = nil