From e94e674d0e8a8ab7641717c2d2ba4b5aac5c7295 Mon Sep 17 00:00:00 2001 From: akr Date: Fri, 30 Jan 2009 12:50:55 +0000 Subject: * lib/pathname.rb (Pathname#realdirpath): new method. [ruby-dev:36290] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21904 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/pathname/test_pathname.rb | 47 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) (limited to 'test/pathname') diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb index 2b0d8a6a99..d979ff7023 100644 --- a/test/pathname/test_pathname.rb +++ b/test/pathname/test_pathname.rb @@ -278,24 +278,37 @@ class TestPathname < Test::Unit::TestCase } end - def realpath(path) - Pathname.new(path).realpath.to_s - end - - def test_realpath + def has_symlink? begin File.symlink(nil, nil) rescue NotImplementedError - return + return false rescue TypeError end + return true + end + + def realpath(path) + Pathname.new(path).realpath.to_s + end + + def test_realpath + return if !has_symlink? with_tmpchdir('rubytest-pathname') {|dir| + assert_raise(Errno::ENOENT) { realpath("#{dir}/not-exist") } File.symlink("not-exist-target", "#{dir}/not-exist") assert_raise(Errno::ENOENT) { realpath("#{dir}/not-exist") } File.symlink("loop", "#{dir}/loop") assert_raise(Errno::ELOOP) { realpath("#{dir}/loop") } + File.symlink("../#{File.basename(dir)}/./not-exist-target", "#{dir}/not-exist2") + assert_raise(Errno::ENOENT) { realpath("#{dir}/not-exist2") } + + File.open("#{dir}/exist-target", "w") {} + File.symlink("../#{File.basename(dir)}/./exist-target", "#{dir}/exist2") + assert_nothing_raised { realpath("#{dir}/exist2") } + File.symlink("loop-relative", "loop-relative") assert_raise(Errno::ELOOP) { realpath("#{dir}/loop-relative") } @@ -332,6 +345,28 @@ class TestPathname < Test::Unit::TestCase } end + def realdirpath(path) + Pathname.new(path).realdirpath.to_s + end + + def test_realdirpath + return if !has_symlink? + Dir.mktmpdir('rubytest-pathname') {|dir| + rdir = realpath(dir) + assert_equal("#{rdir}/not-exist", realdirpath("#{dir}/not-exist")) + assert_raise(Errno::ENOENT) { realdirpath("#{dir}/not-exist/not-exist-child") } + File.symlink("not-exist-target", "#{dir}/not-exist") + assert_equal("#{rdir}/not-exist-target", realdirpath("#{dir}/not-exist")) + File.symlink("../#{File.basename(dir)}/./not-exist-target", "#{dir}/not-exist2") + assert_equal("#{rdir}/not-exist-target", realdirpath("#{dir}/not-exist2")) + File.open("#{dir}/exist-target", "w") {} + File.symlink("../#{File.basename(dir)}/./exist-target", "#{dir}/exist") + assert_equal("#{rdir}/exist-target", realdirpath("#{dir}/exist")) + File.symlink("loop", "#{dir}/loop") + assert_raise(Errno::ELOOP) { realdirpath("#{dir}/loop") } + } + end + def descend(path) Pathname.new(path).enum_for(:descend).map {|v| v.to_s } end -- cgit v1.2.3