From 4eb65435961776fbefcc11ed774b778532c2c8dd Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 9 Mar 2022 23:16:56 +0800 Subject: add path test --- tests/modules/path/test.lua | 88 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) (limited to 'tests/modules/path/test.lua') diff --git a/tests/modules/path/test.lua b/tests/modules/path/test.lua index 80e87c5aa..7f0224b86 100644 --- a/tests/modules/path/test.lua +++ b/tests/modules/path/test.lua @@ -33,3 +33,91 @@ function test_extension(t) t:are_equal(path.extension("/home/foo.so"), ".so") t:are_equal(path.extension("\\home\\foo.so"), ".so") end + +function test_directory(t) + --[[ + t:are_equal(path.directory(""), nil) + t:are_equal(path.directory("."), nil) + if is_host("windows") then + t:are_equal(path.directory("c:"), nil) + t:are_equal(path.directory("c:\\"), nil) + t:are_equal(path.directory("c:\\xxx"), "c:") + t:are_equal(path.directory("c:\\xxx\\yyy"), "c:\\xxx") + else + t:are_equal(path.directory("/tmp"), "/") + t:are_equal(path.directory("/tmp/"), "/") + t:are_equal(path.directory("/tmp/xxx"), "/tmp") + t:are_equal(path.directory("/tmp/xxx/"), "/tmp") + t:are_equal(path.directory("/"), nil) + end]] +end + +function test_absolute(t) + t:are_equal(path.absolute("", ""), nil) + t:are_equal(path.absolute(".", "."), ".") + if is_host("windows") then + t:are_equal(path.absolute("foo", "c:"), "c:\\foo") + t:are_equal(path.absolute("foo", "c:\\"), "c:\\foo") + t:are_equal(path.absolute("foo", "c:\\tmp"), "c:\\tmp\\foo") + t:are_equal(path.absolute("foo", "c:\\tmp\\"), "c:\\tmp\\foo") + else + t:are_equal(path.absolute("", "/"), nil) + t:are_equal(path.absolute("/", "/"), "/") + t:are_equal(path.absolute(".", "/"), "/") + t:are_equal(path.absolute("foo", "/tmp/"), "/tmp/foo") + t:are_equal(path.absolute("foo", "/tmp"), "/tmp/foo") + end +end + +function test_relative(t) + t:are_equal(path.relative("", ""), nil) + t:are_equal(path.relative(".", "."), ".") + if is_host("windows") then + t:are_equal(path.relative("c:", "c:\\"), ".") + t:are_equal(path.relative("c:\\foo", "c:\\foo"), ".") + t:are_equal(path.relative("c:\\foo", "c:\\"), "foo") + t:are_equal(path.relative("c:\\tmp\\foo", "c:\\tmp"), "foo") + t:are_equal(path.relative("c:\\tmp\\foo", "c:\\tmp\\"), "foo") + else + t:are_equal(path.relative("", "/"), nil) + t:are_equal(path.relative("/", "/"), ".") + t:are_equal(path.relative("/tmp/foo", "/tmp/"), "foo") + t:are_equal(path.relative("/tmp/foo", "/tmp"), "foo") + end +end + +function test_translate(t) + t:are_equal(path.translate(""), nil) + t:are_equal(path.translate("."), ".") + t:are_equal(path.translate(".."), "..") + t:are_equal(path.translate("././."), ".") + t:are_equal(path.translate("../foo/..", {reduce_dot2 = true}), "..") + t:are_equal(path.translate("../foo/bar/../..", {reduce_dot2 = true}), "..") + if is_host("windows") then + t:are_equal(path.translate("c:"), "c:") + t:are_equal(path.translate("c:\\"), "c:") + t:are_equal(path.translate("c:\\foo\\.\\.\\"), "c:\\foo") + t:are_equal(path.translate("c:\\foo\\\\\\"), "c:\\foo") + t:are_equal(path.translate("c:\\foo\\..\\.."), "c:\\foo\\..\\..") + t:are_equal(path.translate("c:\\foo\\bar\\.\\..\\xyz", {reduce_dot2 = true}), "c:\\foo\\xyz") + t:are_equal(path.translate("c:\\foo\\.\\..", {reduce_dot2 = true}), "c:") + t:are_equal(path.translate("../..", {reduce_dot2 = true}), "..\\..") + t:are_equal(path.translate("../foo/bar/..", {reduce_dot2 = true}), "..\\foo") + t:are_equal(path.translate("../foo/bar/../../..", {reduce_dot2 = true}), "..\\..") + else + t:are_equal(path.translate("/"), "/"); + t:are_equal(path.translate("////"), "/"); + t:are_equal(path.translate("/./././"), "/"); + t:are_equal(path.translate("/foo/././"), "/foo"); + t:are_equal(path.translate("/foo//////"), "/foo"); + t:are_equal(path.translate("/foo/../.."), "/foo/../.."); + t:are_equal(path.translate("/foo/../../"), "/foo/../.."); + t:are_equal(path.translate("/foo/bar/.//..//xyz", {reduce_dot2 = true}), "/foo/xyz"); + t:are_equal(path.translate("/foo/../..", {reduce_dot2 = true}), "/"); + t:are_equal(path.translate("/foo/bar../..", {reduce_dot2 = true}), "/foo"); + t:are_equal(path.translate("../..", {reduce_dot2 = true}), "../.."); + t:are_equal(path.translate("../foo/bar/..", {reduce_dot2 = true}), "../foo"); + t:are_equal(path.translate("../foo/bar/../../..", {reduce_dot2 = true}), "../.."); + end +end + -- cgit v1.3.1 From fa2e4a9a5f21a8819beafb99a85652d83b7e5429 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 9 Mar 2022 23:25:38 +0800 Subject: improve path.directory --- core/src/xmake/engine.c | 2 ++ core/src/xmake/makefile | 1 + core/src/xmake/path/absolute.c | 4 +-- core/src/xmake/path/directory.c | 51 +++++++++++++++++++++++++++++++++++ core/src/xmake/path/relative.c | 2 -- tests/modules/path/test.lua | 3 +-- xmake/core/base/os.lua | 2 +- xmake/core/base/path.lua | 17 ------------ xmake/modules/core/tools/cl.lua | 3 +-- xmake/modules/core/tools/clang_cl.lua | 3 +-- xmake/modules/core/tools/gcc.lua | 3 +-- 11 files changed, 60 insertions(+), 31 deletions(-) create mode 100644 core/src/xmake/path/directory.c (limited to 'tests/modules/path/test.lua') diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index efc89977c..2f4efd60c 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -173,6 +173,7 @@ tb_int_t xm_io_poller_wait(lua_State* lua); tb_int_t xm_path_relative(lua_State* lua); tb_int_t xm_path_absolute(lua_State* lua); tb_int_t xm_path_translate(lua_State* lua); +tb_int_t xm_path_directory(lua_State* lua); tb_int_t xm_path_is_absolute(lua_State* lua); // the hash functions @@ -366,6 +367,7 @@ static luaL_Reg const g_path_functions[] = { "relative", xm_path_relative } , { "absolute", xm_path_absolute } , { "translate", xm_path_translate } +, { "directory", xm_path_directory } , { "is_absolute", xm_path_is_absolute } , { tb_null, tb_null } }; diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index c85696f1e..d25a12a9b 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -92,6 +92,7 @@ xmake_C_FILES += \ path/relative \ path/absolute \ path/translate \ + path/directory \ path/is_absolute \ hash/uuid4 \ hash/sha256 \ diff --git a/core/src/xmake/path/absolute.c b/core/src/xmake/path/absolute.c index b4baac03d..d8489d25f 100644 --- a/core/src/xmake/path/absolute.c +++ b/core/src/xmake/path/absolute.c @@ -45,10 +45,8 @@ tb_int_t xm_path_absolute(lua_State* lua) // get the root tb_char_t const* root = luaL_optstring(lua, 2, tb_null); - // done path:absolute(root) + // do path:absolute(root) tb_char_t data[TB_PATH_MAXN]; lua_pushstring(lua, tb_path_absolute_to(root, path, data, sizeof(data) - 1)); - - // ok return 1; } diff --git a/core/src/xmake/path/directory.c b/core/src/xmake/path/directory.c new file mode 100644 index 000000000..b8ec7476d --- /dev/null +++ b/core/src/xmake/path/directory.c @@ -0,0 +1,51 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015-present, TBOOX Open Source Group. + * + * @author ruki + * @file directory.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "directory" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_path_directory(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // get the path + tb_char_t const* path = luaL_checkstring(lua, 1); + tb_check_return_val(path, 0); + + // do path:directory() + tb_char_t data[TB_PATH_MAXN]; + tb_char_t const* dir = tb_path_directory(path, data, sizeof(data)); + if (dir) lua_pushstring(lua, dir); + else lua_pushnil(lua); + return 1; +} diff --git a/core/src/xmake/path/relative.c b/core/src/xmake/path/relative.c index 782f0f635..f64667747 100644 --- a/core/src/xmake/path/relative.c +++ b/core/src/xmake/path/relative.c @@ -48,7 +48,5 @@ tb_int_t xm_path_relative(lua_State* lua) // done path:relative(root) tb_char_t data[TB_PATH_MAXN]; lua_pushstring(lua, tb_path_relative_to(root, path, data, sizeof(data) - 1)); - - // ok return 1; } diff --git a/tests/modules/path/test.lua b/tests/modules/path/test.lua index 7f0224b86..05a5a0b40 100644 --- a/tests/modules/path/test.lua +++ b/tests/modules/path/test.lua @@ -35,7 +35,6 @@ function test_extension(t) end function test_directory(t) - --[[ t:are_equal(path.directory(""), nil) t:are_equal(path.directory("."), nil) if is_host("windows") then @@ -49,7 +48,7 @@ function test_directory(t) t:are_equal(path.directory("/tmp/xxx"), "/tmp") t:are_equal(path.directory("/tmp/xxx/"), "/tmp") t:are_equal(path.directory("/"), nil) - end]] + end end function test_absolute(t) diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 19f22e88f..5fefd9ab5 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -364,7 +364,7 @@ function os.match(pattern, mode, callback) if startpos then rootdir = rootdir:sub(1, startpos - 1) end - rootdir = path.directory(rootdir) + rootdir = path.translate(rootdir) -- compute the recursion level -- diff --git a/xmake/core/base/path.lua b/xmake/core/base/path.lua index 92b926705..712d8e604 100644 --- a/xmake/core/base/path.lua +++ b/xmake/core/base/path.lua @@ -24,23 +24,6 @@ local path = path or {} -- load modules local string = require("base/string") --- get the directory of the path -function path.directory(p, sep) - local i = 0 - if sep then - -- if the path has been normalized, we can quickly find it with a unique path separator prompt - i = p:lastof(sep, true) or 0 - else - i = math.max(p:lastof('/', true) or 0, p:lastof('\\', true) or 0) - end - if i > 0 then - if i > 1 then i = i - 1 end - return p:sub(1, i) - else - return "." - end -end - -- get the filename of the path function path.filename(p, sep) local i = 0 diff --git a/xmake/modules/core/tools/cl.lua b/xmake/modules/core/tools/cl.lua index b499542e3..2526536c4 100644 --- a/xmake/modules/core/tools/cl.lua +++ b/xmake/modules/core/tools/cl.lua @@ -409,8 +409,7 @@ end function compile(self, sourcefile, objectfile, dependinfo, flags, opt) -- ensure the object directory - -- @note this path here has been normalized, we can quickly find it by the unique path separator prompt - local objectdir = path.directory(objectfile, path.sep()) + local objectdir = path.directory(objectfile) if not os.isdir(objectdir) then os.mkdir(objectdir) end diff --git a/xmake/modules/core/tools/clang_cl.lua b/xmake/modules/core/tools/clang_cl.lua index 9c4236343..1602b0ac8 100644 --- a/xmake/modules/core/tools/clang_cl.lua +++ b/xmake/modules/core/tools/clang_cl.lua @@ -166,8 +166,7 @@ end function compile(self, sourcefile, objectfile, dependinfo, flags) -- ensure the object directory - -- @note this path here has been normalized, we can quickly find it by the unique path separator prompt - os.mkdir(path.directory(objectfile, path.sep())) + os.mkdir(path.directory(objectfile)) -- compile it local outdata = try diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index a1dc82c9d..0d729692a 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -427,8 +427,7 @@ end function compile(self, sourcefile, objectfile, dependinfo, flags) -- ensure the object directory - -- @note this path here has been normalized, we can quickly find it by the unique path separator prompt - os.mkdir(path.directory(objectfile, path.sep())) + os.mkdir(path.directory(objectfile)) -- compile it local depfile = dependinfo and os.tmpfile() or nil -- cgit v1.3.1 From b839cf85148abac9cb0bff5af013bb6ac7203824 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 9 Mar 2022 23:52:26 +0800 Subject: fix dir --- core/src/tbox/tbox | 2 +- tests/modules/path/test.lua | 1 + xmake/core/base/os.lua | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) (limited to 'tests/modules/path/test.lua') diff --git a/core/src/tbox/tbox b/core/src/tbox/tbox index da64671b8..e823da418 160000 --- a/core/src/tbox/tbox +++ b/core/src/tbox/tbox @@ -1 +1 @@ -Subproject commit da64671b833ba8a7a56d882b9e170388504b70d2 +Subproject commit e823da4188ba5818d13febd05b1ac58a8eb0b616 diff --git a/tests/modules/path/test.lua b/tests/modules/path/test.lua index 05a5a0b40..8954e64a3 100644 --- a/tests/modules/path/test.lua +++ b/tests/modules/path/test.lua @@ -37,6 +37,7 @@ end function test_directory(t) t:are_equal(path.directory(""), nil) t:are_equal(path.directory("."), nil) + t:are_equal(path.directory("foo"), ".") if is_host("windows") then t:are_equal(path.directory("c:"), nil) t:are_equal(path.directory("c:\\"), nil) diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 5fefd9ab5..03c51006b 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -364,7 +364,7 @@ function os.match(pattern, mode, callback) if startpos then rootdir = rootdir:sub(1, startpos - 1) end - rootdir = path.translate(rootdir) + rootdir = path.directory(rootdir .. "_") -- compute the recursion level -- -- cgit v1.3.1