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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-path.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,431 @@
     1.4 +// Copyright Joyent, Inc. and other Node contributors.
     1.5 +//
     1.6 +// Permission is hereby granted, free of charge, to any person obtaining a
     1.7 +// copy of this software and associated documentation files (the
     1.8 +// "Software"), to deal in the Software without restriction, including
     1.9 +// without limitation the rights to use, copy, modify, merge, publish,
    1.10 +// distribute, sublicense, and/or sell copies of the Software, and to permit
    1.11 +// persons to whom the Software is furnished to do so, subject to the
    1.12 +// following conditions:
    1.13 +//
    1.14 +// The above copyright notice and this permission notice shall be included
    1.15 +// in all copies or substantial portions of the Software.
    1.16 +//
    1.17 +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    1.18 +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    1.19 +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
    1.20 +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    1.21 +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
    1.22 +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
    1.23 +// USE OR OTHER DEALINGS IN THE SOFTWARE.
    1.24 +
    1.25 +
    1.26 +// Adapted version of:
    1.27 +// https://github.com/joyent/node/blob/v0.9.1/test/simple/test-path.js
    1.28 +
    1.29 +exports['test path'] = function(assert) {
    1.30 +
    1.31 +var system = require('sdk/system');
    1.32 +var path = require('sdk/fs/path');
    1.33 +
    1.34 +// Shim process global from node.
    1.35 +var process = Object.create(require('sdk/system'));
    1.36 +process.cwd = process.pathFor.bind(process, 'CurProcD');
    1.37 +
    1.38 +var isWindows = require('sdk/system').platform.indexOf('win') === 0;
    1.39 +
    1.40 +assert.equal(path.basename(''), '');
    1.41 +assert.equal(path.basename('/dir/basename.ext'), 'basename.ext');
    1.42 +assert.equal(path.basename('/basename.ext'), 'basename.ext');
    1.43 +assert.equal(path.basename('basename.ext'), 'basename.ext');
    1.44 +assert.equal(path.basename('basename.ext/'), 'basename.ext');
    1.45 +assert.equal(path.basename('basename.ext//'), 'basename.ext');
    1.46 +
    1.47 +if (isWindows) {
    1.48 +  // On Windows a backslash acts as a path separator.
    1.49 +  assert.equal(path.basename('\\dir\\basename.ext'), 'basename.ext');
    1.50 +  assert.equal(path.basename('\\basename.ext'), 'basename.ext');
    1.51 +  assert.equal(path.basename('basename.ext'), 'basename.ext');
    1.52 +  assert.equal(path.basename('basename.ext\\'), 'basename.ext');
    1.53 +  assert.equal(path.basename('basename.ext\\\\'), 'basename.ext');
    1.54 +
    1.55 +} else {
    1.56 +  // On unix a backslash is just treated as any other character.
    1.57 +  assert.equal(path.basename('\\dir\\basename.ext'), '\\dir\\basename.ext');
    1.58 +  assert.equal(path.basename('\\basename.ext'), '\\basename.ext');
    1.59 +  assert.equal(path.basename('basename.ext'), 'basename.ext');
    1.60 +  assert.equal(path.basename('basename.ext\\'), 'basename.ext\\');
    1.61 +  assert.equal(path.basename('basename.ext\\\\'), 'basename.ext\\\\');
    1.62 +}
    1.63 +
    1.64 +// POSIX filenames may include control characters
    1.65 +// c.f. http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html
    1.66 +if (!isWindows) {
    1.67 +  var controlCharFilename = 'Icon' + String.fromCharCode(13);
    1.68 +  assert.equal(path.basename('/a/b/' + controlCharFilename),
    1.69 +               controlCharFilename);
    1.70 +}
    1.71 +
    1.72 +assert.equal(path.dirname('/a/b/'), '/a');
    1.73 +assert.equal(path.dirname('/a/b'), '/a');
    1.74 +assert.equal(path.dirname('/a'), '/');
    1.75 +assert.equal(path.dirname(''), '.');
    1.76 +assert.equal(path.dirname('/'), '/');
    1.77 +assert.equal(path.dirname('////'), '/');
    1.78 +
    1.79 +if (isWindows) {
    1.80 +  assert.equal(path.dirname('c:\\'), 'c:\\');
    1.81 +  assert.equal(path.dirname('c:\\foo'), 'c:\\');
    1.82 +  assert.equal(path.dirname('c:\\foo\\'), 'c:\\');
    1.83 +  assert.equal(path.dirname('c:\\foo\\bar'), 'c:\\foo');
    1.84 +  assert.equal(path.dirname('c:\\foo\\bar\\'), 'c:\\foo');
    1.85 +  assert.equal(path.dirname('c:\\foo\\bar\\baz'), 'c:\\foo\\bar');
    1.86 +  assert.equal(path.dirname('\\'), '\\');
    1.87 +  assert.equal(path.dirname('\\foo'), '\\');
    1.88 +  assert.equal(path.dirname('\\foo\\'), '\\');
    1.89 +  assert.equal(path.dirname('\\foo\\bar'), '\\foo');
    1.90 +  assert.equal(path.dirname('\\foo\\bar\\'), '\\foo');
    1.91 +  assert.equal(path.dirname('\\foo\\bar\\baz'), '\\foo\\bar');
    1.92 +  assert.equal(path.dirname('c:'), 'c:');
    1.93 +  assert.equal(path.dirname('c:foo'), 'c:');
    1.94 +  assert.equal(path.dirname('c:foo\\'), 'c:');
    1.95 +  assert.equal(path.dirname('c:foo\\bar'), 'c:foo');
    1.96 +  assert.equal(path.dirname('c:foo\\bar\\'), 'c:foo');
    1.97 +  assert.equal(path.dirname('c:foo\\bar\\baz'), 'c:foo\\bar');
    1.98 +  assert.equal(path.dirname('\\\\unc\\share'), '\\\\unc\\share');
    1.99 +  assert.equal(path.dirname('\\\\unc\\share\\foo'), '\\\\unc\\share\\');
   1.100 +  assert.equal(path.dirname('\\\\unc\\share\\foo\\'), '\\\\unc\\share\\');
   1.101 +  assert.equal(path.dirname('\\\\unc\\share\\foo\\bar'),
   1.102 +               '\\\\unc\\share\\foo');
   1.103 +  assert.equal(path.dirname('\\\\unc\\share\\foo\\bar\\'),
   1.104 +               '\\\\unc\\share\\foo');
   1.105 +  assert.equal(path.dirname('\\\\unc\\share\\foo\\bar\\baz'),
   1.106 +               '\\\\unc\\share\\foo\\bar');
   1.107 +}
   1.108 +
   1.109 +
   1.110 +assert.equal(path.extname(''), '');
   1.111 +assert.equal(path.extname('/path/to/file'), '');
   1.112 +assert.equal(path.extname('/path/to/file.ext'), '.ext');
   1.113 +assert.equal(path.extname('/path.to/file.ext'), '.ext');
   1.114 +assert.equal(path.extname('/path.to/file'), '');
   1.115 +assert.equal(path.extname('/path.to/.file'), '');
   1.116 +assert.equal(path.extname('/path.to/.file.ext'), '.ext');
   1.117 +assert.equal(path.extname('/path/to/f.ext'), '.ext');
   1.118 +assert.equal(path.extname('/path/to/..ext'), '.ext');
   1.119 +assert.equal(path.extname('file'), '');
   1.120 +assert.equal(path.extname('file.ext'), '.ext');
   1.121 +assert.equal(path.extname('.file'), '');
   1.122 +assert.equal(path.extname('.file.ext'), '.ext');
   1.123 +assert.equal(path.extname('/file'), '');
   1.124 +assert.equal(path.extname('/file.ext'), '.ext');
   1.125 +assert.equal(path.extname('/.file'), '');
   1.126 +assert.equal(path.extname('/.file.ext'), '.ext');
   1.127 +assert.equal(path.extname('.path/file.ext'), '.ext');
   1.128 +assert.equal(path.extname('file.ext.ext'), '.ext');
   1.129 +assert.equal(path.extname('file.'), '.');
   1.130 +assert.equal(path.extname('.'), '');
   1.131 +assert.equal(path.extname('./'), '');
   1.132 +assert.equal(path.extname('.file.ext'), '.ext');
   1.133 +assert.equal(path.extname('.file'), '');
   1.134 +assert.equal(path.extname('.file.'), '.');
   1.135 +assert.equal(path.extname('.file..'), '.');
   1.136 +assert.equal(path.extname('..'), '');
   1.137 +assert.equal(path.extname('../'), '');
   1.138 +assert.equal(path.extname('..file.ext'), '.ext');
   1.139 +assert.equal(path.extname('..file'), '.file');
   1.140 +assert.equal(path.extname('..file.'), '.');
   1.141 +assert.equal(path.extname('..file..'), '.');
   1.142 +assert.equal(path.extname('...'), '.');
   1.143 +assert.equal(path.extname('...ext'), '.ext');
   1.144 +assert.equal(path.extname('....'), '.');
   1.145 +assert.equal(path.extname('file.ext/'), '.ext');
   1.146 +assert.equal(path.extname('file.ext//'), '.ext');
   1.147 +assert.equal(path.extname('file/'), '');
   1.148 +assert.equal(path.extname('file//'), '');
   1.149 +assert.equal(path.extname('file./'), '.');
   1.150 +assert.equal(path.extname('file.//'), '.');
   1.151 +
   1.152 +if (isWindows) {
   1.153 +  // On windows, backspace is a path separator.
   1.154 +  assert.equal(path.extname('.\\'), '');
   1.155 +  assert.equal(path.extname('..\\'), '');
   1.156 +  assert.equal(path.extname('file.ext\\'), '.ext');
   1.157 +  assert.equal(path.extname('file.ext\\\\'), '.ext');
   1.158 +  assert.equal(path.extname('file\\'), '');
   1.159 +  assert.equal(path.extname('file\\\\'), '');
   1.160 +  assert.equal(path.extname('file.\\'), '.');
   1.161 +  assert.equal(path.extname('file.\\\\'), '.');
   1.162 +
   1.163 +} else {
   1.164 +  // On unix, backspace is a valid name component like any other character.
   1.165 +  assert.equal(path.extname('.\\'), '');
   1.166 +  assert.equal(path.extname('..\\'), '.\\');
   1.167 +  assert.equal(path.extname('file.ext\\'), '.ext\\');
   1.168 +  assert.equal(path.extname('file.ext\\\\'), '.ext\\\\');
   1.169 +  assert.equal(path.extname('file\\'), '');
   1.170 +  assert.equal(path.extname('file\\\\'), '');
   1.171 +  assert.equal(path.extname('file.\\'), '.\\');
   1.172 +  assert.equal(path.extname('file.\\\\'), '.\\\\');
   1.173 +}
   1.174 +
   1.175 +// path.join tests
   1.176 +var failures = [];
   1.177 +var joinTests =
   1.178 +    // arguments result
   1.179 +    [[['.', 'x/b', '..', '/b/c.js'], 'x/b/c.js'],
   1.180 +     [['/.', 'x/b', '..', '/b/c.js'], '/x/b/c.js'],
   1.181 +     [['/foo', '../../../bar'], '/bar'],
   1.182 +     [['foo', '../../../bar'], '../../bar'],
   1.183 +     [['foo/', '../../../bar'], '../../bar'],
   1.184 +     [['foo/x', '../../../bar'], '../bar'],
   1.185 +     [['foo/x', './bar'], 'foo/x/bar'],
   1.186 +     [['foo/x/', './bar'], 'foo/x/bar'],
   1.187 +     [['foo/x/', '.', 'bar'], 'foo/x/bar'],
   1.188 +     [['./'], './'],
   1.189 +     [['.', './'], './'],
   1.190 +     [['.', '.', '.'], '.'],
   1.191 +     [['.', './', '.'], '.'],
   1.192 +     [['.', '/./', '.'], '.'],
   1.193 +     [['.', '/////./', '.'], '.'],
   1.194 +     [['.'], '.'],
   1.195 +     [['', '.'], '.'],
   1.196 +     [['', 'foo'], 'foo'],
   1.197 +     [['foo', '/bar'], 'foo/bar'],
   1.198 +     [['', '/foo'], '/foo'],
   1.199 +     [['', '', '/foo'], '/foo'],
   1.200 +     [['', '', 'foo'], 'foo'],
   1.201 +     [['foo', ''], 'foo'],
   1.202 +     [['foo/', ''], 'foo/'],
   1.203 +     [['foo', '', '/bar'], 'foo/bar'],
   1.204 +     [['./', '..', '/foo'], '../foo'],
   1.205 +     [['./', '..', '..', '/foo'], '../../foo'],
   1.206 +     [['.', '..', '..', '/foo'], '../../foo'],
   1.207 +     [['', '..', '..', '/foo'], '../../foo'],
   1.208 +     [['/'], '/'],
   1.209 +     [['/', '.'], '/'],
   1.210 +     [['/', '..'], '/'],
   1.211 +     [['/', '..', '..'], '/'],
   1.212 +     [[''], '.'],
   1.213 +     [['', ''], '.'],
   1.214 +     [[' /foo'], ' /foo'],
   1.215 +     [[' ', 'foo'], ' /foo'],
   1.216 +     [[' ', '.'], ' '],
   1.217 +     [[' ', '/'], ' /'],
   1.218 +     [[' ', ''], ' '],
   1.219 +     [['/', 'foo'], '/foo'],
   1.220 +     [['/', '/foo'], '/foo'],
   1.221 +     [['/', '//foo'], '/foo'],
   1.222 +     [['/', '', '/foo'], '/foo'],
   1.223 +     [['', '/', 'foo'], '/foo'],
   1.224 +     [['', '/', '/foo'], '/foo']
   1.225 +    ];
   1.226 +
   1.227 +// Windows-specific join tests
   1.228 +if (isWindows) {
   1.229 +  joinTests = joinTests.concat(
   1.230 +    [// UNC path expected
   1.231 +     [['//foo/bar'], '//foo/bar/'],
   1.232 +     [['\\/foo/bar'], '//foo/bar/'],
   1.233 +     [['\\\\foo/bar'], '//foo/bar/'],
   1.234 +     // UNC path expected - server and share separate
   1.235 +     [['//foo', 'bar'], '//foo/bar/'],
   1.236 +     [['//foo/', 'bar'], '//foo/bar/'],
   1.237 +     [['//foo', '/bar'], '//foo/bar/'],
   1.238 +     // UNC path expected - questionable
   1.239 +     [['//foo', '', 'bar'], '//foo/bar/'],
   1.240 +     [['//foo/', '', 'bar'], '//foo/bar/'],
   1.241 +     [['//foo/', '', '/bar'], '//foo/bar/'],
   1.242 +     // UNC path expected - even more questionable
   1.243 +     [['', '//foo', 'bar'], '//foo/bar/'],
   1.244 +     [['', '//foo/', 'bar'], '//foo/bar/'],
   1.245 +     [['', '//foo/', '/bar'], '//foo/bar/'],
   1.246 +     // No UNC path expected (no double slash in first component)
   1.247 +     [['\\', 'foo/bar'], '/foo/bar'],
   1.248 +     [['\\', '/foo/bar'], '/foo/bar'],
   1.249 +     [['', '/', '/foo/bar'], '/foo/bar'],
   1.250 +     // No UNC path expected (no non-slashes in first component - questionable)
   1.251 +     [['//', 'foo/bar'], '/foo/bar'],
   1.252 +     [['//', '/foo/bar'], '/foo/bar'],
   1.253 +     [['\\\\', '/', '/foo/bar'], '/foo/bar'],
   1.254 +     [['//'], '/'],
   1.255 +     // No UNC path expected (share name missing - questionable).
   1.256 +     [['//foo'], '/foo'],
   1.257 +     [['//foo/'], '/foo/'],
   1.258 +     [['//foo', '/'], '/foo/'],
   1.259 +     [['//foo', '', '/'], '/foo/'],
   1.260 +     // No UNC path expected (too many leading slashes - questionable)
   1.261 +     [['///foo/bar'], '/foo/bar'],
   1.262 +     [['////foo', 'bar'], '/foo/bar'],
   1.263 +     [['\\\\\\/foo/bar'], '/foo/bar'],
   1.264 +     // Drive-relative vs drive-absolute paths. This merely describes the
   1.265 +     // status quo, rather than being obviously right
   1.266 +     [['c:'], 'c:.'],
   1.267 +     [['c:.'], 'c:.'],
   1.268 +     [['c:', ''], 'c:.'],
   1.269 +     [['', 'c:'], 'c:.'],
   1.270 +     [['c:.', '/'], 'c:./'],
   1.271 +     [['c:.', 'file'], 'c:file'],
   1.272 +     [['c:', '/'], 'c:/'],
   1.273 +     [['c:', 'file'], 'c:/file']
   1.274 +    ]);
   1.275 +}
   1.276 +
   1.277 +// Run the join tests.
   1.278 +joinTests.forEach(function(test) {
   1.279 +  var actual = path.join.apply(path, test[0]);
   1.280 +  var expected = isWindows ? test[1].replace(/\//g, '\\') : test[1];
   1.281 +  var message = 'path.join(' + test[0].map(JSON.stringify).join(',') + ')' +
   1.282 +                '\n expect=' + JSON.stringify(expected) +
   1.283 +                '\n actual=' + JSON.stringify(actual);
   1.284 +  if (actual !== expected) failures.push('\n' + message);
   1.285 +  // assert.equal(actual, expected, message);
   1.286 +});
   1.287 +assert.equal(failures.length, 0, failures.join(''));
   1.288 +var joinThrowTests = [true, false, 7, null, {}, undefined, [], NaN];
   1.289 +joinThrowTests.forEach(function(test) {
   1.290 +  assert.throws(function() {
   1.291 +    path.join(test);
   1.292 +  }, TypeError);
   1.293 +  assert.throws(function() {
   1.294 +    path.resolve(test);
   1.295 +  }, TypeError);
   1.296 +});
   1.297 +
   1.298 +
   1.299 +// path normalize tests
   1.300 +if (isWindows) {
   1.301 +  assert.equal(path.normalize('./fixtures///b/../b/c.js'),
   1.302 +               'fixtures\\b\\c.js');
   1.303 +  assert.equal(path.normalize('/foo/../../../bar'), '\\bar');
   1.304 +  assert.equal(path.normalize('a//b//../b'), 'a\\b');
   1.305 +  assert.equal(path.normalize('a//b//./c'), 'a\\b\\c');
   1.306 +  assert.equal(path.normalize('a//b//.'), 'a\\b');
   1.307 +  assert.equal(path.normalize('//server/share/dir/file.ext'),
   1.308 +               '\\\\server\\share\\dir\\file.ext');
   1.309 +} else {
   1.310 +  assert.equal(path.normalize('./fixtures///b/../b/c.js'),
   1.311 +               'fixtures/b/c.js');
   1.312 +  assert.equal(path.normalize('/foo/../../../bar'), '/bar');
   1.313 +  assert.equal(path.normalize('a//b//../b'), 'a/b');
   1.314 +  assert.equal(path.normalize('a//b//./c'), 'a/b/c');
   1.315 +  assert.equal(path.normalize('a//b//.'), 'a/b');
   1.316 +}
   1.317 +
   1.318 +// path.resolve tests
   1.319 +if (isWindows) {
   1.320 +  // windows
   1.321 +  var resolveTests =
   1.322 +      // arguments result
   1.323 +      [[['c:/blah\\blah', 'd:/games', 'c:../a'], 'c:\\blah\\a'],
   1.324 +       [['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'], 'd:\\e.exe'],
   1.325 +       [['c:/ignore', 'c:/some/file'], 'c:\\some\\file'],
   1.326 +       [['d:/ignore', 'd:some/dir//'], 'd:\\ignore\\some\\dir'],
   1.327 +       [['.'], process.cwd()],
   1.328 +       [['//server/share', '..', 'relative\\'], '\\\\server\\share\\relative'],
   1.329 +       [['c:/', '//'], 'c:\\'],
   1.330 +       [['c:/', '//dir'], 'c:\\dir'],
   1.331 +       [['c:/', '//server/share'], '\\\\server\\share\\'],
   1.332 +       [['c:/', '//server//share'], '\\\\server\\share\\'],
   1.333 +       [['c:/', '///some//dir'], 'c:\\some\\dir']
   1.334 +      ];
   1.335 +} else {
   1.336 +  // Posix
   1.337 +  var resolveTests =
   1.338 +      // arguments result
   1.339 +      [[['/var/lib', '../', 'file/'], '/var/file'],
   1.340 +       [['/var/lib', '/../', 'file/'], '/file'],
   1.341 +       // For some mysterious reasons OSX debug builds resolve incorrectly
   1.342 +       // https://tbpl.mozilla.org/php/getParsedLog.php?id=25105489&tree=Mozilla-Inbound
   1.343 +       // Disable this tests until Bug 891698 is fixed.
   1.344 +       // [['a/b/c/', '../../..'], process.cwd()],
   1.345 +       // [['.'], process.cwd()],
   1.346 +       [['/some/dir', '.', '/absolute/'], '/absolute']];
   1.347 +}
   1.348 +var failures = [];
   1.349 +resolveTests.forEach(function(test) {
   1.350 +  var actual = path.resolve.apply(path, test[0]);
   1.351 +  var expected = test[1];
   1.352 +  var message = 'path.resolve(' + test[0].map(JSON.stringify).join(',') + ')' +
   1.353 +                '\n expect=' + JSON.stringify(expected) +
   1.354 +                '\n actual=' + JSON.stringify(actual);
   1.355 +  if (actual !== expected) failures.push('\n' + message);
   1.356 +  // assert.equal(actual, expected, message);
   1.357 +});
   1.358 +assert.equal(failures.length, 0, failures.join(''));
   1.359 +
   1.360 +// path.isAbsolute tests
   1.361 +if (isWindows) {
   1.362 +  assert.equal(path.isAbsolute('//server/file'), true);
   1.363 +  assert.equal(path.isAbsolute('\\\\server\\file'), true);
   1.364 +  assert.equal(path.isAbsolute('C:/Users/'), true);
   1.365 +  assert.equal(path.isAbsolute('C:\\Users\\'), true);
   1.366 +  assert.equal(path.isAbsolute('C:cwd/another'), false);
   1.367 +  assert.equal(path.isAbsolute('C:cwd\\another'), false);
   1.368 +  assert.equal(path.isAbsolute('directory/directory'), false);
   1.369 +  assert.equal(path.isAbsolute('directory\\directory'), false);
   1.370 +} else {
   1.371 +  assert.equal(path.isAbsolute('/home/foo'), true);
   1.372 +  assert.equal(path.isAbsolute('/home/foo/..'), true);
   1.373 +  assert.equal(path.isAbsolute('bar/'), false);
   1.374 +  assert.equal(path.isAbsolute('./baz'), false);
   1.375 +}
   1.376 +
   1.377 +// path.relative tests
   1.378 +if (isWindows) {
   1.379 +  // windows
   1.380 +  var relativeTests =
   1.381 +      // arguments result
   1.382 +      [['c:/blah\\blah', 'd:/games', 'd:\\games'],
   1.383 +       ['c:/aaaa/bbbb', 'c:/aaaa', '..'],
   1.384 +       ['c:/aaaa/bbbb', 'c:/cccc', '..\\..\\cccc'],
   1.385 +       ['c:/aaaa/bbbb', 'c:/aaaa/bbbb', ''],
   1.386 +       ['c:/aaaa/bbbb', 'c:/aaaa/cccc', '..\\cccc'],
   1.387 +       ['c:/aaaa/', 'c:/aaaa/cccc', 'cccc'],
   1.388 +       ['c:/', 'c:\\aaaa\\bbbb', 'aaaa\\bbbb'],
   1.389 +       ['c:/aaaa/bbbb', 'd:\\', 'd:\\']];
   1.390 +} else {
   1.391 +  // posix
   1.392 +  var relativeTests =
   1.393 +      // arguments result
   1.394 +      [['/var/lib', '/var', '..'],
   1.395 +       ['/var/lib', '/bin', '../../bin'],
   1.396 +       ['/var/lib', '/var/lib', ''],
   1.397 +       ['/var/lib', '/var/apache', '../apache'],
   1.398 +       ['/var/', '/var/lib', 'lib'],
   1.399 +       ['/', '/var/lib', 'var/lib']];
   1.400 +}
   1.401 +var failures = [];
   1.402 +relativeTests.forEach(function(test) {
   1.403 +  var actual = path.relative(test[0], test[1]);
   1.404 +  var expected = test[2];
   1.405 +  var message = 'path.relative(' +
   1.406 +                test.slice(0, 2).map(JSON.stringify).join(',') +
   1.407 +                ')' +
   1.408 +                '\n expect=' + JSON.stringify(expected) +
   1.409 +                '\n actual=' + JSON.stringify(actual);
   1.410 +  if (actual !== expected) failures.push('\n' + message);
   1.411 +});
   1.412 +assert.equal(failures.length, 0, failures.join(''));
   1.413 +
   1.414 +// path.sep tests
   1.415 +if (isWindows) {
   1.416 +  // windows
   1.417 +  assert.equal(path.sep, '\\');
   1.418 +} else {
   1.419 +  // posix
   1.420 +  assert.equal(path.sep, '/');
   1.421 +}
   1.422 +
   1.423 +// path.delimiter tests
   1.424 +if (isWindows) {
   1.425 +  // windows
   1.426 +  assert.equal(path.delimiter, ';');
   1.427 +} else {
   1.428 +  // posix
   1.429 +  assert.equal(path.delimiter, ':');
   1.430 +}
   1.431 +
   1.432 +};
   1.433 +
   1.434 +require('test').run(exports);

mercurial