Skip to content

Commit 00e387f

Browse files
targosMylesBorins
authored andcommitted
test: split path tests into multiple files
Create one file for testing each function of the path module. Keep general error tests and tests for constant properties in test-path.js. PR-URL: #15093 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]>
1 parent e9efe44 commit 00e387f

10 files changed

+605
-587
lines changed

test/parallel/test-path-basename.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const path = require('path');
5+
6+
assert.strictEqual(path.basename(__filename), 'test-path-basename.js');
7+
assert.strictEqual(path.basename(__filename, '.js'), 'test-path-basename');
8+
assert.strictEqual(path.basename('.js', '.js'), '');
9+
assert.strictEqual(path.basename(''), '');
10+
assert.strictEqual(path.basename('/dir/basename.ext'), 'basename.ext');
11+
assert.strictEqual(path.basename('/basename.ext'), 'basename.ext');
12+
assert.strictEqual(path.basename('basename.ext'), 'basename.ext');
13+
assert.strictEqual(path.basename('basename.ext/'), 'basename.ext');
14+
assert.strictEqual(path.basename('basename.ext//'), 'basename.ext');
15+
assert.strictEqual(path.basename('aaa/bbb', '/bbb'), 'bbb');
16+
assert.strictEqual(path.basename('aaa/bbb', 'a/bbb'), 'bbb');
17+
assert.strictEqual(path.basename('aaa/bbb', 'bbb'), 'bbb');
18+
assert.strictEqual(path.basename('aaa/bbb//', 'bbb'), 'bbb');
19+
assert.strictEqual(path.basename('aaa/bbb', 'bb'), 'b');
20+
assert.strictEqual(path.basename('aaa/bbb', 'b'), 'bb');
21+
assert.strictEqual(path.basename('/aaa/bbb', '/bbb'), 'bbb');
22+
assert.strictEqual(path.basename('/aaa/bbb', 'a/bbb'), 'bbb');
23+
assert.strictEqual(path.basename('/aaa/bbb', 'bbb'), 'bbb');
24+
assert.strictEqual(path.basename('/aaa/bbb//', 'bbb'), 'bbb');
25+
assert.strictEqual(path.basename('/aaa/bbb', 'bb'), 'b');
26+
assert.strictEqual(path.basename('/aaa/bbb', 'b'), 'bb');
27+
assert.strictEqual(path.basename('/aaa/bbb'), 'bbb');
28+
assert.strictEqual(path.basename('/aaa/'), 'aaa');
29+
assert.strictEqual(path.basename('/aaa/b'), 'b');
30+
assert.strictEqual(path.basename('/a/b'), 'b');
31+
assert.strictEqual(path.basename('//a'), 'a');
32+
33+
// On Windows a backslash acts as a path separator.
34+
assert.strictEqual(path.win32.basename('\\dir\\basename.ext'), 'basename.ext');
35+
assert.strictEqual(path.win32.basename('\\basename.ext'), 'basename.ext');
36+
assert.strictEqual(path.win32.basename('basename.ext'), 'basename.ext');
37+
assert.strictEqual(path.win32.basename('basename.ext\\'), 'basename.ext');
38+
assert.strictEqual(path.win32.basename('basename.ext\\\\'), 'basename.ext');
39+
assert.strictEqual(path.win32.basename('foo'), 'foo');
40+
assert.strictEqual(path.win32.basename('aaa\\bbb', '\\bbb'), 'bbb');
41+
assert.strictEqual(path.win32.basename('aaa\\bbb', 'a\\bbb'), 'bbb');
42+
assert.strictEqual(path.win32.basename('aaa\\bbb', 'bbb'), 'bbb');
43+
assert.strictEqual(path.win32.basename('aaa\\bbb\\\\\\\\', 'bbb'), 'bbb');
44+
assert.strictEqual(path.win32.basename('aaa\\bbb', 'bb'), 'b');
45+
assert.strictEqual(path.win32.basename('aaa\\bbb', 'b'), 'bb');
46+
assert.strictEqual(path.win32.basename('C:'), '');
47+
assert.strictEqual(path.win32.basename('C:.'), '.');
48+
assert.strictEqual(path.win32.basename('C:\\'), '');
49+
assert.strictEqual(path.win32.basename('C:\\dir\\base.ext'), 'base.ext');
50+
assert.strictEqual(path.win32.basename('C:\\basename.ext'), 'basename.ext');
51+
assert.strictEqual(path.win32.basename('C:basename.ext'), 'basename.ext');
52+
assert.strictEqual(path.win32.basename('C:basename.ext\\'), 'basename.ext');
53+
assert.strictEqual(path.win32.basename('C:basename.ext\\\\'), 'basename.ext');
54+
assert.strictEqual(path.win32.basename('C:foo'), 'foo');
55+
assert.strictEqual(path.win32.basename('file:stream'), 'file:stream');
56+
57+
// On unix a backslash is just treated as any other character.
58+
assert.strictEqual(path.posix.basename('\\dir\\basename.ext'),
59+
'\\dir\\basename.ext');
60+
assert.strictEqual(path.posix.basename('\\basename.ext'), '\\basename.ext');
61+
assert.strictEqual(path.posix.basename('basename.ext'), 'basename.ext');
62+
assert.strictEqual(path.posix.basename('basename.ext\\'), 'basename.ext\\');
63+
assert.strictEqual(path.posix.basename('basename.ext\\\\'), 'basename.ext\\\\');
64+
assert.strictEqual(path.posix.basename('foo'), 'foo');
65+
66+
// POSIX filenames may include control characters
67+
// c.f. http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html
68+
const controlCharFilename = `Icon${String.fromCharCode(13)}`;
69+
assert.strictEqual(path.posix.basename(`/a/b/${controlCharFilename}`),
70+
controlCharFilename);

