Skip to content

Commit

Permalink
Should return "." for File.extname("file.") also on Windows
Browse files Browse the repository at this point in the history
But not changes another cases, such as "file.rb."
[Bug #15267]
  • Loading branch information
unak committed Dec 21, 2019
1 parent 29ea228 commit 61aff0c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
22 changes: 17 additions & 5 deletions file.c
Expand Up @@ -4711,13 +4711,26 @@ ruby_enc_find_extname(const char *name, long *len, rb_encoding *enc)
while (*p) {
if (*p == '.' || istrailinggarbage(*p)) {
#if USE_NTFS
const char *last = p++, *dot = last;
const char *first = 0, *last, *dot;
if (*p == '.') first = p;
last = p++;
dot = last;
while (istrailinggarbage(*p)) {
if (*p == '.') dot = p;
if (*p == '.') {
dot = p;
if (!first) {
first = p;
}
}
p++;
}
if (!*p || isADS(*p)) {
p = last;
if (first == dot && e == 0) {
e = first;
}
else {
p = last;
}
break;
}
if (*last == '.' || dot > last) e = dot;
Expand Down Expand Up @@ -4766,8 +4779,7 @@ 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.") #=> "" on Windows
* File.extname("foo.") #=> "." on non-Windows
* File.extname("foo.") #=> "."
* 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
guard -> { platform_is :windows or ruby_version_is ""..."2.7" } do
ruby_version_is ""..."2.7" do
it "returns ''" do
File.extname(".foo.").should == ""
File.extname("foo.").should == ""
end
end

guard -> { platform_is_not :windows and ruby_version_is "2.7" } do
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 = ""|
appendixes.each do |appendix, ext = "", ext2 = 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(ext.empty? ? ".ext" : appendix, File.extname(path), "File.extname(#{path.inspect})")
assert_equal(ext2.empty? ? ".ext" : appendix, File.extname(path), "File.extname(#{path.inspect})")
end
end
end
Expand Down
6 changes: 1 addition & 5 deletions test/ruby/test_path.rb
Expand Up @@ -239,11 +239,7 @@ def test_extname
ext = '.'
end
assert_equal(ext, File.extname('a.rb.'))
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('a.'))
assert_equal('', File.extname('.x'))
assert_equal('', File.extname('..x'))
end
Expand Down

0 comments on commit 61aff0c

Please sign in to comment.