1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Revert "Should return "." for File.extname("file.") also on Windows"

We want to introduce consistency and better compatibility with unixen,
but the Windows APIs doues not have consistency fundamentally and
we can not found any logical way...

This reverts commit 61aff0cd18.
This commit is contained in:
NAKAMURA Usaku 2019-12-23 11:54:25 +09:00
parent 048f797bf0
commit 204dc3f39f
4 changed files with 15 additions and 23 deletions

22
file.c
View file

@ -4711,26 +4711,13 @@ ruby_enc_find_extname(const char *name, long *len, rb_encoding *enc)
while (*p) { while (*p) {
if (*p == '.' || istrailinggarbage(*p)) { if (*p == '.' || istrailinggarbage(*p)) {
#if USE_NTFS #if USE_NTFS
const char *first = 0, *last, *dot; const char *last = p++, *dot = last;
if (*p == '.') first = p;
last = p++;
dot = last;
while (istrailinggarbage(*p)) { while (istrailinggarbage(*p)) {
if (*p == '.') { if (*p == '.') dot = p;
dot = p;
if (!first) {
first = p;
}
}
p++; p++;
} }
if (!*p || isADS(*p)) { if (!*p || isADS(*p)) {
if (first == dot && e == 0) { p = last;
e = first;
}
else {
p = last;
}
break; break;
} }
if (*last == '.' || dot > last) e = dot; if (*last == '.' || dot > last) e = dot;
@ -4779,7 +4766,8 @@ ruby_enc_find_extname(const char *name, long *len, rb_encoding *enc)
* File.extname("test.rb") #=> ".rb" * File.extname("test.rb") #=> ".rb"
* File.extname("a/b/d/test.rb") #=> ".rb" * File.extname("a/b/d/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.") #=> "" on Windows
* File.extname("foo.") #=> "." on non-Windows
* File.extname("test") #=> "" * File.extname("test") #=> ""
* File.extname(".profile") #=> "" * File.extname(".profile") #=> ""
* File.extname(".profile.sh") #=> ".sh" * File.extname(".profile.sh") #=> ".sh"

View file

@ -23,14 +23,14 @@ describe "File.extname" do
end end
describe "for a filename ending with a dot" do describe "for a filename ending with a dot" do
ruby_version_is ""..."2.7" do guard -> { platform_is :windows or ruby_version_is ""..."2.7" } do
it "returns ''" do it "returns ''" do
File.extname(".foo.").should == "" File.extname(".foo.").should == ""
File.extname("foo.").should == "" File.extname("foo.").should == ""
end end
end end
ruby_version_is "2.7" do guard -> { platform_is_not :windows and ruby_version_is "2.7" } do
it "returns '.'" do it "returns '.'" do
File.extname(".foo.").should == "." File.extname(".foo.").should == "."
File.extname("foo.").should == "." File.extname("foo.").should == "."

View file

@ -1268,19 +1268,19 @@ class TestFileExhaustive < Test::Unit::TestCase
infixes2 = infixes + [".ext "] infixes2 = infixes + [".ext "]
appendixes = [""] appendixes = [""]
if NTFS if NTFS
appendixes << " " << [".", ".", ""] << "::$DATA" << "::$DATA.bar" appendixes << " " << "." << "::$DATA" << "::$DATA.bar"
else else
appendixes << [".", "."] appendixes << [".", "."]
end end
prefixes.each do |prefix| prefixes.each do |prefix|
appendixes.each do |appendix, ext = "", ext2 = ext| appendixes.each do |appendix, ext = ""|
infixes.each do |infix| infixes.each do |infix|
path = "#{prefix}foo#{infix}#{appendix}" path = "#{prefix}foo#{infix}#{appendix}"
assert_equal(ext, File.extname(path), "File.extname(#{path.inspect})") assert_equal(ext, File.extname(path), "File.extname(#{path.inspect})")
end end
infixes2.each do |infix| infixes2.each do |infix|
path = "#{prefix}foo#{infix}.ext#{appendix}" path = "#{prefix}foo#{infix}.ext#{appendix}"
assert_equal(ext2.empty? ? ".ext" : appendix, File.extname(path), "File.extname(#{path.inspect})") assert_equal(ext.empty? ? ".ext" : appendix, File.extname(path), "File.extname(#{path.inspect})")
end end
end end
end end

View file

@ -239,7 +239,11 @@ class TestPath < Test::Unit::TestCase
ext = '.' ext = '.'
end end
assert_equal(ext, File.extname('a.rb.')) 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'))
assert_equal('', File.extname('..x')) assert_equal('', File.extname('..x'))
end end