test/parallel/test-path-dirname.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const path = require('path');
5+
6+
assert.strictEqual(path.dirname(__filename).substr(-13),
7+
common.isWindows ? 'test\\parallel' : 'test/parallel');
8+
9+
assert.strictEqual(path.posix.dirname('/a/b/'), '/a');
10+
assert.strictEqual(path.posix.dirname('/a/b'), '/a');
11+
assert.strictEqual(path.posix.dirname('/a'), '/');
12+
assert.strictEqual(path.posix.dirname(''), '.');
13+
assert.strictEqual(path.posix.dirname('/'), '/');
14+
assert.strictEqual(path.posix.dirname('////'), '/');
15+
assert.strictEqual(path.posix.dirname('//a'), '//');
16+
assert.strictEqual(path.posix.dirname('foo'), '.');
17+
18+
assert.strictEqual(path.win32.dirname('c:\\'), 'c:\\');
19+
assert.strictEqual(path.win32.dirname('c:\\foo'), 'c:\\');
20+
assert.strictEqual(path.win32.dirname('c:\\foo\\'), 'c:\\');
21+
assert.strictEqual(path.win32.dirname('c:\\foo\\bar'), 'c:\\foo');
22+
assert.strictEqual(path.win32.dirname('c:\\foo\\bar\\'), 'c:\\foo');
23+
assert.strictEqual(path.win32.dirname('c:\\foo\\bar\\baz'), 'c:\\foo\\bar');
24+
assert.strictEqual(path.win32.dirname('\\'), '\\');
25+
assert.strictEqual(path.win32.dirname('\\foo'), '\\');
26+
assert.strictEqual(path.win32.dirname('\\foo\\'), '\\');
27+
assert.strictEqual(path.win32.dirname('\\foo\\bar'), '\\foo');
28+
assert.strictEqual(path.win32.dirname('\\foo\\bar\\'), '\\foo');
29+
assert.strictEqual(path.win32.dirname('\\foo\\bar\\baz'), '\\foo\\bar');
30+
assert.strictEqual(path.win32.dirname('c:'), 'c:');
31+
assert.strictEqual(path.win32.dirname('c:foo'), 'c:');
32+
assert.strictEqual(path.win32.dirname('c:foo\\'), 'c:');
33+
assert.strictEqual(path.win32.dirname('c:foo\\bar'), 'c:foo');
34+
assert.strictEqual(path.win32.dirname('c:foo\\bar\\'), 'c:foo');
35+
assert.strictEqual(path.win32.dirname('c:foo\\bar\\baz'), 'c:foo\\bar');
36+
assert.strictEqual(path.win32.dirname('file:stream'), '.');
37+
assert.strictEqual(path.win32.dirname('dir\\file:stream'), 'dir');
38+
assert.strictEqual(path.win32.dirname('\\\\unc\\share'),
39+
'\\\\unc\\share');
40+
assert.strictEqual(path.win32.dirname('\\\\unc\\share\\foo'),
41+
'\\\\unc\\share\\');
42+
assert.strictEqual(path.win32.dirname('\\\\unc\\share\\foo\\'),
43+
'\\\\unc\\share\\');
44+
assert.strictEqual(path.win32.dirname('\\\\unc\\share\\foo\\bar'),
45+
'\\\\unc\\share\\foo');
46+
assert.strictEqual(path.win32.dirname('\\\\unc\\share\\foo\\bar\\'),
47+
'\\\\unc\\share\\foo');
48+
assert.strictEqual(path.win32.dirname('\\\\unc\\share\\foo\\bar\\baz'),
49+
'\\\\unc\\share\\foo\\bar');
50+
assert.strictEqual(path.win32.dirname('/a/b/'), '/a');
51+
assert.strictEqual(path.win32.dirname('/a/b'), '/a');
52+
assert.strictEqual(path.win32.dirname('/a'), '/');
53+
assert.strictEqual(path.win32.dirname(''), '.');
54+
assert.strictEqual(path.win32.dirname('/'), '/');
55+
assert.strictEqual(path.win32.dirname('////'), '/');
56+
assert.strictEqual(path.win32.dirname('foo'), '.');

