Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,8 +855,10 @@ const win32 = {
}
}

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

if (end === -1)
return '';
if (start === end)
end = firstNonSlashEnd;
else if (end === -1)
end = path.length;
return path.slice(start, end);
} else {
for (i = path.length - 1; i >= 0; --i) {
Expand Down
53 changes: 23 additions & 30 deletions test/parallel/test-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ 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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a few tests for absolute paths with this case?

Copy link
Contributor Author

@mscdex mscdex May 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've now added absolute versions of these tests, is that what you meant?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking maybe something more like path.basename('/aaa/bbb') since we have seen regressions for absolute vs relative paths. They may already be there though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I've added that and a couple more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evanlucas does this LGTY now?

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');
assert.equal(path.basename('/aaa/bbb'), 'bbb');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering, why were these tests removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TypeError checks are already done later on in the test file
for all path functions.

assert.equal(path.basename('/aaa/'), 'aaa');
assert.equal(path.basename('/aaa/b'), 'b');
assert.equal(path.basename('/a/b'), 'b');
assert.equal(path.basename('//a'), 'a');

// On Windows a backslash acts as a path separator.
assert.equal(path.win32.basename('\\dir\\basename.ext'), 'basename.ext');
Expand All @@ -23,11 +40,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('foo'), 'foo');
assert.throws(path.win32.basename.bind(null, null), TypeError);
assert.throws(path.win32.basename.bind(null, true), TypeError);
assert.throws(path.win32.basename.bind(null, 1), TypeError);
assert.throws(path.win32.basename.bind(null), TypeError);
assert.throws(path.win32.basename.bind(null, {}), TypeError);
assert.equal(path.win32.basename('aaa\\bbb', '\\bbb'), 'bbb');
assert.equal(path.win32.basename('aaa\\bbb', 'a\\bbb'), 'bbb');
assert.equal(path.win32.basename('aaa\\bbb', 'bbb'), 'bbb');
assert.equal(path.win32.basename('aaa\\bbb\\\\\\\\', 'bbb'), 'bbb');
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.
assert.equal(path.posix.basename('\\dir\\basename.ext'), '\\dir\\basename.ext');
Expand All @@ -36,11 +54,6 @@ assert.equal(path.posix.basename('basename.ext'), 'basename.ext');
assert.equal(path.posix.basename('basename.ext\\'), 'basename.ext\\');
assert.equal(path.posix.basename('basename.ext\\\\'), 'basename.ext\\\\');
assert.equal(path.posix.basename('foo'), 'foo');
assert.throws(path.posix.basename.bind(null, null), TypeError);
assert.throws(path.posix.basename.bind(null, true), TypeError);
assert.throws(path.posix.basename.bind(null, 1), TypeError);
assert.throws(path.posix.basename.bind(null), TypeError);
assert.throws(path.posix.basename.bind(null, {}), TypeError);

// POSIX filenames may include control characters
// c.f. http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html
Expand All @@ -60,11 +73,6 @@ assert.equal(path.posix.dirname(''), '.');
assert.equal(path.posix.dirname('/'), '/');
assert.equal(path.posix.dirname('////'), '/');
assert.equal(path.posix.dirname('foo'), '.');
assert.throws(path.posix.dirname.bind(null, null), TypeError);
assert.throws(path.posix.dirname.bind(null, true), TypeError);
assert.throws(path.posix.dirname.bind(null, 1), TypeError);
assert.throws(path.posix.dirname.bind(null), TypeError);
assert.throws(path.posix.dirname.bind(null, {}), TypeError);

assert.equal(path.win32.dirname('c:\\'), 'c:\\');
assert.equal(path.win32.dirname('c:\\foo'), 'c:\\');
Expand Down Expand Up @@ -100,11 +108,6 @@ assert.equal(path.win32.dirname(''), '.');
assert.equal(path.win32.dirname('/'), '/');
assert.equal(path.win32.dirname('////'), '/');
assert.equal(path.win32.dirname('foo'), '.');
assert.throws(path.win32.dirname.bind(null, null), TypeError);
assert.throws(path.win32.dirname.bind(null, true), TypeError);
assert.throws(path.win32.dirname.bind(null, 1), TypeError);
assert.throws(path.win32.dirname.bind(null), TypeError);
assert.throws(path.win32.dirname.bind(null, {}), TypeError);


// path.extname tests
Expand Down Expand Up @@ -183,11 +186,6 @@ assert.equal(path.win32.extname('file\\'), '');
assert.equal(path.win32.extname('file\\\\'), '');
assert.equal(path.win32.extname('file.\\'), '.');
assert.equal(path.win32.extname('file.\\\\'), '.');
assert.throws(path.win32.extname.bind(null, null), TypeError);
assert.throws(path.win32.extname.bind(null, true), TypeError);
assert.throws(path.win32.extname.bind(null, 1), TypeError);
assert.throws(path.win32.extname.bind(null), TypeError);
assert.throws(path.win32.extname.bind(null, {}), TypeError);

// On *nix, backslash is a valid name component like any other character.
assert.equal(path.posix.extname('.\\'), '');
Expand All @@ -198,11 +196,6 @@ assert.equal(path.posix.extname('file\\'), '');
assert.equal(path.posix.extname('file\\\\'), '');
assert.equal(path.posix.extname('file.\\'), '.\\');
assert.equal(path.posix.extname('file.\\\\'), '.\\\\');
assert.throws(path.posix.extname.bind(null, null), TypeError);
assert.throws(path.posix.extname.bind(null, true), TypeError);
assert.throws(path.posix.extname.bind(null, 1), TypeError);
assert.throws(path.posix.extname.bind(null), TypeError);
assert.throws(path.posix.extname.bind(null, {}), TypeError);


// path.join tests
Expand Down