2 var common = require('../common');
3 var assert = require('assert');
5 var path = require('path');
9 assert.equal(path.basename(f), 'test-path.js');
10 assert.equal(path.basename(f, '.js'), 'test-path');
11 assert.equal(path.basename(''), '');
12 assert.equal(path.basename('/dir/basename.ext'), 'basename.ext');
13 assert.equal(path.basename('/basename.ext'), 'basename.ext');
14 assert.equal(path.basename('basename.ext'), 'basename.ext');
15 assert.equal(path.basename('basename.ext/'), 'basename.ext');
16 assert.equal(path.basename('basename.ext//'), 'basename.ext');
18 // On Windows a backslash acts as a path separator.
19 assert.equal(path.win32.basename('\\dir\\basename.ext'), 'basename.ext');
20 assert.equal(path.win32.basename('\\basename.ext'), 'basename.ext');
21 assert.equal(path.win32.basename('basename.ext'), 'basename.ext');
22 assert.equal(path.win32.basename('basename.ext\\'), 'basename.ext');
23 assert.equal(path.win32.basename('basename.ext\\\\'), 'basename.ext');
25 // On unix a backslash is just treated as any other character.
26 assert.equal(path.posix.basename('\\dir\\basename.ext'), '\\dir\\basename.ext');
27 assert.equal(path.posix.basename('\\basename.ext'), '\\basename.ext');
28 assert.equal(path.posix.basename('basename.ext'), 'basename.ext');
29 assert.equal(path.posix.basename('basename.ext\\'), 'basename.ext\\');
30 assert.equal(path.posix.basename('basename.ext\\\\'), 'basename.ext\\\\');
32 // POSIX filenames may include control characters
33 // c.f. http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html
34 if (!common.isWindows) {
35 var controlCharFilename = 'Icon' + String.fromCharCode(13);
36 assert.equal(path.basename('/a/b/' + controlCharFilename),
40 assert.equal(path.extname(f), '.js');
42 assert.equal(path.dirname(f).substr(-13),
43 common.isWindows ? 'test\\parallel' : 'test/parallel');
44 assert.equal(path.dirname('/a/b/'), '/a');
45 assert.equal(path.dirname('/a/b'), '/a');
46 assert.equal(path.dirname('/a'), '/');
47 assert.equal(path.dirname(''), '.');
48 assert.equal(path.dirname('/'), '/');
49 assert.equal(path.dirname('////'), '/');
51 assert.equal(path.win32.dirname('c:\\'), 'c:\\');
52 assert.equal(path.win32.dirname('c:\\foo'), 'c:\\');
53 assert.equal(path.win32.dirname('c:\\foo\\'), 'c:\\');
54 assert.equal(path.win32.dirname('c:\\foo\\bar'), 'c:\\foo');
55 assert.equal(path.win32.dirname('c:\\foo\\bar\\'), 'c:\\foo');
56 assert.equal(path.win32.dirname('c:\\foo\\bar\\baz'), 'c:\\foo\\bar');
57 assert.equal(path.win32.dirname('\\'), '\\');
58 assert.equal(path.win32.dirname('\\foo'), '\\');
59 assert.equal(path.win32.dirname('\\foo\\'), '\\');
60 assert.equal(path.win32.dirname('\\foo\\bar'), '\\foo');
61 assert.equal(path.win32.dirname('\\foo\\bar\\'), '\\foo');
62 assert.equal(path.win32.dirname('\\foo\\bar\\baz'), '\\foo\\bar');
63 assert.equal(path.win32.dirname('c:'), 'c:');
64 assert.equal(path.win32.dirname('c:foo'), 'c:');
65 assert.equal(path.win32.dirname('c:foo\\'), 'c:');
66 assert.equal(path.win32.dirname('c:foo\\bar'), 'c:foo');
67 assert.equal(path.win32.dirname('c:foo\\bar\\'), 'c:foo');
68 assert.equal(path.win32.dirname('c:foo\\bar\\baz'), 'c:foo\\bar');
69 assert.equal(path.win32.dirname('\\\\unc\\share'), '\\\\unc\\share');
70 assert.equal(path.win32.dirname('\\\\unc\\share\\foo'), '\\\\unc\\share\\');
71 assert.equal(path.win32.dirname('\\\\unc\\share\\foo\\'), '\\\\unc\\share\\');
72 assert.equal(path.win32.dirname('\\\\unc\\share\\foo\\bar'),
73 '\\\\unc\\share\\foo');
74 assert.equal(path.win32.dirname('\\\\unc\\share\\foo\\bar\\'),
75 '\\\\unc\\share\\foo');
76 assert.equal(path.win32.dirname('\\\\unc\\share\\foo\\bar\\baz'),
77 '\\\\unc\\share\\foo\\bar');
80 assert.equal(path.extname(''), '');
81 assert.equal(path.extname('/path/to/file'), '');
82 assert.equal(path.extname('/path/to/file.ext'), '.ext');
83 assert.equal(path.extname('/path.to/file.ext'), '.ext');
84 assert.equal(path.extname('/path.to/file'), '');
85 assert.equal(path.extname('/path.to/.file'), '');
86 assert.equal(path.extname('/path.to/.file.ext'), '.ext');
87 assert.equal(path.extname('/path/to/f.ext'), '.ext');
88 assert.equal(path.extname('/path/to/..ext'), '.ext');
89 assert.equal(path.extname('file'), '');
90 assert.equal(path.extname('file.ext'), '.ext');
91 assert.equal(path.extname('.file'), '');
92 assert.equal(path.extname('.file.ext'), '.ext');
93 assert.equal(path.extname('/file'), '');
94 assert.equal(path.extname('/file.ext'), '.ext');
95 assert.equal(path.extname('/.file'), '');
96 assert.equal(path.extname('/.file.ext'), '.ext');
97 assert.equal(path.extname('.path/file.ext'), '.ext');
98 assert.equal(path.extname('file.ext.ext'), '.ext');
99 assert.equal(path.extname('file.'), '.');
100 assert.equal(path.extname('.'), '');
101 assert.equal(path.extname('./'), '');
102 assert.equal(path.extname('.file.ext'), '.ext');
103 assert.equal(path.extname('.file'), '');
104 assert.equal(path.extname('.file.'), '.');
105 assert.equal(path.extname('.file..'), '.');
106 assert.equal(path.extname('..'), '');
107 assert.equal(path.extname('../'), '');
108 assert.equal(path.extname('..file.ext'), '.ext');
109 assert.equal(path.extname('..file'), '.file');
110 assert.equal(path.extname('..file.'), '.');
111 assert.equal(path.extname('..file..'), '.');
112 assert.equal(path.extname('...'), '.');
113 assert.equal(path.extname('...ext'), '.ext');
114 assert.equal(path.extname('....'), '.');
115 assert.equal(path.extname('file.ext/'), '.ext');
116 assert.equal(path.extname('file.ext//'), '.ext');
117 assert.equal(path.extname('file/'), '');
118 assert.equal(path.extname('file//'), '');
119 assert.equal(path.extname('file./'), '.');
120 assert.equal(path.extname('file.//'), '.');
122 // On windows, backspace is a path separator.
123 assert.equal(path.win32.extname('.\\'), '');
124 assert.equal(path.win32.extname('..\\'), '');
125 assert.equal(path.win32.extname('file.ext\\'), '.ext');
126 assert.equal(path.win32.extname('file.ext\\\\'), '.ext');
127 assert.equal(path.win32.extname('file\\'), '');
128 assert.equal(path.win32.extname('file\\\\'), '');
129 assert.equal(path.win32.extname('file.\\'), '.');
130 assert.equal(path.win32.extname('file.\\\\'), '.');
132 // On unix, backspace is a valid name component like any other character.
133 assert.equal(path.posix.extname('.\\'), '');
134 assert.equal(path.posix.extname('..\\'), '.\\');
135 assert.equal(path.posix.extname('file.ext\\'), '.ext\\');
136 assert.equal(path.posix.extname('file.ext\\\\'), '.ext\\\\');
137 assert.equal(path.posix.extname('file\\'), '');
138 assert.equal(path.posix.extname('file\\\\'), '');
139 assert.equal(path.posix.extname('file.\\'), '.\\');
140 assert.equal(path.posix.extname('file.\\\\'), '.\\\\');
146 [[['.', 'x/b', '..', '/b/c.js'], 'x/b/c.js'],
147 [['/.', 'x/b', '..', '/b/c.js'], '/x/b/c.js'],
148 [['/foo', '../../../bar'], '/bar'],
149 [['foo', '../../../bar'], '../../bar'],
150 [['foo/', '../../../bar'], '../../bar'],
151 [['foo/x', '../../../bar'], '../bar'],
152 [['foo/x', './bar'], 'foo/x/bar'],
153 [['foo/x/', './bar'], 'foo/x/bar'],
154 [['foo/x/', '.', 'bar'], 'foo/x/bar'],
157 [['.', '.', '.'], '.'],
158 [['.', './', '.'], '.'],
159 [['.', '/./', '.'], '.'],
160 [['.', '/////./', '.'], '.'],
163 [['', 'foo'], 'foo'],
164 [['foo', '/bar'], 'foo/bar'],
165 [['', '/foo'], '/foo'],
166 [['', '', '/foo'], '/foo'],
167 [['', '', 'foo'], 'foo'],
168 [['foo', ''], 'foo'],
169 [['foo/', ''], 'foo/'],
170 [['foo', '', '/bar'], 'foo/bar'],
171 [['./', '..', '/foo'], '../foo'],
172 [['./', '..', '..', '/foo'], '../../foo'],
173 [['.', '..', '..', '/foo'], '../../foo'],
174 [['', '..', '..', '/foo'], '../../foo'],
178 [['/', '..', '..'], '/'],
181 [[' /foo'], ' /foo'],
182 [[' ', 'foo'], ' /foo'],
186 [['/', 'foo'], '/foo'],
187 [['/', '/foo'], '/foo'],
188 [['/', '//foo'], '/foo'],
189 [['/', '', '/foo'], '/foo'],
190 [['', '/', 'foo'], '/foo'],
191 [['', '/', '/foo'], '/foo']
194 // Windows-specific join tests
195 if (common.isWindows) {
196 joinTests = joinTests.concat(
197 [// UNC path expected
198 [['//foo/bar'], '//foo/bar/'],
199 [['\\/foo/bar'], '//foo/bar/'],
200 [['\\\\foo/bar'], '//foo/bar/'],
201 // UNC path expected - server and share separate
202 [['//foo', 'bar'], '//foo/bar/'],
203 [['//foo/', 'bar'], '//foo/bar/'],
204 [['//foo', '/bar'], '//foo/bar/'],
205 // UNC path expected - questionable
206 [['//foo', '', 'bar'], '//foo/bar/'],
207 [['//foo/', '', 'bar'], '//foo/bar/'],
208 [['//foo/', '', '/bar'], '//foo/bar/'],
209 // UNC path expected - even more questionable
210 [['', '//foo', 'bar'], '//foo/bar/'],
211 [['', '//foo/', 'bar'], '//foo/bar/'],
212 [['', '//foo/', '/bar'], '//foo/bar/'],
213 // No UNC path expected (no double slash in first component)
214 [['\\', 'foo/bar'], '/foo/bar'],
215 [['\\', '/foo/bar'], '/foo/bar'],
216 [['', '/', '/foo/bar'], '/foo/bar'],
217 // No UNC path expected (no non-slashes in first component - questionable)
218 [['//', 'foo/bar'], '/foo/bar'],
219 [['//', '/foo/bar'], '/foo/bar'],
220 [['\\\\', '/', '/foo/bar'], '/foo/bar'],
222 // No UNC path expected (share name missing - questionable).
224 [['//foo/'], '/foo/'],
225 [['//foo', '/'], '/foo/'],
226 [['//foo', '', '/'], '/foo/'],
227 // No UNC path expected (too many leading slashes - questionable)
228 [['///foo/bar'], '/foo/bar'],
229 [['////foo', 'bar'], '/foo/bar'],
230 [['\\\\\\/foo/bar'], '/foo/bar'],
231 // Drive-relative vs drive-absolute paths. This merely describes the
232 // status quo, rather than being obviously right
237 [['c:.', '/'], 'c:./'],
238 [['c:.', 'file'], 'c:file'],
239 [['c:', '/'], 'c:/'],
240 [['c:', 'file'], 'c:/file']
244 // Run the join tests.
245 joinTests.forEach(function(test) {
246 var actual = path.join.apply(path, test[0]);
247 var expected = common.isWindows ? test[1].replace(/\//g, '\\') : test[1];
248 var message = 'path.join(' + test[0].map(JSON.stringify).join(',') + ')' +
249 '\n expect=' + JSON.stringify(expected) +
250 '\n actual=' + JSON.stringify(actual);
251 if (actual !== expected) failures.push('\n' + message);
252 // assert.equal(actual, expected, message);
254 assert.equal(failures.length, 0, failures.join(''));
256 // Test thrown TypeErrors
257 var typeErrorTests = [true, false, 7, null, {}, undefined, [], NaN];
260 var args = Array.prototype.slice.call(arguments, 1);
262 assert.throws(function() {
263 fn.apply(null, args);
267 typeErrorTests.forEach(function(test) {
268 fail(path.join, test);
269 fail(path.resolve, test);
270 fail(path.normalize, test);
271 fail(path.isAbsolute, test);
272 fail(path.relative, test, 'foo');
273 fail(path.relative, 'foo', test);
274 fail(path.parse, test);
276 // These methods should throw a TypeError, but do not for backwards
277 // compatibility. Uncommenting these lines in the future should be a goal.
278 // fail(path.dirname, test);
279 // fail(path.basename, test);
280 // fail(path.extname, test);
282 // undefined is a valid value as the second argument to basename
283 if (test !== undefined) {
284 fail(path.basename, 'foo', test);
289 // path normalize tests
290 assert.equal(path.win32.normalize('./fixtures///b/../b/c.js'),
291 'fixtures\\b\\c.js');
292 assert.equal(path.win32.normalize('/foo/../../../bar'), '\\bar');
293 assert.equal(path.win32.normalize('a//b//../b'), 'a\\b');
294 assert.equal(path.win32.normalize('a//b//./c'), 'a\\b\\c');
295 assert.equal(path.win32.normalize('a//b//.'), 'a\\b');
296 assert.equal(path.win32.normalize('//server/share/dir/file.ext'),
297 '\\\\server\\share\\dir\\file.ext');
299 assert.equal(path.posix.normalize('./fixtures///b/../b/c.js'),
301 assert.equal(path.posix.normalize('/foo/../../../bar'), '/bar');
302 assert.equal(path.posix.normalize('a//b//../b'), 'a/b');
303 assert.equal(path.posix.normalize('a//b//./c'), 'a/b/c');
304 assert.equal(path.posix.normalize('a//b//.'), 'a/b');
306 // path.resolve tests
308 if (common.isWindows) {
312 [[['c:/blah\\blah', 'd:/games', 'c:../a'], 'c:\\blah\\a'],
313 [['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'], 'd:\\e.exe'],
314 [['c:/ignore', 'c:/some/file'], 'c:\\some\\file'],
315 [['d:/ignore', 'd:some/dir//'], 'd:\\ignore\\some\\dir'],
316 [['.'], process.cwd()],
317 [['//server/share', '..', 'relative\\'], '\\\\server\\share\\relative'],
318 [['c:/', '//'], 'c:\\'],
319 [['c:/', '//dir'], 'c:\\dir'],
320 [['c:/', '//server/share'], '\\\\server\\share\\'],
321 [['c:/', '//server//share'], '\\\\server\\share\\'],
322 [['c:/', '///some//dir'], 'c:\\some\\dir']
328 [[['/var/lib', '../', 'file/'], '/var/file'],
329 [['/var/lib', '/../', 'file/'], '/file'],
330 [['a/b/c/', '../../..'], process.cwd()],
331 [['.'], process.cwd()],
332 [['/some/dir', '.', '/absolute/'], '/absolute']];
335 resolveTests.forEach(function(test) {
336 var actual = path.resolve.apply(path, test[0]);
337 var expected = test[1];
338 var message = 'path.resolve(' + test[0].map(JSON.stringify).join(',') + ')' +
339 '\n expect=' + JSON.stringify(expected) +
340 '\n actual=' + JSON.stringify(actual);
341 if (actual !== expected) failures.push('\n' + message);
342 // assert.equal(actual, expected, message);
344 assert.equal(failures.length, 0, failures.join(''));
346 // path.isAbsolute tests
347 assert.equal(path.win32.isAbsolute('/'), true);
348 assert.equal(path.win32.isAbsolute('//'), true);
349 assert.equal(path.win32.isAbsolute('//server'), true);
350 assert.equal(path.win32.isAbsolute('//server/file'), true);
351 assert.equal(path.win32.isAbsolute('\\\\server\\file'), true);
352 assert.equal(path.win32.isAbsolute('\\\\server'), true);
353 assert.equal(path.win32.isAbsolute('\\\\'), true);
354 assert.equal(path.win32.isAbsolute('c'), false);
355 assert.equal(path.win32.isAbsolute('c:'), false);
356 assert.equal(path.win32.isAbsolute('c:\\'), true);
357 assert.equal(path.win32.isAbsolute('c:/'), true);
358 assert.equal(path.win32.isAbsolute('c://'), true);
359 assert.equal(path.win32.isAbsolute('C:/Users/'), true);
360 assert.equal(path.win32.isAbsolute('C:\\Users\\'), true);
361 assert.equal(path.win32.isAbsolute('C:cwd/another'), false);
362 assert.equal(path.win32.isAbsolute('C:cwd\\another'), false);
363 assert.equal(path.win32.isAbsolute('directory/directory'), false);
364 assert.equal(path.win32.isAbsolute('directory\\directory'), false);
366 assert.equal(path.posix.isAbsolute('/home/foo'), true);
367 assert.equal(path.posix.isAbsolute('/home/foo/..'), true);
368 assert.equal(path.posix.isAbsolute('bar/'), false);
369 assert.equal(path.posix.isAbsolute('./baz'), false);
371 // path.relative tests
373 if (common.isWindows) {
377 [['c:/blah\\blah', 'd:/games', 'd:\\games'],
378 ['c:/aaaa/bbbb', 'c:/aaaa', '..'],
379 ['c:/aaaa/bbbb', 'c:/cccc', '..\\..\\cccc'],
380 ['c:/aaaa/bbbb', 'c:/aaaa/bbbb', ''],
381 ['c:/aaaa/bbbb', 'c:/aaaa/cccc', '..\\cccc'],
382 ['c:/aaaa/', 'c:/aaaa/cccc', 'cccc'],
383 ['c:/', 'c:\\aaaa\\bbbb', 'aaaa\\bbbb'],
384 ['c:/aaaa/bbbb', 'd:\\', 'd:\\']];
389 [['/var/lib', '/var', '..'],
390 ['/var/lib', '/bin', '../../bin'],
391 ['/var/lib', '/var/lib', ''],
392 ['/var/lib', '/var/apache', '../apache'],
393 ['/var/', '/var/lib', 'lib'],
394 ['/', '/var/lib', 'var/lib']];
397 relativeTests.forEach(function(test) {
398 var actual = path.relative(test[0], test[1]);
399 var expected = test[2];
400 var message = 'path.relative(' +
401 test.slice(0, 2).map(JSON.stringify).join(',') +
403 '\n expect=' + JSON.stringify(expected) +
404 '\n actual=' + JSON.stringify(actual);
405 if (actual !== expected) failures.push('\n' + message);
407 assert.equal(failures.length, 0, failures.join(''));
410 assert.equal(path.win32.sep, '\\');
412 assert.equal(path.posix.sep, '/');
414 // path.delimiter tests
416 assert.equal(path.win32.delimiter, ';');
419 assert.equal(path.posix.delimiter, ':');
422 if (common.isWindows)
423 assert.deepEqual(path, path.win32, 'should be win32 path module');
425 assert.deepEqual(path, path.posix, 'should be posix path module');