test/parallel/test-path-extname.js

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const path = require('path');
5+
6+
const failures = [];
7+
const slashRE = /\//g;
8+
9+
[
10+
[__filename, '.js'],
11+
['', ''],
12+
['/path/to/file', ''],
13+
['/path/to/file.ext', '.ext'],
14+
['/path.to/file.ext', '.ext'],
15+
['/path.to/file', ''],
16+
['/path.to/.file', ''],
17+
['/path.to/.file.ext', '.ext'],
18+
['/path/to/f.ext', '.ext'],
19+
['/path/to/..ext', '.ext'],
20+
['/path/to/..', ''],
21+
['file', ''],
22+
['file.ext', '.ext'],
23+
['.file', ''],
24+
['.file.ext', '.ext'],
25+
['/file', ''],
26+
['/file.ext', '.ext'],
27+
['/.file', ''],
28+
['/.file.ext', '.ext'],
29+
['.path/file.ext', '.ext'],
30+
['file.ext.ext', '.ext'],
31+
['file.', '.'],
32+
['.', ''],
33+
['./', ''],
34+
['.file.ext', '.ext'],
35+
['.file', ''],
36+
['.file.', '.'],
37+
['.file..', '.'],
38+
['..', ''],
39+
['../', ''],
40+
['..file.ext', '.ext'],
41+
['..file', '.file'],
42+
['..file.', '.'],
43+
['..file..', '.'],
44+
['...', '.'],
45+
['...ext', '.ext'],
46+
['....', '.'],
47+
['file.ext/', '.ext'],
48+
['file.ext//', '.ext'],
49+
['file/', ''],
50+
['file//', ''],
51+
['file./', '.'],
52+
['file.//', '.'],
53+
].forEach((test) => {
54+
const expected = test[1];
55+
[path.posix.extname, path.win32.extname].forEach((extname) => {
56+
let input = test[0];
57+
let os;
58+
if (extname === path.win32.extname) {
59+
input = input.replace(slashRE, '\\');
60+
os = 'win32';
61+
} else {
62+
os = 'posix';
63+
}
64+
const actual = extname(input);
65+
const message = `path.${os}.extname(${JSON.stringify(input)})\n expect=${
66+
JSON.stringify(expected)}\n actual=${JSON.stringify(actual)}`;
67+
if (actual !== expected)
68+
failures.push(`\n${message}`);
69+
});
70+
{
71+
const input = `C:${test[0].replace(slashRE, '\\')}`;
72+
const actual = path.win32.extname(input);
73+
const message = `path.win32.extname(${JSON.stringify(input)})\n expect=${
74+
JSON.stringify(expected)}\n actual=${JSON.stringify(actual)}`;
75+
if (actual !== expected)
76+
failures.push(`\n${message}`);
77+
}
78+
});
79+
assert.strictEqual(failures.length, 0, failures.join(''));
80+
81+
// On Windows, backslash is a path separator.
82+
assert.strictEqual(path.win32.extname('.\\'), '');
83+
assert.strictEqual(path.win32.extname('..\\'), '');
84+
assert.strictEqual(path.win32.extname('file.ext\\'), '.ext');
85+
assert.strictEqual(path.win32.extname('file.ext\\\\'), '.ext');
86+
assert.strictEqual(path.win32.extname('file\\'), '');
87+
assert.strictEqual(path.win32.extname('file\\\\'), '');
88+
assert.strictEqual(path.win32.extname('file.\\'), '.');
89+
assert.strictEqual(path.win32.extname('file.\\\\'), '.');
90+
91+
// On *nix, backslash is a valid name component like any other character.
92+
assert.strictEqual(path.posix.extname('.\\'), '');
93+
assert.strictEqual(path.posix.extname('..\\'), '.\\');
94+
assert.strictEqual(path.posix.extname('file.ext\\'), '.ext\\');
95+
assert.strictEqual(path.posix.extname('file.ext\\\\'), '.ext\\\\');
96+
assert.strictEqual(path.posix.extname('file\\'), '');
97+
assert.strictEqual(path.posix.extname('file\\\\'), '');
98+
assert.strictEqual(path.posix.extname('file.\\'), '.\\');
99+
assert.strictEqual(path.posix.extname('file.\\\\'), '.\\\\');

