Skip to content

Commit a8f4db2

Browse files
mscdexrvagg
authored andcommitted
test: improve path tests
This commit adds new tests, executes tests for other platforms instead of limiting platform-specific tests to those platforms, and fixes a few style/formatting inconsistencies. PR-URL: #5123 Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent edf8f8a commit a8f4db2

File tree

3 files changed

+356
-183
lines changed

3 files changed

+356
-183
lines changed

test/parallel/test-path-parse-format.js

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,17 @@ var unixPaths = [
3535
'.\\file',
3636
'./file',
3737
'C:\\foo',
38-
''
38+
'/',
39+
'',
40+
'.',
41+
'..',
42+
'/foo',
43+
'/foo.',
44+
'/foo.bar',
45+
'/.',
46+
'/.foo',
47+
'/.foo.bar',
48+
'/foo/bar.baz',
3949
];
4050

4151
var unixSpecialCaseFormatTests = [
@@ -76,6 +86,67 @@ checkErrors(path.posix);
7686
checkFormat(path.win32, winSpecialCaseFormatTests);
7787
checkFormat(path.posix, unixSpecialCaseFormatTests);
7888

89+
// Test removal of trailing path separators
90+
const trailingTests = [
91+
[ path.win32.parse,
92+
[['.\\', { root: '', dir: '', base: '.', ext: '', name: '.' }],
93+
['\\\\', { root: '\\', dir: '\\', base: '', ext: '', name: '' }],
94+
['\\\\', { root: '\\', dir: '\\', base: '', ext: '', name: '' }],
95+
['c:\\foo\\\\\\',
96+
{ root: 'c:\\', dir: 'c:\\', base: 'foo', ext: '', name: 'foo' }],
97+
['D:\\foo\\\\\\bar.baz',
98+
{ root: 'D:\\',
99+
dir: 'D:\\foo\\\\',
100+
base: 'bar.baz',
101+
ext: '.baz',
102+
name: 'bar'
103+
}
104+
]
105+
]
106+
],
107+
[ path.posix.parse,
108+
[['./', { root: '', dir: '', base: '.', ext: '', name: '.' }],
109+
['//', { root: '/', dir: '/', base: '', ext: '', name: '' }],
110+
['///', { root: '/', dir: '/', base: '', ext: '', name: '' }],
111+
['/foo///', { root: '/', dir: '/', base: 'foo', ext: '', name: 'foo' }],
112+
['/foo///bar.baz',
113+
{ root: '/', dir: '/foo//', base: 'bar.baz', ext: '.baz', name: 'bar' }
114+
]
115+
]
116+
]
117+
];
118+
const failures = [];
119+
trailingTests.forEach(function(test) {
120+
const parse = test[0];
121+
test[1].forEach(function(test) {
122+
const actual = parse(test[0]);
123+
const expected = test[1];
124+
const fn = 'path.' +
125+
(parse === path.win32.parse ? 'win32' : 'posix') +
126+
'.parse(';
127+
const message = fn +
128+
JSON.stringify(test[0]) +
129+
')' +
130+
'\n expect=' + JSON.stringify(expected) +
131+
'\n actual=' + JSON.stringify(actual);
132+
const actualKeys = Object.keys(actual);
133+
const expectedKeys = Object.keys(expected);
134+
let failed = (actualKeys.length !== expectedKeys.length);
135+
if (!failed) {
136+
for (let i = 0; i < actualKeys.length; ++i) {
137+
const key = actualKeys[i];
138+
if (expectedKeys.indexOf(key) === -1 || actual[key] !== expected[key]) {
139+
failed = true;
140+
break;
141+
}
142+
}
143+
}
144+
if (failed)
145+
failures.push('\n' + message);
146+
});
147+
});
148+
assert.equal(failures.length, 0, failures.join(''));
149+
79150
function checkErrors(path) {
80151
errors.forEach(function(errorCase) {
81152
try {

test/parallel/test-path-zero-length-strings.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@ const pwd = process.cwd();
1212

1313
// join will internally ignore all the zero-length strings and it will return
1414
// '.' if the joined string is a zero-length string.
15-
assert.equal(path.join(''), '.');
16-
assert.equal(path.join('', ''), '.');
15+
assert.equal(path.posix.join(''), '.');
16+
assert.equal(path.posix.join('', ''), '.');
17+
assert.equal(path.win32.join(''), '.');
18+
assert.equal(path.win32.join('', ''), '.');
1719
assert.equal(path.join(pwd), pwd);
1820
assert.equal(path.join(pwd, ''), pwd);
1921

2022
// normalize will return '.' if the input is a zero-length string
21-
assert.equal(path.normalize(''), '.');
23+
assert.equal(path.posix.normalize(''), '.');
24+
assert.equal(path.win32.normalize(''), '.');
2225
assert.equal(path.normalize(pwd), pwd);
2326

2427
// Since '' is not a valid path in any of the common environments, return false
25-
assert.equal(path.isAbsolute(''), false);
28+
assert.equal(path.posix.isAbsolute(''), false);
29+
assert.equal(path.win32.isAbsolute(''), false);
2630

2731
// resolve, internally ignores all the zero-length strings and returns the
2832
// current working directory

0 commit comments

Comments
 (0)