Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,20 @@ jobs:
run: bundle install
- name: Run test
run: rake compile test

spec:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- uses: ruby/setup-ruby@v1
with:
ruby-version: ruby
- uses: actions/checkout@v5
with:
repository: ruby/spec
path: rubyspec
- name: Clone MSpec
run: git clone https://github.com/ruby/mspec.git ../mspec
- run: bundle install
- run: rake compile
- run: ../mspec/bin/mspec -Ilib rubyspec/library/pathname
19 changes: 0 additions & 19 deletions ext/pathname/pathname.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
static VALUE rb_cPathname;
static ID id_at_path;
static ID id_sub;
static ID id_realdirpath;

static VALUE
get_strpath(VALUE obj)
Expand Down Expand Up @@ -84,22 +83,6 @@ path_sub(int argc, VALUE *argv, VALUE self)
return rb_class_new_instance(1, &str, rb_obj_class(self));
}

/*
* Returns the real (absolute) pathname of +self+ in the actual filesystem.
*
* Does not contain symlinks or useless dots, +..+ and +.+.
*
* The last component of the real pathname can be nonexistent.
*/
static VALUE
path_realdirpath(int argc, VALUE *argv, VALUE self)
{
VALUE basedir, str;
rb_scan_args(argc, argv, "01", &basedir);
str = rb_funcall(rb_cFile, id_realdirpath, 2, get_strpath(self), basedir);
return rb_class_new_instance(1, &str, rb_obj_class(self));
}

static void init_ids(void);

void
Expand All @@ -119,7 +102,6 @@ InitVM_pathname(void)
rb_cPathname = rb_define_class("Pathname", rb_cObject);
rb_define_method(rb_cPathname, "<=>", path_cmp, 1);
rb_define_method(rb_cPathname, "sub", path_sub, -1);
rb_define_method(rb_cPathname, "realdirpath", path_realdirpath, -1);
}

void
Expand All @@ -128,5 +110,4 @@ init_ids(void)
#undef rb_intern
id_at_path = rb_intern("@path");
id_sub = rb_intern("sub");
id_realdirpath = rb_intern("realdirpath");
}
41 changes: 23 additions & 18 deletions lib/pathname.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ module ::Kernel
# - #read(*args)
# - #binread(*args)
# - #readlines(*args)
# - #sysopen(*args)
# - #write(*args)
# - #binwrite(*args)
# - #atime
Expand Down Expand Up @@ -190,11 +191,6 @@ module ::Kernel
# - #mkdir(*args)
# - #opendir(*args)
#
# === IO
#
# This method is a facade for IO:
# - #sysopen(*args)
#
# === Utilities
#
# These methods are a mixture of Find, FileUtils, and others:
Expand Down Expand Up @@ -239,9 +235,10 @@ class Pathname
# If +path+ contains a NUL character (<tt>\0</tt>), an ArgumentError is raised.
#
def initialize(path)
path = path.to_path if path.respond_to? :to_path

raise TypeError unless path.is_a?(String) # Compatibility for C version
unless String === path
path = path.to_path if path.respond_to? :to_path
raise TypeError unless String === path
end

if path.include?("\0")
raise ArgumentError, "pathname contains \\0: #{path.inspect}"
Expand Down Expand Up @@ -884,11 +881,6 @@ def relative_path_from(base_directory)
end
end

class Pathname # * IO *
# See <tt>IO.sysopen</tt>.
def sysopen(...) IO.sysopen(@path, ...) end
end

class Pathname # * File *
#
# #each_line iterates over the line in the file. It yields a String object
Expand All @@ -911,6 +903,9 @@ def binread(...) File.binread(@path, ...) end
# See <tt>File.readlines</tt>. Returns all the lines from the file.
def readlines(...) File.readlines(@path, ...) end

# See <tt>File.sysopen</tt>.
def sysopen(...) File.sysopen(@path, ...) end

# Writes +contents+ to the file. See <tt>File.write</tt>.
def write(...) File.write(@path, ...) end

Expand Down Expand Up @@ -986,6 +981,13 @@ def truncate(length) File.truncate(@path, length) end
# See <tt>File.utime</tt>. Update the access and modification times.
def utime(atime, mtime) File.utime(atime, mtime, @path) end

# Update the access and modification times of the file.
#
# Same as Pathname#utime, but does not follow symbolic links.
#
# See File.lutime.
def lutime(atime, mtime) File.lutime(atime, mtime, @path) end

# See <tt>File.basename</tt>. Returns the last component of the path.
def basename(...) self.class.new(File.basename(@path, ...)) end

Expand All @@ -1012,6 +1014,13 @@ def split()
#
# All components of the pathname must exist when this method is called.
def realpath(...) self.class.new(File.realpath(@path, ...)) end

# Returns the real (absolute) pathname of +self+ in the actual filesystem.
#
# Does not contain symlinks or useless dots, +..+ and +.+.
#
# The last component of the real pathname can be nonexistent.
def realdirpath(...) self.class.new(File.realdirpath(@path, ...)) end
end


Expand Down Expand Up @@ -1236,12 +1245,8 @@ module Kernel
#
# This method is available since 1.8.5.
def Pathname(path) # :doc:
Kernel.Pathname(path)
end
private :Pathname

def self.Pathname(path) # Compatibility for C version
return path if Pathname === path
Pathname.new(path)
end
module_function :Pathname
end
12 changes: 9 additions & 3 deletions test/pathname/test_pathname.rb
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def has_symlink?
rescue NotImplementedError
return false
rescue Errno::ENOENT
return false
return true
rescue Errno::EACCES
return false
end
Expand All @@ -370,10 +370,11 @@ def has_hardlink?
end

def realpath(path, basedir=nil)
Pathname.new(path).realpath(basedir).to_s
Pathname.new(path).realpath(*basedir).to_s
end

def test_realpath
omit "not working yet" if RUBY_ENGINE == "jruby"
return if !has_symlink?
with_tmpchdir('rubytest-pathname') {|dir|
assert_raise(Errno::ENOENT) { realpath("#{dir}/not-exist") }
Expand Down Expand Up @@ -434,6 +435,7 @@ def realdirpath(path)
end

def test_realdirpath
omit "not working yet" if RUBY_ENGINE == "jruby"
return if !has_symlink?
Dir.mktmpdir('rubytest-pathname') {|dir|
rdir = realpath(dir)
Expand Down Expand Up @@ -1054,7 +1056,11 @@ def test_lutime
latime = Time.utc(2000)
lmtime = Time.utc(1999)
File.symlink("a", "l")
Pathname("l").utime(latime, lmtime)
begin
Pathname("l").lutime(latime, lmtime)
rescue NotImplementedError
next
end
s = File.lstat("a")
ls = File.lstat("l")
assert_equal(atime, s.atime)
Expand Down
Loading