test/parallel/test-path-isabsolute.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const path = require('path');
5+
6+
assert.strictEqual(path.win32.isAbsolute('/'), true);
7+
assert.strictEqual(path.win32.isAbsolute('//'), true);
8+
assert.strictEqual(path.win32.isAbsolute('//server'), true);
9+
assert.strictEqual(path.win32.isAbsolute('//server/file'), true);
10+
assert.strictEqual(path.win32.isAbsolute('\\\\server\\file'), true);
11+
assert.strictEqual(path.win32.isAbsolute('\\\\server'), true);
12+
assert.strictEqual(path.win32.isAbsolute('\\\\'), true);
13+
assert.strictEqual(path.win32.isAbsolute('c'), false);
14+
assert.strictEqual(path.win32.isAbsolute('c:'), false);
15+
assert.strictEqual(path.win32.isAbsolute('c:\\'), true);
16+
assert.strictEqual(path.win32.isAbsolute('c:/'), true);
17+
assert.strictEqual(path.win32.isAbsolute('c://'), true);
18+
assert.strictEqual(path.win32.isAbsolute('C:/Users/'), true);
19+
assert.strictEqual(path.win32.isAbsolute('C:\\Users\\'), true);
20+
assert.strictEqual(path.win32.isAbsolute('C:cwd/another'), false);
21+
assert.strictEqual(path.win32.isAbsolute('C:cwd\\another'), false);
22+
assert.strictEqual(path.win32.isAbsolute('directory/directory'), false);
23+
assert.strictEqual(path.win32.isAbsolute('directory\\directory'), false);
24+
25+
assert.strictEqual(path.posix.isAbsolute('/home/foo'), true);
26+
assert.strictEqual(path.posix.isAbsolute('/home/foo/..'), true);
27+
assert.strictEqual(path.posix.isAbsolute('bar/'), false);
28+
assert.strictEqual(path.posix.isAbsolute('./baz'), false);

