45fd282d089144f7765d476ac21a7e2e9f4234ea
[platform/upstream/nodejs.git] / test / parallel / test-path.js
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 var common = require('../common');
23 var assert = require('assert');
24
25 var path = require('path');
26
27 var isWindows = process.platform === 'win32';
28
29 var f = __filename;
30
31 assert.equal(path.basename(f), 'test-path.js');
32 assert.equal(path.basename(f, '.js'), 'test-path');
33 assert.equal(path.basename(''), '');
34 assert.equal(path.basename('/dir/basename.ext'), 'basename.ext');
35 assert.equal(path.basename('/basename.ext'), 'basename.ext');
36 assert.equal(path.basename('basename.ext'), 'basename.ext');
37 assert.equal(path.basename('basename.ext/'), 'basename.ext');
38 assert.equal(path.basename('basename.ext//'), 'basename.ext');
39
40 // On Windows a backslash acts as a path separator.
41 assert.equal(path.win32.basename('\\dir\\basename.ext'), 'basename.ext');
42 assert.equal(path.win32.basename('\\basename.ext'), 'basename.ext');
43 assert.equal(path.win32.basename('basename.ext'), 'basename.ext');
44 assert.equal(path.win32.basename('basename.ext\\'), 'basename.ext');
45 assert.equal(path.win32.basename('basename.ext\\\\'), 'basename.ext');
46
47 // On unix a backslash is just treated as any other character.
48 assert.equal(path.posix.basename('\\dir\\basename.ext'), '\\dir\\basename.ext');
49 assert.equal(path.posix.basename('\\basename.ext'), '\\basename.ext');
50 assert.equal(path.posix.basename('basename.ext'), 'basename.ext');
51 assert.equal(path.posix.basename('basename.ext\\'), 'basename.ext\\');
52 assert.equal(path.posix.basename('basename.ext\\\\'), 'basename.ext\\\\');
53
54 // POSIX filenames may include control characters
55 // c.f. http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html
56 if (!isWindows) {
57   var controlCharFilename = 'Icon' + String.fromCharCode(13);
58   assert.equal(path.basename('/a/b/' + controlCharFilename),
59                controlCharFilename);
60 }
61
62 assert.equal(path.extname(f), '.js');
63
64 assert.equal(path.dirname(f).substr(-13),
65              isWindows ? 'test\\parallel' : 'test/parallel');
66 assert.equal(path.dirname('/a/b/'), '/a');
67 assert.equal(path.dirname('/a/b'), '/a');
68 assert.equal(path.dirname('/a'), '/');
69 assert.equal(path.dirname(''), '.');
70 assert.equal(path.dirname('/'), '/');
71 assert.equal(path.dirname('////'), '/');
72
73 assert.equal(path.win32.dirname('c:\\'), 'c:\\');
74 assert.equal(path.win32.dirname('c:\\foo'), 'c:\\');
75 assert.equal(path.win32.dirname('c:\\foo\\'), 'c:\\');
76 assert.equal(path.win32.dirname('c:\\foo\\bar'), 'c:\\foo');
77 assert.equal(path.win32.dirname('c:\\foo\\bar\\'), 'c:\\foo');
78 assert.equal(path.win32.dirname('c:\\foo\\bar\\baz'), 'c:\\foo\\bar');
79 assert.equal(path.win32.dirname('\\'), '\\');
80 assert.equal(path.win32.dirname('\\foo'), '\\');
81 assert.equal(path.win32.dirname('\\foo\\'), '\\');
82 assert.equal(path.win32.dirname('\\foo\\bar'), '\\foo');
83 assert.equal(path.win32.dirname('\\foo\\bar\\'), '\\foo');
84 assert.equal(path.win32.dirname('\\foo\\bar\\baz'), '\\foo\\bar');
85 assert.equal(path.win32.dirname('c:'), 'c:');
86 assert.equal(path.win32.dirname('c:foo'), 'c:');
87 assert.equal(path.win32.dirname('c:foo\\'), 'c:');
88 assert.equal(path.win32.dirname('c:foo\\bar'), 'c:foo');
89 assert.equal(path.win32.dirname('c:foo\\bar\\'), 'c:foo');
90 assert.equal(path.win32.dirname('c:foo\\bar\\baz'), 'c:foo\\bar');
91 assert.equal(path.win32.dirname('\\\\unc\\share'), '\\\\unc\\share');
92 assert.equal(path.win32.dirname('\\\\unc\\share\\foo'), '\\\\unc\\share\\');
93 assert.equal(path.win32.dirname('\\\\unc\\share\\foo\\'), '\\\\unc\\share\\');
94 assert.equal(path.win32.dirname('\\\\unc\\share\\foo\\bar'),
95              '\\\\unc\\share\\foo');
96 assert.equal(path.win32.dirname('\\\\unc\\share\\foo\\bar\\'),
97              '\\\\unc\\share\\foo');
98 assert.equal(path.win32.dirname('\\\\unc\\share\\foo\\bar\\baz'),
99              '\\\\unc\\share\\foo\\bar');
100
101
102 assert.equal(path.extname(''), '');
103 assert.equal(path.extname('/path/to/file'), '');
104 assert.equal(path.extname('/path/to/file.ext'), '.ext');
105 assert.equal(path.extname('/path.to/file.ext'), '.ext');
106 assert.equal(path.extname('/path.to/file'), '');
107 assert.equal(path.extname('/path.to/.file'), '');
108 assert.equal(path.extname('/path.to/.file.ext'), '.ext');
109 assert.equal(path.extname('/path/to/f.ext'), '.ext');
110 assert.equal(path.extname('/path/to/..ext'), '.ext');
111 assert.equal(path.extname('file'), '');
112 assert.equal(path.extname('file.ext'), '.ext');
113 assert.equal(path.extname('.file'), '');
114 assert.equal(path.extname('.file.ext'), '.ext');
115 assert.equal(path.extname('/file'), '');
116 assert.equal(path.extname('/file.ext'), '.ext');
117 assert.equal(path.extname('/.file'), '');
118 assert.equal(path.extname('/.file.ext'), '.ext');
119 assert.equal(path.extname('.path/file.ext'), '.ext');
120 assert.equal(path.extname('file.ext.ext'), '.ext');
121 assert.equal(path.extname('file.'), '.');
122 assert.equal(path.extname('.'), '');
123 assert.equal(path.extname('./'), '');
124 assert.equal(path.extname('.file.ext'), '.ext');
125 assert.equal(path.extname('.file'), '');
126 assert.equal(path.extname('.file.'), '.');
127 assert.equal(path.extname('.file..'), '.');
128 assert.equal(path.extname('..'), '');
129 assert.equal(path.extname('../'), '');
130 assert.equal(path.extname('..file.ext'), '.ext');
131 assert.equal(path.extname('..file'), '.file');
132 assert.equal(path.extname('..file.'), '.');
133 assert.equal(path.extname('..file..'), '.');
134 assert.equal(path.extname('...'), '.');
135 assert.equal(path.extname('...ext'), '.ext');
136 assert.equal(path.extname('....'), '.');
137 assert.equal(path.extname('file.ext/'), '.ext');
138 assert.equal(path.extname('file.ext//'), '.ext');
139 assert.equal(path.extname('file/'), '');
140 assert.equal(path.extname('file//'), '');
141 assert.equal(path.extname('file./'), '.');
142 assert.equal(path.extname('file.//'), '.');
143
144 // On windows, backspace is a path separator.
145 assert.equal(path.win32.extname('.\\'), '');
146 assert.equal(path.win32.extname('..\\'), '');
147 assert.equal(path.win32.extname('file.ext\\'), '.ext');
148 assert.equal(path.win32.extname('file.ext\\\\'), '.ext');
149 assert.equal(path.win32.extname('file\\'), '');
150 assert.equal(path.win32.extname('file\\\\'), '');
151 assert.equal(path.win32.extname('file.\\'), '.');
152 assert.equal(path.win32.extname('file.\\\\'), '.');
153
154 // On unix, backspace is a valid name component like any other character.
155 assert.equal(path.posix.extname('.\\'), '');
156 assert.equal(path.posix.extname('..\\'), '.\\');
157 assert.equal(path.posix.extname('file.ext\\'), '.ext\\');
158 assert.equal(path.posix.extname('file.ext\\\\'), '.ext\\\\');
159 assert.equal(path.posix.extname('file\\'), '');
160 assert.equal(path.posix.extname('file\\\\'), '');
161 assert.equal(path.posix.extname('file.\\'), '.\\');
162 assert.equal(path.posix.extname('file.\\\\'), '.\\\\');
163
164 // path.join tests
165 var failures = [];
166 var joinTests =
167     // arguments                     result
168     [[['.', 'x/b', '..', '/b/c.js'], 'x/b/c.js'],
169      [['/.', 'x/b', '..', '/b/c.js'], '/x/b/c.js'],
170      [['/foo', '../../../bar'], '/bar'],
171      [['foo', '../../../bar'], '../../bar'],
172      [['foo/', '../../../bar'], '../../bar'],
173      [['foo/x', '../../../bar'], '../bar'],
174      [['foo/x', './bar'], 'foo/x/bar'],
175      [['foo/x/', './bar'], 'foo/x/bar'],
176      [['foo/x/', '.', 'bar'], 'foo/x/bar'],
177      [['./'], './'],
178      [['.', './'], './'],
179      [['.', '.', '.'], '.'],
180      [['.', './', '.'], '.'],
181      [['.', '/./', '.'], '.'],
182      [['.', '/////./', '.'], '.'],
183      [['.'], '.'],
184      [['', '.'], '.'],
185      [['', 'foo'], 'foo'],
186      [['foo', '/bar'], 'foo/bar'],
187      [['', '/foo'], '/foo'],
188      [['', '', '/foo'], '/foo'],
189      [['', '', 'foo'], 'foo'],
190      [['foo', ''], 'foo'],
191      [['foo/', ''], 'foo/'],
192      [['foo', '', '/bar'], 'foo/bar'],
193      [['./', '..', '/foo'], '../foo'],
194      [['./', '..', '..', '/foo'], '../../foo'],
195      [['.', '..', '..', '/foo'], '../../foo'],
196      [['', '..', '..', '/foo'], '../../foo'],
197      [['/'], '/'],
198      [['/', '.'], '/'],
199      [['/', '..'], '/'],
200      [['/', '..', '..'], '/'],
201      [[''], '.'],
202      [['', ''], '.'],
203      [[' /foo'], ' /foo'],
204      [[' ', 'foo'], ' /foo'],
205      [[' ', '.'], ' '],
206      [[' ', '/'], ' /'],
207      [[' ', ''], ' '],
208      [['/', 'foo'], '/foo'],
209      [['/', '/foo'], '/foo'],
210      [['/', '//foo'], '/foo'],
211      [['/', '', '/foo'], '/foo'],
212      [['', '/', 'foo'], '/foo'],
213      [['', '/', '/foo'], '/foo']
214     ];
215
216 // Windows-specific join tests
217 if (isWindows) {
218   joinTests = joinTests.concat(
219     [// UNC path expected
220      [['//foo/bar'], '//foo/bar/'],
221      [['\\/foo/bar'], '//foo/bar/'],
222      [['\\\\foo/bar'], '//foo/bar/'],
223      // UNC path expected - server and share separate
224      [['//foo', 'bar'], '//foo/bar/'],
225      [['//foo/', 'bar'], '//foo/bar/'],
226      [['//foo', '/bar'], '//foo/bar/'],
227      // UNC path expected - questionable
228      [['//foo', '', 'bar'], '//foo/bar/'],
229      [['//foo/', '', 'bar'], '//foo/bar/'],
230      [['//foo/', '', '/bar'], '//foo/bar/'],
231      // UNC path expected - even more questionable
232      [['', '//foo', 'bar'], '//foo/bar/'],
233      [['', '//foo/', 'bar'], '//foo/bar/'],
234      [['', '//foo/', '/bar'], '//foo/bar/'],
235      // No UNC path expected (no double slash in first component)
236      [['\\', 'foo/bar'], '/foo/bar'],
237      [['\\', '/foo/bar'], '/foo/bar'],
238      [['', '/', '/foo/bar'], '/foo/bar'],
239      // No UNC path expected (no non-slashes in first component - questionable)
240      [['//', 'foo/bar'], '/foo/bar'],
241      [['//', '/foo/bar'], '/foo/bar'],
242      [['\\\\', '/', '/foo/bar'], '/foo/bar'],
243      [['//'], '/'],
244      // No UNC path expected (share name missing - questionable).
245      [['//foo'], '/foo'],
246      [['//foo/'], '/foo/'],
247      [['//foo', '/'], '/foo/'],
248      [['//foo', '', '/'], '/foo/'],
249      // No UNC path expected (too many leading slashes - questionable)
250      [['///foo/bar'], '/foo/bar'],
251      [['////foo', 'bar'], '/foo/bar'],
252      [['\\\\\\/foo/bar'], '/foo/bar'],
253      // Drive-relative vs drive-absolute paths. This merely describes the
254      // status quo, rather than being obviously right
255      [['c:'], 'c:.'],
256      [['c:.'], 'c:.'],
257      [['c:', ''], 'c:.'],
258      [['', 'c:'], 'c:.'],
259      [['c:.', '/'], 'c:./'],
260      [['c:.', 'file'], 'c:file'],
261      [['c:', '/'], 'c:/'],
262      [['c:', 'file'], 'c:/file']
263     ]);
264 }
265
266 // Run the join tests.
267 joinTests.forEach(function(test) {
268   var actual = path.join.apply(path, test[0]);
269   var expected = isWindows ? test[1].replace(/\//g, '\\') : test[1];
270   var message = 'path.join(' + test[0].map(JSON.stringify).join(',') + ')' +
271                 '\n  expect=' + JSON.stringify(expected) +
272                 '\n  actual=' + JSON.stringify(actual);
273   if (actual !== expected) failures.push('\n' + message);
274   // assert.equal(actual, expected, message);
275 });
276 assert.equal(failures.length, 0, failures.join(''));
277 var joinThrowTests = [true, false, 7, null, {}, undefined, [], NaN];
278 joinThrowTests.forEach(function(test) {
279   assert.throws(function() {
280     path.join(test);
281   }, TypeError);
282   assert.throws(function() {
283     path.resolve(test);
284   }, TypeError);
285 });
286
287
288 // path normalize tests
289 assert.equal(path.win32.normalize('./fixtures///b/../b/c.js'),
290              'fixtures\\b\\c.js');
291 assert.equal(path.win32.normalize('/foo/../../../bar'), '\\bar');
292 assert.equal(path.win32.normalize('a//b//../b'), 'a\\b');
293 assert.equal(path.win32.normalize('a//b//./c'), 'a\\b\\c');
294 assert.equal(path.win32.normalize('a//b//.'), 'a\\b');
295 assert.equal(path.win32.normalize('//server/share/dir/file.ext'),
296              '\\\\server\\share\\dir\\file.ext');
297
298 assert.equal(path.posix.normalize('./fixtures///b/../b/c.js'),
299              'fixtures/b/c.js');
300 assert.equal(path.posix.normalize('/foo/../../../bar'), '/bar');
301 assert.equal(path.posix.normalize('a//b//../b'), 'a/b');
302 assert.equal(path.posix.normalize('a//b//./c'), 'a/b/c');
303 assert.equal(path.posix.normalize('a//b//.'), 'a/b');
304
305 // path.resolve tests
306 if (isWindows) {
307   // windows
308   var resolveTests =
309       // arguments                                    result
310       [[['c:/blah\\blah', 'd:/games', 'c:../a'], 'c:\\blah\\a'],
311        [['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'], 'd:\\e.exe'],
312        [['c:/ignore', 'c:/some/file'], 'c:\\some\\file'],
313        [['d:/ignore', 'd:some/dir//'], 'd:\\ignore\\some\\dir'],
314        [['.'], process.cwd()],
315        [['//server/share', '..', 'relative\\'], '\\\\server\\share\\relative'],
316        [['c:/', '//'], 'c:\\'],
317        [['c:/', '//dir'], 'c:\\dir'],
318        [['c:/', '//server/share'], '\\\\server\\share\\'],
319        [['c:/', '//server//share'], '\\\\server\\share\\'],
320        [['c:/', '///some//dir'], 'c:\\some\\dir']
321       ];
322 } else {
323   // Posix
324   var resolveTests =
325       // arguments                                    result
326       [[['/var/lib', '../', 'file/'], '/var/file'],
327        [['/var/lib', '/../', 'file/'], '/file'],
328        [['a/b/c/', '../../..'], process.cwd()],
329        [['.'], process.cwd()],
330        [['/some/dir', '.', '/absolute/'], '/absolute']];
331 }
332 var failures = [];
333 resolveTests.forEach(function(test) {
334   var actual = path.resolve.apply(path, test[0]);
335   var expected = test[1];
336   var message = 'path.resolve(' + test[0].map(JSON.stringify).join(',') + ')' +
337                 '\n  expect=' + JSON.stringify(expected) +
338                 '\n  actual=' + JSON.stringify(actual);
339   if (actual !== expected) failures.push('\n' + message);
340   // assert.equal(actual, expected, message);
341 });
342 assert.equal(failures.length, 0, failures.join(''));
343
344 // path.isAbsolute tests
345 assert.equal(path.win32.isAbsolute('//server/file'), true);
346 assert.equal(path.win32.isAbsolute('\\\\server\\file'), true);
347 assert.equal(path.win32.isAbsolute('C:/Users/'), true);
348 assert.equal(path.win32.isAbsolute('C:\\Users\\'), true);
349 assert.equal(path.win32.isAbsolute('C:cwd/another'), false);
350 assert.equal(path.win32.isAbsolute('C:cwd\\another'), false);
351 assert.equal(path.win32.isAbsolute('directory/directory'), false);
352 assert.equal(path.win32.isAbsolute('directory\\directory'), false);
353
354 assert.equal(path.posix.isAbsolute('/home/foo'), true);
355 assert.equal(path.posix.isAbsolute('/home/foo/..'), true);
356 assert.equal(path.posix.isAbsolute('bar/'), false);
357 assert.equal(path.posix.isAbsolute('./baz'), false);
358
359 // path.relative tests
360 if (isWindows) {
361   // windows
362   var relativeTests =
363       // arguments                     result
364       [['c:/blah\\blah', 'd:/games', 'd:\\games'],
365        ['c:/aaaa/bbbb', 'c:/aaaa', '..'],
366        ['c:/aaaa/bbbb', 'c:/cccc', '..\\..\\cccc'],
367        ['c:/aaaa/bbbb', 'c:/aaaa/bbbb', ''],
368        ['c:/aaaa/bbbb', 'c:/aaaa/cccc', '..\\cccc'],
369        ['c:/aaaa/', 'c:/aaaa/cccc', 'cccc'],
370        ['c:/', 'c:\\aaaa\\bbbb', 'aaaa\\bbbb'],
371        ['c:/aaaa/bbbb', 'd:\\', 'd:\\']];
372 } else {
373   // posix
374   var relativeTests =
375       // arguments                    result
376       [['/var/lib', '/var', '..'],
377        ['/var/lib', '/bin', '../../bin'],
378        ['/var/lib', '/var/lib', ''],
379        ['/var/lib', '/var/apache', '../apache'],
380        ['/var/', '/var/lib', 'lib'],
381        ['/', '/var/lib', 'var/lib']];
382 }
383 var failures = [];
384 relativeTests.forEach(function(test) {
385   var actual = path.relative(test[0], test[1]);
386   var expected = test[2];
387   var message = 'path.relative(' +
388                 test.slice(0, 2).map(JSON.stringify).join(',') +
389                 ')' +
390                 '\n  expect=' + JSON.stringify(expected) +
391                 '\n  actual=' + JSON.stringify(actual);
392   if (actual !== expected) failures.push('\n' + message);
393 });
394 assert.equal(failures.length, 0, failures.join(''));
395
396 // windows
397 assert.equal(path.win32.sep, '\\');
398 // posix
399 assert.equal(path.posix.sep, '/');
400
401 // path.delimiter tests
402 // windows
403 assert.equal(path.win32.delimiter, ';');
404
405 // posix
406 assert.equal(path.posix.delimiter, ':');
407
408
409 if (isWindows)
410   assert.deepEqual(path, path.win32, 'should be win32 path module');
411 else
412   assert.deepEqual(path, path.posix, 'should be posix path module');