Skip to content

Commit 5a54e45

Browse files
mscdexjasnell
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: nodejs#5123 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent e1348b0 commit 5a54e45

File tree

3 files changed

+355
-183
lines changed

3 files changed

+355
-183
lines changed

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

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,16 @@ const unixPaths = [
4141
'./file',
4242
'C:\\foo',
4343
'/',
44-
''
44+
'',
45+
'.',
46+
'..',
47+
'/foo',
48+
'/foo.',
49+
'/foo.bar',
50+
'/.',
51+
'/.foo',
52+
'/.foo.bar',
53+
'/foo/bar.baz',
4554
];
4655

4756
const unixSpecialCaseFormatTests = [
@@ -82,6 +91,67 @@ checkErrors(path.posix);
8291
checkFormat(path.win32, winSpecialCaseFormatTests);
8392
checkFormat(path.posix, unixSpecialCaseFormatTests);
8493

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