Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path: fix regression #26912

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ const win32 = {
ret.root = ret.dir = path;
return ret;
}
ret.base = ret.name = path;
return ret;
}
// Try to match a root
Expand Down
54 changes: 24 additions & 30 deletions test/parallel/test-path-parse-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ const winPaths = [
];

const winSpecialCaseParseTests = [
['/foo/bar', { root: '/' }],
['t', { base: 't', name: 't', root: '', dir: '', ext: '' }],
['/foo/bar', { root: '/', dir: '/foo', base: 'bar', ext: '', name: 'bar' }],
];

const winSpecialCaseFormatTests = [
Expand Down Expand Up @@ -98,21 +99,16 @@ const unixSpecialCaseFormatTests = [
[{}, '']
];

const expectedMessage = common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError
}, 18);

const errors = [
{ method: 'parse', input: [null], message: expectedMessage },
{ method: 'parse', input: [{}], message: expectedMessage },
{ method: 'parse', input: [true], message: expectedMessage },
{ method: 'parse', input: [1], message: expectedMessage },
{ method: 'parse', input: [], message: expectedMessage },
{ method: 'format', input: [null], message: expectedMessage },
{ method: 'format', input: [''], message: expectedMessage },
{ method: 'format', input: [true], message: expectedMessage },
{ method: 'format', input: [1], message: expectedMessage },
{ method: 'parse', input: [null] },
{ method: 'parse', input: [{}] },
{ method: 'parse', input: [true] },
{ method: 'parse', input: [1] },
{ method: 'parse', input: [] },
{ method: 'format', input: [null] },
{ method: 'format', input: [''] },
{ method: 'format', input: [true] },
{ method: 'format', input: [1] },
];

checkParseFormat(path.win32, winPaths);
Expand Down Expand Up @@ -153,10 +149,10 @@ const trailingTests = [
]
];
const failures = [];
trailingTests.forEach(function(test) {
trailingTests.forEach((test) => {
const parse = test[0];
const os = parse === path.win32.parse ? 'win32' : 'posix';
test[1].forEach(function(test) {
test[1].forEach((test) => {
const actual = parse(test[0]);
const expected = test[1];
const message = `path.${os}.parse(${JSON.stringify(test[0])})\n expect=${
Expand All @@ -180,15 +176,18 @@ trailingTests.forEach(function(test) {
assert.strictEqual(failures.length, 0, failures.join(''));

function checkErrors(path) {
errors.forEach(function(errorCase) {
errors.forEach(({ method, input }) => {
assert.throws(() => {
path[errorCase.method].apply(path, errorCase.input);
}, errorCase.message);
path[method].apply(path, input);
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError'
});
});
}

function checkParseFormat(path, paths) {
paths.forEach(function([element, root]) {
paths.forEach(([element, root]) => {
const output = path.parse(element);
assert.strictEqual(typeof output.root, 'string');
assert.strictEqual(typeof output.dir, 'string');
Expand All @@ -205,19 +204,14 @@ function checkParseFormat(path, paths) {
}

function checkSpecialCaseParseFormat(path, testCases) {
testCases.forEach(function(testCase) {
const element = testCase[0];
const expect = testCase[1];
const output = path.parse(element);
Object.keys(expect).forEach(function(key) {
assert.strictEqual(output[key], expect[key]);
});
testCases.forEach(([element, expect]) => {
assert.deepStrictEqual(path.parse(element), expect);
});
}

function checkFormat(path, testCases) {
testCases.forEach(function(testCase) {
assert.strictEqual(path.format(testCase[0]), testCase[1]);
testCases.forEach(([element, expect]) => {
assert.strictEqual(path.format(element), expect);
});

[null, undefined, 1, true, false, 'string'].forEach((pathObject) => {
Expand Down