test: ensure win32.isAbsolute() is consistent
[platform/upstream/nodejs.git] / test / parallel / test-path.js
1 'use strict';
2 var common = require('../common');
3 var assert = require('assert');
4
5 var path = require('path');
6
7 var f = __filename;
8
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');
17
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');
24
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\\\\');
31
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),
37                controlCharFilename);
38 }
39
40 assert.equal(path.extname(f), '.js');
41
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('////'), '/');
50
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');
78
79
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.//'), '.');
121
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.\\\\'), '.');
131
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.\\\\'), '.\\\\');
141
142 // path.join tests
143 var failures = [];
144 var joinTests =
145     // arguments                     result
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'],
155      [['./'], './'],
156      [['.', './'], './'],
157      [['.', '.', '.'], '.'],
158      [['.', './', '.'], '.'],
159      [['.', '/./', '.'], '.'],
160      [['.', '/////./', '.'], '.'],
161      [['.'], '.'],
162      [['', '.'], '.'],
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'],
175      [['/'], '/'],
176      [['/', '.'], '/'],
177      [['/', '..'], '/'],
178      [['/', '..', '..'], '/'],
179      [[''], '.'],
180      [['', ''], '.'],
181      [[' /foo'], ' /foo'],
182      [[' ', 'foo'], ' /foo'],
183      [[' ', '.'], ' '],
184      [[' ', '/'], ' /'],
185      [[' ', ''], ' '],
186      [['/', 'foo'], '/foo'],
187      [['/', '/foo'], '/foo'],
188      [['/', '//foo'], '/foo'],
189      [['/', '', '/foo'], '/foo'],
190      [['', '/', 'foo'], '/foo'],
191      [['', '/', '/foo'], '/foo']
192     ];
193
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'],
221      [['//'], '/'],
222      // No UNC path expected (share name missing - questionable).
223      [['//foo'], '/foo'],
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
233      [['c:'], 'c:.'],
234      [['c:.'], 'c:.'],
235      [['c:', ''], 'c:.'],
236      [['', 'c:'], 'c:.'],
237      [['c:.', '/'], 'c:./'],
238      [['c:.', 'file'], 'c:file'],
239      [['c:', '/'], 'c:/'],
240      [['c:', 'file'], 'c:/file']
241     ]);
242 }
243
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);
253 });
254 assert.equal(failures.length, 0, failures.join(''));
255
256 // Test thrown TypeErrors
257 var typeErrorTests = [true, false, 7, null, {}, undefined, [], NaN];
258
259 function fail(fn) {
260   var args = Array.prototype.slice.call(arguments, 1);
261
262   assert.throws(function() {
263     fn.apply(null, args);
264   }, TypeError);
265 }
266
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);
275
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);
281
282   // undefined is a valid value as the second argument to basename
283   if (test !== undefined) {
284     fail(path.basename, 'foo', test);
285   }
286 });
287
288
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');
298
299 assert.equal(path.posix.normalize('./fixtures///b/../b/c.js'),
300              'fixtures/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');
305
306 // path.resolve tests
307 var resolveTests;
308 if (common.isWindows) {
309   // windows
310   resolveTests =
311       // arguments                                    result
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']
323       ];
324 } else {
325   // Posix
326   resolveTests =
327       // arguments                                    result
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']];
333 }
334 failures = [];
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);
343 });
344 assert.equal(failures.length, 0, failures.join(''));
345
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);
365
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);
370
371 // path.relative tests
372 var relativeTests;
373 if (common.isWindows) {
374   // windows
375   relativeTests =
376       // arguments                     result
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:\\']];
385 } else {
386   // posix
387   relativeTests =
388       // arguments                    result
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']];
395 }
396 failures = [];
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(',') +
402                 ')' +
403                 '\n  expect=' + JSON.stringify(expected) +
404                 '\n  actual=' + JSON.stringify(actual);
405   if (actual !== expected) failures.push('\n' + message);
406 });
407 assert.equal(failures.length, 0, failures.join(''));
408
409 // windows
410 assert.equal(path.win32.sep, '\\');
411 // posix
412 assert.equal(path.posix.sep, '/');
413
414 // path.delimiter tests
415 // windows
416 assert.equal(path.win32.delimiter, ';');
417
418 // posix
419 assert.equal(path.posix.delimiter, ':');
420
421
422 if (common.isWindows)
423   assert.deepEqual(path, path.win32, 'should be win32 path module');
424 else
425   assert.deepEqual(path, path.posix, 'should be posix path module');