Permalink
Browse files

path, refactor: rewrite path.isAbsolute, more compatible with node.js

  • Loading branch information...
xicilion committed Nov 7, 2017
1 parent 780ba6b commit c3ba036c2fe5a2872e4305298eeaafb1adb2bd82
Showing with 50 additions and 13 deletions.
  1. +13 −1 fibjs/src/fs/path_win32.cpp
  2. +37 −12 test/path_test.js
@@ -42,7 +42,19 @@ result_t path_win32_base::fullpath(exlib::string path, exlib::string& retVal)
result_t path_win32_base::isAbsolute(exlib::string path, bool& retVal)
{
const char* c_str = path.c_str();
retVal = isWin32PathSlash(c_str[0]) || (qisascii(c_str[0]) && c_str[1] == ':');
if (isWin32PathSlash(c_str[0])) {
retVal = true;
return 0;
}
if (qisascii(c_str[0]) && c_str[1] == ':' && isWin32PathSlash(c_str[2])) {
retVal = true;
return 0;
}
retVal = false;
return 0;
}
View
@@ -408,18 +408,43 @@ describe('path', () => {
});
it('isAbsolute', () => {
assert.isTrue(path.posix.isAbsolute('/foo/bar'));
assert.isTrue(path.posix.isAbsolute('/baz/..'));
assert.isFalse(path.posix.isAbsolute('qux/'));
assert.isFalse(path.posix.isAbsolute('.'));
assert.isTrue(path.win32.isAbsolute('//server'));
assert.isTrue(path.win32.isAbsolute('\\\\server'));
assert.isTrue(path.win32.isAbsolute('C:/foo/..'));
assert.isTrue(path.win32.isAbsolute('C:\\foo\\..'));
assert.isFalse(path.win32.isAbsolute('bar\\baz'));
assert.isFalse(path.win32.isAbsolute('bar/baz'));
assert.isFalse(path.win32.isAbsolute('.'));
function testPosix(fn) {
assert.isTrue(fn('/foo/bar'));
assert.isTrue(fn('/baz/..'));
assert.isFalse(fn('qux/'));
assert.isFalse(fn('.'));
assert.isFalse(fn('./foo'));
}
function testWin32(fn) {
assert.isTrue(fn('/'));
assert.isTrue(fn('//'));
assert.isTrue(fn('//server'));
assert.isTrue(fn('//server/file'));
assert.isTrue(fn('\\\\server\\file'));
assert.isTrue(fn('\\\\server'));
assert.isTrue(fn('\\\\'));
assert.isFalse(fn('c'));
assert.isFalse(fn('c:'));
assert.isTrue(fn('c:\\'));
assert.isTrue(fn('c:/'));
assert.isTrue(fn('c://'));
assert.isTrue(fn('C:/Users/'));
assert.isTrue(fn('C:\\Users\\'));
assert.isFalse(fn('C:cwd/another'));
assert.isFalse(fn('C:cwd\\another'));
assert.isFalse(fn('directory/directory'));
assert.isFalse(fn('directory\\directory'));
}
testPosix(path.posix.isAbsolute.bind(path.posix));
testWin32(path.win32.isAbsolute.bind(path.win32));
if (isWindows) {
testWin32(path.isAbsolute.bind(path));
} else {
testPosix(path.isAbsolute.bind(path));
}
});
it('join', () => {

0 comments on commit c3ba036

Please sign in to comment.