Browse Source

path: fix basename() regressions

This commit fixes a regression in basename() for the case when a
supplied extension name completely matches the resulting basename.

Fixes: https://github.com/nodejs/node/issues/6587
PR-URL: https://github.com/nodejs/node/pull/6590
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
v7.x
Brian White 9 years ago
parent
commit
dfaa9c9055
No known key found for this signature in database GPG Key ID: 606D7358F94DA209
  1. 12
      lib/path.js
  2. 17
      test/parallel/test-path.js

12
lib/path.js

@ -855,8 +855,10 @@ const win32 = {
} }
} }
if (end === -1) if (start === end)
return ''; end = firstNonSlashEnd;
else if (end === -1)
end = path.length;
return path.slice(start, end); return path.slice(start, end);
} else { } else {
for (i = path.length - 1; i >= start; --i) { for (i = path.length - 1; i >= start; --i) {
@ -1398,8 +1400,10 @@ const posix = {
} }
} }
if (end === -1) if (start === end)
return ''; end = firstNonSlashEnd;
else if (end === -1)
end = path.length;
return path.slice(start, end); return path.slice(start, end);
} else { } else {
for (i = path.length - 1; i >= 0; --i) { for (i = path.length - 1; i >= 0; --i) {

17
test/parallel/test-path.js

@ -15,6 +15,12 @@ assert.equal(path.basename('/basename.ext'), 'basename.ext');
assert.equal(path.basename('basename.ext'), 'basename.ext'); assert.equal(path.basename('basename.ext'), 'basename.ext');
assert.equal(path.basename('basename.ext/'), 'basename.ext'); assert.equal(path.basename('basename.ext/'), 'basename.ext');
assert.equal(path.basename('basename.ext//'), 'basename.ext'); assert.equal(path.basename('basename.ext//'), 'basename.ext');
assert.equal(path.basename('aaa/bbb', '/bbb'), 'bbb');
assert.equal(path.basename('aaa/bbb', 'a/bbb'), 'bbb');
assert.equal(path.basename('aaa/bbb', 'bbb'), 'bbb');
assert.equal(path.basename('aaa/bbb//', 'bbb'), 'bbb');
assert.equal(path.basename('aaa/bbb', 'bb'), 'b');
assert.equal(path.basename('aaa/bbb', 'b'), 'bb');
// On Windows a backslash acts as a path separator. // On Windows a backslash acts as a path separator.
assert.equal(path.win32.basename('\\dir\\basename.ext'), 'basename.ext'); assert.equal(path.win32.basename('\\dir\\basename.ext'), 'basename.ext');
@ -23,11 +29,12 @@ assert.equal(path.win32.basename('basename.ext'), 'basename.ext');
assert.equal(path.win32.basename('basename.ext\\'), 'basename.ext'); assert.equal(path.win32.basename('basename.ext\\'), 'basename.ext');
assert.equal(path.win32.basename('basename.ext\\\\'), 'basename.ext'); assert.equal(path.win32.basename('basename.ext\\\\'), 'basename.ext');
assert.equal(path.win32.basename('foo'), 'foo'); assert.equal(path.win32.basename('foo'), 'foo');
assert.throws(path.win32.basename.bind(null, null), TypeError); assert.equal(path.win32.basename('aaa\\bbb', '\\bbb'), 'bbb');
assert.throws(path.win32.basename.bind(null, true), TypeError); assert.equal(path.win32.basename('aaa\\bbb', 'a\\bbb'), 'bbb');
assert.throws(path.win32.basename.bind(null, 1), TypeError); assert.equal(path.win32.basename('aaa\\bbb', 'bbb'), 'bbb');
assert.throws(path.win32.basename.bind(null), TypeError); assert.equal(path.win32.basename('aaa\\bbb\\\\\\\\', 'bbb'), 'bbb');
assert.throws(path.win32.basename.bind(null, {}), TypeError); assert.equal(path.win32.basename('aaa\\bbb', 'bb'), 'b');
assert.equal(path.win32.basename('aaa\\bbb', 'b'), 'bb');
// On unix a backslash is just treated as any other character. // On unix a backslash is just treated as any other character.
assert.equal(path.posix.basename('\\dir\\basename.ext'), '\\dir\\basename.ext'); assert.equal(path.posix.basename('\\dir\\basename.ext'), '\\dir\\basename.ext');

Loading…
Cancel
Save