Skip to content

Commit

Permalink
Merge remote-tracking branch 'lest/file-realpath'
Browse files Browse the repository at this point in the history
  • Loading branch information
brixen committed Dec 3, 2011
2 parents 1f4e119 + 8807bd3 commit f1e4c91
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
21 changes: 21 additions & 0 deletions kernel/common/file19.rb
Expand Up @@ -66,6 +66,27 @@ def self.world_writable?(path)
end

alias_method :to_path, :path

def self.realpath(path, basedir = nil)
path = expand_path(path, basedir || Dir.pwd)
real = ''
while !path.empty?
pos = path.index(SEPARATOR, 1)

if pos
name = path[0...pos]
path = path[pos..-1]
else
name = path
path = ''
end

real = join(real, name)
real = realpath(readlink(real)) if symlink?(real)
end

real
end
end

class File::Stat
Expand Down
42 changes: 41 additions & 1 deletion spec/ruby/core/file/realpath_spec.rb
Expand Up @@ -2,6 +2,46 @@

ruby_version_is "1.9" do
describe "File.realpath" do
it "needs to be reviewed for spec completeness"
before do
@real_dir = tmp('dir_realpath_real')
@link_dir = tmp('dir_realpath_link')

mkdir_p @real_dir
File.symlink(@real_dir, @link_dir)

@file = File.join(@real_dir, 'file')
@link = File.join(@link_dir, 'link')

touch @file
File.symlink(@file, @link)
end

after do
File.unlink @link
File.unlink @link_dir
rm_r @file, @real_dir
end

it "returns root for root" do
File.realpath('/').should == '/'
end

it "returns the real (absolute) pathname not containing symlinks" do
File.realpath(@link).should == @file
end

it "uses base directory for interpreting relative pathname" do
File.realpath(File.basename(@link), @link_dir).should == @file
end

it "uses current directory for interpreting relative pathname" do
old_dir = Dir.pwd
begin
Dir.chdir @link_dir
File.realpath(File.basename(@link)).should == @file
ensure
Dir.chdir old_dir
end
end
end
end

0 comments on commit f1e4c91

Please sign in to comment.