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

ruby-changes:58292

From: Nobuyoshi <ko1@a...>
Date: Thu, 17 Oct 2019 18:52:11 +0900 (JST)
Subject: [ruby-changes:58292] e169ad93f4 (master): Fixed File.extname at a name ending with a dot

https://git.ruby-lang.org/ruby.git/commit/?id=e169ad93f4

From e169ad93f44e1944ecf7bb65133fd34e8b868ea8 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Thu, 17 Oct 2019 14:42:38 +0900
Subject: Fixed File.extname at a name ending with a dot

File.extname now returns a dot string at a name ending with a dot.
[Bug #15267]

diff --git a/NEWS b/NEWS
index 84f9c1e2..e8fdfe0 100644
--- a/NEWS
+++ b/NEWS
@@ -191,6 +191,14 @@ Fiber:: https://github.com/ruby/ruby/blob/trunk/NEWS#L191
     * Added Fiber#raise that behaves like Fiber#resume but raises an
       exception on the resumed fiber.  [Feature #10344]
 
+File::
+  Modified method::
+
+    * File.extname now returns a dot string at a name ending with a
+      dot.  [Bug #15267]
+
+          File.extname("foo.") #=> "."
+
 FrozenError::
 
   New method::
diff --git a/file.c b/file.c
index e333c86..d10aeaa 100644
--- a/file.c
+++ b/file.c
@@ -4792,7 +4792,7 @@ ruby_enc_find_extname(const char *name, long *len, rb_encoding *enc) https://github.com/ruby/ruby/blob/trunk/file.c#L4792
  *     File.extname("test.rb")         #=> ".rb"
  *     File.extname("a/b/d/test.rb")   #=> ".rb"
  *     File.extname(".a/b/d/test.rb")  #=> ".rb"
- *     File.extname("foo.")            #=> ""
+ *     File.extname("foo.")            #=> "."
  *     File.extname("test")            #=> ""
  *     File.extname(".profile")        #=> ""
  *     File.extname(".profile.sh")     #=> ".sh"
@@ -4810,7 +4810,7 @@ rb_file_s_extname(VALUE klass, VALUE fname) https://github.com/ruby/ruby/blob/trunk/file.c#L4810
     name = StringValueCStr(fname);
     len = RSTRING_LEN(fname);
     e = ruby_enc_find_extname(name, &len, rb_enc_get(fname));
-    if (len <= 1)
+    if (len < 1)
 	return rb_str_new(0, 0);
     extname = rb_str_subseq(fname, e - name, len); /* keep the dot, too! */
     OBJ_INFECT(extname, fname);
diff --git a/spec/ruby/core/file/extname_spec.rb b/spec/ruby/core/file/extname_spec.rb
index 358690b..3b7fa5a 100644
--- a/spec/ruby/core/file/extname_spec.rb
+++ b/spec/ruby/core/file/extname_spec.rb
@@ -20,8 +20,14 @@ describe "File.extname" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/file/extname_spec.rb#L20
     File.extname("..").should ==  ""
     File.extname("...").should ==  ""
     File.extname("....").should ==  ""
-    File.extname(".foo.").should ==  ""
-    File.extname("foo.").should ==  ""
+    guard -> { platform_is :windows or ruby_version_is ""..."2.7" } do
+      File.extname(".foo.").should == ""
+      File.extname("foo.").should == ""
+    end
+    guard -> { platform_is_not :windows and ruby_version_is "2.7" } do
+      File.extname(".foo.").should == "."
+      File.extname("foo.").should == "."
+    end
   end
 
   it "returns only the last extension of a file with several dots" do
diff --git a/test/ruby/test_file_exhaustive.rb b/test/ruby/test_file_exhaustive.rb
index 6b80b82..4bb5479 100644
--- a/test/ruby/test_file_exhaustive.rb
+++ b/test/ruby/test_file_exhaustive.rb
@@ -1308,21 +1308,23 @@ class TestFileExhaustive < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_file_exhaustive.rb#L1308
     assert_equal(".test", File.extname(regular_file))
     assert_equal(".test", File.extname(utf8_file))
     prefixes = ["", "/", ".", "/.", "bar/.", "/bar/."]
-    infixes = ["", " ", "."]
+    infixes = ["", " "]
     infixes2 = infixes + [".ext "]
     appendixes = [""]
     if NTFS
       appendixes << " " << "." << "::$DATA" << "::$DATA.bar"
+    else
+      appendixes << [".", "."]
     end
     prefixes.each do |prefix|
-      appendixes.each do |appendix|
+      appendixes.each do |appendix, ext = ""|
         infixes.each do |infix|
           path = "#{prefix}foo#{infix}#{appendix}"
-          assert_equal("", File.extname(path), "File.extname(#{path.inspect})")
+          assert_equal(ext, File.extname(path), "File.extname(#{path.inspect})")
         end
         infixes2.each do |infix|
           path = "#{prefix}foo#{infix}.ext#{appendix}"
-          assert_equal(".ext", File.extname(path), "File.extname(#{path.inspect})")
+          assert_equal(ext.empty? ? ".ext" : appendix, File.extname(path), "File.extname(#{path.inspect})")
         end
       end
     end
diff --git a/test/ruby/test_path.rb b/test/ruby/test_path.rb
index 6af4fb6..b35e942 100644
--- a/test/ruby/test_path.rb
+++ b/test/ruby/test_path.rb
@@ -236,10 +236,14 @@ class TestPath < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_path.rb#L236
     assert_equal(ext, File.extname('.a/b/d/test.rb'))
     unless /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
       # trailing spaces and dots are ignored on NTFS.
-      ext = ''
+      ext = '.'
     end
     assert_equal(ext, File.extname('a.rb.'))
-    assert_equal('', File.extname('a.'))
+    if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM
+      # trailing spaces and dots are ignored on NTFS.
+      ext = ''
+    end
+    assert_equal(ext, File.extname('a.'))
     assert_equal('', File.extname('.x'))
     assert_equal('', File.extname('..x'))
   end
-- 
cgit v0.10.2


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

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