Permalink
Browse files

path, refactor: rewrite path.resolve, more compatible with node.js (#374

)
  • Loading branch information...
ngot authored and xicilion committed Nov 7, 2017
1 parent 402f772 commit bf6a56159bd6732c13ca4ad4431de972934fbc2c
Showing with 63 additions and 120 deletions.
  1. +10 −4 fibjs/include/path.h
  2. +53 −116 test/path_test.js
View
@@ -36,10 +36,16 @@ class Path {
const char* c_str = other.c_str();
if (isWin32PathSlash(c_str[0]) && isWin32PathSlash(c_str[1])
&& !isWin32PathSlash(c_str[2])) {
m_buf = other;
return;
if (other.length() > 2) {
if (isWin32PathSlash(c_str[0]) && isWin32PathSlash(c_str[1])
&& !isWin32PathSlash(c_str[2])) {
for (int i = 3; i < other.length(); i++) {
if (isWin32PathSlash(c_str[i])) {
m_buf = other;
return;
}
}
}
}
char diskId = 0;
View
@@ -562,124 +562,61 @@ describe('path', () => {
});
it('resolve', () => {
var resolveTestsWin32 =
// arguments result
[
[
['c:/blah\\blah', 'd:/games', 'c:../a'], 'c:\\blah\\a'
],
[
['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'], 'd:\\e.exe'
],
[
['c:/ignore', 'c:/some/file'], 'c:\\some\\file'
],
[
['d:/ignore', 'd:some/dir//'], 'd:\\ignore\\some\\dir'
],
[
['//server/share', '..', 'relative\\'], '\\\\server\\share\\relative'
],
// [
// ['c:/', '//'], 'c:\\'
// ],
// [
// ['c:/', '//dir'], 'c:\\dir'
// ],
[
['c:/', 'C://'], 'C:\\'
],
[
['C:/', 'c://'], 'c:\\'
],
[
['', '//server/share'], '\\\\server\\share\\'
],
[
['c:/', '//server//share'], '\\\\server\\share\\'
],
[
['c:/', '///some//dir'], 'c:\\some\\dir'
],
[
['C:\\foo\\tmp.3\\', '..\\tmp.3\\cycles\\root.js'],
'C:\\foo\\tmp.3\\cycles\\root.js'
]
];
// Posix
var resolveTestsPosix =
// arguments result
[
[
['/var/lib', '../', 'file/'], '/var/file'
],
[
['/var/lib', '/../', 'file/'], '/file'
],
[
['a/b/c/', '../../..'], process.cwd()
],
[
['.'], process.cwd()
],
[
['/some/dir', '.', '/absolute/'], '/absolute'
],
[
['/foo/tmp.3/', '../tmp.3/cycles/root.js'], '/foo/tmp.3/cycles/root.js'
]
];
var failures = [];
var resolveTests = isWindows ? resolveTestsWin32 : resolveTestsPosix;
// path.resolve
resolveTests.forEach(test => {
var actual = path.resolve.apply(path, test[0]);
var expected = test[1];
var message = 'path.resolve(' + test[0].map(JSON.stringify).join(',') + ')' +
'\n expect=' + JSON.stringify(expected) +
'\n actual=' + JSON.stringify(actual);
if (isWindows) {
actual = actual.toLowerCase();
expected = expected.toLowerCase();
}
if (actual !== expected) failures.push('\n' + message);
assert.equal(actual, expected, message);
});
assert.equal(failures.length, 0, failures.join(''));
// path.posix.resolve
resolveTestsPosix.forEach(test => {
var actual = path.posix.resolve.apply(path.posix, test[0]);
var expected = test[1];
var message = 'path.posix.resolve(' + test[0].map(JSON.stringify).join(',') + ')' +
'\n expect=' + JSON.stringify(expected) +
'\n actual=' + JSON.stringify(actual);
if (actual !== expected) failures.push('\n' + message);
assert.equal(actual, expected, message);
});
assert.equal(failures.length, 0, failures.join(''));
// path.win32.resolve
resolveTestsWin32.forEach(test => {
var actual = path.win32.resolve.apply(path.win32, test[0]);
var expected = test[1];
var message = 'path.win32.resolve(' + test[0].map(JSON.stringify).join(',') + ')' +
'\n expect=' + JSON.stringify(expected) +
'\n actual=' + JSON.stringify(actual);
const failures = [];
const slashRE = /\//g;
const backslashRE = /\\/g;
actual = actual.toLowerCase();
expected = expected.toLowerCase();
if (actual !== expected) failures.push('\n' + message);
assert.equal(actual, expected, message);
const resolveTests = [
[path.win32.resolve,
// arguments result
[[['c:/blah\\blah', 'd:/games', 'c:../a'], 'c:\\blah\\a'],
[['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'], 'd:\\e.exe'],
[['c:/ignore', 'c:/some/file'], 'c:\\some\\file'],
[['d:/ignore', 'd:some/dir//'], 'd:\\ignore\\some\\dir'],
[['.'], process.cwd()],
[['//server/share', '..', 'relative\\'], '\\\\server\\share\\relative'],
[['c:/', '//'], 'c:\\'],
[['c:/', '//dir'], 'c:\\dir'],
[['c:/', '//server/share'], '\\\\server\\share\\'],
[['c:/', '//server//share'], '\\\\server\\share\\'],
[['c:/', '///some//dir'], 'c:\\some\\dir'],
[['C:\\foo\\tmp.3\\', '..\\tmp.3\\cycles\\root.js'],
'C:\\foo\\tmp.3\\cycles\\root.js']
],
[['c:/', 'C://'], 'C:\\'],
[['C:/', 'c://'], 'c:\\'],
],
[path.posix.resolve,
// arguments result
[[['/var/lib', '../', 'file/'], '/var/file'],
[['/var/lib', '/../', 'file/'], '/file'],
[['a/b/c/', '../../..'], process.cwd()],
[['.'], process.cwd()],
[['/some/dir', '.', '/absolute/'], '/absolute'],
[['/foo/tmp.3/', '../tmp.3/cycles/root.js'], '/foo/tmp.3/cycles/root.js']
]
]
];
resolveTests.forEach((test) => {
const resolve = test[0];
test[1].forEach((test) => {
const actual = resolve.apply(null, test[0]);
let actualAlt;
const os = resolve === path.win32.resolve ? 'win32' : 'posix';
if (resolve === path.win32.resolve && !isWindows)
actualAlt = actual.replace(backslashRE, '/');
else if (resolve !== path.win32.resolve && isWindows)
actualAlt = actual.replace(slashRE, '\\');
const expected = test[1];
const message =
`path.${os}.resolve(${test[0].map(JSON.stringify).join(',')})\n expect=${
JSON.stringify(expected)}\n actual=${JSON.stringify(actual)}`;
if (actual !== expected && actualAlt !== expected)
failures.push(`\n${message}`);
});
});
assert.equal(failures.length, 0, failures.join(''));
assert.strictEqual(failures.length, 0, failures.join(''));
});
it("fullpath", () => {

0 comments on commit bf6a561

Please sign in to comment.