Skip to content

Commit

Permalink
path: support path.isAbsolute.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Jun 1, 2017
1 parent 7c5084a commit 6bd3ff3
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 21 deletions.
18 changes: 18 additions & 0 deletions fibjs/include/ifs/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class path_base : public object_base {
static result_t extname(exlib::string path, exlib::string& retVal);
static result_t dirname(exlib::string path, exlib::string& retVal);
static result_t fullpath(exlib::string path, exlib::string& retVal);
static result_t isAbsolute(exlib::string path, bool& retVal);
static result_t join(const v8::FunctionCallbackInfo<v8::Value>& args, exlib::string& retVal);
static result_t resolve(const v8::FunctionCallbackInfo<v8::Value>& args, exlib::string& retVal);
static result_t get_sep(exlib::string& retVal);
Expand All @@ -50,6 +51,7 @@ class path_base : public object_base {
static void s_extname(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_dirname(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_fullpath(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_isAbsolute(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_join(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_resolve(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_get_sep(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args);
Expand All @@ -68,6 +70,7 @@ inline ClassInfo& path_base::class_info()
{ "extname", s_extname, true },
{ "dirname", s_dirname, true },
{ "fullpath", s_fullpath, true },
{ "isAbsolute", s_isAbsolute, true },
{ "join", s_join, true },
{ "resolve", s_resolve, true }
};
Expand Down Expand Up @@ -165,6 +168,21 @@ inline void path_base::s_fullpath(const v8::FunctionCallbackInfo<v8::Value>& arg
METHOD_RETURN();
}

inline void path_base::s_isAbsolute(const v8::FunctionCallbackInfo<v8::Value>& args)
{
bool vr;

METHOD_ENTER();

METHOD_OVER(1, 1);

ARG(exlib::string, 0);

hr = isAbsolute(v0, vr);

METHOD_RETURN();
}

inline void path_base::s_join(const v8::FunctionCallbackInfo<v8::Value>& args)
{
exlib::string vr;
Expand Down
7 changes: 7 additions & 0 deletions fibjs/include/ifs/path.idl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ module path
*/
static String fullpath(String path);

/*! @brief 识别给定的路径是否是绝对路径

@param path 给定需要识别的路径
@return 是绝对路径则返回 true
*/
static Boolean isAbsolute(String path);

/*! @brief 合并一系列路径成为一个单一路径

@param ... 一个或多个相关的路径
Expand Down
18 changes: 18 additions & 0 deletions fibjs/include/ifs/path_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class path_posix_base : public object_base {
static result_t extname(exlib::string path, exlib::string& retVal);
static result_t dirname(exlib::string path, exlib::string& retVal);
static result_t fullpath(exlib::string path, exlib::string& retVal);
static result_t isAbsolute(exlib::string path, bool& retVal);
static result_t join(const v8::FunctionCallbackInfo<v8::Value>& args, exlib::string& retVal);
static result_t resolve(const v8::FunctionCallbackInfo<v8::Value>& args, exlib::string& retVal);
static result_t get_sep(exlib::string& retVal);
Expand All @@ -50,6 +51,7 @@ class path_posix_base : public object_base {
static void s_extname(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_dirname(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_fullpath(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_isAbsolute(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_join(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_resolve(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_get_sep(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args);
Expand All @@ -68,6 +70,7 @@ inline ClassInfo& path_posix_base::class_info()
{ "extname", s_extname, true },
{ "dirname", s_dirname, true },
{ "fullpath", s_fullpath, true },
{ "isAbsolute", s_isAbsolute, true },
{ "join", s_join, true },
{ "resolve", s_resolve, true }
};
Expand Down Expand Up @@ -165,6 +168,21 @@ inline void path_posix_base::s_fullpath(const v8::FunctionCallbackInfo<v8::Value
METHOD_RETURN();
}

inline void path_posix_base::s_isAbsolute(const v8::FunctionCallbackInfo<v8::Value>& args)
{
bool vr;

METHOD_ENTER();

METHOD_OVER(1, 1);

ARG(exlib::string, 0);

hr = isAbsolute(v0, vr);

METHOD_RETURN();
}

inline void path_posix_base::s_join(const v8::FunctionCallbackInfo<v8::Value>& args)
{
exlib::string vr;
Expand Down
7 changes: 7 additions & 0 deletions fibjs/include/ifs/path_posix.idl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ module path_posix
*/
static String fullpath(String path);

/*! @brief 识别给定的路径是否是绝对路径

@param path 给定需要识别的路径
@return 是绝对路径则返回 true
*/
static Boolean isAbsolute(String path);

/*! @brief 合并一系列路径成为一个单一路径

@param ... 一个或多个相关的路径
Expand Down
18 changes: 18 additions & 0 deletions fibjs/include/ifs/path_win32.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class path_win32_base : public object_base {
static result_t extname(exlib::string path, exlib::string& retVal);
static result_t dirname(exlib::string path, exlib::string& retVal);
static result_t fullpath(exlib::string path, exlib::string& retVal);
static result_t isAbsolute(exlib::string path, bool& retVal);
static result_t join(const v8::FunctionCallbackInfo<v8::Value>& args, exlib::string& retVal);
static result_t resolve(const v8::FunctionCallbackInfo<v8::Value>& args, exlib::string& retVal);
static result_t get_sep(exlib::string& retVal);
Expand All @@ -50,6 +51,7 @@ class path_win32_base : public object_base {
static void s_extname(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_dirname(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_fullpath(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_isAbsolute(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_join(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_resolve(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_get_sep(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args);
Expand All @@ -68,6 +70,7 @@ inline ClassInfo& path_win32_base::class_info()
{ "extname", s_extname, true },
{ "dirname", s_dirname, true },
{ "fullpath", s_fullpath, true },
{ "isAbsolute", s_isAbsolute, true },
{ "join", s_join, true },
{ "resolve", s_resolve, true }
};
Expand Down Expand Up @@ -165,6 +168,21 @@ inline void path_win32_base::s_fullpath(const v8::FunctionCallbackInfo<v8::Value
METHOD_RETURN();
}

inline void path_win32_base::s_isAbsolute(const v8::FunctionCallbackInfo<v8::Value>& args)
{
bool vr;

METHOD_ENTER();

METHOD_OVER(1, 1);

ARG(exlib::string, 0);

hr = isAbsolute(v0, vr);

METHOD_RETURN();
}

inline void path_win32_base::s_join(const v8::FunctionCallbackInfo<v8::Value>& args)
{
exlib::string vr;
Expand Down
7 changes: 7 additions & 0 deletions fibjs/include/ifs/path_win32.idl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ module path_win32
*/
static String fullpath(String path);

/*! @brief 识别给定的路径是否是绝对路径

@param path 给定需要识别的路径
@return 是绝对路径则返回 true
*/
static Boolean isAbsolute(String path);

/*! @brief 合并一系列路径成为一个单一路径

@param ... 一个或多个相关的路径
Expand Down
6 changes: 6 additions & 0 deletions fibjs/src/fs/path_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ result_t path_posix_base::fullpath(exlib::string path, exlib::string& retVal)
return _fullpath(path, retVal);
}

result_t path_posix_base::isAbsolute(exlib::string path, bool& retVal)
{
retVal = isPosixPathSlash(path.c_str()[0]);
return 0;
}

result_t path_posix_base::join(const v8::FunctionCallbackInfo<v8::Value>& args, exlib::string& retVal)
{
return _join(args, retVal);
Expand Down
7 changes: 7 additions & 0 deletions fibjs/src/fs/path_win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ result_t path_win32_base::fullpath(exlib::string path, exlib::string& retVal)
return _fullpath_win32(path, 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] == ':');
return 0;
}

result_t path_win32_base::join(const v8::FunctionCallbackInfo<v8::Value>& args, exlib::string& retVal)
{
return _join_win32(args, retVal);
Expand Down
88 changes: 67 additions & 21 deletions test/path_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,21 @@ 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('.'));
});

it('join', () => {
var failures = [];
var joinTests = [
Expand Down Expand Up @@ -339,36 +354,67 @@ describe('path', () => {
assert.equal(path.win32.join('c:/path1', 'd:path2'), 'd:path2');
});

it("resolve", function() {
it("resolve", function () {
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'],
[['.'], process.cwd().replace(/\//g, '\\')],
[['//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:/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().replace(/\//g, '\\')
],
[
['//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'
]
];
// 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']];
[
[
['/var/lib', '../', 'file/'], '/var/file'
],
[
['/var/lib', '/../', 'file/'], '/file'
],
[
['a/b/c/', '../../..'], process.cwd()
],
[
['.'], process.cwd()
],
[
['/some/dir', '.', '/absolute/'], '/absolute'
]
];

var failures = [];

var resolveTests = isWindows ? resolveTestsWin32 : resolveTestsPosix;

// path.resolve
resolveTests.forEach(function(test) {
resolveTests.forEach(function (test) {
var actual = path.resolve.apply(path, test[0]);
var expected = test[1];
var message = 'path.resolve(' + test[0].map(JSON.stringify).join(',') + ')' +
Expand All @@ -386,7 +432,7 @@ describe('path', () => {
assert.equal(failures.length, 0, failures.join(''));

// path.posix.resolve
resolveTestsPosix.forEach(function(test) {
resolveTestsPosix.forEach(function (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(',') + ')' +
Expand All @@ -399,7 +445,7 @@ describe('path', () => {
assert.equal(failures.length, 0, failures.join(''));

// path.win32.resolve
resolveTestsWin32.forEach(function(test) {
resolveTestsWin32.forEach(function (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(',') + ')' +
Expand All @@ -424,4 +470,4 @@ describe('path', () => {
});
});

// test.run(console.DEBUG);
// test.run(console.DEBUG);

0 comments on commit 6bd3ff3

Please sign in to comment.