Projekt

Obecné

Profil

Stáhnout (16.1 KB) Statistiky
| Větev: | Revize:
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 assert = require('assert');
23

    
24
var path = require('.');
25

    
26
var isWindows = process.platform === 'win32';
27

    
28
// Mock the node.js path
29
var f = __dirname + '/simple/test-path.js';
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
if (isWindows) {
41
  // On Windows a backslash acts as a path separator.
42
  assert.equal(path.basename('\\dir\\basename.ext'), 'basename.ext');
43
  assert.equal(path.basename('\\basename.ext'), 'basename.ext');
44
  assert.equal(path.basename('basename.ext'), 'basename.ext');
45
  assert.equal(path.basename('basename.ext\\'), 'basename.ext');
46
  assert.equal(path.basename('basename.ext\\\\'), 'basename.ext');
47

    
48
} else {
49
  // On unix a backslash is just treated as any other character.
50
  assert.equal(path.basename('\\dir\\basename.ext'), '\\dir\\basename.ext');
51
  assert.equal(path.basename('\\basename.ext'), '\\basename.ext');
52
  assert.equal(path.basename('basename.ext'), 'basename.ext');
53
  assert.equal(path.basename('basename.ext\\'), 'basename.ext\\');
54
  assert.equal(path.basename('basename.ext\\\\'), 'basename.ext\\\\');
55
}
56

    
57
// POSIX filenames may include control characters
58
// c.f. http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html
59
if (!isWindows) {
60
  var controlCharFilename = 'Icon' + String.fromCharCode(13);
61
  assert.equal(path.basename('/a/b/' + controlCharFilename),
62
               controlCharFilename);
63
}
64

    
65
assert.equal(path.extname(f), '.js');
66

    
67
assert.equal(path.dirname(f).substr(-11),
68
             isWindows ? 'test\\simple' : 'test/simple');
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
       [['a/b/c/', '../../..'], process.cwd()],
339
       [['.'], process.cwd()],
340
       [['/some/dir', '.', '/absolute/'], '/absolute']];
341
}
342
var failures = [];
343
resolveTests.forEach(function(test) {
344
  var actual = path.resolve.apply(path, test[0]);
345
  var expected = test[1];
346
  var message = 'path.resolve(' + test[0].map(JSON.stringify).join(',') + ')' +
347
                '\n  expect=' + JSON.stringify(expected) +
348
                '\n  actual=' + JSON.stringify(actual);
349
  if (actual !== expected) failures.push('\n' + message);
350
  // assert.equal(actual, expected, message);
351
});
352
assert.equal(failures.length, 0, failures.join(''));
353

    
354
// path.isAbsolute tests
355
if (isWindows) {
356
  assert.equal(path.isAbsolute('//server/file'), true);
357
  assert.equal(path.isAbsolute('\\\\server\\file'), true);
358
  assert.equal(path.isAbsolute('C:/Users/'), true);
359
  assert.equal(path.isAbsolute('C:\\Users\\'), true);
360
  assert.equal(path.isAbsolute('C:cwd/another'), false);
361
  assert.equal(path.isAbsolute('C:cwd\\another'), false);
362
  assert.equal(path.isAbsolute('directory/directory'), false);
363
  assert.equal(path.isAbsolute('directory\\directory'), false);
364
} else {
365
  assert.equal(path.isAbsolute('/home/foo'), true);
366
  assert.equal(path.isAbsolute('/home/foo/..'), true);
367
  assert.equal(path.isAbsolute('bar/'), false);
368
  assert.equal(path.isAbsolute('./baz'), false);
369
}
370

    
371
// path.relative tests
372
if (isWindows) {
373
  // windows
374
  var relativeTests =
375
      // arguments                     result
376
      [['c:/blah\\blah', 'd:/games', 'd:\\games'],
377
       ['c:/aaaa/bbbb', 'c:/aaaa', '..'],
378
       ['c:/aaaa/bbbb', 'c:/cccc', '..\\..\\cccc'],
379
       ['c:/aaaa/bbbb', 'c:/aaaa/bbbb', ''],
380
       ['c:/aaaa/bbbb', 'c:/aaaa/cccc', '..\\cccc'],
381
       ['c:/aaaa/', 'c:/aaaa/cccc', 'cccc'],
382
       ['c:/', 'c:\\aaaa\\bbbb', 'aaaa\\bbbb'],
383
       ['c:/aaaa/bbbb', 'd:\\', 'd:\\']];
384
} else {
385
  // posix
386
  var relativeTests =
387
      // arguments                    result
388
      [['/var/lib', '/var', '..'],
389
       ['/var/lib', '/bin', '../../bin'],
390
       ['/var/lib', '/var/lib', ''],
391
       ['/var/lib', '/var/apache', '../apache'],
392
       ['/var/', '/var/lib', 'lib'],
393
       ['/', '/var/lib', 'var/lib']];
394
}
395
var failures = [];
396
relativeTests.forEach(function(test) {
397
  var actual = path.relative(test[0], test[1]);
398
  var expected = test[2];
399
  var message = 'path.relative(' +
400
                test.slice(0, 2).map(JSON.stringify).join(',') +
401
                ')' +
402
                '\n  expect=' + JSON.stringify(expected) +
403
                '\n  actual=' + JSON.stringify(actual);
404
  if (actual !== expected) failures.push('\n' + message);
405
});
406
assert.equal(failures.length, 0, failures.join(''));
407

    
408
// path.sep tests
409
if (isWindows) {
410
  // windows
411
  assert.equal(path.sep, '\\');
412
} else {
413
  // posix
414
  assert.equal(path.sep, '/');
415
}
416

    
417
// path.delimiter tests
418
if (isWindows) {
419
  // windows
420
  assert.equal(path.delimiter, ';');
421
} else {
422
  // posix
423
  assert.equal(path.delimiter, ':');
424
}
    (1-1/1)