path: allow calling platform specific methods

Add path.posix and path.win32 which have the specific methods like
resolve and normalize so you can specifically normalize or resolve
based on the target platform.

PR-URL: https://github.com/joyent/node/pull/5661
Reviewed-by: Chris Dickinson <christopher.s.dickinson@gmail.com>
This commit is contained in:
Timothy J Fontaine 2013-06-10 17:09:54 -07:00 committed by Chris Dickinson
parent 4dc8b26bbe
commit 6a90a06002
3 changed files with 535 additions and 502 deletions

View File

@ -201,3 +201,13 @@ An example on Windows:
process.env.PATH.split(path.delimiter) process.env.PATH.split(path.delimiter)
// returns // returns
['C:\Windows\system32', 'C:\Windows', 'C:\Program Files\nodejs\'] ['C:\Windows\system32', 'C:\Windows', 'C:\Program Files\nodejs\']
## path.posix
Provide access to aforementioned `path` methods but always interact in a posix
compatible way.
## path.win32
Provide access to aforementioned `path` methods but always interact in a win32
compatible way.

View File

@ -55,19 +55,19 @@ function normalizeArray(parts, allowAboveRoot) {
} }
if (isWindows) { // Regex to split a windows path into three parts: [*, device, slash,
// Regex to split a windows path into three parts: [*, device, slash, // tail] windows-only
// tail] windows-only var splitDeviceRe =
var splitDeviceRe =
/^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?([\\\/])?([\s\S]*?)$/; /^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?([\\\/])?([\s\S]*?)$/;
// Regex to split the tail part of the above into [*, dir, basename, ext] // Regex to split the tail part of the above into [*, dir, basename, ext]
var splitTailRe = var splitTailRe =
/^([\s\S]*?)((?:\.{1,2}|[^\\\/]+?|)(\.[^.\/\\]*|))(?:[\\\/]*)$/; /^([\s\S]*?)((?:\.{1,2}|[^\\\/]+?|)(\.[^.\/\\]*|))(?:[\\\/]*)$/;
// Function to split a filename into [root, dir, basename, ext] var win32 = {};
// windows version
var splitPath = function(filename) { // Function to split a filename into [root, dir, basename, ext]
win32.splitPath = function(filename) {
// Separate device+slash from tail // Separate device+slash from tail
var result = splitDeviceRe.exec(filename), var result = splitDeviceRe.exec(filename),
device = (result[1] || '') + (result[2] || ''), device = (result[1] || '') + (result[2] || ''),
@ -78,15 +78,14 @@ if (isWindows) {
basename = result2[2], basename = result2[2],
ext = result2[3]; ext = result2[3];
return [device, dir, basename, ext]; return [device, dir, basename, ext];
}; };
var normalizeUNCRoot = function(device) { var normalizeUNCRoot = function(device) {
return '\\\\' + device.replace(/^[\\\/]+/, '').replace(/[\\\/]+/g, '\\'); return '\\\\' + device.replace(/^[\\\/]+/, '').replace(/[\\\/]+/g, '\\');
}; };
// path.resolve([from ...], to) // path.resolve([from ...], to)
// windows version win32.resolve = function() {
exports.resolve = function() {
var resolvedDevice = '', var resolvedDevice = '',
resolvedTail = '', resolvedTail = '',
resolvedAbsolute = false; resolvedAbsolute = false;
@ -121,7 +120,7 @@ if (isWindows) {
var result = splitDeviceRe.exec(path), var result = splitDeviceRe.exec(path),
device = result[1] || '', device = result[1] || '',
isUnc = device && device.charAt(1) !== ':', isUnc = device && device.charAt(1) !== ':',
isAbsolute = exports.isAbsolute(path), isAbsolute = win32.isAbsolute(path),
tail = result[3]; tail = result[3];
if (device && if (device &&
@ -171,14 +170,14 @@ if (isWindows) {
return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) || return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) ||
'.'; '.';
}; };
// windows version
exports.normalize = function(path) { win32.normalize = function(path) {
var result = splitDeviceRe.exec(path), var result = splitDeviceRe.exec(path),
device = result[1] || '', device = result[1] || '',
isUnc = device && device.charAt(1) !== ':', isUnc = device && device.charAt(1) !== ':',
isAbsolute = exports.isAbsolute(path), isAbsolute = win32.isAbsolute(path),
tail = result[3], tail = result[3],
trailingSlash = /[\\\/]$/.test(tail); trailingSlash = /[\\\/]$/.test(tail);
@ -206,19 +205,18 @@ if (isWindows) {
} }
return device + (isAbsolute ? '\\' : '') + tail; return device + (isAbsolute ? '\\' : '') + tail;
}; };
// windows version
exports.isAbsolute = function(path) { win32.isAbsolute = function(path) {
var result = splitDeviceRe.exec(path), var result = splitDeviceRe.exec(path),
device = result[1] || '', device = result[1] || '',
isUnc = !!device && device.charAt(1) !== ':'; isUnc = !!device && device.charAt(1) !== ':';
// UNC paths are always absolute // UNC paths are always absolute
return !!result[2] || isUnc; return !!result[2] || isUnc;
}; };
// windows version win32.join = function() {
exports.join = function() {
function f(p) { function f(p) {
if (!util.isString(p)) { if (!util.isString(p)) {
throw new TypeError('Arguments to path.join must be strings'); throw new TypeError('Arguments to path.join must be strings');
@ -246,18 +244,18 @@ if (isWindows) {
joined = joined.replace(/^[\\\/]{2,}/, '\\'); joined = joined.replace(/^[\\\/]{2,}/, '\\');
} }
return exports.normalize(joined); return win32.normalize(joined);
}; };
// path.relative(from, to)
// it will solve the relative path from 'from' to 'to', for instance: // path.relative(from, to)
// from = 'C:\\orandea\\test\\aaa' // it will solve the relative path from 'from' to 'to', for instance:
// to = 'C:\\orandea\\impl\\bbb' // from = 'C:\\orandea\\test\\aaa'
// The output of the function should be: '..\\..\\impl\\bbb' // to = 'C:\\orandea\\impl\\bbb'
// windows version // The output of the function should be: '..\\..\\impl\\bbb'
exports.relative = function(from, to) { win32.relative = function(from, to) {
from = exports.resolve(from); from = win32.resolve(from);
to = exports.resolve(to); to = win32.resolve(to);
// windows is not case sensitive // windows is not case sensitive
var lowerFrom = from.toLowerCase(); var lowerFrom = from.toLowerCase();
@ -304,24 +302,87 @@ if (isWindows) {
outputParts = outputParts.concat(toParts.slice(samePartsLength)); outputParts = outputParts.concat(toParts.slice(samePartsLength));
return outputParts.join('\\'); return outputParts.join('\\');
}; };
exports.sep = '\\';
exports.delimiter = ';';
} else /* posix */ { win32._makeLong = function(path) {
// Note: this will *probably* throw somewhere.
if (!util.isString(path))
return path;
// Split a filename into [root, dir, basename, ext], unix version if (!path) {
// 'root' is just a slash, or nothing. return '';
var splitPathRe = }
var resolvedPath = win32.resolve(path);
if (/^[a-zA-Z]\:\\/.test(resolvedPath)) {
// path is local filesystem path, which needs to be converted
// to long UNC path.
return '\\\\?\\' + resolvedPath;
} else if (/^\\\\[^?.]/.test(resolvedPath)) {
// path is network UNC path, which needs to be converted
// to long UNC path.
return '\\\\?\\UNC\\' + resolvedPath.substring(2);
}
return path;
};
win32.dirname = function(path) {
var result = win32.splitPath(path),
root = result[0],
dir = result[1];
if (!root && !dir) {
// No dirname whatsoever
return '.';
}
if (dir) {
// It has a dirname, strip trailing slash
dir = dir.substr(0, dir.length - 1);
}
return root + dir;
};
win32.basename = function(path, ext) {
var f = win32.splitPath(path)[2];
// TODO: make this comparison case-insensitive on windows?
if (ext && f.substr(-1 * ext.length) === ext) {
f = f.substr(0, f.length - ext.length);
}
return f;
};
win32.extname = function(path) {
return win32.splitPath(path)[3];
};
win32.sep = '\\';
win32.delimiter = ';';
// Split a filename into [root, dir, basename, ext], unix version
// 'root' is just a slash, or nothing.
var splitPathRe =
/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/; /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
var splitPath = function(filename) { var posix = {};
return splitPathRe.exec(filename).slice(1);
};
// path.resolve([from ...], to)
// posix version posix.splitPath = function(filename) {
exports.resolve = function() { return splitPathRe.exec(filename).slice(1);
};
// path.resolve([from ...], to)
// posix version
posix.resolve = function() {
var resolvedPath = '', var resolvedPath = '',
resolvedAbsolute = false; resolvedAbsolute = false;
@ -348,13 +409,13 @@ if (isWindows) {
}), !resolvedAbsolute).join('/'); }), !resolvedAbsolute).join('/');
return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.'; return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
}; };
// path.normalize(path) // path.normalize(path)
// posix version // posix version
exports.normalize = function(path) { posix.normalize = function(path) {
var isAbsolute = exports.isAbsolute(path), var isAbsolute = posix.isAbsolute(path),
trailingSlash = path[path.length - 1] === '/', trailingSlash = path.substr(-1) === '/',
segments = path.split('/'), segments = path.split('/'),
nonEmptySegments = []; nonEmptySegments = [];
@ -374,15 +435,15 @@ if (isWindows) {
} }
return (isAbsolute ? '/' : '') + path; return (isAbsolute ? '/' : '') + path;
}; };
// posix version // posix version
exports.isAbsolute = function(path) { posix.isAbsolute = function(path) {
return path.charAt(0) === '/'; return path.charAt(0) === '/';
}; };
// posix version // posix version
exports.join = function() { posix.join = function() {
var path = ''; var path = '';
for (var i = 0; i < arguments.length; i++) { for (var i = 0; i < arguments.length; i++) {
var segment = arguments[i]; var segment = arguments[i];
@ -397,15 +458,15 @@ if (isWindows) {
} }
} }
} }
return exports.normalize(path); return posix.normalize(path);
}; };
// path.relative(from, to) // path.relative(from, to)
// posix version // posix version
exports.relative = function(from, to) { posix.relative = function(from, to) {
from = exports.resolve(from).substr(1); from = posix.resolve(from).substr(1);
to = exports.resolve(to).substr(1); to = posix.resolve(to).substr(1);
function trim(arr) { function trim(arr) {
var start = 0; var start = 0;
@ -442,14 +503,16 @@ if (isWindows) {
outputParts = outputParts.concat(toParts.slice(samePartsLength)); outputParts = outputParts.concat(toParts.slice(samePartsLength));
return outputParts.join('/'); return outputParts.join('/');
}; };
exports.sep = '/';
exports.delimiter = ':';
}
exports.dirname = function(path) { posix._makeLong = function(path) {
var result = splitPath(path), return path;
};
posix.dirname = function(path) {
var result = posix.splitPath(path),
root = result[0], root = result[0],
dir = result[1]; dir = result[1];
@ -467,8 +530,8 @@ exports.dirname = function(path) {
}; };
exports.basename = function(path, ext) { posix.basename = function(path, ext) {
var f = splitPath(path)[2]; var f = posix.splitPath(path)[2];
// TODO: make this comparison case-insensitive on windows? // TODO: make this comparison case-insensitive on windows?
if (ext && f.substr(-1 * ext.length) === ext) { if (ext && f.substr(-1 * ext.length) === ext) {
f = f.substr(0, f.length - ext.length); f = f.substr(0, f.length - ext.length);
@ -477,47 +540,19 @@ exports.basename = function(path, ext) {
}; };
exports.extname = function(path) { posix.extname = function(path) {
return splitPath(path)[3]; return posix.splitPath(path)[3];
}; };
exports.exists = util.deprecate(function(path, callback) { posix.sep = '/';
require('fs').exists(path, callback); posix.delimiter = ':';
}, 'path.exists is now called `fs.exists`.');
exports.existsSync = util.deprecate(function(path) { if (isWindows)
return require('fs').existsSync(path); module.exports = win32;
}, 'path.existsSync is now called `fs.existsSync`.'); else /* posix */
module.exports = posix;
module.exports.posix = posix;
if (isWindows) { module.exports.win32 = win32;
exports._makeLong = function(path) {
// Note: this will *probably* throw somewhere.
if (!util.isString(path))
return path;
if (!path) {
return '';
}
var resolvedPath = exports.resolve(path);
if (/^[a-zA-Z]\:\\/.test(resolvedPath)) {
// path is local filesystem path, which needs to be converted
// to long UNC path.
return '\\\\?\\' + resolvedPath;
} else if (/^\\\\[^?.]/.test(resolvedPath)) {
// path is network UNC path, which needs to be converted
// to long UNC path.
return '\\\\?\\UNC\\' + resolvedPath.substring(2);
}
return path;
};
} else {
exports._makeLong = function(path) {
return path;
};
}

View File

@ -37,22 +37,19 @@ assert.equal(path.basename('basename.ext'), 'basename.ext');
assert.equal(path.basename('basename.ext/'), 'basename.ext'); assert.equal(path.basename('basename.ext/'), 'basename.ext');
assert.equal(path.basename('basename.ext//'), 'basename.ext'); assert.equal(path.basename('basename.ext//'), 'basename.ext');
if (isWindows) { // On Windows a backslash acts as a path separator.
// On Windows a backslash acts as a path separator. assert.equal(path.win32.basename('\\dir\\basename.ext'), 'basename.ext');
assert.equal(path.basename('\\dir\\basename.ext'), 'basename.ext'); assert.equal(path.win32.basename('\\basename.ext'), 'basename.ext');
assert.equal(path.basename('\\basename.ext'), 'basename.ext'); assert.equal(path.win32.basename('basename.ext'), 'basename.ext');
assert.equal(path.basename('basename.ext'), 'basename.ext'); assert.equal(path.win32.basename('basename.ext\\'), 'basename.ext');
assert.equal(path.basename('basename.ext\\'), 'basename.ext'); assert.equal(path.win32.basename('basename.ext\\\\'), 'basename.ext');
assert.equal(path.basename('basename.ext\\\\'), 'basename.ext');
} else { // On unix a backslash is just treated as any other character.
// On unix a backslash is just treated as any other character. assert.equal(path.posix.basename('\\dir\\basename.ext'), '\\dir\\basename.ext');
assert.equal(path.basename('\\dir\\basename.ext'), '\\dir\\basename.ext'); assert.equal(path.posix.basename('\\basename.ext'), '\\basename.ext');
assert.equal(path.basename('\\basename.ext'), '\\basename.ext'); assert.equal(path.posix.basename('basename.ext'), 'basename.ext');
assert.equal(path.basename('basename.ext'), 'basename.ext'); assert.equal(path.posix.basename('basename.ext\\'), 'basename.ext\\');
assert.equal(path.basename('basename.ext\\'), 'basename.ext\\'); assert.equal(path.posix.basename('basename.ext\\\\'), 'basename.ext\\\\');
assert.equal(path.basename('basename.ext\\\\'), 'basename.ext\\\\');
}
// POSIX filenames may include control characters // POSIX filenames may include control characters
// c.f. http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html // c.f. http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html
@ -73,35 +70,33 @@ assert.equal(path.dirname(''), '.');
assert.equal(path.dirname('/'), '/'); assert.equal(path.dirname('/'), '/');
assert.equal(path.dirname('////'), '/'); assert.equal(path.dirname('////'), '/');
if (isWindows) { assert.equal(path.win32.dirname('c:\\'), 'c:\\');
assert.equal(path.dirname('c:\\'), 'c:\\'); assert.equal(path.win32.dirname('c:\\foo'), 'c:\\');
assert.equal(path.dirname('c:\\foo'), 'c:\\'); assert.equal(path.win32.dirname('c:\\foo\\'), 'c:\\');
assert.equal(path.dirname('c:\\foo\\'), 'c:\\'); assert.equal(path.win32.dirname('c:\\foo\\bar'), 'c:\\foo');
assert.equal(path.dirname('c:\\foo\\bar'), 'c:\\foo'); assert.equal(path.win32.dirname('c:\\foo\\bar\\'), 'c:\\foo');
assert.equal(path.dirname('c:\\foo\\bar\\'), 'c:\\foo'); assert.equal(path.win32.dirname('c:\\foo\\bar\\baz'), 'c:\\foo\\bar');
assert.equal(path.dirname('c:\\foo\\bar\\baz'), 'c:\\foo\\bar'); assert.equal(path.win32.dirname('\\'), '\\');
assert.equal(path.dirname('\\'), '\\'); assert.equal(path.win32.dirname('\\foo'), '\\');
assert.equal(path.dirname('\\foo'), '\\'); assert.equal(path.win32.dirname('\\foo\\'), '\\');
assert.equal(path.dirname('\\foo\\'), '\\'); assert.equal(path.win32.dirname('\\foo\\bar'), '\\foo');
assert.equal(path.dirname('\\foo\\bar'), '\\foo'); assert.equal(path.win32.dirname('\\foo\\bar\\'), '\\foo');
assert.equal(path.dirname('\\foo\\bar\\'), '\\foo'); assert.equal(path.win32.dirname('\\foo\\bar\\baz'), '\\foo\\bar');
assert.equal(path.dirname('\\foo\\bar\\baz'), '\\foo\\bar'); assert.equal(path.win32.dirname('c:'), 'c:');
assert.equal(path.dirname('c:'), 'c:'); assert.equal(path.win32.dirname('c:foo'), 'c:');
assert.equal(path.dirname('c:foo'), 'c:'); assert.equal(path.win32.dirname('c:foo\\'), 'c:');
assert.equal(path.dirname('c:foo\\'), 'c:'); assert.equal(path.win32.dirname('c:foo\\bar'), 'c:foo');
assert.equal(path.dirname('c:foo\\bar'), 'c:foo'); assert.equal(path.win32.dirname('c:foo\\bar\\'), 'c:foo');
assert.equal(path.dirname('c:foo\\bar\\'), 'c:foo'); assert.equal(path.win32.dirname('c:foo\\bar\\baz'), 'c:foo\\bar');
assert.equal(path.dirname('c:foo\\bar\\baz'), 'c:foo\\bar'); assert.equal(path.win32.dirname('\\\\unc\\share'), '\\\\unc\\share');
assert.equal(path.dirname('\\\\unc\\share'), '\\\\unc\\share'); assert.equal(path.win32.dirname('\\\\unc\\share\\foo'), '\\\\unc\\share\\');
assert.equal(path.dirname('\\\\unc\\share\\foo'), '\\\\unc\\share\\'); assert.equal(path.win32.dirname('\\\\unc\\share\\foo\\'), '\\\\unc\\share\\');
assert.equal(path.dirname('\\\\unc\\share\\foo\\'), '\\\\unc\\share\\'); assert.equal(path.win32.dirname('\\\\unc\\share\\foo\\bar'),
assert.equal(path.dirname('\\\\unc\\share\\foo\\bar'),
'\\\\unc\\share\\foo'); '\\\\unc\\share\\foo');
assert.equal(path.dirname('\\\\unc\\share\\foo\\bar\\'), assert.equal(path.win32.dirname('\\\\unc\\share\\foo\\bar\\'),
'\\\\unc\\share\\foo'); '\\\\unc\\share\\foo');
assert.equal(path.dirname('\\\\unc\\share\\foo\\bar\\baz'), assert.equal(path.win32.dirname('\\\\unc\\share\\foo\\bar\\baz'),
'\\\\unc\\share\\foo\\bar'); '\\\\unc\\share\\foo\\bar');
}
assert.equal(path.extname(''), ''); assert.equal(path.extname(''), '');
@ -146,28 +141,25 @@ assert.equal(path.extname('file//'), '');
assert.equal(path.extname('file./'), '.'); assert.equal(path.extname('file./'), '.');
assert.equal(path.extname('file.//'), '.'); assert.equal(path.extname('file.//'), '.');
if (isWindows) { // On windows, backspace is a path separator.
// On windows, backspace is a path separator. assert.equal(path.win32.extname('.\\'), '');
assert.equal(path.extname('.\\'), ''); assert.equal(path.win32.extname('..\\'), '');
assert.equal(path.extname('..\\'), ''); assert.equal(path.win32.extname('file.ext\\'), '.ext');
assert.equal(path.extname('file.ext\\'), '.ext'); assert.equal(path.win32.extname('file.ext\\\\'), '.ext');
assert.equal(path.extname('file.ext\\\\'), '.ext'); assert.equal(path.win32.extname('file\\'), '');
assert.equal(path.extname('file\\'), ''); assert.equal(path.win32.extname('file\\\\'), '');
assert.equal(path.extname('file\\\\'), ''); assert.equal(path.win32.extname('file.\\'), '.');
assert.equal(path.extname('file.\\'), '.'); assert.equal(path.win32.extname('file.\\\\'), '.');
assert.equal(path.extname('file.\\\\'), '.');
} else { // On unix, backspace is a valid name component like any other character.
// On unix, backspace is a valid name component like any other character. assert.equal(path.posix.extname('.\\'), '');
assert.equal(path.extname('.\\'), ''); assert.equal(path.posix.extname('..\\'), '.\\');
assert.equal(path.extname('..\\'), '.\\'); assert.equal(path.posix.extname('file.ext\\'), '.ext\\');
assert.equal(path.extname('file.ext\\'), '.ext\\'); assert.equal(path.posix.extname('file.ext\\\\'), '.ext\\\\');
assert.equal(path.extname('file.ext\\\\'), '.ext\\\\'); assert.equal(path.posix.extname('file\\'), '');
assert.equal(path.extname('file\\'), ''); assert.equal(path.posix.extname('file\\\\'), '');
assert.equal(path.extname('file\\\\'), ''); assert.equal(path.posix.extname('file.\\'), '.\\');
assert.equal(path.extname('file.\\'), '.\\'); assert.equal(path.posix.extname('file.\\\\'), '.\\\\');
assert.equal(path.extname('file.\\\\'), '.\\\\');
}
// path.join tests // path.join tests
var failures = []; var failures = [];
@ -294,23 +286,21 @@ joinThrowTests.forEach(function(test) {
// path normalize tests // path normalize tests
if (isWindows) { assert.equal(path.win32.normalize('./fixtures///b/../b/c.js'),
assert.equal(path.normalize('./fixtures///b/../b/c.js'),
'fixtures\\b\\c.js'); 'fixtures\\b\\c.js');
assert.equal(path.normalize('/foo/../../../bar'), '\\bar'); assert.equal(path.win32.normalize('/foo/../../../bar'), '\\bar');
assert.equal(path.normalize('a//b//../b'), 'a\\b'); assert.equal(path.win32.normalize('a//b//../b'), 'a\\b');
assert.equal(path.normalize('a//b//./c'), 'a\\b\\c'); assert.equal(path.win32.normalize('a//b//./c'), 'a\\b\\c');
assert.equal(path.normalize('a//b//.'), 'a\\b'); assert.equal(path.win32.normalize('a//b//.'), 'a\\b');
assert.equal(path.normalize('//server/share/dir/file.ext'), assert.equal(path.win32.normalize('//server/share/dir/file.ext'),
'\\\\server\\share\\dir\\file.ext'); '\\\\server\\share\\dir\\file.ext');
} else {
assert.equal(path.normalize('./fixtures///b/../b/c.js'), assert.equal(path.posix.normalize('./fixtures///b/../b/c.js'),
'fixtures/b/c.js'); 'fixtures/b/c.js');
assert.equal(path.normalize('/foo/../../../bar'), '/bar'); assert.equal(path.posix.normalize('/foo/../../../bar'), '/bar');
assert.equal(path.normalize('a//b//../b'), 'a/b'); assert.equal(path.posix.normalize('a//b//../b'), 'a/b');
assert.equal(path.normalize('a//b//./c'), 'a/b/c'); assert.equal(path.posix.normalize('a//b//./c'), 'a/b/c');
assert.equal(path.normalize('a//b//.'), 'a/b'); assert.equal(path.posix.normalize('a//b//.'), 'a/b');
}
// path.resolve tests // path.resolve tests
if (isWindows) { if (isWindows) {
@ -352,21 +342,19 @@ resolveTests.forEach(function(test) {
assert.equal(failures.length, 0, failures.join('')); assert.equal(failures.length, 0, failures.join(''));
// path.isAbsolute tests // path.isAbsolute tests
if (isWindows) { assert.equal(path.win32.isAbsolute('//server/file'), true);
assert.equal(path.isAbsolute('//server/file'), true); assert.equal(path.win32.isAbsolute('\\\\server\\file'), true);
assert.equal(path.isAbsolute('\\\\server\\file'), true); assert.equal(path.win32.isAbsolute('C:/Users/'), true);
assert.equal(path.isAbsolute('C:/Users/'), true); assert.equal(path.win32.isAbsolute('C:\\Users\\'), true);
assert.equal(path.isAbsolute('C:\\Users\\'), true); assert.equal(path.win32.isAbsolute('C:cwd/another'), false);
assert.equal(path.isAbsolute('C:cwd/another'), false); assert.equal(path.win32.isAbsolute('C:cwd\\another'), false);
assert.equal(path.isAbsolute('C:cwd\\another'), false); assert.equal(path.win32.isAbsolute('directory/directory'), false);
assert.equal(path.isAbsolute('directory/directory'), false); assert.equal(path.win32.isAbsolute('directory\\directory'), false);
assert.equal(path.isAbsolute('directory\\directory'), false);
} else { assert.equal(path.posix.isAbsolute('/home/foo'), true);
assert.equal(path.isAbsolute('/home/foo'), true); assert.equal(path.posix.isAbsolute('/home/foo/..'), true);
assert.equal(path.isAbsolute('/home/foo/..'), true); assert.equal(path.posix.isAbsolute('bar/'), false);
assert.equal(path.isAbsolute('bar/'), false); assert.equal(path.posix.isAbsolute('./baz'), false);
assert.equal(path.isAbsolute('./baz'), false);
}
// path.relative tests // path.relative tests
if (isWindows) { if (isWindows) {
@ -405,20 +393,20 @@ relativeTests.forEach(function(test) {
}); });
assert.equal(failures.length, 0, failures.join('')); assert.equal(failures.length, 0, failures.join(''));
// path.sep tests // windows
if (isWindows) { assert.equal(path.win32.sep, '\\');
// windows // posix
assert.equal(path.sep, '\\'); assert.equal(path.posix.sep, '/');
} else {
// posix
assert.equal(path.sep, '/');
}
// path.delimiter tests // path.delimiter tests
if (isWindows) { // windows
// windows assert.equal(path.win32.delimiter, ';');
assert.equal(path.delimiter, ';');
} else { // posix
// posix assert.equal(path.posix.delimiter, ':');
assert.equal(path.delimiter, ':');
}
if (isWindows)
assert.deepEqual(path, path.win32, 'should be win32 path module');
else
assert.deepEqual(path, path.posix, 'should be posix path module');