From 8db0b94e880a897ab98e6ea1e25ed8eb2e11e78d Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sat, 23 Aug 2025 13:39:04 +0200 Subject: [PATCH 1/9] Run pathname specs from ruby/spec in CI * Provides extra testing and coverage. --- .github/workflows/test.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ed8b613..92b03e9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 From 560a877665bcd0195b5405248d42866372120aac Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sat, 23 Aug 2025 13:46:11 +0200 Subject: [PATCH 2/9] The Pathname method should be a module_function of Kernel * Found by ruby/spec, and confirmed pathname.c did the same. --- lib/pathname.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/pathname.rb b/lib/pathname.rb index 404be1a..98ea3da 100644 --- a/lib/pathname.rb +++ b/lib/pathname.rb @@ -1236,12 +1236,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 From 05f74a27c0097cf889fc5ef91411e8c38e3e410d Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sat, 23 Aug 2025 14:06:17 +0200 Subject: [PATCH 3/9] Pathname#initialize should raise TypeError if coercion fails * Found by ruby/spec, and confirmed pathname.c did the same. --- lib/pathname.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/pathname.rb b/lib/pathname.rb index 98ea3da..09c34c9 100644 --- a/lib/pathname.rb +++ b/lib/pathname.rb @@ -239,9 +239,10 @@ class Pathname # If +path+ contains a NUL character (\0), 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}" From 379e19fd202383ef4a7531773fd43647e72761da Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sat, 23 Aug 2025 14:07:49 +0200 Subject: [PATCH 4/9] Add back Pathname#realdirpath * This has no test in test_pathname.rb, that is why it was missed. * Found by ruby/spec, and confirmed pathname.c did the same. --- lib/pathname.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/pathname.rb b/lib/pathname.rb index 09c34c9..205e7ef 100644 --- a/lib/pathname.rb +++ b/lib/pathname.rb @@ -1013,6 +1013,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 From 13c8458a9dd1f11c390cfebb1d03a567bb21444b Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Tue, 26 Aug 2025 09:39:58 +0200 Subject: [PATCH 5/9] Revert "Restore Pathname#realdirpath" * This reverts commit a9ef32efbbe5da84e4ba2cdb3ea123dcb322c7b8. * It is already added in Ruby code. --- ext/pathname/pathname.c | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/ext/pathname/pathname.c b/ext/pathname/pathname.c index 73be963..10d055f 100644 --- a/ext/pathname/pathname.c +++ b/ext/pathname/pathname.c @@ -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) @@ -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 @@ -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 @@ -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"); } From 85992c3939e4118bd4320960eb5c5d7a94797d4d Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sat, 23 Aug 2025 14:14:46 +0200 Subject: [PATCH 6/9] Use File.sysopen for consistency * There is no point to call IO.sysopen and that's the last IO method, with Pathname we know it's always a file path. --- lib/pathname.rb | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/lib/pathname.rb b/lib/pathname.rb index 205e7ef..e921b0a 100644 --- a/lib/pathname.rb +++ b/lib/pathname.rb @@ -150,6 +150,7 @@ module ::Kernel # - #read(*args) # - #binread(*args) # - #readlines(*args) +# - #sysopen(*args) # - #write(*args) # - #binwrite(*args) # - #atime @@ -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: @@ -885,11 +881,6 @@ def relative_path_from(base_directory) end end -class Pathname # * IO * - # See IO.sysopen. - def sysopen(...) IO.sysopen(@path, ...) end -end - class Pathname # * File * # # #each_line iterates over the line in the file. It yields a String object @@ -912,6 +903,9 @@ def binread(...) File.binread(@path, ...) end # See File.readlines. Returns all the lines from the file. def readlines(...) File.readlines(@path, ...) end + # See File.sysopen. + def sysopen(...) File.sysopen(@path, ...) end + # Writes +contents+ to the file. See File.write. def write(...) File.write(@path, ...) end From abc9be57d3d561a51541279816f22985a04c62a7 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sat, 23 Aug 2025 14:25:17 +0200 Subject: [PATCH 7/9] Fix TestPathname#has_symlink? it was returning false on Linux --- test/pathname/test_pathname.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb index 7e0011c..b837f63 100644 --- a/test/pathname/test_pathname.rb +++ b/test/pathname/test_pathname.rb @@ -348,7 +348,7 @@ def has_symlink? rescue NotImplementedError return false rescue Errno::ENOENT - return false + return true rescue Errno::EACCES return false end From 122b088ec58e4143e8b5e6329179fbfbd9c91c96 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sat, 23 Aug 2025 14:26:40 +0200 Subject: [PATCH 8/9] Fix Pathname#lutime test and add the method back --- lib/pathname.rb | 7 +++++++ test/pathname/test_pathname.rb | 6 +++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/pathname.rb b/lib/pathname.rb index e921b0a..a0db812 100644 --- a/lib/pathname.rb +++ b/lib/pathname.rb @@ -981,6 +981,13 @@ def truncate(length) File.truncate(@path, length) end # See File.utime. 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 File.basename. Returns the last component of the path. def basename(...) self.class.new(File.basename(@path, ...)) end diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb index b837f63..43d6304 100644 --- a/test/pathname/test_pathname.rb +++ b/test/pathname/test_pathname.rb @@ -1054,7 +1054,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) From ef196ccd11ebec7f32bf68b9648fe1fa36557f78 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sat, 23 Aug 2025 14:41:35 +0200 Subject: [PATCH 9/9] Exclude a couple tests failing on JRuby for multiple reasons * https://github.com/jruby/jruby/issues/8972 but also at least 2 more incompatibilities. --- test/pathname/test_pathname.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb index 43d6304..e80473e 100644 --- a/test/pathname/test_pathname.rb +++ b/test/pathname/test_pathname.rb @@ -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") } @@ -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)