From ce87d03e8ea89130933b2bc1b52e8bc31257fcfd Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 10 Jan 2021 23:00:09 +0800 Subject: improve tests --- tests/modules/os/test.lua | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) (limited to 'tests/modules') diff --git a/tests/modules/os/test.lua b/tests/modules/os/test.lua index 407421a5c..cf55366e8 100644 --- a/tests/modules/os/test.lua +++ b/tests/modules/os/test.lua @@ -63,25 +63,41 @@ end function test_argv(t) t:are_equal(os.argv(""), {}) + -- $cli aa bb cc t:are_equal(os.argv("aa bb cc"), {"aa", "bb", "cc"}) + -- $cli aa --bb=bbb -c t:are_equal(os.argv("aa --bb=bbb -c"), {"aa", "--bb=bbb", "-c"}) - t:are_equal(os.argv("\"aa bb cc\" dd"), {"aa bb cc", "dd"}) - t:are_equal(os.argv("\"aa(bb)cc\" dd"), {"aa(bb)cc", "dd"}) - t:are_equal(os.argv("aa\\bb/cc dd"), {"aa\\bb/cc", "dd"}) - t:are_equal(os.argv("\"aa\\\\bb/cc dd\" ee"), {"aa\\bb/cc dd", "ee"}) - t:are_equal(os.argv("\"aa\\\\bb/cc (dd)\" ee"), {"aa\\bb/cc (dd)", "ee"}) - t:are_equal(os.argv("-D__prefix__=\\\"tbox\\\""), {"-D__prefix__=\"tbox\""}) + -- $cli "aa bb cc" dd + t:are_equal(os.argv('"aa bb cc" dd'), {"aa bb cc", "dd"}) + -- $cli aa(bb)cc dd + t:are_equal(os.argv('"aa(bb)cc" dd'), {"aa(bb)cc", "dd"}) + -- $cli aa\\bb/cc dd + t:are_equal(os.argv('aa\\bb/cc dd'), {"aa\\bb/cc", "dd"}) + -- $cli "aa\\bb/cc dd" ee + t:are_equal(os.argv('"aa\\\\bb/cc dd" ee'), {"aa\\bb/cc dd", "ee"}) + -- $cli "aa\\bb/cc (dd)" ee + t:are_equal(os.argv('"aa\\\\bb/cc (dd)" ee'), {"aa\\bb/cc (dd)", "ee"}) + -- $cli -DTEST=\"hello\" + t:are_equal(os.argv('-DTEST=\\"hello\\"'), {'-DTEST="hello"'}) + -- $cli -DTEST="hello" + t:are_equal(os.argv('-DTEST="hello"'), {'-DTEST=hello'}) + -- $cli "-DTEST=\"hello world\"" "-DTEST2="\hello world2\"" + t:are_equal(os.argv('"-DTEST=\\\"hello world\\\"" "-DTEST2=\\\"hello world2\\\""'), {'-DTEST="hello world"', '-DTEST2="hello world2"'}) + -- $cli '-DTEST="hello world"' '-DTEST2="hello world2"' + t:are_equal(os.argv("'-DTEST=\"hello world\"' '-DTEST2=\"hello world2\"'"), {'-DTEST="hello world"', '-DTEST2="hello world2"'}) end function test_args(t) t:are_equal(os.args({}), "") t:are_equal(os.args({"aa", "bb", "cc"}), "aa bb cc") t:are_equal(os.args({"aa", "--bb=bbb", "-c"}), "aa --bb=bbb -c") - t:are_equal(os.args({"aa bb cc", "dd"}), "\"aa bb cc\" dd") - t:are_equal(os.args({"aa(bb)cc", "dd"}), "\"aa(bb)cc\" dd") + t:are_equal(os.args({"aa bb cc", "dd"}), '"aa bb cc" dd') + t:are_equal(os.args({"aa(bb)cc", "dd"}), '"aa(bb)cc" dd') t:are_equal(os.args({"aa\\bb/cc", "dd"}), "aa\\bb/cc dd") - t:are_equal(os.args({"aa\\bb/cc dd", "ee"}), "\"aa\\\\bb/cc dd\" ee") - t:are_equal(os.args({"aa\\bb/cc (dd)", "ee"}), "\"aa\\\\bb/cc (dd)\" ee") - t:are_equal(os.args("-D__prefix__=\"tbox\""), "-D__prefix__=\\\"tbox\\\"") + t:are_equal(os.args({"aa\\bb/cc dd", "ee"}), '"aa\\\\bb/cc dd" ee') + t:are_equal(os.args({"aa\\bb/cc (dd)", "ee"}), '"aa\\\\bb/cc (dd)" ee') t:are_equal(os.args({"aa\\bb/cc", "dd"}, {escape = true}), "aa\\\\bb/cc dd") + t:are_equal(os.args('-DTEST="hello"'), '-DTEST=\\"hello\\"') + t:are_equal(os.args('-DTEST=hello'), '-DTEST=hello') -- irreversible +-- t:are_equal(os.args({'-DTEST="hello world"', '-DTEST2="hello world2"'}), '"-DTEST=\\\"hello world\\\"" "-DTEST2=\\\"hello world2\\\""') end -- cgit v1.3.1 From e2b269b725607e0458cc337555e64c9c7fecde9f Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 10 Jan 2021 23:27:43 +0800 Subject: improve tests again --- tests/modules/os/test.lua | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'tests/modules') diff --git a/tests/modules/os/test.lua b/tests/modules/os/test.lua index cf55366e8..e5e27456b 100644 --- a/tests/modules/os/test.lua +++ b/tests/modules/os/test.lua @@ -79,8 +79,14 @@ function test_argv(t) t:are_equal(os.argv('"aa\\\\bb/cc (dd)" ee'), {"aa\\bb/cc (dd)", "ee"}) -- $cli -DTEST=\"hello\" t:are_equal(os.argv('-DTEST=\\"hello\\"'), {'-DTEST="hello"'}) + -- $cli -DTEST=\"hello\" -DTEST=\"hello\" + t:are_equal(os.argv('-DTEST=\\"hello\\" -DTEST2=\\"hello\\"'), {'-DTEST="hello"', '-DTEST2="hello"'}) -- $cli -DTEST="hello" t:are_equal(os.argv('-DTEST="hello"'), {'-DTEST=hello'}) + -- $cli -DTEST="hello world" + t:are_equal(os.argv('-DTEST="hello world"'), {'-DTEST=hello world'}) + -- $cli -DTEST=\"hello world\" + t:are_equal(os.argv('-DTEST=\\"hello world\\"'), {'-DTEST="hello', 'world\"'}) -- $cli "-DTEST=\"hello world\"" "-DTEST2="\hello world2\"" t:are_equal(os.argv('"-DTEST=\\\"hello world\\\"" "-DTEST2=\\\"hello world2\\\""'), {'-DTEST="hello world"', '-DTEST2="hello world2"'}) -- $cli '-DTEST="hello world"' '-DTEST2="hello world2"' @@ -98,6 +104,7 @@ function test_args(t) t:are_equal(os.args({"aa\\bb/cc (dd)", "ee"}), '"aa\\\\bb/cc (dd)" ee') t:are_equal(os.args({"aa\\bb/cc", "dd"}, {escape = true}), "aa\\\\bb/cc dd") t:are_equal(os.args('-DTEST="hello"'), '-DTEST=\\"hello\\"') + t:are_equal(os.args({'-DTEST="hello"', '-DTEST2="hello"'}), '-DTEST=\\"hello\\" -DTEST2=\\"hello\\"') t:are_equal(os.args('-DTEST=hello'), '-DTEST=hello') -- irreversible -- t:are_equal(os.args({'-DTEST="hello world"', '-DTEST2="hello world2"'}), '"-DTEST=\\\"hello world\\\"" "-DTEST2=\\\"hello world2\\\""') end -- cgit v1.3.1 From bbb48dbc7ee40d510ff6a192417b1b4497d0ca95 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 12 Jan 2021 00:29:12 +0800 Subject: fix os.args and split flags --- core/src/tbox/tbox | 2 +- core/src/xmake/os/args.c | 46 +++++++++++++++++----------------------- core/src/xmake/os/argv.c | 16 ++++++++++---- tests/apis/add_defines/xmake.lua | 3 +-- tests/modules/os/test.lua | 5 ++++- xmake/core/tool/builder.lua | 2 +- 6 files changed, 38 insertions(+), 36 deletions(-) (limited to 'tests/modules') diff --git a/core/src/tbox/tbox b/core/src/tbox/tbox index a175e4409..3b24fdb96 160000 --- a/core/src/tbox/tbox +++ b/core/src/tbox/tbox @@ -1 +1 @@ -Subproject commit a175e44090dac8891769d28968a7c06191b1813f +Subproject commit 3b24fdb96bccec99b2fbb99f1d6b04f67f249da6 diff --git a/core/src/xmake/os/args.c b/core/src/xmake/os/args.c index adfe843a9..4f1c89f7f 100644 --- a/core/src/xmake/os/args.c +++ b/core/src/xmake/os/args.c @@ -38,43 +38,35 @@ static tb_void_t tb_os_args_append(tb_string_ref_t result, tb_char_t const* cstr // check tb_assert_and_check_return(size < TB_PATH_MAXN); - // wrap and escape characters + // need wrap quote? tb_char_t ch; - tb_size_t n = 0; tb_char_t const* p = cstr; tb_bool_t wrap_quote = tb_false; - tb_char_t buff[TB_PATH_MAXN]; - tb_size_t m = tb_arrayn(buff); - while ((ch = *p) && n < m) + if (!nowrap) { - // escape '"' or '\\' - if (ch == '\"' || (escape && ch == '\\')) + while ((ch = *p)) { - if (n < m) buff[n++] = '\\'; + if (ch == ' ' || ch == '(' || ch == ')') wrap_quote = tb_true; + p++; } - else if (ch == ' ' || ch == '(' || ch == ')') wrap_quote = tb_true; - if (n < m) buff[n++] = ch; - p++; } - tb_assert_and_check_return(n < m); - buff[n] = '\0'; - // wrap "" if exists escape characters and spaces? - if (wrap_quote && !nowrap) + // wrap begin quote + if (wrap_quote) tb_string_chrcat(result, '\"'); + + // escape characters + p = cstr; + while ((ch = *p)) { - tb_string_chrcat(result, '\"'); - tb_size_t i = 0; - tb_char_t ch; - for (i = 0; i < n; i++) - { - ch = buff[i]; - if (ch == '\\') // escape the '\\' characters in "" - tb_string_chrcat(result, '\\'); - tb_string_chrcat(result, ch); - } - tb_string_chrcat(result, '\"'); + // escape '"' or '\\' + if (ch == '\"' || ((escape || wrap_quote) && ch == '\\')) + tb_string_chrcat(result, '\\'); + tb_string_chrcat(result, ch); + p++; } - else if (n) tb_string_cstrncat(result, buff, n); + + // wrap end quote + if (wrap_quote) tb_string_chrcat(result, '\"'); } /* ////////////////////////////////////////////////////////////////////////////////////// diff --git a/core/src/xmake/os/argv.c b/core/src/xmake/os/argv.c index f484fc6cf..980be8863 100644 --- a/core/src/xmake/os/argv.c +++ b/core/src/xmake/os/argv.c @@ -42,7 +42,17 @@ tb_int_t xm_os_argv(lua_State* lua) tb_char_t const* args = luaL_checkstring(lua, 1); tb_check_return_val(args, 0); - // done + // split only? do not escape + tb_bool_t splitonly = tb_false; + if (lua_istable(lua, 2)) + { + lua_pushstring(lua, "splitonly"); + lua_gettable(lua, 2); + splitonly = lua_toboolean(lua, -1); + lua_pop(lua, 1); + } + + // parse argument list tb_string_t arg; do { @@ -88,7 +98,7 @@ tb_int_t xm_os_argv(lua_State* lua) } // save this charactor to argument - if (!skip) tb_string_chrcat(&arg, ch); + if (splitonly || !skip) tb_string_chrcat(&arg, ch); // step and cancel escape if (escape == 1) escape++; @@ -114,7 +124,5 @@ tb_int_t xm_os_argv(lua_State* lua) // exit arg tb_string_exit(&arg); - - // ok return 1; } diff --git a/tests/apis/add_defines/xmake.lua b/tests/apis/add_defines/xmake.lua index b1085c074..1e00f4f62 100644 --- a/tests/apis/add_defines/xmake.lua +++ b/tests/apis/add_defines/xmake.lua @@ -7,5 +7,4 @@ target("test") add_defines("TEST2=\"hello xmake\"") add_defines("TEST3=3") add_cxflags("-DTEST4=\"hello\"") - add_cxflags("-DTEST5=\"hello xmake\"") - add_cxflags("-DTEST6=3") + add_cxflags("-DTEST5=\"hello xmake\" -DTEST6=3") diff --git a/tests/modules/os/test.lua b/tests/modules/os/test.lua index e5e27456b..2b26cd939 100644 --- a/tests/modules/os/test.lua +++ b/tests/modules/os/test.lua @@ -91,6 +91,9 @@ function test_argv(t) t:are_equal(os.argv('"-DTEST=\\\"hello world\\\"" "-DTEST2=\\\"hello world2\\\""'), {'-DTEST="hello world"', '-DTEST2="hello world2"'}) -- $cli '-DTEST="hello world"' '-DTEST2="hello world2"' t:are_equal(os.argv("'-DTEST=\"hello world\"' '-DTEST2=\"hello world2\"'"), {'-DTEST="hello world"', '-DTEST2="hello world2"'}) + -- only split + t:are_equal(os.argv('-DTEST="hello world"', {splitonly = true}), {'-DTEST="hello world"'}) + t:are_equal(os.argv('-DTEST="hello world" -DTEST2="hello world2"', {splitonly = true}), {'-DTEST="hello world"', '-DTEST2="hello world2"'}) end function test_args(t) @@ -106,5 +109,5 @@ function test_args(t) t:are_equal(os.args('-DTEST="hello"'), '-DTEST=\\"hello\\"') t:are_equal(os.args({'-DTEST="hello"', '-DTEST2="hello"'}), '-DTEST=\\"hello\\" -DTEST2=\\"hello\\"') t:are_equal(os.args('-DTEST=hello'), '-DTEST=hello') -- irreversible --- t:are_equal(os.args({'-DTEST="hello world"', '-DTEST2="hello world2"'}), '"-DTEST=\\\"hello world\\\"" "-DTEST2=\\\"hello world2\\\""') + t:are_equal(os.args({'-DTEST="hello world"', '-DTEST2="hello world2"'}), '"-DTEST=\\\"hello world\\\"" "-DTEST2=\\\"hello world2\\\""') end diff --git a/xmake/core/tool/builder.lua b/xmake/core/tool/builder.lua index 984b23992..00067b435 100644 --- a/xmake/core/tool/builder.lua +++ b/xmake/core/tool/builder.lua @@ -373,7 +373,7 @@ function builder:_preprocess_flags(flags) flag = flag:trim() if #flag > 0 then if flag:find(" ", 1, true) then - table.join2(results, os.argv(flag)) + table.join2(results, os.argv(flag, {splitonly = true})) else table.insert(results, flag) end -- cgit v1.3.1