[前][次][番号順一覧][スレッド一覧]

ruby-changes:54195

From: akr <ko1@a...>
Date: Sun, 16 Dec 2018 21:26:59 +0900 (JST)
Subject: [ruby-changes:54195] akr:r66416 (trunk): Pathname#relative_path_from compatible with mock.

akr	2018-12-16 21:26:52 +0900 (Sun, 16 Dec 2018)

  New Revision: 66416

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66416

  Log:
    Pathname#relative_path_from compatible with mock.
    
    [Fix GH-2049]

  Modified files:
    trunk/ext/pathname/lib/pathname.rb
    trunk/test/pathname/test_pathname.rb
Index: ext/pathname/lib/pathname.rb
===================================================================
--- ext/pathname/lib/pathname.rb	(revision 66415)
+++ ext/pathname/lib/pathname.rb	(revision 66416)
@@ -503,9 +503,13 @@ class Pathname https://github.com/ruby/ruby/blob/trunk/ext/pathname/lib/pathname.rb#L503
   # ArgumentError is raised when it cannot find a relative path.
   #
   def relative_path_from(base_directory)
-    base_directory = Pathname.new(base_directory) unless Pathname === base_directory
     dest_directory = self.cleanpath.to_s
-    base_directory = base_directory.cleanpath.to_s
+    base_directory =
+      if base_directory.respond_to? :cleanpath
+        base_directory
+      else
+        Pathname.new(base_directory)
+      end.cleanpath.to_s
     dest_prefix = dest_directory
     dest_names = []
     while r = chop_basename(dest_prefix)
Index: test/pathname/test_pathname.rb
===================================================================
--- test/pathname/test_pathname.rb	(revision 66415)
+++ test/pathname/test_pathname.rb	(revision 66416)
@@ -1429,4 +1429,18 @@ class TestPathname < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/pathname/test_pathname.rb#L1429
       assert_instance_of(Pathname, foo.relative_path_from(bar))
     end;
   end
+
+  def test_relative_path_from_mock
+    assert_equal(
+      Pathname.new("../bar"),
+      Pathname.new("/foo/bar").relative_path_from(Pathname.new("/foo/baz")))
+    assert_equal(
+      Pathname.new("../bar"),
+      Pathname.new("/foo/bar").relative_path_from("/foo/baz"))
+    obj = Object.new
+    def obj.cleanpath() Pathname.new("/foo/baz") end
+    assert_equal(
+      Pathname.new("../bar"),
+      Pathname.new("/foo/bar").relative_path_from(obj))
+  end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]