Skip to content

Commit

Permalink
Revert "Should return "." for File.extname("file.") also on Windows"
Browse files Browse the repository at this point in the history
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 61aff0c.
  • Loading branch information
unak committed Dec 23, 2019
1 parent 048f797 commit 204dc3f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 23 deletions.
22 changes: 5 additions & 17 deletions file.c
Expand Up @@ -4711,26 +4711,13 @@ ruby_enc_find_extname(const char *name, long *len, rb_encoding *enc)
while (*p) {
if (*p == '.' || istrailinggarbage(*p)) {
#if USE_NTFS
const char *first = 0, *last, *dot;
if (*p == '.') first = p;
last = p++;
dot = last;
const char *last = p++, *dot = last;
while (istrailinggarbage(*p)) {
if (*p == '.') {
dot = p;
if (!first) {
first = p;
}
}
if (*p == '.') dot = p;
p++;
}
if (!*p || isADS(*p)) {
if (first == dot && e == 0) {
e = first;
}
else {
p = last;
}
p = last;
break;
}
if (*last == '.' || dot > last) e = dot;
Expand Down Expand Up @@ -4779,7 +4766,8 @@ ruby_enc_find_extname(const char *name, long *len, rb_encoding *enc)
* 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.") #=> "" on Windows
* File.extname("foo.") #=> "." on non-Windows
* File.extname("test") #=> ""
* File.extname(".profile") #=> ""
* File.extname(".profile.sh") #=> ".sh"
Expand Down
4 changes: 2 additions & 2 deletions spec/ruby/core/file/extname_spec.rb
Expand Up @@ -23,14 +23,14 @@
end

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
File.extname(".foo.").should == ""
File.extname("foo.").should == ""
end
end

ruby_version_is "2.7" do
guard -> { platform_is_not :windows and ruby_version_is "2.7" } do
it "returns '.'" do
File.extname(".foo.").should == "."
File.extname("foo.").should == "."
Expand Down
6 changes: 3 additions & 3 deletions test/ruby/test_file_exhaustive.rb
Expand Up @@ -1268,19 +1268,19 @@ def test_extname
infixes2 = infixes + [".ext "]
appendixes = [""]
if NTFS
appendixes << " " << [".", ".", ""] << "::$DATA" << "::$DATA.bar"
appendixes << " " << "." << "::$DATA" << "::$DATA.bar"
else
appendixes << [".", "."]
end
prefixes.each do |prefix|
appendixes.each do |appendix, ext = "", ext2 = ext|
appendixes.each do |appendix, ext = ""|
infixes.each do |infix|
path = "#{prefix}foo#{infix}#{appendix}"
assert_equal(ext, File.extname(path), "File.extname(#{path.inspect})")
end
infixes2.each do |infix|
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
Expand Down
6 changes: 5 additions & 1 deletion test/ruby/test_path.rb
Expand Up @@ -239,7 +239,11 @@ def test_extname
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
Expand Down

0 comments on commit 204dc3f

Please sign in to comment.