michael@0: // Copyright Joyent, Inc. and other Node contributors. michael@0: // michael@0: // Permission is hereby granted, free of charge, to any person obtaining a michael@0: // copy of this software and associated documentation files (the michael@0: // "Software"), to deal in the Software without restriction, including michael@0: // without limitation the rights to use, copy, modify, merge, publish, michael@0: // distribute, sublicense, and/or sell copies of the Software, and to permit michael@0: // persons to whom the Software is furnished to do so, subject to the michael@0: // following conditions: michael@0: // michael@0: // The above copyright notice and this permission notice shall be included michael@0: // in all copies or substantial portions of the Software. michael@0: // michael@0: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS michael@0: // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF michael@0: // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN michael@0: // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, michael@0: // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR michael@0: // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE michael@0: // USE OR OTHER DEALINGS IN THE SOFTWARE. michael@0: michael@0: michael@0: // Adapted version of: michael@0: // https://github.com/joyent/node/blob/v0.9.1/test/simple/test-path.js michael@0: michael@0: exports['test path'] = function(assert) { michael@0: michael@0: var system = require('sdk/system'); michael@0: var path = require('sdk/fs/path'); michael@0: michael@0: // Shim process global from node. michael@0: var process = Object.create(require('sdk/system')); michael@0: process.cwd = process.pathFor.bind(process, 'CurProcD'); michael@0: michael@0: var isWindows = require('sdk/system').platform.indexOf('win') === 0; michael@0: michael@0: assert.equal(path.basename(''), ''); michael@0: assert.equal(path.basename('/dir/basename.ext'), 'basename.ext'); michael@0: assert.equal(path.basename('/basename.ext'), 'basename.ext'); michael@0: assert.equal(path.basename('basename.ext'), 'basename.ext'); michael@0: assert.equal(path.basename('basename.ext/'), 'basename.ext'); michael@0: assert.equal(path.basename('basename.ext//'), 'basename.ext'); michael@0: michael@0: if (isWindows) { michael@0: // On Windows a backslash acts as a path separator. michael@0: assert.equal(path.basename('\\dir\\basename.ext'), 'basename.ext'); michael@0: assert.equal(path.basename('\\basename.ext'), 'basename.ext'); michael@0: assert.equal(path.basename('basename.ext'), 'basename.ext'); michael@0: assert.equal(path.basename('basename.ext\\'), 'basename.ext'); michael@0: assert.equal(path.basename('basename.ext\\\\'), 'basename.ext'); michael@0: michael@0: } else { michael@0: // On unix a backslash is just treated as any other character. michael@0: assert.equal(path.basename('\\dir\\basename.ext'), '\\dir\\basename.ext'); michael@0: assert.equal(path.basename('\\basename.ext'), '\\basename.ext'); michael@0: assert.equal(path.basename('basename.ext'), 'basename.ext'); michael@0: assert.equal(path.basename('basename.ext\\'), 'basename.ext\\'); michael@0: assert.equal(path.basename('basename.ext\\\\'), 'basename.ext\\\\'); michael@0: } michael@0: michael@0: // POSIX filenames may include control characters michael@0: // c.f. http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html michael@0: if (!isWindows) { michael@0: var controlCharFilename = 'Icon' + String.fromCharCode(13); michael@0: assert.equal(path.basename('/a/b/' + controlCharFilename), michael@0: controlCharFilename); michael@0: } michael@0: michael@0: assert.equal(path.dirname('/a/b/'), '/a'); michael@0: assert.equal(path.dirname('/a/b'), '/a'); michael@0: assert.equal(path.dirname('/a'), '/'); michael@0: assert.equal(path.dirname(''), '.'); michael@0: assert.equal(path.dirname('/'), '/'); michael@0: assert.equal(path.dirname('////'), '/'); michael@0: michael@0: if (isWindows) { michael@0: assert.equal(path.dirname('c:\\'), 'c:\\'); michael@0: assert.equal(path.dirname('c:\\foo'), 'c:\\'); michael@0: assert.equal(path.dirname('c:\\foo\\'), 'c:\\'); michael@0: assert.equal(path.dirname('c:\\foo\\bar'), 'c:\\foo'); michael@0: assert.equal(path.dirname('c:\\foo\\bar\\'), 'c:\\foo'); michael@0: assert.equal(path.dirname('c:\\foo\\bar\\baz'), 'c:\\foo\\bar'); michael@0: assert.equal(path.dirname('\\'), '\\'); michael@0: assert.equal(path.dirname('\\foo'), '\\'); michael@0: assert.equal(path.dirname('\\foo\\'), '\\'); michael@0: assert.equal(path.dirname('\\foo\\bar'), '\\foo'); michael@0: assert.equal(path.dirname('\\foo\\bar\\'), '\\foo'); michael@0: assert.equal(path.dirname('\\foo\\bar\\baz'), '\\foo\\bar'); michael@0: assert.equal(path.dirname('c:'), 'c:'); michael@0: assert.equal(path.dirname('c:foo'), 'c:'); michael@0: assert.equal(path.dirname('c:foo\\'), 'c:'); michael@0: assert.equal(path.dirname('c:foo\\bar'), 'c:foo'); michael@0: assert.equal(path.dirname('c:foo\\bar\\'), 'c:foo'); michael@0: assert.equal(path.dirname('c:foo\\bar\\baz'), 'c:foo\\bar'); michael@0: assert.equal(path.dirname('\\\\unc\\share'), '\\\\unc\\share'); michael@0: assert.equal(path.dirname('\\\\unc\\share\\foo'), '\\\\unc\\share\\'); michael@0: assert.equal(path.dirname('\\\\unc\\share\\foo\\'), '\\\\unc\\share\\'); michael@0: assert.equal(path.dirname('\\\\unc\\share\\foo\\bar'), michael@0: '\\\\unc\\share\\foo'); michael@0: assert.equal(path.dirname('\\\\unc\\share\\foo\\bar\\'), michael@0: '\\\\unc\\share\\foo'); michael@0: assert.equal(path.dirname('\\\\unc\\share\\foo\\bar\\baz'), michael@0: '\\\\unc\\share\\foo\\bar'); michael@0: } michael@0: michael@0: michael@0: assert.equal(path.extname(''), ''); michael@0: assert.equal(path.extname('/path/to/file'), ''); michael@0: assert.equal(path.extname('/path/to/file.ext'), '.ext'); michael@0: assert.equal(path.extname('/path.to/file.ext'), '.ext'); michael@0: assert.equal(path.extname('/path.to/file'), ''); michael@0: assert.equal(path.extname('/path.to/.file'), ''); michael@0: assert.equal(path.extname('/path.to/.file.ext'), '.ext'); michael@0: assert.equal(path.extname('/path/to/f.ext'), '.ext'); michael@0: assert.equal(path.extname('/path/to/..ext'), '.ext'); michael@0: assert.equal(path.extname('file'), ''); michael@0: assert.equal(path.extname('file.ext'), '.ext'); michael@0: assert.equal(path.extname('.file'), ''); michael@0: assert.equal(path.extname('.file.ext'), '.ext'); michael@0: assert.equal(path.extname('/file'), ''); michael@0: assert.equal(path.extname('/file.ext'), '.ext'); michael@0: assert.equal(path.extname('/.file'), ''); michael@0: assert.equal(path.extname('/.file.ext'), '.ext'); michael@0: assert.equal(path.extname('.path/file.ext'), '.ext'); michael@0: assert.equal(path.extname('file.ext.ext'), '.ext'); michael@0: assert.equal(path.extname('file.'), '.'); michael@0: assert.equal(path.extname('.'), ''); michael@0: assert.equal(path.extname('./'), ''); michael@0: assert.equal(path.extname('.file.ext'), '.ext'); michael@0: assert.equal(path.extname('.file'), ''); michael@0: assert.equal(path.extname('.file.'), '.'); michael@0: assert.equal(path.extname('.file..'), '.'); michael@0: assert.equal(path.extname('..'), ''); michael@0: assert.equal(path.extname('../'), ''); michael@0: assert.equal(path.extname('..file.ext'), '.ext'); michael@0: assert.equal(path.extname('..file'), '.file'); michael@0: assert.equal(path.extname('..file.'), '.'); michael@0: assert.equal(path.extname('..file..'), '.'); michael@0: assert.equal(path.extname('...'), '.'); michael@0: assert.equal(path.extname('...ext'), '.ext'); michael@0: assert.equal(path.extname('....'), '.'); michael@0: assert.equal(path.extname('file.ext/'), '.ext'); michael@0: assert.equal(path.extname('file.ext//'), '.ext'); michael@0: assert.equal(path.extname('file/'), ''); michael@0: assert.equal(path.extname('file//'), ''); michael@0: assert.equal(path.extname('file./'), '.'); michael@0: assert.equal(path.extname('file.//'), '.'); michael@0: michael@0: if (isWindows) { michael@0: // On windows, backspace is a path separator. michael@0: assert.equal(path.extname('.\\'), ''); michael@0: assert.equal(path.extname('..\\'), ''); michael@0: assert.equal(path.extname('file.ext\\'), '.ext'); michael@0: assert.equal(path.extname('file.ext\\\\'), '.ext'); michael@0: assert.equal(path.extname('file\\'), ''); michael@0: assert.equal(path.extname('file\\\\'), ''); michael@0: assert.equal(path.extname('file.\\'), '.'); michael@0: assert.equal(path.extname('file.\\\\'), '.'); michael@0: michael@0: } else { michael@0: // On unix, backspace is a valid name component like any other character. michael@0: assert.equal(path.extname('.\\'), ''); michael@0: assert.equal(path.extname('..\\'), '.\\'); michael@0: assert.equal(path.extname('file.ext\\'), '.ext\\'); michael@0: assert.equal(path.extname('file.ext\\\\'), '.ext\\\\'); michael@0: assert.equal(path.extname('file\\'), ''); michael@0: assert.equal(path.extname('file\\\\'), ''); michael@0: assert.equal(path.extname('file.\\'), '.\\'); michael@0: assert.equal(path.extname('file.\\\\'), '.\\\\'); michael@0: } michael@0: michael@0: // path.join tests michael@0: var failures = []; michael@0: var joinTests = michael@0: // arguments result michael@0: [[['.', 'x/b', '..', '/b/c.js'], 'x/b/c.js'], michael@0: [['/.', 'x/b', '..', '/b/c.js'], '/x/b/c.js'], michael@0: [['/foo', '../../../bar'], '/bar'], michael@0: [['foo', '../../../bar'], '../../bar'], michael@0: [['foo/', '../../../bar'], '../../bar'], michael@0: [['foo/x', '../../../bar'], '../bar'], michael@0: [['foo/x', './bar'], 'foo/x/bar'], michael@0: [['foo/x/', './bar'], 'foo/x/bar'], michael@0: [['foo/x/', '.', 'bar'], 'foo/x/bar'], michael@0: [['./'], './'], michael@0: [['.', './'], './'], michael@0: [['.', '.', '.'], '.'], michael@0: [['.', './', '.'], '.'], michael@0: [['.', '/./', '.'], '.'], michael@0: [['.', '/////./', '.'], '.'], michael@0: [['.'], '.'], michael@0: [['', '.'], '.'], michael@0: [['', 'foo'], 'foo'], michael@0: [['foo', '/bar'], 'foo/bar'], michael@0: [['', '/foo'], '/foo'], michael@0: [['', '', '/foo'], '/foo'], michael@0: [['', '', 'foo'], 'foo'], michael@0: [['foo', ''], 'foo'], michael@0: [['foo/', ''], 'foo/'], michael@0: [['foo', '', '/bar'], 'foo/bar'], michael@0: [['./', '..', '/foo'], '../foo'], michael@0: [['./', '..', '..', '/foo'], '../../foo'], michael@0: [['.', '..', '..', '/foo'], '../../foo'], michael@0: [['', '..', '..', '/foo'], '../../foo'], michael@0: [['/'], '/'], michael@0: [['/', '.'], '/'], michael@0: [['/', '..'], '/'], michael@0: [['/', '..', '..'], '/'], michael@0: [[''], '.'], michael@0: [['', ''], '.'], michael@0: [[' /foo'], ' /foo'], michael@0: [[' ', 'foo'], ' /foo'], michael@0: [[' ', '.'], ' '], michael@0: [[' ', '/'], ' /'], michael@0: [[' ', ''], ' '], michael@0: [['/', 'foo'], '/foo'], michael@0: [['/', '/foo'], '/foo'], michael@0: [['/', '//foo'], '/foo'], michael@0: [['/', '', '/foo'], '/foo'], michael@0: [['', '/', 'foo'], '/foo'], michael@0: [['', '/', '/foo'], '/foo'] michael@0: ]; michael@0: michael@0: // Windows-specific join tests michael@0: if (isWindows) { michael@0: joinTests = joinTests.concat( michael@0: [// UNC path expected michael@0: [['//foo/bar'], '//foo/bar/'], michael@0: [['\\/foo/bar'], '//foo/bar/'], michael@0: [['\\\\foo/bar'], '//foo/bar/'], michael@0: // UNC path expected - server and share separate michael@0: [['//foo', 'bar'], '//foo/bar/'], michael@0: [['//foo/', 'bar'], '//foo/bar/'], michael@0: [['//foo', '/bar'], '//foo/bar/'], michael@0: // UNC path expected - questionable michael@0: [['//foo', '', 'bar'], '//foo/bar/'], michael@0: [['//foo/', '', 'bar'], '//foo/bar/'], michael@0: [['//foo/', '', '/bar'], '//foo/bar/'], michael@0: // UNC path expected - even more questionable michael@0: [['', '//foo', 'bar'], '//foo/bar/'], michael@0: [['', '//foo/', 'bar'], '//foo/bar/'], michael@0: [['', '//foo/', '/bar'], '//foo/bar/'], michael@0: // No UNC path expected (no double slash in first component) michael@0: [['\\', 'foo/bar'], '/foo/bar'], michael@0: [['\\', '/foo/bar'], '/foo/bar'], michael@0: [['', '/', '/foo/bar'], '/foo/bar'], michael@0: // No UNC path expected (no non-slashes in first component - questionable) michael@0: [['//', 'foo/bar'], '/foo/bar'], michael@0: [['//', '/foo/bar'], '/foo/bar'], michael@0: [['\\\\', '/', '/foo/bar'], '/foo/bar'], michael@0: [['//'], '/'], michael@0: // No UNC path expected (share name missing - questionable). michael@0: [['//foo'], '/foo'], michael@0: [['//foo/'], '/foo/'], michael@0: [['//foo', '/'], '/foo/'], michael@0: [['//foo', '', '/'], '/foo/'], michael@0: // No UNC path expected (too many leading slashes - questionable) michael@0: [['///foo/bar'], '/foo/bar'], michael@0: [['////foo', 'bar'], '/foo/bar'], michael@0: [['\\\\\\/foo/bar'], '/foo/bar'], michael@0: // Drive-relative vs drive-absolute paths. This merely describes the michael@0: // status quo, rather than being obviously right michael@0: [['c:'], 'c:.'], michael@0: [['c:.'], 'c:.'], michael@0: [['c:', ''], 'c:.'], michael@0: [['', 'c:'], 'c:.'], michael@0: [['c:.', '/'], 'c:./'], michael@0: [['c:.', 'file'], 'c:file'], michael@0: [['c:', '/'], 'c:/'], michael@0: [['c:', 'file'], 'c:/file'] michael@0: ]); michael@0: } michael@0: michael@0: // Run the join tests. michael@0: joinTests.forEach(function(test) { michael@0: var actual = path.join.apply(path, test[0]); michael@0: var expected = isWindows ? test[1].replace(/\//g, '\\') : test[1]; michael@0: var message = 'path.join(' + test[0].map(JSON.stringify).join(',') + ')' + michael@0: '\n expect=' + JSON.stringify(expected) + michael@0: '\n actual=' + JSON.stringify(actual); michael@0: if (actual !== expected) failures.push('\n' + message); michael@0: // assert.equal(actual, expected, message); michael@0: }); michael@0: assert.equal(failures.length, 0, failures.join('')); michael@0: var joinThrowTests = [true, false, 7, null, {}, undefined, [], NaN]; michael@0: joinThrowTests.forEach(function(test) { michael@0: assert.throws(function() { michael@0: path.join(test); michael@0: }, TypeError); michael@0: assert.throws(function() { michael@0: path.resolve(test); michael@0: }, TypeError); michael@0: }); michael@0: michael@0: michael@0: // path normalize tests michael@0: if (isWindows) { michael@0: assert.equal(path.normalize('./fixtures///b/../b/c.js'), michael@0: 'fixtures\\b\\c.js'); michael@0: assert.equal(path.normalize('/foo/../../../bar'), '\\bar'); michael@0: assert.equal(path.normalize('a//b//../b'), 'a\\b'); michael@0: assert.equal(path.normalize('a//b//./c'), 'a\\b\\c'); michael@0: assert.equal(path.normalize('a//b//.'), 'a\\b'); michael@0: assert.equal(path.normalize('//server/share/dir/file.ext'), michael@0: '\\\\server\\share\\dir\\file.ext'); michael@0: } else { michael@0: assert.equal(path.normalize('./fixtures///b/../b/c.js'), michael@0: 'fixtures/b/c.js'); michael@0: assert.equal(path.normalize('/foo/../../../bar'), '/bar'); michael@0: assert.equal(path.normalize('a//b//../b'), 'a/b'); michael@0: assert.equal(path.normalize('a//b//./c'), 'a/b/c'); michael@0: assert.equal(path.normalize('a//b//.'), 'a/b'); michael@0: } michael@0: michael@0: // path.resolve tests michael@0: if (isWindows) { michael@0: // windows michael@0: var resolveTests = michael@0: // arguments result michael@0: [[['c:/blah\\blah', 'd:/games', 'c:../a'], 'c:\\blah\\a'], michael@0: [['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'], 'd:\\e.exe'], michael@0: [['c:/ignore', 'c:/some/file'], 'c:\\some\\file'], michael@0: [['d:/ignore', 'd:some/dir//'], 'd:\\ignore\\some\\dir'], michael@0: [['.'], process.cwd()], michael@0: [['//server/share', '..', 'relative\\'], '\\\\server\\share\\relative'], michael@0: [['c:/', '//'], 'c:\\'], michael@0: [['c:/', '//dir'], 'c:\\dir'], michael@0: [['c:/', '//server/share'], '\\\\server\\share\\'], michael@0: [['c:/', '//server//share'], '\\\\server\\share\\'], michael@0: [['c:/', '///some//dir'], 'c:\\some\\dir'] michael@0: ]; michael@0: } else { michael@0: // Posix michael@0: var resolveTests = michael@0: // arguments result michael@0: [[['/var/lib', '../', 'file/'], '/var/file'], michael@0: [['/var/lib', '/../', 'file/'], '/file'], michael@0: // For some mysterious reasons OSX debug builds resolve incorrectly michael@0: // https://tbpl.mozilla.org/php/getParsedLog.php?id=25105489&tree=Mozilla-Inbound michael@0: // Disable this tests until Bug 891698 is fixed. michael@0: // [['a/b/c/', '../../..'], process.cwd()], michael@0: // [['.'], process.cwd()], michael@0: [['/some/dir', '.', '/absolute/'], '/absolute']]; michael@0: } michael@0: var failures = []; michael@0: resolveTests.forEach(function(test) { michael@0: var actual = path.resolve.apply(path, test[0]); michael@0: var expected = test[1]; michael@0: var message = 'path.resolve(' + test[0].map(JSON.stringify).join(',') + ')' + michael@0: '\n expect=' + JSON.stringify(expected) + michael@0: '\n actual=' + JSON.stringify(actual); michael@0: if (actual !== expected) failures.push('\n' + message); michael@0: // assert.equal(actual, expected, message); michael@0: }); michael@0: assert.equal(failures.length, 0, failures.join('')); michael@0: michael@0: // path.isAbsolute tests michael@0: if (isWindows) { michael@0: assert.equal(path.isAbsolute('//server/file'), true); michael@0: assert.equal(path.isAbsolute('\\\\server\\file'), true); michael@0: assert.equal(path.isAbsolute('C:/Users/'), true); michael@0: assert.equal(path.isAbsolute('C:\\Users\\'), true); michael@0: assert.equal(path.isAbsolute('C:cwd/another'), false); michael@0: assert.equal(path.isAbsolute('C:cwd\\another'), false); michael@0: assert.equal(path.isAbsolute('directory/directory'), false); michael@0: assert.equal(path.isAbsolute('directory\\directory'), false); michael@0: } else { michael@0: assert.equal(path.isAbsolute('/home/foo'), true); michael@0: assert.equal(path.isAbsolute('/home/foo/..'), true); michael@0: assert.equal(path.isAbsolute('bar/'), false); michael@0: assert.equal(path.isAbsolute('./baz'), false); michael@0: } michael@0: michael@0: // path.relative tests michael@0: if (isWindows) { michael@0: // windows michael@0: var relativeTests = michael@0: // arguments result michael@0: [['c:/blah\\blah', 'd:/games', 'd:\\games'], michael@0: ['c:/aaaa/bbbb', 'c:/aaaa', '..'], michael@0: ['c:/aaaa/bbbb', 'c:/cccc', '..\\..\\cccc'], michael@0: ['c:/aaaa/bbbb', 'c:/aaaa/bbbb', ''], michael@0: ['c:/aaaa/bbbb', 'c:/aaaa/cccc', '..\\cccc'], michael@0: ['c:/aaaa/', 'c:/aaaa/cccc', 'cccc'], michael@0: ['c:/', 'c:\\aaaa\\bbbb', 'aaaa\\bbbb'], michael@0: ['c:/aaaa/bbbb', 'd:\\', 'd:\\']]; michael@0: } else { michael@0: // posix michael@0: var relativeTests = michael@0: // arguments result michael@0: [['/var/lib', '/var', '..'], michael@0: ['/var/lib', '/bin', '../../bin'], michael@0: ['/var/lib', '/var/lib', ''], michael@0: ['/var/lib', '/var/apache', '../apache'], michael@0: ['/var/', '/var/lib', 'lib'], michael@0: ['/', '/var/lib', 'var/lib']]; michael@0: } michael@0: var failures = []; michael@0: relativeTests.forEach(function(test) { michael@0: var actual = path.relative(test[0], test[1]); michael@0: var expected = test[2]; michael@0: var message = 'path.relative(' + michael@0: test.slice(0, 2).map(JSON.stringify).join(',') + michael@0: ')' + michael@0: '\n expect=' + JSON.stringify(expected) + michael@0: '\n actual=' + JSON.stringify(actual); michael@0: if (actual !== expected) failures.push('\n' + message); michael@0: }); michael@0: assert.equal(failures.length, 0, failures.join('')); michael@0: michael@0: // path.sep tests michael@0: if (isWindows) { michael@0: // windows michael@0: assert.equal(path.sep, '\\'); michael@0: } else { michael@0: // posix michael@0: assert.equal(path.sep, '/'); michael@0: } michael@0: michael@0: // path.delimiter tests michael@0: if (isWindows) { michael@0: // windows michael@0: assert.equal(path.delimiter, ';'); michael@0: } else { michael@0: // posix michael@0: assert.equal(path.delimiter, ':'); michael@0: } michael@0: michael@0: }; michael@0: michael@0: require('test').run(exports);