test/parallel/test-path-join.js

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const path = require('path');
5+
6+
const failures = [];
7+
const backslashRE = /\\/g;
8+
9+
const joinTests = [
10+
[ [path.posix.join, path.win32.join],
11+
// arguments result
12+
[[['.', 'x/b', '..', '/b/c.js'], 'x/b/c.js'],
13+
[[], '.'],
14+
[['/.', 'x/b', '..', '/b/c.js'], '/x/b/c.js'],
15+
[['/foo', '../../../bar'], '/bar'],
16+
[['foo', '../../../bar'], '../../bar'],
17+
[['foo/', '../../../bar'], '../../bar'],
18+
[['foo/x', '../../../bar'], '../bar'],
19+
[['foo/x', './bar'], 'foo/x/bar'],
20+
[['foo/x/', './bar'], 'foo/x/bar'],
21+
[['foo/x/', '.', 'bar'], 'foo/x/bar'],
22+
[['./'], './'],
23+
[['.', './'], './'],
24+
[['.', '.', '.'], '.'],
25+
[['.', './', '.'], '.'],
26+
[['.', '/./', '.'], '.'],
27+
[['.', '/////./', '.'], '.'],
28+
[['.'], '.'],
29+
[['', '.'], '.'],
30+
[['', 'foo'], 'foo'],
31+
[['foo', '/bar'], 'foo/bar'],
32+
[['', '/foo'], '/foo'],
33+
[['', '', '/foo'], '/foo'],
34+
[['', '', 'foo'], 'foo'],
35+
[['foo', ''], 'foo'],
36+
[['foo/', ''], 'foo/'],
37+
[['foo', '', '/bar'], 'foo/bar'],
38+
[['./', '..', '/foo'], '../foo'],
39+
[['./', '..', '..', '/foo'], '../../foo'],
40+
[['.', '..', '..', '/foo'], '../../foo'],
41+
[['', '..', '..', '/foo'], '../../foo'],
42+
[['/'], '/'],
43+
[['/', '.'], '/'],
44+
[['/', '..'], '/'],
45+
[['/', '..', '..'], '/'],
46+
[[''], '.'],
47+
[['', ''], '.'],
48+
[[' /foo'], ' /foo'],
49+
[[' ', 'foo'], ' /foo'],
50+
[[' ', '.'], ' '],
51+
[[' ', '/'], ' /'],
52+
[[' ', ''], ' '],
53+
[['/', 'foo'], '/foo'],
54+
[['/', '/foo'], '/foo'],
55+
[['/', '//foo'], '/foo'],
56+
[['/', '', '/foo'], '/foo'],
57+
[['', '/', 'foo'], '/foo'],
58+
[['', '/', '/foo'], '/foo']
59+
]
60+
]
61+
];
62+
63+
// Windows-specific join tests
64+
joinTests.push([
65+
path.win32.join,
66+
joinTests[0][1].slice(0).concat(
67+
[// arguments result
68+
// UNC path expected
69+
[['//foo/bar'], '\\\\foo\\bar\\'],
70+
[['\\/foo/bar'], '\\\\foo\\bar\\'],
71+
[['\\\\foo/bar'], '\\\\foo\\bar\\'],
72+
// UNC path expected - server and share separate
73+
[['//foo', 'bar'], '\\\\foo\\bar\\'],
74+
[['//foo/', 'bar'], '\\\\foo\\bar\\'],
75+
[['//foo', '/bar'], '\\\\foo\\bar\\'],
76+
// UNC path expected - questionable
77+
[['//foo', '', 'bar'], '\\\\foo\\bar\\'],
78+
[['//foo/', '', 'bar'], '\\\\foo\\bar\\'],
79+
[['//foo/', '', '/bar'], '\\\\foo\\bar\\'],
80+
// UNC path expected - even more questionable
81+
[['', '//foo', 'bar'], '\\\\foo\\bar\\'],
82+
[['', '//foo/', 'bar'], '\\\\foo\\bar\\'],
83+
[['', '//foo/', '/bar'], '\\\\foo\\bar\\'],
84+
// No UNC path expected (no double slash in first component)
85+
[['\\', 'foo/bar'], '\\foo\\bar'],
86+
[['\\', '/foo/bar'], '\\foo\\bar'],
87+
[['', '/', '/foo/bar'], '\\foo\\bar'],
88+
// No UNC path expected (no non-slashes in first component -
89+
// questionable)
90+
[['//', 'foo/bar'], '\\foo\\bar'],
91+
[['//', '/foo/bar'], '\\foo\\bar'],
92+
[['\\\\', '/', '/foo/bar'], '\\foo\\bar'],
93+
[['//'], '/'],
94+
// No UNC path expected (share name missing - questionable).
95+
[['//foo'], '\\foo'],
96+
[['//foo/'], '\\foo\\'],
97+
[['//foo', '/'], '\\foo\\'],
98+
[['//foo', '', '/'], '\\foo\\'],
99+
// No UNC path expected (too many leading slashes - questionable)
100+
[['///foo/bar'], '\\foo\\bar'],
101+
[['////foo', 'bar'], '\\foo\\bar'],
102+
[['\\\\\\/foo/bar'], '\\foo\\bar'],
103+
// Drive-relative vs drive-absolute paths. This merely describes the
104+
// status quo, rather than being obviously right
105+
[['c:'], 'c:.'],
106+
[['c:.'], 'c:.'],
107+
[['c:', ''], 'c:.'],
108+
[['', 'c:'], 'c:.'],
109+
[['c:.', '/'], 'c:.\\'],
110+
[['c:.', 'file'], 'c:file'],
111+
[['c:', '/'], 'c:\\'],
112+
[['c:', 'file'], 'c:\\file']
113+
]
114+
)
115+
]);
116+
joinTests.forEach((test) => {
117+
if (!Array.isArray(test[0]))
118+
test[0] = [test[0]];
119+
test[0].forEach((join) => {
120+
test[1].forEach((test) => {
121+
const actual = join.apply(null, test[0]);
122+
const expected = test[1];
123+
// For non-Windows specific tests with the Windows join(), we need to try
124+
// replacing the slashes since the non-Windows specific tests' `expected`
125+
// use forward slashes
126+
let actualAlt;
127+
let os;
128+
if (join === path.win32.join) {
129+
actualAlt = actual.replace(backslashRE, '/');
130+
os = 'win32';
131+
} else {
132+
os = 'posix';
133+
}
134+
const message =
135+
`path.${os}.join(${test[0].map(JSON.stringify).join(',')})\n expect=${
136+
JSON.stringify(expected)}\n actual=${JSON.stringify(actual)}`;
137+
if (actual !== expected && actualAlt !== expected)
138+
failures.push(`\n${message}`);
139+
});
140+
});
141+
});
142+
assert.strictEqual(failures.length, 0, failures.join(''));

0 commit comments

Comments
 (0)