addon-sdk/source/test/test-path.js

branch
TOR_BUG_9701
changeset 10
ac0c01689b40
equal deleted inserted replaced
-1:000000000000 0:4762d07a09e4
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
23 // Adapted version of:
24 // https://github.com/joyent/node/blob/v0.9.1/test/simple/test-path.js
25
26 exports['test path'] = function(assert) {
27
28 var system = require('sdk/system');
29 var path = require('sdk/fs/path');
30
31 // Shim process global from node.
32 var process = Object.create(require('sdk/system'));
33 process.cwd = process.pathFor.bind(process, 'CurProcD');
34
35 var isWindows = require('sdk/system').platform.indexOf('win') === 0;
36
37 assert.equal(path.basename(''), '');
38 assert.equal(path.basename('/dir/basename.ext'), 'basename.ext');
39 assert.equal(path.basename('/basename.ext'), 'basename.ext');
40 assert.equal(path.basename('basename.ext'), 'basename.ext');
41 assert.equal(path.basename('basename.ext/'), 'basename.ext');
42 assert.equal(path.basename('basename.ext//'), 'basename.ext');
43
44 if (isWindows) {
45 // On Windows a backslash acts as a path separator.
46 assert.equal(path.basename('\\dir\\basename.ext'), 'basename.ext');
47 assert.equal(path.basename('\\basename.ext'), 'basename.ext');
48 assert.equal(path.basename('basename.ext'), 'basename.ext');
49 assert.equal(path.basename('basename.ext\\'), 'basename.ext');
50 assert.equal(path.basename('basename.ext\\\\'), 'basename.ext');
51
52 } else {
53 // On unix a backslash is just treated as any other character.
54 assert.equal(path.basename('\\dir\\basename.ext'), '\\dir\\basename.ext');
55 assert.equal(path.basename('\\basename.ext'), '\\basename.ext');
56 assert.equal(path.basename('basename.ext'), 'basename.ext');
57 assert.equal(path.basename('basename.ext\\'), 'basename.ext\\');
58 assert.equal(path.basename('basename.ext\\\\'), 'basename.ext\\\\');
59 }
60
61 // POSIX filenames may include control characters
62 // c.f. http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html
63 if (!isWindows) {
64 var controlCharFilename = 'Icon' + String.fromCharCode(13);
65 assert.equal(path.basename('/a/b/' + controlCharFilename),
66 controlCharFilename);
67 }
68
69 assert.equal(path.dirname('/a/b/'), '/a');
70 assert.equal(path.dirname('/a/b'), '/a');
71 assert.equal(path.dirname('/a'), '/');
72 assert.equal(path.dirname(''), '.');
73 assert.equal(path.dirname('/'), '/');
74 assert.equal(path.dirname('////'), '/');
75
76 if (isWindows) {
77 assert.equal(path.dirname('c:\\'), 'c:\\');
78 assert.equal(path.dirname('c:\\foo'), 'c:\\');
79 assert.equal(path.dirname('c:\\foo\\'), 'c:\\');
80 assert.equal(path.dirname('c:\\foo\\bar'), 'c:\\foo');
81 assert.equal(path.dirname('c:\\foo\\bar\\'), 'c:\\foo');
82 assert.equal(path.dirname('c:\\foo\\bar\\baz'), 'c:\\foo\\bar');
83 assert.equal(path.dirname('\\'), '\\');
84 assert.equal(path.dirname('\\foo'), '\\');
85 assert.equal(path.dirname('\\foo\\'), '\\');
86 assert.equal(path.dirname('\\foo\\bar'), '\\foo');
87 assert.equal(path.dirname('\\foo\\bar\\'), '\\foo');
88 assert.equal(path.dirname('\\foo\\bar\\baz'), '\\foo\\bar');
89 assert.equal(path.dirname('c:'), 'c:');
90 assert.equal(path.dirname('c:foo'), 'c:');
91 assert.equal(path.dirname('c:foo\\'), 'c:');
92 assert.equal(path.dirname('c:foo\\bar'), 'c:foo');
93 assert.equal(path.dirname('c:foo\\bar\\'), 'c:foo');
94 assert.equal(path.dirname('c:foo\\bar\\baz'), 'c:foo\\bar');
95 assert.equal(path.dirname('\\\\unc\\share'), '\\\\unc\\share');
96 assert.equal(path.dirname('\\\\unc\\share\\foo'), '\\\\unc\\share\\');
97 assert.equal(path.dirname('\\\\unc\\share\\foo\\'), '\\\\unc\\share\\');
98 assert.equal(path.dirname('\\\\unc\\share\\foo\\bar'),
99 '\\\\unc\\share\\foo');
100 assert.equal(path.dirname('\\\\unc\\share\\foo\\bar\\'),
101 '\\\\unc\\share\\foo');
102 assert.equal(path.dirname('\\\\unc\\share\\foo\\bar\\baz'),
103 '\\\\unc\\share\\foo\\bar');
104 }
105
106
107 assert.equal(path.extname(''), '');
108 assert.equal(path.extname('/path/to/file'), '');
109 assert.equal(path.extname('/path/to/file.ext'), '.ext');
110 assert.equal(path.extname('/path.to/file.ext'), '.ext');
111 assert.equal(path.extname('/path.to/file'), '');
112 assert.equal(path.extname('/path.to/.file'), '');
113 assert.equal(path.extname('/path.to/.file.ext'), '.ext');
114 assert.equal(path.extname('/path/to/f.ext'), '.ext');
115 assert.equal(path.extname('/path/to/..ext'), '.ext');
116 assert.equal(path.extname('file'), '');
117 assert.equal(path.extname('file.ext'), '.ext');
118 assert.equal(path.extname('.file'), '');
119 assert.equal(path.extname('.file.ext'), '.ext');
120 assert.equal(path.extname('/file'), '');
121 assert.equal(path.extname('/file.ext'), '.ext');
122 assert.equal(path.extname('/.file'), '');
123 assert.equal(path.extname('/.file.ext'), '.ext');
124 assert.equal(path.extname('.path/file.ext'), '.ext');
125 assert.equal(path.extname('file.ext.ext'), '.ext');
126 assert.equal(path.extname('file.'), '.');
127 assert.equal(path.extname('.'), '');
128 assert.equal(path.extname('./'), '');
129 assert.equal(path.extname('.file.ext'), '.ext');
130 assert.equal(path.extname('.file'), '');
131 assert.equal(path.extname('.file.'), '.');
132 assert.equal(path.extname('.file..'), '.');
133 assert.equal(path.extname('..'), '');
134 assert.equal(path.extname('../'), '');
135 assert.equal(path.extname('..file.ext'), '.ext');
136 assert.equal(path.extname('..file'), '.file');
137 assert.equal(path.extname('..file.'), '.');
138 assert.equal(path.extname('..file..'), '.');
139 assert.equal(path.extname('...'), '.');
140 assert.equal(path.extname('...ext'), '.ext');
141 assert.equal(path.extname('....'), '.');
142 assert.equal(path.extname('file.ext/'), '.ext');
143 assert.equal(path.extname('file.ext//'), '.ext');
144 assert.equal(path.extname('file/'), '');
145 assert.equal(path.extname('file//'), '');
146 assert.equal(path.extname('file./'), '.');
147 assert.equal(path.extname('file.//'), '.');
148
149 if (isWindows) {
150 // On windows, backspace is a path separator.
151 assert.equal(path.extname('.\\'), '');
152 assert.equal(path.extname('..\\'), '');
153 assert.equal(path.extname('file.ext\\'), '.ext');
154 assert.equal(path.extname('file.ext\\\\'), '.ext');
155 assert.equal(path.extname('file\\'), '');
156 assert.equal(path.extname('file\\\\'), '');
157 assert.equal(path.extname('file.\\'), '.');
158 assert.equal(path.extname('file.\\\\'), '.');
159
160 } else {
161 // On unix, backspace is a valid name component like any other character.
162 assert.equal(path.extname('.\\'), '');
163 assert.equal(path.extname('..\\'), '.\\');
164 assert.equal(path.extname('file.ext\\'), '.ext\\');
165 assert.equal(path.extname('file.ext\\\\'), '.ext\\\\');
166 assert.equal(path.extname('file\\'), '');
167 assert.equal(path.extname('file\\\\'), '');
168 assert.equal(path.extname('file.\\'), '.\\');
169 assert.equal(path.extname('file.\\\\'), '.\\\\');
170 }
171
172 // path.join tests
173 var failures = [];
174 var joinTests =
175 // arguments result
176 [[['.', 'x/b', '..', '/b/c.js'], 'x/b/c.js'],
177 [['/.', 'x/b', '..', '/b/c.js'], '/x/b/c.js'],
178 [['/foo', '../../../bar'], '/bar'],
179 [['foo', '../../../bar'], '../../bar'],
180 [['foo/', '../../../bar'], '../../bar'],
181 [['foo/x', '../../../bar'], '../bar'],
182 [['foo/x', './bar'], 'foo/x/bar'],
183 [['foo/x/', './bar'], 'foo/x/bar'],
184 [['foo/x/', '.', 'bar'], 'foo/x/bar'],
185 [['./'], './'],
186 [['.', './'], './'],
187 [['.', '.', '.'], '.'],
188 [['.', './', '.'], '.'],
189 [['.', '/./', '.'], '.'],
190 [['.', '/////./', '.'], '.'],
191 [['.'], '.'],
192 [['', '.'], '.'],
193 [['', 'foo'], 'foo'],
194 [['foo', '/bar'], 'foo/bar'],
195 [['', '/foo'], '/foo'],
196 [['', '', '/foo'], '/foo'],
197 [['', '', 'foo'], 'foo'],
198 [['foo', ''], 'foo'],
199 [['foo/', ''], 'foo/'],
200 [['foo', '', '/bar'], 'foo/bar'],
201 [['./', '..', '/foo'], '../foo'],
202 [['./', '..', '..', '/foo'], '../../foo'],
203 [['.', '..', '..', '/foo'], '../../foo'],
204 [['', '..', '..', '/foo'], '../../foo'],
205 [['/'], '/'],
206 [['/', '.'], '/'],
207 [['/', '..'], '/'],
208 [['/', '..', '..'], '/'],
209 [[''], '.'],
210 [['', ''], '.'],
211 [[' /foo'], ' /foo'],
212 [[' ', 'foo'], ' /foo'],
213 [[' ', '.'], ' '],
214 [[' ', '/'], ' /'],
215 [[' ', ''], ' '],
216 [['/', 'foo'], '/foo'],
217 [['/', '/foo'], '/foo'],
218 [['/', '//foo'], '/foo'],
219 [['/', '', '/foo'], '/foo'],
220 [['', '/', 'foo'], '/foo'],
221 [['', '/', '/foo'], '/foo']
222 ];
223
224 // Windows-specific join tests
225 if (isWindows) {
226 joinTests = joinTests.concat(
227 [// UNC path expected
228 [['//foo/bar'], '//foo/bar/'],
229 [['\\/foo/bar'], '//foo/bar/'],
230 [['\\\\foo/bar'], '//foo/bar/'],
231 // UNC path expected - server and share separate
232 [['//foo', 'bar'], '//foo/bar/'],
233 [['//foo/', 'bar'], '//foo/bar/'],
234 [['//foo', '/bar'], '//foo/bar/'],
235 // UNC path expected - questionable
236 [['//foo', '', 'bar'], '//foo/bar/'],
237 [['//foo/', '', 'bar'], '//foo/bar/'],
238 [['//foo/', '', '/bar'], '//foo/bar/'],
239 // UNC path expected - even more questionable
240 [['', '//foo', 'bar'], '//foo/bar/'],
241 [['', '//foo/', 'bar'], '//foo/bar/'],
242 [['', '//foo/', '/bar'], '//foo/bar/'],
243 // No UNC path expected (no double slash in first component)
244 [['\\', 'foo/bar'], '/foo/bar'],
245 [['\\', '/foo/bar'], '/foo/bar'],
246 [['', '/', '/foo/bar'], '/foo/bar'],
247 // No UNC path expected (no non-slashes in first component - questionable)
248 [['//', 'foo/bar'], '/foo/bar'],
249 [['//', '/foo/bar'], '/foo/bar'],
250 [['\\\\', '/', '/foo/bar'], '/foo/bar'],
251 [['//'], '/'],
252 // No UNC path expected (share name missing - questionable).
253 [['//foo'], '/foo'],
254 [['//foo/'], '/foo/'],
255 [['//foo', '/'], '/foo/'],
256 [['//foo', '', '/'], '/foo/'],
257 // No UNC path expected (too many leading slashes - questionable)
258 [['///foo/bar'], '/foo/bar'],
259 [['////foo', 'bar'], '/foo/bar'],
260 [['\\\\\\/foo/bar'], '/foo/bar'],
261 // Drive-relative vs drive-absolute paths. This merely describes the
262 // status quo, rather than being obviously right
263 [['c:'], 'c:.'],
264 [['c:.'], 'c:.'],
265 [['c:', ''], 'c:.'],
266 [['', 'c:'], 'c:.'],
267 [['c:.', '/'], 'c:./'],
268 [['c:.', 'file'], 'c:file'],
269 [['c:', '/'], 'c:/'],
270 [['c:', 'file'], 'c:/file']
271 ]);
272 }
273
274 // Run the join tests.
275 joinTests.forEach(function(test) {
276 var actual = path.join.apply(path, test[0]);
277 var expected = isWindows ? test[1].replace(/\//g, '\\') : test[1];
278 var message = 'path.join(' + test[0].map(JSON.stringify).join(',') + ')' +
279 '\n expect=' + JSON.stringify(expected) +
280 '\n actual=' + JSON.stringify(actual);
281 if (actual !== expected) failures.push('\n' + message);
282 // assert.equal(actual, expected, message);
283 });
284 assert.equal(failures.length, 0, failures.join(''));
285 var joinThrowTests = [true, false, 7, null, {}, undefined, [], NaN];
286 joinThrowTests.forEach(function(test) {
287 assert.throws(function() {
288 path.join(test);
289 }, TypeError);
290 assert.throws(function() {
291 path.resolve(test);
292 }, TypeError);
293 });
294
295
296 // path normalize tests
297 if (isWindows) {
298 assert.equal(path.normalize('./fixtures///b/../b/c.js'),
299 'fixtures\\b\\c.js');
300 assert.equal(path.normalize('/foo/../../../bar'), '\\bar');
301 assert.equal(path.normalize('a//b//../b'), 'a\\b');
302 assert.equal(path.normalize('a//b//./c'), 'a\\b\\c');
303 assert.equal(path.normalize('a//b//.'), 'a\\b');
304 assert.equal(path.normalize('//server/share/dir/file.ext'),
305 '\\\\server\\share\\dir\\file.ext');
306 } else {
307 assert.equal(path.normalize('./fixtures///b/../b/c.js'),
308 'fixtures/b/c.js');
309 assert.equal(path.normalize('/foo/../../../bar'), '/bar');
310 assert.equal(path.normalize('a//b//../b'), 'a/b');
311 assert.equal(path.normalize('a//b//./c'), 'a/b/c');
312 assert.equal(path.normalize('a//b//.'), 'a/b');
313 }
314
315 // path.resolve tests
316 if (isWindows) {
317 // windows
318 var resolveTests =
319 // arguments result
320 [[['c:/blah\\blah', 'd:/games', 'c:../a'], 'c:\\blah\\a'],
321 [['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'], 'd:\\e.exe'],
322 [['c:/ignore', 'c:/some/file'], 'c:\\some\\file'],
323 [['d:/ignore', 'd:some/dir//'], 'd:\\ignore\\some\\dir'],
324 [['.'], process.cwd()],
325 [['//server/share', '..', 'relative\\'], '\\\\server\\share\\relative'],
326 [['c:/', '//'], 'c:\\'],
327 [['c:/', '//dir'], 'c:\\dir'],
328 [['c:/', '//server/share'], '\\\\server\\share\\'],
329 [['c:/', '//server//share'], '\\\\server\\share\\'],
330 [['c:/', '///some//dir'], 'c:\\some\\dir']
331 ];
332 } else {
333 // Posix
334 var resolveTests =
335 // arguments result
336 [[['/var/lib', '../', 'file/'], '/var/file'],
337 [['/var/lib', '/../', 'file/'], '/file'],
338 // For some mysterious reasons OSX debug builds resolve incorrectly
339 // https://tbpl.mozilla.org/php/getParsedLog.php?id=25105489&tree=Mozilla-Inbound
340 // Disable this tests until Bug 891698 is fixed.
341 // [['a/b/c/', '../../..'], process.cwd()],
342 // [['.'], process.cwd()],
343 [['/some/dir', '.', '/absolute/'], '/absolute']];
344 }
345 var failures = [];
346 resolveTests.forEach(function(test) {
347 var actual = path.resolve.apply(path, test[0]);
348 var expected = test[1];
349 var message = 'path.resolve(' + test[0].map(JSON.stringify).join(',') + ')' +
350 '\n expect=' + JSON.stringify(expected) +
351 '\n actual=' + JSON.stringify(actual);
352 if (actual !== expected) failures.push('\n' + message);
353 // assert.equal(actual, expected, message);
354 });
355 assert.equal(failures.length, 0, failures.join(''));
356
357 // path.isAbsolute tests
358 if (isWindows) {
359 assert.equal(path.isAbsolute('//server/file'), true);
360 assert.equal(path.isAbsolute('\\\\server\\file'), true);
361 assert.equal(path.isAbsolute('C:/Users/'), true);
362 assert.equal(path.isAbsolute('C:\\Users\\'), true);
363 assert.equal(path.isAbsolute('C:cwd/another'), false);
364 assert.equal(path.isAbsolute('C:cwd\\another'), false);
365 assert.equal(path.isAbsolute('directory/directory'), false);
366 assert.equal(path.isAbsolute('directory\\directory'), false);
367 } else {
368 assert.equal(path.isAbsolute('/home/foo'), true);
369 assert.equal(path.isAbsolute('/home/foo/..'), true);
370 assert.equal(path.isAbsolute('bar/'), false);
371 assert.equal(path.isAbsolute('./baz'), false);
372 }
373
374 // path.relative tests
375 if (isWindows) {
376 // windows
377 var relativeTests =
378 // arguments result
379 [['c:/blah\\blah', 'd:/games', 'd:\\games'],
380 ['c:/aaaa/bbbb', 'c:/aaaa', '..'],
381 ['c:/aaaa/bbbb', 'c:/cccc', '..\\..\\cccc'],
382 ['c:/aaaa/bbbb', 'c:/aaaa/bbbb', ''],
383 ['c:/aaaa/bbbb', 'c:/aaaa/cccc', '..\\cccc'],
384 ['c:/aaaa/', 'c:/aaaa/cccc', 'cccc'],
385 ['c:/', 'c:\\aaaa\\bbbb', 'aaaa\\bbbb'],
386 ['c:/aaaa/bbbb', 'd:\\', 'd:\\']];
387 } else {
388 // posix
389 var relativeTests =
390 // arguments result
391 [['/var/lib', '/var', '..'],
392 ['/var/lib', '/bin', '../../bin'],
393 ['/var/lib', '/var/lib', ''],
394 ['/var/lib', '/var/apache', '../apache'],
395 ['/var/', '/var/lib', 'lib'],
396 ['/', '/var/lib', 'var/lib']];
397 }
398 var failures = [];
399 relativeTests.forEach(function(test) {
400 var actual = path.relative(test[0], test[1]);
401 var expected = test[2];
402 var message = 'path.relative(' +
403 test.slice(0, 2).map(JSON.stringify).join(',') +
404 ')' +
405 '\n expect=' + JSON.stringify(expected) +
406 '\n actual=' + JSON.stringify(actual);
407 if (actual !== expected) failures.push('\n' + message);
408 });
409 assert.equal(failures.length, 0, failures.join(''));
410
411 // path.sep tests
412 if (isWindows) {
413 // windows
414 assert.equal(path.sep, '\\');
415 } else {
416 // posix
417 assert.equal(path.sep, '/');
418 }
419
420 // path.delimiter tests
421 if (isWindows) {
422 // windows
423 assert.equal(path.delimiter, ';');
424 } else {
425 // posix
426 assert.equal(path.delimiter, ':');
427 }
428
429 };
430
431 require('test').run(exports);

mercurial