From 554f7a23f12ba4df73c23de64d52855e82a37088 Mon Sep 17 00:00:00 2001 From: Saikari Date: Sun, 1 Feb 2026 14:06:52 +0300 Subject: refactor: improve stdin handling and shell command execution across platforms --- tests/modules/stdin/test.lua | 75 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 tests/modules/stdin/test.lua (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua new file mode 100644 index 000000000..72551ed9c --- /dev/null +++ b/tests/modules/stdin/test.lua @@ -0,0 +1,75 @@ +target("test") + set_kind("phony") + on_run(function (target) + local xmake = path.unix(os.programfile()) + if os.host() == "windows" then + xmake = xmake:gsub("/", "\\") + end + + local function test_shell(name, cmd, expect) + print("testing " .. name .. ": " .. cmd) + local outfile = os.tmpfile() + local errfile = os.tmpfile() + local full_cmd = string.format("%s > \"%s\" 2> \"%s\"", cmd, outfile, errfile) + local ret = -1 + try { + function () + if os.host() ~= "windows" then + ret = os.execv("sh", {"-c", full_cmd}) + else + ret = os.exec(full_cmd) + end + end + } + local out = "" + if os.isfile(outfile) then + out = io.readfile(outfile) + if out and out:find("\0", 1, true) then + out = out:gsub("\0", "") + end + end + local err = "" + if os.isfile(errfile) then + err = io.readfile(errfile) + end + if out:find(expect) then + print(" -> passed") + else + print(" -> failed") + raise("[test_stdin]: Test failed! Expect: ", expect) + end + print(" out: " .. (out or "")) + print(" err: " .. (err or "")) + os.tryrm(outfile) + os.tryrm(errfile) + end + + local pwsh = "" + if os.host() == "windows" then + -- Test cmd + test_shell("cmd_single", string.format('cmd /c echo "print(\'hello_cmd\')" | %s l --stdin', xmake), "hello_cmd") + test_shell("cmd_multi", string.format('cmd /c echo "print(\'line1\')\\nprint(\'line2\')" | %s l --stdin', xmake), "line1[\r\n]+line2") + -- Test powershell (if available) + local pwsh = "powershell" + if os.exec("pwsh -v") == 0 then + pwsh = "pwsh" + end + test_shell("pwsh_single", string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), "hello_pwsh") + test_shell("pwsh_multi", string.format('%s -c "echo \\"print(\'pline1\')\\nprint(\'pline2\')\\" | %s l --stdin"', pwsh, xmake), "pline1[\r\n]+pline2") + else + -- Linux/MacOS + local pwsh = "" + try { function () os.iorun("pwsh -v"); pwsh = "pwsh" end } + if pwsh == "" then + try { function () os.iorun("powershell -v"); pwsh = "powershell" end } + end + + if pwsh ~= "" then + test_shell("pwsh_single", string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), "hello_pwsh") + test_shell("pwsh_multi", string.format('%s -c "echo \\"print(\'pline1\')\\nprint(\'pline2\')\\" | %s l --stdin"', pwsh, xmake), "pline1[\r\n]+pline2") + end + + test_shell("sh_single", string.format('echo "print(\'hello_sh\')" | %s l --stdin', xmake), "hello_sh") + test_shell("sh_multi", string.format('printf "print(\'shell_line1\')\\nprint(\'shell_line2\')" | %s l --stdin', xmake), "shell_line1[\r\n]+shell_line2") + end + end) -- cgit v1.3.1 From 5313286703675d51f54cde0a8d0ea382619ae84c Mon Sep 17 00:00:00 2001 From: Saikari Date: Sun, 1 Feb 2026 14:20:33 +0300 Subject: refactor: restructure test function and improve output handling --- tests/modules/stdin/test.lua | 137 ++++++++++++++++++++++--------------------- 1 file changed, 69 insertions(+), 68 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 72551ed9c..c58e0705d 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -1,75 +1,76 @@ -target("test") - set_kind("phony") - on_run(function (target) - local xmake = path.unix(os.programfile()) - if os.host() == "windows" then - xmake = xmake:gsub("/", "\\") - end +function main(t) +local xmake = path.unix(os.programfile()) +if os.host() == "windows" then + xmake = xmake:gsub("/", "\\") +end - local function test_shell(name, cmd, expect) - print("testing " .. name .. ": " .. cmd) - local outfile = os.tmpfile() - local errfile = os.tmpfile() - local full_cmd = string.format("%s > \"%s\" 2> \"%s\"", cmd, outfile, errfile) - local ret = -1 - try { - function () - if os.host() ~= "windows" then - ret = os.execv("sh", {"-c", full_cmd}) - else - ret = os.exec(full_cmd) - end - end - } - local out = "" - if os.isfile(outfile) then - out = io.readfile(outfile) - if out and out:find("\0", 1, true) then - out = out:gsub("\0", "") - end - end - local err = "" - if os.isfile(errfile) then - err = io.readfile(errfile) - end - if out:find(expect) then - print(" -> passed") +local function test_shell(name, cmd, expect) + print("testing " .. name .. ": " .. cmd) + local outfile = os.tmpfile() + local errfile = os.tmpfile() + local full_cmd = string.format("%s > \"%s\" 2> \"%s\"", cmd, outfile, errfile) + local ret = -1 + try { + function () + if os.host() ~= "windows" then + ret = os.execv("sh", {"-c", full_cmd}) else - print(" -> failed") - raise("[test_stdin]: Test failed! Expect: ", expect) + ret = os.exec(full_cmd) end - print(" out: " .. (out or "")) - print(" err: " .. (err or "")) - os.tryrm(outfile) - os.tryrm(errfile) end + } + local out = "" + if os.isfile(outfile) then + out = io.readfile(outfile) + if out and out:find("\0", 1, true) then + out = out:gsub("\0", "") + end + end + local err = "" + if os.isfile(errfile) then + err = io.readfile(errfile) + end + local passed = out:find(expect) + if passed then + print(" -> passed") + else + print(" -> failed") + end + print(" out: " .. (out or "")) + print(" err: " .. (err or "")) + if not passed then + raise("[test_stdin]: Test failed! Expect: ", expect) + end + os.tryrm(outfile) + os.tryrm(errfile) +end - local pwsh = "" - if os.host() == "windows" then - -- Test cmd - test_shell("cmd_single", string.format('cmd /c echo "print(\'hello_cmd\')" | %s l --stdin', xmake), "hello_cmd") - test_shell("cmd_multi", string.format('cmd /c echo "print(\'line1\')\\nprint(\'line2\')" | %s l --stdin', xmake), "line1[\r\n]+line2") - -- Test powershell (if available) - local pwsh = "powershell" - if os.exec("pwsh -v") == 0 then - pwsh = "pwsh" - end - test_shell("pwsh_single", string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), "hello_pwsh") - test_shell("pwsh_multi", string.format('%s -c "echo \\"print(\'pline1\')\\nprint(\'pline2\')\\" | %s l --stdin"', pwsh, xmake), "pline1[\r\n]+pline2") - else - -- Linux/MacOS - local pwsh = "" - try { function () os.iorun("pwsh -v"); pwsh = "pwsh" end } - if pwsh == "" then - try { function () os.iorun("powershell -v"); pwsh = "powershell" end } - end +local pwsh = "" +if os.host() == "windows" then + -- Test cmd + test_shell("cmd_single", string.format('cmd /c echo "print(\'hello_cmd\')" | %s l --stdin', xmake), "hello_cmd") + test_shell("cmd_multi", string.format('cmd /c echo "print(\'line1\')\\nprint(\'line2\')" | %s l --stdin', xmake), "line1[\r\n]+line2") + -- Test powershell (if available) + local pwsh = "powershell" + if os.exec("pwsh -v") == 0 then + pwsh = "pwsh" + end + test_shell("pwsh_single", string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), "hello_pwsh") + test_shell("pwsh_multi", string.format('%s -c "echo \\"print(\'pline1\')\\nprint(\'pline2\')\\" | %s l --stdin"', pwsh, xmake), "pline1[\r\n]+pline2") +else + -- Linux/MacOS + local pwsh = "" + try { function () os.iorun("pwsh -v"); pwsh = "pwsh" end } + if pwsh == "" then + try { function () os.iorun("powershell -v"); pwsh = "powershell" end } + end - if pwsh ~= "" then - test_shell("pwsh_single", string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), "hello_pwsh") - test_shell("pwsh_multi", string.format('%s -c "echo \\"print(\'pline1\')\\nprint(\'pline2\')\\" | %s l --stdin"', pwsh, xmake), "pline1[\r\n]+pline2") - end + if pwsh ~= "" then + test_shell("pwsh_single", string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), "hello_pwsh") + test_shell("pwsh_multi", string.format('%s -c "echo \\"print(\'pline1\')\\nprint(\'pline2\')\\" | %s l --stdin"', pwsh, xmake), "pline1[\r\n]+pline2") + end - test_shell("sh_single", string.format('echo "print(\'hello_sh\')" | %s l --stdin', xmake), "hello_sh") - test_shell("sh_multi", string.format('printf "print(\'shell_line1\')\\nprint(\'shell_line2\')" | %s l --stdin', xmake), "shell_line1[\r\n]+shell_line2") - end - end) + test_shell("sh_single", string.format('echo "print(\'hello_sh\')" | %s l --stdin', xmake), "hello_sh") + test_shell("sh_multi", string.format('printf "print(\'shell_line1\')\\nprint(\'shell_line2\')" | %s l --stdin', xmake), "shell_line1[\r\n]+shell_line2") +end +end -- cgit v1.3.1 From 2b7c02a0ef930e394829027fab3d7acaa948d6e9 Mon Sep 17 00:00:00 2001 From: Saikari Date: Sun, 1 Feb 2026 14:31:00 +0300 Subject: refactor: add additional test cases for shell and powershell calculations --- tests/modules/stdin/test.lua | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index c58e0705d..1cef7a362 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -49,6 +49,7 @@ local pwsh = "" if os.host() == "windows" then -- Test cmd test_shell("cmd_single", string.format('cmd /c echo "print(\'hello_cmd\')" | %s l --stdin', xmake), "hello_cmd") + test_shell("cmd_calc", string.format('cmd /c echo "local f = 1+1; print(f)" | %s l --stdin', xmake), "2") test_shell("cmd_multi", string.format('cmd /c echo "print(\'line1\')\\nprint(\'line2\')" | %s l --stdin', xmake), "line1[\r\n]+line2") -- Test powershell (if available) local pwsh = "powershell" @@ -56,6 +57,7 @@ if os.host() == "windows" then pwsh = "pwsh" end test_shell("pwsh_single", string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), "hello_pwsh") + test_shell("pwsh_calc", string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s l --stdin"', pwsh, xmake), "2") test_shell("pwsh_multi", string.format('%s -c "echo \\"print(\'pline1\')\\nprint(\'pline2\')\\" | %s l --stdin"', pwsh, xmake), "pline1[\r\n]+pline2") else -- Linux/MacOS @@ -67,10 +69,12 @@ else if pwsh ~= "" then test_shell("pwsh_single", string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), "hello_pwsh") + test_shell("pwsh_calc", string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s l --stdin"', pwsh, xmake), "2") test_shell("pwsh_multi", string.format('%s -c "echo \\"print(\'pline1\')\\nprint(\'pline2\')\\" | %s l --stdin"', pwsh, xmake), "pline1[\r\n]+pline2") end test_shell("sh_single", string.format('echo "print(\'hello_sh\')" | %s l --stdin', xmake), "hello_sh") + test_shell("sh_calc", string.format('echo "local f = 1+1; print(f)" | %s l --stdin', xmake), "2") test_shell("sh_multi", string.format('printf "print(\'shell_line1\')\\nprint(\'shell_line2\')" | %s l --stdin', xmake), "shell_line1[\r\n]+shell_line2") end end -- cgit v1.3.1 From 50b428269a657fd0adce1de35b01abe42c13708b Mon Sep 17 00:00:00 2001 From: Saikari Date: Sun, 1 Feb 2026 14:40:44 +0300 Subject: refactor: enhance stdin handling by adding utf8 BOM removal and main function check --- tests/modules/stdin/test.lua | 4 +++- xmake/plugins/lua/main.lua | 10 +++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 1cef7a362..bcd9efca6 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -70,11 +70,13 @@ else if pwsh ~= "" then test_shell("pwsh_single", string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), "hello_pwsh") test_shell("pwsh_calc", string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s l --stdin"', pwsh, xmake), "2") - test_shell("pwsh_multi", string.format('%s -c "echo \\"print(\'pline1\')\\nprint(\'pline2\')\\" | %s l --stdin"', pwsh, xmake), "pline1[\r\n]+pline2") + test_shell("pwsh_main", string.format('%s -c "echo \\"function main() print(\'in_pwsh_main\') end\\" | %s l --stdin"', pwsh, xmake), "in_pwsh_main") + test_shell("pwsh_multi", string.format('%s -c "echo \\"print(\'pline1\')\\" \\"print(\'pline2\')\\" | %s l --stdin"', pwsh, xmake), "pline1[\r\n]+pline2") end test_shell("sh_single", string.format('echo "print(\'hello_sh\')" | %s l --stdin', xmake), "hello_sh") test_shell("sh_calc", string.format('echo "local f = 1+1; print(f)" | %s l --stdin', xmake), "2") + test_shell("sh_main", string.format('echo "function main() print(\'in_sh_main\') end" | %s l --stdin', xmake), "in_sh_main") test_shell("sh_multi", string.format('printf "print(\'shell_line1\')\\nprint(\'shell_line2\')" | %s l --stdin', xmake), "shell_line1[\r\n]+shell_line2") end end diff --git a/xmake/plugins/lua/main.lua b/xmake/plugins/lua/main.lua index 0c0b44005..1cc13dbc3 100644 --- a/xmake/plugins/lua/main.lua +++ b/xmake/plugins/lua/main.lua @@ -63,6 +63,10 @@ function main() if script == "-" or from_stdin then local script_content = io.read("*a") if script_content then + -- remove utf8 bom + if script_content:startswith("\239\187\191") then + script_content = script_content:sub(4) + end import("core.base.tty") local shell = tty.shell() if shell == "cmd" or shell == "powershell" or shell == "pwsh" or os.host() == "windows" then @@ -72,7 +76,11 @@ function main() end script_content = script_content:replace("\\n", "\n", {plain = true}):replace("\\r", "\r", {plain = true}) end - script_content = "function main(...)\n" .. script_content .. "\nend" + + if not script_content:find("function main", 1, true) then + script_content = "function main(...)\n" .. script_content .. "\nend" + end + script = os.tmpfile() .. ".lua" io.writefile(script, script_content) script_file_to_remove = script -- cgit v1.3.1 From 0588ca91219e1ac614e234b7307941ec9b6df161 Mon Sep 17 00:00:00 2001 From: Saikari Date: Sun, 1 Feb 2026 15:03:58 +0300 Subject: refactor: wrap PowerShell version check in a try block for improved error handling --- tests/modules/stdin/test.lua | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index bcd9efca6..89e6e5a38 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -53,9 +53,11 @@ if os.host() == "windows" then test_shell("cmd_multi", string.format('cmd /c echo "print(\'line1\')\\nprint(\'line2\')" | %s l --stdin', xmake), "line1[\r\n]+line2") -- Test powershell (if available) local pwsh = "powershell" - if os.exec("pwsh -v") == 0 then - pwsh = "pwsh" - end + try { function () + if os.exec("pwsh -v") == 0 then + pwsh = "pwsh" + end + end } test_shell("pwsh_single", string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), "hello_pwsh") test_shell("pwsh_calc", string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s l --stdin"', pwsh, xmake), "2") test_shell("pwsh_multi", string.format('%s -c "echo \\"print(\'pline1\')\\nprint(\'pline2\')\\" | %s l --stdin"', pwsh, xmake), "pline1[\r\n]+pline2") -- cgit v1.3.1 From 9d45443b6cf3a6bb2a0e0f6ab30347cb826c700d Mon Sep 17 00:00:00 2001 From: Saikari Date: Sun, 1 Feb 2026 16:03:52 +0300 Subject: stylua fmt? --- tests/modules/stdin/test.lua | 216 +++++++++++++++++++++++++++---------------- 1 file changed, 138 insertions(+), 78 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 89e6e5a38..bd4bb5964 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -1,84 +1,144 @@ function main(t) -local xmake = path.unix(os.programfile()) -if os.host() == "windows" then - xmake = xmake:gsub("/", "\\") -end + local xmake = path.unix(os.programfile()) + if os.host() == "windows" then + xmake = xmake:gsub("/", "\\") + end -local function test_shell(name, cmd, expect) - print("testing " .. name .. ": " .. cmd) - local outfile = os.tmpfile() - local errfile = os.tmpfile() - local full_cmd = string.format("%s > \"%s\" 2> \"%s\"", cmd, outfile, errfile) - local ret = -1 - try { - function () - if os.host() ~= "windows" then - ret = os.execv("sh", {"-c", full_cmd}) - else - ret = os.exec(full_cmd) - end - end - } - local out = "" - if os.isfile(outfile) then - out = io.readfile(outfile) - if out and out:find("\0", 1, true) then - out = out:gsub("\0", "") - end - end - local err = "" - if os.isfile(errfile) then - err = io.readfile(errfile) - end - local passed = out:find(expect) - if passed then - print(" -> passed") - else - print(" -> failed") - end - print(" out: " .. (out or "")) - print(" err: " .. (err or "")) - if not passed then - raise("[test_stdin]: Test failed! Expect: ", expect) - end - os.tryrm(outfile) - os.tryrm(errfile) -end + local function test_shell(name, cmd, expect) + print("testing " .. name .. ": " .. cmd) + local outfile = os.tmpfile() + local errfile = os.tmpfile() + local full_cmd = string.format('%s > "%s" 2> "%s"', cmd, outfile, errfile) + local ret = -1 + try({ + function() + if os.host() ~= "windows" then + ret = os.execv("sh", { "-c", full_cmd }) + else + ret = os.exec(full_cmd) + end + end, + }) + local out = "" + if os.isfile(outfile) then + out = io.readfile(outfile) + if out and out:find("\0", 1, true) then + out = out:gsub("\0", "") + end + end + local err = "" + if os.isfile(errfile) then + err = io.readfile(errfile) + end + local passed = out:find(expect) + if passed then + print(" -> passed") + else + print(" -> failed") + end + print(" out: " .. (out or "")) + print(" err: " .. (err or "")) + if not passed then + raise("[test_stdin]: Test failed! Expect: ", expect) + end + os.tryrm(outfile) + os.tryrm(errfile) + end -local pwsh = "" -if os.host() == "windows" then - -- Test cmd - test_shell("cmd_single", string.format('cmd /c echo "print(\'hello_cmd\')" | %s l --stdin', xmake), "hello_cmd") - test_shell("cmd_calc", string.format('cmd /c echo "local f = 1+1; print(f)" | %s l --stdin', xmake), "2") - test_shell("cmd_multi", string.format('cmd /c echo "print(\'line1\')\\nprint(\'line2\')" | %s l --stdin', xmake), "line1[\r\n]+line2") - -- Test powershell (if available) - local pwsh = "powershell" - try { function () - if os.exec("pwsh -v") == 0 then - pwsh = "pwsh" - end - end } - test_shell("pwsh_single", string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), "hello_pwsh") - test_shell("pwsh_calc", string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s l --stdin"', pwsh, xmake), "2") - test_shell("pwsh_multi", string.format('%s -c "echo \\"print(\'pline1\')\\nprint(\'pline2\')\\" | %s l --stdin"', pwsh, xmake), "pline1[\r\n]+pline2") -else - -- Linux/MacOS - local pwsh = "" - try { function () os.iorun("pwsh -v"); pwsh = "pwsh" end } - if pwsh == "" then - try { function () os.iorun("powershell -v"); pwsh = "powershell" end } - end + local pwsh = "" + if os.host() == "windows" then + -- Test cmd + test_shell("cmd_single", string.format("cmd /c echo \"print('hello_cmd')\" | %s l --stdin", xmake), "hello_cmd") + test_shell("cmd_calc", string.format('cmd /c echo "local f = 1+1; print(f)" | %s l --stdin', xmake), "2") + test_shell( + "cmd_multi", + string.format("cmd /c echo \"print('line1')\\nprint('line2')\" | %s l --stdin", xmake), + "line1[\r\n]+line2" + ) + -- Test powershell (if available) + local pwsh = "powershell" + try({ + function() + if os.exec("pwsh -v") == 0 then + pwsh = "pwsh" + end + end, + }) + test_shell( + "pwsh_single", + string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), + "hello_pwsh" + ) + test_shell( + "pwsh_calc", + string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s l --stdin"', pwsh, xmake), + "2" + ) + test_shell( + "pwsh_multi", + string.format("%s -c \"echo \\\"print('pline1')\\nprint('pline2')\\\" | %s l --stdin\"", pwsh, xmake), + "pline1[\r\n]+pline2" + ) + else + -- Linux/MacOS + local pwsh = "" + try({ + function() + os.iorun("pwsh -v") + pwsh = "pwsh" + end, + }) + if pwsh == "" then + try({ + function() + os.iorun("powershell -v") + pwsh = "powershell" + end, + }) + end - if pwsh ~= "" then - test_shell("pwsh_single", string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), "hello_pwsh") - test_shell("pwsh_calc", string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s l --stdin"', pwsh, xmake), "2") - test_shell("pwsh_main", string.format('%s -c "echo \\"function main() print(\'in_pwsh_main\') end\\" | %s l --stdin"', pwsh, xmake), "in_pwsh_main") - test_shell("pwsh_multi", string.format('%s -c "echo \\"print(\'pline1\')\\" \\"print(\'pline2\')\\" | %s l --stdin"', pwsh, xmake), "pline1[\r\n]+pline2") - end + if pwsh ~= "" then + test_shell( + "pwsh_single", + string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), + "hello_pwsh" + ) + test_shell( + "pwsh_calc", + string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s l --stdin"', pwsh, xmake), + "2" + ) + test_shell( + "pwsh_main", + string.format( + '%s -c "echo \\"function main() print(\'in_pwsh_main\') end\\" | %s l --stdin"', + pwsh, + xmake + ), + "in_pwsh_main" + ) + test_shell( + "pwsh_multi", + string.format( + '%s -c "echo \\"print(\'pline1\')\\" \\"print(\'pline2\')\\" | %s l --stdin"', + pwsh, + xmake + ), + "pline1[\r\n]+pline2" + ) + end - test_shell("sh_single", string.format('echo "print(\'hello_sh\')" | %s l --stdin', xmake), "hello_sh") - test_shell("sh_calc", string.format('echo "local f = 1+1; print(f)" | %s l --stdin', xmake), "2") - test_shell("sh_main", string.format('echo "function main() print(\'in_sh_main\') end" | %s l --stdin', xmake), "in_sh_main") - test_shell("sh_multi", string.format('printf "print(\'shell_line1\')\\nprint(\'shell_line2\')" | %s l --stdin', xmake), "shell_line1[\r\n]+shell_line2") -end + test_shell("sh_single", string.format("echo \"print('hello_sh')\" | %s l --stdin", xmake), "hello_sh") + test_shell("sh_calc", string.format('echo "local f = 1+1; print(f)" | %s l --stdin', xmake), "2") + test_shell( + "sh_main", + string.format("echo \"function main() print('in_sh_main') end\" | %s l --stdin", xmake), + "in_sh_main" + ) + test_shell( + "sh_multi", + string.format("printf \"print('shell_line1')\\nprint('shell_line2')\" | %s l --stdin", xmake), + "shell_line1[\r\n]+shell_line2" + ) + end end -- cgit v1.3.1 From 0e07b14891d27765bfcabf071f102f6758db6666 Mon Sep 17 00:00:00 2001 From: Saikari Date: Sun, 1 Feb 2026 16:36:56 +0300 Subject: refactor: update PowerShell test commands to use sh for stdin execution --- tests/modules/stdin/test.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index bd4bb5964..96d166591 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -100,18 +100,18 @@ function main(t) if pwsh ~= "" then test_shell( "pwsh_single", - string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), + string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | sh -c \\"%s l --stdin\\""', pwsh, xmake), "hello_pwsh" ) test_shell( "pwsh_calc", - string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s l --stdin"', pwsh, xmake), + string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | sh -c \\"%s l --stdin\\""', pwsh, xmake), "2" ) test_shell( "pwsh_main", string.format( - '%s -c "echo \\"function main() print(\'in_pwsh_main\') end\\" | %s l --stdin"', + '%s -c "echo \\"function main() print(\'in_pwsh_main\') end\\" | sh -c \\"%s l --stdin\\""', pwsh, xmake ), @@ -120,7 +120,7 @@ function main(t) test_shell( "pwsh_multi", string.format( - '%s -c "echo \\"print(\'pline1\')\\" \\"print(\'pline2\')\\" | %s l --stdin"', + '%s -c "echo \\"print(\'pline1\')\\" \\"print(\'pline2\')\\" | sh -c \\"%s l --stdin\\""', pwsh, xmake ), -- cgit v1.3.1 From e2b38bd5e164e6584aceb6318556b0d2a1f08875 Mon Sep 17 00:00:00 2001 From: Saikari Date: Sun, 1 Feb 2026 17:07:53 +0300 Subject: refactor: update PowerShell test commands to use a unified run_stdin format. Maybe would work with APE file format? --- tests/modules/stdin/test.lua | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 96d166591..bf51a5cd2 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -98,31 +98,32 @@ function main(t) end if pwsh ~= "" then + local run_stdin = string.format('env "%s" l --stdin', xmake) test_shell( "pwsh_single", - string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | sh -c \\"%s l --stdin\\""', pwsh, xmake), + string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s"', pwsh, run_stdin), "hello_pwsh" ) test_shell( "pwsh_calc", - string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | sh -c \\"%s l --stdin\\""', pwsh, xmake), + string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s"', pwsh, run_stdin), "2" ) test_shell( "pwsh_main", string.format( - '%s -c "echo \\"function main() print(\'in_pwsh_main\') end\\" | sh -c \\"%s l --stdin\\""', + '%s -c "echo \\"function main() print(\'in_pwsh_main\') end\\" | %s"', pwsh, - xmake + run_stdin ), "in_pwsh_main" ) test_shell( "pwsh_multi", string.format( - '%s -c "echo \\"print(\'pline1\')\\" \\"print(\'pline2\')\\" | sh -c \\"%s l --stdin\\""', + '%s -c "echo \\"print(\'pline1\')\\" \\"print(\'pline2\')\\" | %s"', pwsh, - xmake + run_stdin ), "pline1[\r\n]+pline2" ) -- cgit v1.3.1 From 74a37990201fa351fc433ff3cc2d698eeb9846c4 Mon Sep 17 00:00:00 2001 From: Saikari Date: Sun, 1 Feb 2026 17:36:59 +0300 Subject: refactor: update PowerShell stdin command format for macOS compatibility --- tests/modules/stdin/test.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index bf51a5cd2..d942b7858 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -98,7 +98,10 @@ function main(t) end if pwsh ~= "" then - local run_stdin = string.format('env "%s" l --stdin', xmake) + local run_stdin = string.format("%s l --stdin", xmake) + if os.host() == "macosx" then + run_stdin = string.format('sh -c \\"%s\\"', run_stdin) + end test_shell( "pwsh_single", string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s"', pwsh, run_stdin), -- cgit v1.3.1 From 1056c09cb31f7e873573d4e0a5a24d06af8297df Mon Sep 17 00:00:00 2001 From: Saikari Date: Sun, 1 Feb 2026 18:15:24 +0300 Subject: refactor: enhance APE/Cosmocc workaround by creating a temporary xmake alias for compatibility --- tests/modules/stdin/test.lua | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index d942b7858..162b58d53 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -4,6 +4,20 @@ function main(t) xmake = xmake:gsub("/", "\\") end + -- APE/Cosmocc workaround: force xmake name to avoid APE loader mode issues (e.g. ape-x86_64.elf) + local function setup_xmake_alias(xmake) + if path.filename(xmake):find("ape-", 1, true) then + local xmake_dir = path.join(os.tmpdir(), "xmake_ape_" .. os.time()) + os.mkdir(xmake_dir) + local xmake_alias = path.join(xmake_dir, os.host() == "windows" and "xmake.exe" or "xmake") + os.trycp(xmake, xmake_alias) + return xmake_alias + end + return xmake + end + + xmake = setup_xmake_alias(xmake) + local function test_shell(name, cmd, expect) print("testing " .. name .. ": " .. cmd) local outfile = os.tmpfile() @@ -114,20 +128,12 @@ function main(t) ) test_shell( "pwsh_main", - string.format( - '%s -c "echo \\"function main() print(\'in_pwsh_main\') end\\" | %s"', - pwsh, - run_stdin - ), + string.format('%s -c "echo \\"function main() print(\'in_pwsh_main\') end\\" | %s"', pwsh, run_stdin), "in_pwsh_main" ) test_shell( "pwsh_multi", - string.format( - '%s -c "echo \\"print(\'pline1\')\\" \\"print(\'pline2\')\\" | %s"', - pwsh, - run_stdin - ), + string.format('%s -c "echo \\"print(\'pline1\')\\" \\"print(\'pline2\')\\" | %s"', pwsh, run_stdin), "pline1[\r\n]+pline2" ) end -- cgit v1.3.1 From 00343d9994967c9f83a742bb049bd1fea450c887 Mon Sep 17 00:00:00 2001 From: Saikari Date: Sun, 1 Feb 2026 18:51:05 +0300 Subject: refactor: ensure executable permissions for xmake alias on non-Windows platforms --- tests/modules/stdin/test.lua | 3 +++ 1 file changed, 3 insertions(+) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 162b58d53..e0ebe7760 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -11,6 +11,9 @@ function main(t) os.mkdir(xmake_dir) local xmake_alias = path.join(xmake_dir, os.host() == "windows" and "xmake.exe" or "xmake") os.trycp(xmake, xmake_alias) + if os.host() ~= "windows" then + os.exec("chmod +x " .. xmake_alias) + end return xmake_alias end return xmake -- cgit v1.3.1 From 2fbded813d6b413f3174c85cf5ca4e6ec76dda28 Mon Sep 17 00:00:00 2001 From: Saikari Date: Sun, 1 Feb 2026 18:54:23 +0300 Subject: refactor: standardize indentation and formatting in stdin test script --- tests/modules/stdin/test.lua | 295 +++++++++++++++++++++---------------------- 1 file changed, 146 insertions(+), 149 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index e0ebe7760..f2609c531 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -1,157 +1,154 @@ function main(t) - local xmake = path.unix(os.programfile()) - if os.host() == "windows" then - xmake = xmake:gsub("/", "\\") - end + local xmake = path.unix(os.programfile()) + if os.host() == "windows" then + xmake = xmake:gsub("/", "\\") + end - -- APE/Cosmocc workaround: force xmake name to avoid APE loader mode issues (e.g. ape-x86_64.elf) - local function setup_xmake_alias(xmake) - if path.filename(xmake):find("ape-", 1, true) then - local xmake_dir = path.join(os.tmpdir(), "xmake_ape_" .. os.time()) - os.mkdir(xmake_dir) - local xmake_alias = path.join(xmake_dir, os.host() == "windows" and "xmake.exe" or "xmake") - os.trycp(xmake, xmake_alias) - if os.host() ~= "windows" then - os.exec("chmod +x " .. xmake_alias) - end - return xmake_alias - end - return xmake - end + -- APE/Cosmocc workaround: force xmake name to avoid APE loader mode issues (e.g. ape-x86_64.elf) + local function setup_xmake_alias(xmake) + if path.filename(xmake):find("ape-", 1, true) then + local xmake_dir = path.join(os.tmpdir(), "xmake_ape_" .. os.time()) + os.mkdir(xmake_dir) + local xmake_alias = path.join(xmake_dir, os.host() == "windows" and "xmake.exe" or "xmake") + os.trycp(xmake, xmake_alias) + if os.host() ~= "windows" then + os.exec("chmod +x " .. xmake_alias) + end + return xmake_alias + end + return xmake + end - xmake = setup_xmake_alias(xmake) + xmake = setup_xmake_alias(xmake) - local function test_shell(name, cmd, expect) - print("testing " .. name .. ": " .. cmd) - local outfile = os.tmpfile() - local errfile = os.tmpfile() - local full_cmd = string.format('%s > "%s" 2> "%s"', cmd, outfile, errfile) - local ret = -1 - try({ - function() - if os.host() ~= "windows" then - ret = os.execv("sh", { "-c", full_cmd }) - else - ret = os.exec(full_cmd) - end - end, - }) - local out = "" - if os.isfile(outfile) then - out = io.readfile(outfile) - if out and out:find("\0", 1, true) then - out = out:gsub("\0", "") - end - end - local err = "" - if os.isfile(errfile) then - err = io.readfile(errfile) - end - local passed = out:find(expect) - if passed then - print(" -> passed") - else - print(" -> failed") - end - print(" out: " .. (out or "")) - print(" err: " .. (err or "")) - if not passed then - raise("[test_stdin]: Test failed! Expect: ", expect) - end - os.tryrm(outfile) - os.tryrm(errfile) - end + local function test_shell(name, cmd, expect) + print("testing " .. name .. ": " .. cmd) + local outfile = os.tmpfile() + local errfile = os.tmpfile() + local full_cmd = string.format('%s > "%s" 2> "%s"', cmd, outfile, errfile) + local ret = -1 + try({ + function() + if os.host() ~= "windows" then + ret = os.execv("sh", { "-c", full_cmd }) + else + ret = os.exec(full_cmd) + end + end, + }) + local out = "" + if os.isfile(outfile) then + out = io.readfile(outfile) + if out and out:find("\0", 1, true) then + out = out:gsub("\0", "") + end + end + local err = "" + if os.isfile(errfile) then + err = io.readfile(errfile) + end + local passed = out:find(expect) + if passed then + print(" -> passed") + else + print(" -> failed") + end + print(" out: " .. (out or "")) + print(" err: " .. (err or "")) + if not passed then + raise("[test_stdin]: Test failed! Expect: ", expect) + end + os.tryrm(outfile) + os.tryrm(errfile) + end - local pwsh = "" - if os.host() == "windows" then - -- Test cmd - test_shell("cmd_single", string.format("cmd /c echo \"print('hello_cmd')\" | %s l --stdin", xmake), "hello_cmd") - test_shell("cmd_calc", string.format('cmd /c echo "local f = 1+1; print(f)" | %s l --stdin', xmake), "2") - test_shell( - "cmd_multi", - string.format("cmd /c echo \"print('line1')\\nprint('line2')\" | %s l --stdin", xmake), - "line1[\r\n]+line2" - ) - -- Test powershell (if available) - local pwsh = "powershell" - try({ - function() - if os.exec("pwsh -v") == 0 then - pwsh = "pwsh" - end - end, - }) - test_shell( - "pwsh_single", - string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), - "hello_pwsh" - ) - test_shell( - "pwsh_calc", - string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s l --stdin"', pwsh, xmake), - "2" - ) - test_shell( - "pwsh_multi", - string.format("%s -c \"echo \\\"print('pline1')\\nprint('pline2')\\\" | %s l --stdin\"", pwsh, xmake), - "pline1[\r\n]+pline2" - ) - else - -- Linux/MacOS - local pwsh = "" - try({ - function() - os.iorun("pwsh -v") - pwsh = "pwsh" - end, - }) - if pwsh == "" then - try({ - function() - os.iorun("powershell -v") - pwsh = "powershell" - end, - }) - end + local pwsh = "" + if os.host() == "windows" then + -- Test cmd + test_shell("cmd_single", string.format("cmd /c echo \"print('hello_cmd')\" | %s l --stdin", xmake), "hello_cmd") + test_shell("cmd_calc", string.format('cmd /c echo "local f = 1+1; print(f)" | %s l --stdin', xmake), "2") + test_shell( + "cmd_multi", + string.format("cmd /c echo \"print('line1')\\nprint('line2')\" | %s l --stdin", xmake), + "line1[\r\n]+line2" + ) + -- Test powershell (if available) + local pwsh = "powershell" + try({ + function() + if os.exec("pwsh -v") == 0 then + pwsh = "pwsh" + end + end, + }) + test_shell( + "pwsh_single", + string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), + "hello_pwsh" + ) + test_shell( + "pwsh_calc", + string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s l --stdin"', pwsh, xmake), + "2" + ) + test_shell( + "pwsh_multi", + string.format("%s -c \"echo \\\"print('pline1')\\nprint('pline2')\\\" | %s l --stdin\"", pwsh, xmake), + "pline1[\r\n]+pline2" + ) + else + -- Linux/MacOS + local pwsh = "" + try({ + function() + os.iorun("pwsh -v") + pwsh = "pwsh" + end, + }) + if pwsh == "" then + try({ + function() + os.iorun("powershell -v") + pwsh = "powershell" + end, + }) + end - if pwsh ~= "" then - local run_stdin = string.format("%s l --stdin", xmake) - if os.host() == "macosx" then - run_stdin = string.format('sh -c \\"%s\\"', run_stdin) - end - test_shell( - "pwsh_single", - string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s"', pwsh, run_stdin), - "hello_pwsh" - ) - test_shell( - "pwsh_calc", - string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s"', pwsh, run_stdin), - "2" - ) - test_shell( - "pwsh_main", - string.format('%s -c "echo \\"function main() print(\'in_pwsh_main\') end\\" | %s"', pwsh, run_stdin), - "in_pwsh_main" - ) - test_shell( - "pwsh_multi", - string.format('%s -c "echo \\"print(\'pline1\')\\" \\"print(\'pline2\')\\" | %s"', pwsh, run_stdin), - "pline1[\r\n]+pline2" - ) - end + if pwsh ~= "" then + local run_stdin = string.format("%s l --stdin", xmake) + test_shell( + "pwsh_single", + string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s"', pwsh, run_stdin), + "hello_pwsh" + ) + test_shell( + "pwsh_calc", + string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s"', pwsh, run_stdin), + "2" + ) + test_shell( + "pwsh_main", + string.format('%s -c "echo \\"function main() print(\'in_pwsh_main\') end\\" | %s"', pwsh, run_stdin), + "in_pwsh_main" + ) + test_shell( + "pwsh_multi", + string.format('%s -c "echo \\"print(\'pline1\')\\" \\"print(\'pline2\')\\" | %s"', pwsh, run_stdin), + "pline1[\r\n]+pline2" + ) + end - test_shell("sh_single", string.format("echo \"print('hello_sh')\" | %s l --stdin", xmake), "hello_sh") - test_shell("sh_calc", string.format('echo "local f = 1+1; print(f)" | %s l --stdin', xmake), "2") - test_shell( - "sh_main", - string.format("echo \"function main() print('in_sh_main') end\" | %s l --stdin", xmake), - "in_sh_main" - ) - test_shell( - "sh_multi", - string.format("printf \"print('shell_line1')\\nprint('shell_line2')\" | %s l --stdin", xmake), - "shell_line1[\r\n]+shell_line2" - ) - end + test_shell("sh_single", string.format("echo \"print('hello_sh')\" | %s l --stdin", xmake), "hello_sh") + test_shell("sh_calc", string.format('echo "local f = 1+1; print(f)" | %s l --stdin', xmake), "2") + test_shell( + "sh_main", + string.format("echo \"function main() print('in_sh_main') end\" | %s l --stdin", xmake), + "in_sh_main" + ) + test_shell( + "sh_multi", + string.format("printf \"print('shell_line1')\\nprint('shell_line2')\" | %s l --stdin", xmake), + "shell_line1[\r\n]+shell_line2" + ) + end end -- cgit v1.3.1 From c68524a15b90a0c400930c9ca9c4ba4e7911e569 Mon Sep 17 00:00:00 2001 From: Saikari Date: Sun, 1 Feb 2026 19:28:23 +0300 Subject: refactor: update PowerShell stdin command format for macOS compatibility --- tests/modules/stdin/test.lua | 3 +++ 1 file changed, 3 insertions(+) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index f2609c531..44d0da2b7 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -116,6 +116,9 @@ function main(t) if pwsh ~= "" then local run_stdin = string.format("%s l --stdin", xmake) + if os.host() == "macosx" then + run_stdin = string.format('sh -c \\"%s\\"', run_stdin) + end test_shell( "pwsh_single", string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s"', pwsh, run_stdin), -- cgit v1.3.1 From c6f69e8df34f2adc620a82036dda48a82fbd15bb Mon Sep 17 00:00:00 2001 From: Saikari Date: Sun, 1 Feb 2026 20:42:34 +0300 Subject: refactor: rename temporary xmake directory and update copy command for alias setup --- tests/modules/stdin/test.lua | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 44d0da2b7..44521d6ad 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -7,10 +7,10 @@ function main(t) -- APE/Cosmocc workaround: force xmake name to avoid APE loader mode issues (e.g. ape-x86_64.elf) local function setup_xmake_alias(xmake) if path.filename(xmake):find("ape-", 1, true) then - local xmake_dir = path.join(os.tmpdir(), "xmake_ape_" .. os.time()) + local xmake_dir = path.join(os.tmpdir(), "xm_alias_" .. os.time()) os.mkdir(xmake_dir) local xmake_alias = path.join(xmake_dir, os.host() == "windows" and "xmake.exe" or "xmake") - os.trycp(xmake, xmake_alias) + os.cp(xmake, xmake_alias) if os.host() ~= "windows" then os.exec("chmod +x " .. xmake_alias) end @@ -19,8 +19,17 @@ function main(t) return xmake end + local is_ape = path.filename(xmake):find("ape-", 1, true) + + -- Fix pwsh and cosmocc "err: ape error: l: not found (maybe chmod +x or ./ needed)" for Linux xmake = setup_xmake_alias(xmake) + local run_stdin = string.format('env "%s" l --stdin', xmake) + -- Fix pwsh and cosmocc "exec format error" for MacOS + if is_ape and os.host() == "macosx" then + run_stdin = string.format('sh -c \\"%s\\"', run_stdin) + end + local function test_shell(name, cmd, expect) print("testing " .. name .. ": " .. cmd) local outfile = os.tmpfile() @@ -115,10 +124,6 @@ function main(t) end if pwsh ~= "" then - local run_stdin = string.format("%s l --stdin", xmake) - if os.host() == "macosx" then - run_stdin = string.format('sh -c \\"%s\\"', run_stdin) - end test_shell( "pwsh_single", string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s"', pwsh, run_stdin), -- cgit v1.3.1 From d155d5d080335d5932c4b65d2c713814ea28c00d Mon Sep 17 00:00:00 2001 From: Saikari Date: Sun, 1 Feb 2026 21:38:14 +0300 Subject: refactor: update stdin command format for Linux compatibility --- tests/modules/stdin/test.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 44521d6ad..461e76d68 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -26,7 +26,7 @@ function main(t) local run_stdin = string.format('env "%s" l --stdin', xmake) -- Fix pwsh and cosmocc "exec format error" for MacOS - if is_ape and os.host() == "macosx" then + if is_ape and (os.host() == "macosx" or os.host() == "linux") then run_stdin = string.format('sh -c \\"%s\\"', run_stdin) end -- cgit v1.3.1 From 42fb29e925668f87329ac8ea42ec2af0f6e37634 Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 03:19:20 +0300 Subject: refactor: update run_stdin command for MacOS compatibility --- tests/modules/stdin/test.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 461e76d68..665857595 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -26,9 +26,9 @@ function main(t) local run_stdin = string.format('env "%s" l --stdin', xmake) -- Fix pwsh and cosmocc "exec format error" for MacOS - if is_ape and (os.host() == "macosx" or os.host() == "linux") then - run_stdin = string.format('sh -c \\"%s\\"', run_stdin) - end + if is_ape and os.host() ~= "windows" then + run_stdin = string.format("sh -c ' \\\"%s\\\" l --stdin '", xmake) + end local function test_shell(name, cmd, expect) print("testing " .. name .. ": " .. cmd) -- cgit v1.3.1 From f3b8a1296313ee90a3451257f0f2cfa2964795b0 Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 03:33:23 +0300 Subject: refactor: enhance Linux APE loader mode handling and update MacOS stdin command format --- tests/modules/stdin/test.lua | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 665857595..f54bb97c7 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -4,6 +4,27 @@ function main(t) xmake = xmake:gsub("/", "\\") end + -- Fix /usr/bin/ape loader mode on Linux + if os.host() == "linux" and path.filename(xmake) == "ape" and os.isfile("/proc/self/cmdline") then + local file = io.open("/proc/self/cmdline", "rb") + if file then + local content = file:read("*a") + file:close() + if content then + local args = {} + for arg in content:gmatch("[^\0]+") do + table.insert(args, arg) + end + if #args >= 2 and path.unix(args[1]) == xmake then + xmake = path.unix(args[2]) + if not path.is_absolute(xmake) then + xmake = path.absolute(xmake) + end + end + end + end + end + -- APE/Cosmocc workaround: force xmake name to avoid APE loader mode issues (e.g. ape-x86_64.elf) local function setup_xmake_alias(xmake) if path.filename(xmake):find("ape-", 1, true) then @@ -22,11 +43,11 @@ function main(t) local is_ape = path.filename(xmake):find("ape-", 1, true) -- Fix pwsh and cosmocc "err: ape error: l: not found (maybe chmod +x or ./ needed)" for Linux - xmake = setup_xmake_alias(xmake) + --xmake = setup_xmake_alias(xmake) local run_stdin = string.format('env "%s" l --stdin', xmake) -- Fix pwsh and cosmocc "exec format error" for MacOS - if is_ape and os.host() ~= "windows" then + if is_ape and (os.host() == "macosx" or os.host() == "linux") then run_stdin = string.format("sh -c ' \\\"%s\\\" l --stdin '", xmake) end -- cgit v1.3.1 From 39797ac3c7cce56399185ab71b37ee92cafc67d3 Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 03:37:52 +0300 Subject: refactor: streamline APE loader mode handling for Linux and improve MacOS compatibility --- tests/modules/stdin/test.lua | 53 ++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 32 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index f54bb97c7..3f3539c61 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -5,49 +5,38 @@ function main(t) end -- Fix /usr/bin/ape loader mode on Linux - if os.host() == "linux" and path.filename(xmake) == "ape" and os.isfile("/proc/self/cmdline") then - local file = io.open("/proc/self/cmdline", "rb") - if file then - local content = file:read("*a") - file:close() - if content then - local args = {} - for arg in content:gmatch("[^\0]+") do - table.insert(args, arg) - end - if #args >= 2 and path.unix(args[1]) == xmake then - xmake = path.unix(args[2]) - if not path.is_absolute(xmake) then - xmake = path.absolute(xmake) + local function fix_ape_programfile(xmake) + if os.host() == "linux" and path.filename(xmake) == "ape" and os.isfile("/proc/self/cmdline") then + local file = io.open("/proc/self/cmdline", "rb") + if file then + local content = file:read("*a") + file:close() + if content then + local args = {} + for arg in content:gmatch("[^\0]+") do + table.insert(args, arg) + end + if #args >= 2 and path.unix(args[1]) == xmake then + xmake = path.unix(args[2]) + if not path.is_absolute(xmake) then + xmake = path.absolute(xmake) + end end end end end - end - - -- APE/Cosmocc workaround: force xmake name to avoid APE loader mode issues (e.g. ape-x86_64.elf) - local function setup_xmake_alias(xmake) - if path.filename(xmake):find("ape-", 1, true) then - local xmake_dir = path.join(os.tmpdir(), "xm_alias_" .. os.time()) - os.mkdir(xmake_dir) - local xmake_alias = path.join(xmake_dir, os.host() == "windows" and "xmake.exe" or "xmake") - os.cp(xmake, xmake_alias) - if os.host() ~= "windows" then - os.exec("chmod +x " .. xmake_alias) - end - return xmake_alias - end return xmake end - local is_ape = path.filename(xmake):find("ape-", 1, true) - -- Fix pwsh and cosmocc "err: ape error: l: not found (maybe chmod +x or ./ needed)" for Linux - --xmake = setup_xmake_alias(xmake) + xmake = fix_ape_programfile(xmake) + + local is_ape = path.filename(xmake):find("ape-", 1, true) + print("Using xmake programfile is_ape = ", is_ape) local run_stdin = string.format('env "%s" l --stdin', xmake) -- Fix pwsh and cosmocc "exec format error" for MacOS - if is_ape and (os.host() == "macosx" or os.host() == "linux") then + if is_ape and os.host() == "macosx" then run_stdin = string.format("sh -c ' \\\"%s\\\" l --stdin '", xmake) end -- cgit v1.3.1 From 7629549db84dc57a7c5e71b93ec9baf628be5e7e Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 04:25:09 +0300 Subject: refactor: improve ape programfile detection for Linux and enhance MacOS stdin command handling --- tests/modules/stdin/test.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 3f3539c61..9911048d5 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -6,7 +6,7 @@ function main(t) -- Fix /usr/bin/ape loader mode on Linux local function fix_ape_programfile(xmake) - if os.host() == "linux" and path.filename(xmake) == "ape" and os.isfile("/proc/self/cmdline") then + if os.host() == "linux" and path.filename(xmake):find("ape", 1, true) and os.isfile("/proc/self/cmdline") then local file = io.open("/proc/self/cmdline", "rb") if file then local content = file:read("*a") @@ -36,7 +36,7 @@ function main(t) local run_stdin = string.format('env "%s" l --stdin', xmake) -- Fix pwsh and cosmocc "exec format error" for MacOS - if is_ape and os.host() == "macosx" then + if is_ape and os.host() ~= "windows" then run_stdin = string.format("sh -c ' \\\"%s\\\" l --stdin '", xmake) end -- cgit v1.3.1 From c6aa73fa21bba45a591aa185b4d4a3648417b35f Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 05:02:58 +0300 Subject: refactor: enhance path resolution for xmake and improve MacOS stdin command handling --- tests/modules/stdin/test.lua | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 9911048d5..8aac5a38e 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -1,8 +1,18 @@ function main(t) + local function resolve_path(p) + if path.is_absolute(p) then return p end + local root = path.join(os.scriptdir(), "../../..") + local p_root = path.join(root, p) + if os.isfile(p_root) then return path.absolute(p_root) end + if os.isfile(p) then return path.absolute(p) end + return p + end + local xmake = path.unix(os.programfile()) if os.host() == "windows" then xmake = xmake:gsub("/", "\\") end + xmake = resolve_path(xmake) -- Fix /usr/bin/ape loader mode on Linux local function fix_ape_programfile(xmake) @@ -18,9 +28,7 @@ function main(t) end if #args >= 2 and path.unix(args[1]) == xmake then xmake = path.unix(args[2]) - if not path.is_absolute(xmake) then - xmake = path.absolute(xmake) - end + xmake = resolve_path(xmake) end end end @@ -31,13 +39,12 @@ function main(t) -- Fix pwsh and cosmocc "err: ape error: l: not found (maybe chmod +x or ./ needed)" for Linux xmake = fix_ape_programfile(xmake) - local is_ape = path.filename(xmake):find("ape-", 1, true) - print("Using xmake programfile is_ape = ", is_ape) + local is_ape = path.filename(xmake):find("ape-", 1, true) ~= nil local run_stdin = string.format('env "%s" l --stdin', xmake) -- Fix pwsh and cosmocc "exec format error" for MacOS if is_ape and os.host() ~= "windows" then - run_stdin = string.format("sh -c ' \\\"%s\\\" l --stdin '", xmake) + run_stdin = string.format("sh -c ' \"%s\" l --stdin '", xmake) end local function test_shell(name, cmd, expect) -- cgit v1.3.1 From eb8a34c1838e72534d3662f1da6efe273d6cc370 Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 09:03:39 +0300 Subject: refactor: reorder resolve_path and main function definitions for improved readability --- tests/modules/stdin/test.lua | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 8aac5a38e..784e55489 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -1,13 +1,13 @@ -function main(t) - local function resolve_path(p) - if path.is_absolute(p) then return p end - local root = path.join(os.scriptdir(), "../../..") - local p_root = path.join(root, p) - if os.isfile(p_root) then return path.absolute(p_root) end - if os.isfile(p) then return path.absolute(p) end - return p - end +function resolve_path(p) + if path.is_absolute(p) then return p end + local root = path.join(os.scriptdir(), "../../..") + local p_root = path.join(root, p) + if os.isfile(p_root) then return path.absolute(p_root) end + if os.isfile(p) then return path.absolute(p) end + return p +end +function main(t) local xmake = path.unix(os.programfile()) if os.host() == "windows" then xmake = xmake:gsub("/", "\\") -- cgit v1.3.1 From 20960586d09f1b83b20798223d853aa834078dbc Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 09:07:30 +0300 Subject: refactor: extract fix_ape_programfile function for improved readability and maintainability --- tests/modules/stdin/test.lua | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 784e55489..26c7d4b47 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -7,6 +7,28 @@ function resolve_path(p) return p end +-- Fix /usr/bin/ape loader mode on Linux +function fix_ape_programfile(xmake) + if os.host() == "linux" and path.filename(xmake):find("ape", 1, true) and os.isfile("/proc/self/cmdline") then + local file = io.open("/proc/self/cmdline", "rb") + if file then + local content = file:read("*a") + file:close() + if content then + local args = {} + for arg in content:gmatch("[^\0]+") do + table.insert(args, arg) + end + if #args >= 2 and path.unix(args[1]) == xmake then + xmake = path.unix(args[2]) + xmake = resolve_path(xmake) + end + end + end + end + return xmake +end + function main(t) local xmake = path.unix(os.programfile()) if os.host() == "windows" then @@ -14,28 +36,6 @@ function main(t) end xmake = resolve_path(xmake) - -- Fix /usr/bin/ape loader mode on Linux - local function fix_ape_programfile(xmake) - if os.host() == "linux" and path.filename(xmake):find("ape", 1, true) and os.isfile("/proc/self/cmdline") then - local file = io.open("/proc/self/cmdline", "rb") - if file then - local content = file:read("*a") - file:close() - if content then - local args = {} - for arg in content:gmatch("[^\0]+") do - table.insert(args, arg) - end - if #args >= 2 and path.unix(args[1]) == xmake then - xmake = path.unix(args[2]) - xmake = resolve_path(xmake) - end - end - end - end - return xmake - end - -- Fix pwsh and cosmocc "err: ape error: l: not found (maybe chmod +x or ./ needed)" for Linux xmake = fix_ape_programfile(xmake) -- cgit v1.3.1 From 16235c0f74b9957aa26fd93e0e87308296fc294f Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 09:08:54 +0300 Subject: refactor: replace os.host() checks with is_host() for consistency and clarity --- tests/modules/stdin/test.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 26c7d4b47..fb62fa32c 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -9,10 +9,10 @@ end -- Fix /usr/bin/ape loader mode on Linux function fix_ape_programfile(xmake) - if os.host() == "linux" and path.filename(xmake):find("ape", 1, true) and os.isfile("/proc/self/cmdline") then + if is_host("linux") and path.filename(xmake):find("ape", 1, true) and os.isfile("/proc/self/cmdline") then local file = io.open("/proc/self/cmdline", "rb") if file then - local content = file:read("*a") + local content = io.readfile("/proc/self/cmdline") file:close() if content then local args = {} @@ -31,7 +31,7 @@ end function main(t) local xmake = path.unix(os.programfile()) - if os.host() == "windows" then + if is_host("windows") then xmake = xmake:gsub("/", "\\") end xmake = resolve_path(xmake) @@ -43,7 +43,7 @@ function main(t) local run_stdin = string.format('env "%s" l --stdin', xmake) -- Fix pwsh and cosmocc "exec format error" for MacOS - if is_ape and os.host() ~= "windows" then + if is_ape and not is_host("windows") then run_stdin = string.format("sh -c ' \"%s\" l --stdin '", xmake) end @@ -55,7 +55,7 @@ function main(t) local ret = -1 try({ function() - if os.host() ~= "windows" then + if not is_host("windows") then ret = os.execv("sh", { "-c", full_cmd }) else ret = os.exec(full_cmd) @@ -89,7 +89,7 @@ function main(t) end local pwsh = "" - if os.host() == "windows" then + if is_host("windows") then -- Test cmd test_shell("cmd_single", string.format("cmd /c echo \"print('hello_cmd')\" | %s l --stdin", xmake), "hello_cmd") test_shell("cmd_calc", string.format('cmd /c echo "local f = 1+1; print(f)" | %s l --stdin', xmake), "2") -- cgit v1.3.1 From e77b312440b1061261633fa41148275da74a75e5 Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 09:27:39 +0300 Subject: refactor: simplify file handling in fix_ape_programfile function --- tests/modules/stdin/test.lua | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index fb62fa32c..6b1b4026b 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -10,19 +10,15 @@ end -- Fix /usr/bin/ape loader mode on Linux function fix_ape_programfile(xmake) if is_host("linux") and path.filename(xmake):find("ape", 1, true) and os.isfile("/proc/self/cmdline") then - local file = io.open("/proc/self/cmdline", "rb") - if file then - local content = io.readfile("/proc/self/cmdline") - file:close() - if content then - local args = {} - for arg in content:gmatch("[^\0]+") do - table.insert(args, arg) - end - if #args >= 2 and path.unix(args[1]) == xmake then - xmake = path.unix(args[2]) - xmake = resolve_path(xmake) - end + local content = io.readfile("/proc/self/cmdline") + if content then + local args = {} + for arg in content:gmatch("[^\0]+") do + table.insert(args, arg) + end + if #args >= 2 and path.unix(args[1]) == xmake then + xmake = path.unix(args[2]) + xmake = resolve_path(xmake) end end end @@ -48,7 +44,6 @@ function main(t) end local function test_shell(name, cmd, expect) - print("testing " .. name .. ": " .. cmd) local outfile = os.tmpfile() local errfile = os.tmpfile() local full_cmd = string.format('%s > "%s" 2> "%s"', cmd, outfile, errfile) -- cgit v1.3.1 From d1653c5b32f4efa7fec6ab40cb93eb14b0014c38 Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 10:28:45 +0300 Subject: refactor: enhance stdin script handling by improving string trimming logic --- tests/modules/stdin/test.lua | 7 ++++++- xmake/plugins/lua/main.lua | 11 ++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 6b1b4026b..b0c908c3c 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -112,9 +112,14 @@ function main(t) string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s l --stdin"', pwsh, xmake), "2" ) + test_shell( + "pwsh_main", + string.format('%s -c "echo \\"function main() print(\'in_pwsh_main\') end\\" | %s"', pwsh, run_stdin), + "in_pwsh_main" + ) test_shell( "pwsh_multi", - string.format("%s -c \"echo \\\"print('pline1')\\nprint('pline2')\\\" | %s l --stdin\"", pwsh, xmake), + string.format('%s -c "echo \\"print(\'pline1\')\\" \\"print(\'pline2\')\\" | %s"', pwsh, run_stdin), "pline1[\r\n]+pline2" ) else diff --git a/xmake/plugins/lua/main.lua b/xmake/plugins/lua/main.lua index 261dffaa0..b2a104949 100644 --- a/xmake/plugins/lua/main.lua +++ b/xmake/plugins/lua/main.lua @@ -53,9 +53,14 @@ function _get_script_from_stdin() script_content = script_content:ltrim(utf8.bom) local shell = os.shell() if shell == "cmd" or shell == "powershell" or shell == "pwsh" or is_host("windows") then - script_content = script_content:trim() - script_content = script_content:trim('\"') - script_content = script_content:replace("\\n", "\n", {plain = true}):replace("\\r", "\r", {plain = true}) + local trimmed = script_content:trim() + if trimmed:startswith('"') and trimmed:endswith('"') then + script_content = trimmed:trim('"') + script_content = script_content:replace("\\n", "\n", {plain = true}) + :replace("\\r", "\r", {plain = true}) + else + script_content = trimmed + end end if not script_content:find("function main", 1, true) then -- cgit v1.3.1 From cd45a05d8b83235f8e2c421769f12afa063ce669 Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 11:04:21 +0300 Subject: refactor: enhance fix_ape_programfile function to improve argument handling and add executable header check --- tests/modules/stdin/test.lua | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index b0c908c3c..c6544a6b1 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -10,13 +10,13 @@ end -- Fix /usr/bin/ape loader mode on Linux function fix_ape_programfile(xmake) if is_host("linux") and path.filename(xmake):find("ape", 1, true) and os.isfile("/proc/self/cmdline") then - local content = io.readfile("/proc/self/cmdline") + local content = io.readfile("/proc/self/cmdline", {encoding = "binary"}) if content then local args = {} for arg in content:gmatch("[^\0]+") do table.insert(args, arg) end - if #args >= 2 and path.unix(args[1]) == xmake then + if #args >= 2 and (path.unix(args[1]) == xmake or path.filename(path.unix(args[1])) == path.filename(xmake)) then xmake = path.unix(args[2]) xmake = resolve_path(xmake) end @@ -35,7 +35,17 @@ function main(t) -- Fix pwsh and cosmocc "err: ape error: l: not found (maybe chmod +x or ./ needed)" for Linux xmake = fix_ape_programfile(xmake) - local is_ape = path.filename(xmake):find("ape-", 1, true) ~= nil + local is_ape = path.filename(xmake):find("ape", 1, true) ~= nil or xmake:endswith(".com") + if not is_ape and is_host("linux") and os.isfile(xmake) then + local file = io.open(xmake, "rb") + if file then + local header = file:read(2) + file:close() + if header == "MZ" then + is_ape = true + end + end + end local run_stdin = string.format('env "%s" l --stdin', xmake) -- Fix pwsh and cosmocc "exec format error" for MacOS -- cgit v1.3.1 From 586227cf172385c9abebe6f19c2343bbf7f94a2c Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 11:32:51 +0300 Subject: refactor: improve stdin command formatting for better cross-platform compatibility --- tests/modules/stdin/test.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index c6544a6b1..e2d139e87 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -47,7 +47,10 @@ function main(t) end end - local run_stdin = string.format('env "%s" l --stdin', xmake) + local run_stdin = string.format('"%s" l --stdin', xmake) + if not is_host("windows") then + run_stdin = string.format('env "%s" l --stdin', xmake) + end -- Fix pwsh and cosmocc "exec format error" for MacOS if is_ape and not is_host("windows") then run_stdin = string.format("sh -c ' \"%s\" l --stdin '", xmake) @@ -100,7 +103,7 @@ function main(t) test_shell("cmd_calc", string.format('cmd /c echo "local f = 1+1; print(f)" | %s l --stdin', xmake), "2") test_shell( "cmd_multi", - string.format("cmd /c echo \"print('line1')\\nprint('line2')\" | %s l --stdin", xmake), + string.format("cmd /c \"(echo print 'line1'& echo print 'line2')\" | %s l --stdin", xmake), "line1[\r\n]+line2" ) -- Test powershell (if available) -- cgit v1.3.1 From 30cfb753c860681adf34ddf3507119ff63f9f8be Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 12:26:20 +0300 Subject: refactor: remove unnecessary header check for ape executable on Linux --- tests/modules/stdin/test.lua | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index e2d139e87..1780c622c 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -36,16 +36,6 @@ function main(t) xmake = fix_ape_programfile(xmake) local is_ape = path.filename(xmake):find("ape", 1, true) ~= nil or xmake:endswith(".com") - if not is_ape and is_host("linux") and os.isfile(xmake) then - local file = io.open(xmake, "rb") - if file then - local header = file:read(2) - file:close() - if header == "MZ" then - is_ape = true - end - end - end local run_stdin = string.format('"%s" l --stdin', xmake) if not is_host("windows") then -- cgit v1.3.1 From 71b0520a355f1a5e4ba0be5f2ef042118aed8432 Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 13:28:27 +0300 Subject: refactor: enhance stdin command tests by adding multi-line and semicolon command handling --- tests/modules/stdin/test.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 1780c622c..f1aee7f27 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -92,10 +92,15 @@ function main(t) test_shell("cmd_single", string.format("cmd /c echo \"print('hello_cmd')\" | %s l --stdin", xmake), "hello_cmd") test_shell("cmd_calc", string.format('cmd /c echo "local f = 1+1; print(f)" | %s l --stdin', xmake), "2") test_shell( - "cmd_multi", + "cmd_multi_lines", string.format("cmd /c \"(echo print 'line1'& echo print 'line2')\" | %s l --stdin", xmake), "line1[\r\n]+line2" ) + test_shell( + "cmd_multi_semicolon", + string.format("cmd /c echo \"print('semi1'); print('semi2')\" | %s l --stdin", xmake), + "semi1[\r\n]+semi2" + ) -- Test powershell (if available) local pwsh = "powershell" try({ -- cgit v1.3.1 From a7d56edc0dfdcfce0d4c0eb56465c63c86bf5f98 Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 13:42:32 +0300 Subject: refactor: streamline xmake path resolution and improve ape programfile handling --- tests/modules/stdin/test.lua | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index f1aee7f27..4d24d1dd6 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -1,3 +1,5 @@ +import("core.base.binutils") + function resolve_path(p) if path.is_absolute(p) then return p end local root = path.join(os.scriptdir(), "../../..") @@ -7,7 +9,6 @@ function resolve_path(p) return p end --- Fix /usr/bin/ape loader mode on Linux function fix_ape_programfile(xmake) if is_host("linux") and path.filename(xmake):find("ape", 1, true) and os.isfile("/proc/self/cmdline") then local content = io.readfile("/proc/self/cmdline", {encoding = "binary"}) @@ -27,15 +28,11 @@ end function main(t) local xmake = path.unix(os.programfile()) - if is_host("windows") then - xmake = xmake:gsub("/", "\\") - end - xmake = resolve_path(xmake) -- Fix pwsh and cosmocc "err: ape error: l: not found (maybe chmod +x or ./ needed)" for Linux xmake = fix_ape_programfile(xmake) - local is_ape = path.filename(xmake):find("ape", 1, true) ~= nil or xmake:endswith(".com") + local is_ape = binutils.format(os.programfile()) == "ape" local run_stdin = string.format('"%s" l --stdin', xmake) if not is_host("windows") then -- cgit v1.3.1 From 081a3360c9d4c7d15ec035129edd25719e9fd9ab Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 13:45:17 +0300 Subject: refactor: correct variable usage for ape programfile detection in main function --- tests/modules/stdin/test.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 4d24d1dd6..985346d39 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -32,7 +32,7 @@ function main(t) -- Fix pwsh and cosmocc "err: ape error: l: not found (maybe chmod +x or ./ needed)" for Linux xmake = fix_ape_programfile(xmake) - local is_ape = binutils.format(os.programfile()) == "ape" + local is_ape = binutils.format(xmake) == "ape" local run_stdin = string.format('"%s" l --stdin', xmake) if not is_host("windows") then -- cgit v1.3.1 From 313d34ffc104c8f9371ad3d94ad633112d2430bc Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 13:58:47 +0300 Subject: refactor: correct path handling for xmake program file in main function --- tests/modules/stdin/test.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 985346d39..7f01b6f86 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -27,7 +27,7 @@ function fix_ape_programfile(xmake) end function main(t) - local xmake = path.unix(os.programfile()) + local xmake = path.translate(os.programfile()) -- Fix pwsh and cosmocc "err: ape error: l: not found (maybe chmod +x or ./ needed)" for Linux xmake = fix_ape_programfile(xmake) -- cgit v1.3.1 From 93d38be02f934c234dd6e62f5fd6cfd25d4d59c4 Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 14:07:20 +0300 Subject: refactor: enhance test_shell function for improved command execution and output handling --- tests/modules/stdin/test.lua | 93 +++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 45 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 7f01b6f86..af2954517 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -26,6 +26,37 @@ function fix_ape_programfile(xmake) return xmake end +function test_shell(t, name, cmd, expect) + local outfile = os.tmpfile() + local errfile = os.tmpfile() + local full_cmd = string.format('%s > "%s" 2> "%s"', cmd, outfile, errfile) + local ret = -1 + try({ + function() + if not is_host("windows") then + ret = os.execv("sh", { "-c", full_cmd }) + else + ret = os.exec(full_cmd) + end + end, + }) + local out = "" + if os.isfile(outfile) then + out = io.readfile(outfile) + if out and out:find("\0", 1, true) then + out = out:gsub("\0", "") + end + end + local err = "" + if os.isfile(errfile) then + err = io.readfile(errfile) + end + local passed = out:find(expect) + t:require(passed) + os.tryrm(outfile) + os.tryrm(errfile) +end + function main(t) local xmake = path.translate(os.programfile()) @@ -43,57 +74,19 @@ function main(t) run_stdin = string.format("sh -c ' \"%s\" l --stdin '", xmake) end - local function test_shell(name, cmd, expect) - local outfile = os.tmpfile() - local errfile = os.tmpfile() - local full_cmd = string.format('%s > "%s" 2> "%s"', cmd, outfile, errfile) - local ret = -1 - try({ - function() - if not is_host("windows") then - ret = os.execv("sh", { "-c", full_cmd }) - else - ret = os.exec(full_cmd) - end - end, - }) - local out = "" - if os.isfile(outfile) then - out = io.readfile(outfile) - if out and out:find("\0", 1, true) then - out = out:gsub("\0", "") - end - end - local err = "" - if os.isfile(errfile) then - err = io.readfile(errfile) - end - local passed = out:find(expect) - if passed then - print(" -> passed") - else - print(" -> failed") - end - print(" out: " .. (out or "")) - print(" err: " .. (err or "")) - if not passed then - raise("[test_stdin]: Test failed! Expect: ", expect) - end - os.tryrm(outfile) - os.tryrm(errfile) - end - local pwsh = "" if is_host("windows") then -- Test cmd - test_shell("cmd_single", string.format("cmd /c echo \"print('hello_cmd')\" | %s l --stdin", xmake), "hello_cmd") - test_shell("cmd_calc", string.format('cmd /c echo "local f = 1+1; print(f)" | %s l --stdin', xmake), "2") + test_shell(t, "cmd_single", string.format("cmd /c echo print 'hello_cmd' | %s l --stdin", xmake), "hello_cmd") + test_shell(t, "cmd_calc", string.format('cmd /c echo local f = 1+1; print^(f^) | %s l --stdin', xmake), "2") test_shell( + t, "cmd_multi_lines", - string.format("cmd /c \"(echo print 'line1'& echo print 'line2')\" | %s l --stdin", xmake), + string.format("cmd /c \"(echo print 'line1'&& echo print 'line2')\" | %s l --stdin", xmake), "line1[\r\n]+line2" ) test_shell( + t, "cmd_multi_semicolon", string.format("cmd /c echo \"print('semi1'); print('semi2')\" | %s l --stdin", xmake), "semi1[\r\n]+semi2" @@ -108,21 +101,25 @@ function main(t) end, }) test_shell( + t, "pwsh_single", string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), "hello_pwsh" ) test_shell( + t, "pwsh_calc", string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s l --stdin"', pwsh, xmake), "2" ) test_shell( + t, "pwsh_main", string.format('%s -c "echo \\"function main() print(\'in_pwsh_main\') end\\" | %s"', pwsh, run_stdin), "in_pwsh_main" ) test_shell( + t, "pwsh_multi", string.format('%s -c "echo \\"print(\'pline1\')\\" \\"print(\'pline2\')\\" | %s"', pwsh, run_stdin), "pline1[\r\n]+pline2" @@ -147,35 +144,41 @@ function main(t) if pwsh ~= "" then test_shell( + t, "pwsh_single", string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s"', pwsh, run_stdin), "hello_pwsh" ) test_shell( + t, "pwsh_calc", string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s"', pwsh, run_stdin), "2" ) test_shell( + t, "pwsh_main", string.format('%s -c "echo \\"function main() print(\'in_pwsh_main\') end\\" | %s"', pwsh, run_stdin), "in_pwsh_main" ) test_shell( + t, "pwsh_multi", string.format('%s -c "echo \\"print(\'pline1\')\\" \\"print(\'pline2\')\\" | %s"', pwsh, run_stdin), "pline1[\r\n]+pline2" ) end - test_shell("sh_single", string.format("echo \"print('hello_sh')\" | %s l --stdin", xmake), "hello_sh") - test_shell("sh_calc", string.format('echo "local f = 1+1; print(f)" | %s l --stdin', xmake), "2") + test_shell(t, "sh_single", string.format("echo \"print('hello_sh')\" | %s l --stdin", xmake), "hello_sh") + test_shell(t, "sh_calc", string.format('echo "local f = 1+1; print(f)" | %s l --stdin', xmake), "2") test_shell( + t, "sh_main", string.format("echo \"function main() print('in_sh_main') end\" | %s l --stdin", xmake), "in_sh_main" ) test_shell( + t, "sh_multi", string.format("printf \"print('shell_line1')\\nprint('shell_line2')\" | %s l --stdin", xmake), "shell_line1[\r\n]+shell_line2" -- cgit v1.3.1 From 7294865c853930c4a50009cf3be1f8d1b302198d Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 14:36:27 +0300 Subject: refactor: simplify ape program file handling in test cases and remove unused functions --- core/src/xmake/engine.c | 24 ++++++++++++++++++++++++ tests/modules/stdin/test.lua | 39 +++------------------------------------ 2 files changed, 27 insertions(+), 36 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index df5ae06be..7a76da7a1 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -874,6 +874,30 @@ static tb_bool_t xm_engine_get_program_file(xm_engine_t *engine, tb_char_t **arg ssize_t size = readlink(XM_PROC_SELF_FILE, path, (size_t)maxn); if (size > 0 && size < maxn) { path[size] = '\0'; +#if defined(TB_CONFIG_OS_LINUX) + // fix /usr/bin/ape for cosmocc + if (size > 3 && !tb_strcmp(path + size - 3, "ape")) { + FILE* fp = fopen("/proc/self/cmdline", "rb"); + if (fp) { + tb_char_t line[TB_PATH_MAXN * 2]; + if (fread(line, 1, sizeof(line), fp) > 0) { + tb_char_t* p = line; + // if argv[0] is /usr/bin/ape, we use argv[1] + if (tb_strstr(p, "ape")) { + p += tb_strlen(p) + 1; + } + // get absolute path + if (p < line + sizeof(line) && *p) { + tb_char_t buf[TB_PATH_MAXN]; + if (tb_path_absolute(p, buf, sizeof(buf))) { + tb_strlcpy(path, buf, maxn); + } + } + } + fclose(fp); + } + } +#endif ok = tb_true; } #elif defined(TB_CONFIG_OS_BSD) && defined(KERN_PROC_PATHNAME) diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index af2954517..d92b0f1f8 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -1,31 +1,5 @@ import("core.base.binutils") -function resolve_path(p) - if path.is_absolute(p) then return p end - local root = path.join(os.scriptdir(), "../../..") - local p_root = path.join(root, p) - if os.isfile(p_root) then return path.absolute(p_root) end - if os.isfile(p) then return path.absolute(p) end - return p -end - -function fix_ape_programfile(xmake) - if is_host("linux") and path.filename(xmake):find("ape", 1, true) and os.isfile("/proc/self/cmdline") then - local content = io.readfile("/proc/self/cmdline", {encoding = "binary"}) - if content then - local args = {} - for arg in content:gmatch("[^\0]+") do - table.insert(args, arg) - end - if #args >= 2 and (path.unix(args[1]) == xmake or path.filename(path.unix(args[1])) == path.filename(xmake)) then - xmake = path.unix(args[2]) - xmake = resolve_path(xmake) - end - end - end - return xmake -end - function test_shell(t, name, cmd, expect) local outfile = os.tmpfile() local errfile = os.tmpfile() @@ -60,17 +34,10 @@ end function main(t) local xmake = path.translate(os.programfile()) - -- Fix pwsh and cosmocc "err: ape error: l: not found (maybe chmod +x or ./ needed)" for Linux - xmake = fix_ape_programfile(xmake) - local is_ape = binutils.format(xmake) == "ape" - local run_stdin = string.format('"%s" l --stdin', xmake) - if not is_host("windows") then - run_stdin = string.format('env "%s" l --stdin', xmake) - end - -- Fix pwsh and cosmocc "exec format error" for MacOS - if is_ape and not is_host("windows") then + -- Fix pwsh and cosmocc "exec format error" for MacOS + if is_ape and not is_host("windows") then run_stdin = string.format("sh -c ' \"%s\" l --stdin '", xmake) end @@ -78,7 +45,7 @@ function main(t) if is_host("windows") then -- Test cmd test_shell(t, "cmd_single", string.format("cmd /c echo print 'hello_cmd' | %s l --stdin", xmake), "hello_cmd") - test_shell(t, "cmd_calc", string.format('cmd /c echo local f = 1+1; print^(f^) | %s l --stdin', xmake), "2") + test_shell(t, "cmd_calc", string.format("cmd /c echo local f = 1+1; print^(f^) | %s l --stdin", xmake), "2") test_shell( t, "cmd_multi_lines", -- cgit v1.3.1 From fb950ed66855524a213ed9144c74bfa0f4f91f17 Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 15:29:11 +0300 Subject: refactor: update MacOS handling in main function for xmake execution --- tests/modules/stdin/test.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index d92b0f1f8..078d374ea 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -33,11 +33,10 @@ end function main(t) local xmake = path.translate(os.programfile()) - local is_ape = binutils.format(xmake) == "ape" local run_stdin = string.format('"%s" l --stdin', xmake) -- Fix pwsh and cosmocc "exec format error" for MacOS - if is_ape and not is_host("windows") then + if is_ape and is_host("macosx") then run_stdin = string.format("sh -c ' \"%s\" l --stdin '", xmake) end -- cgit v1.3.1 From 9f82e7f5c72f8f120c24c05cb25468226dbc0ae9 Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 15:49:07 +0300 Subject: rerun for cosmocc ci --- tests/modules/stdin/test.lua | 1 - 1 file changed, 1 deletion(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 078d374ea..f3490a064 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -39,7 +39,6 @@ function main(t) if is_ape and is_host("macosx") then run_stdin = string.format("sh -c ' \"%s\" l --stdin '", xmake) end - local pwsh = "" if is_host("windows") then -- Test cmd -- cgit v1.3.1 From 307947cc644243e9b8248a14d8ee2b3b94a549fc Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 15:58:01 +0300 Subject: refactor: remove MacOS specific workaround for pwsh and cosmocc exec format error --- tests/modules/stdin/test.lua | 4 ---- 1 file changed, 4 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index f3490a064..2c3460f0d 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -35,10 +35,6 @@ function main(t) local xmake = path.translate(os.programfile()) local is_ape = binutils.format(xmake) == "ape" local run_stdin = string.format('"%s" l --stdin', xmake) - -- Fix pwsh and cosmocc "exec format error" for MacOS - if is_ape and is_host("macosx") then - run_stdin = string.format("sh -c ' \"%s\" l --stdin '", xmake) - end local pwsh = "" if is_host("windows") then -- Test cmd -- cgit v1.3.1 From 03f89e0d3a6bff306ad95705afe18c5e12792bbc Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 16:17:42 +0300 Subject: refactor: fix pwsh and cosmocc "exec format error" handling for MacOS --- tests/modules/stdin/test.lua | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 2c3460f0d..f3490a064 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -35,6 +35,10 @@ function main(t) local xmake = path.translate(os.programfile()) local is_ape = binutils.format(xmake) == "ape" local run_stdin = string.format('"%s" l --stdin', xmake) + -- Fix pwsh and cosmocc "exec format error" for MacOS + if is_ape and is_host("macosx") then + run_stdin = string.format("sh -c ' \"%s\" l --stdin '", xmake) + end local pwsh = "" if is_host("windows") then -- Test cmd -- cgit v1.3.1 From d46368bd6d2d5c0331e701a1cfa62590407f074c Mon Sep 17 00:00:00 2001 From: Saikari Date: Mon, 2 Feb 2026 16:46:22 +0300 Subject: try --- tests/modules/stdin/test.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index f3490a064..ae550e789 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -36,7 +36,7 @@ function main(t) local is_ape = binutils.format(xmake) == "ape" local run_stdin = string.format('"%s" l --stdin', xmake) -- Fix pwsh and cosmocc "exec format error" for MacOS - if is_ape and is_host("macosx") then + if is_ape and not is_host("windows") then run_stdin = string.format("sh -c ' \"%s\" l --stdin '", xmake) end local pwsh = "" -- cgit v1.3.1 From 7799a7947779a1c962e8c3256bcd615891f2a338 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 2 Feb 2026 22:34:42 +0800 Subject: Update test.lua --- tests/modules/stdin/test.lua | 134 ++++++++++++++++++++----------------------- 1 file changed, 61 insertions(+), 73 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index ae550e789..db37a0806 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -1,6 +1,7 @@ import("core.base.binutils") +import("lib.detect.find_tool") -function test_shell(t, name, cmd, expect) +function _test_shell(t, name, cmd, expect) local outfile = os.tmpfile() local errfile = os.tmpfile() local full_cmd = string.format('%s > "%s" 2> "%s"', cmd, outfile, errfile) @@ -31,7 +32,7 @@ function test_shell(t, name, cmd, expect) os.tryrm(errfile) end -function main(t) +function _get_xmake_and_run_stdin() local xmake = path.translate(os.programfile()) local is_ape = binutils.format(xmake) == "ape" local run_stdin = string.format('"%s" l --stdin', xmake) @@ -39,114 +40,101 @@ function main(t) if is_ape and not is_host("windows") then run_stdin = string.format("sh -c ' \"%s\" l --stdin '", xmake) end - local pwsh = "" + return xmake, run_stdin +end + +function test_sh(t) + local xmake, run_stdin = _get_xmake_and_run_stdin() if is_host("windows") then -- Test cmd - test_shell(t, "cmd_single", string.format("cmd /c echo print 'hello_cmd' | %s l --stdin", xmake), "hello_cmd") - test_shell(t, "cmd_calc", string.format("cmd /c echo local f = 1+1; print^(f^) | %s l --stdin", xmake), "2") - test_shell( + _test_shell(t, "cmd_single", string.format("cmd /c echo print 'hello_cmd' | %s l --stdin", xmake), "hello_cmd") + _test_shell(t, "cmd_calc", string.format("cmd /c echo local f = 1+1; print^(f^) | %s l --stdin", xmake), "2") + _test_shell( t, "cmd_multi_lines", string.format("cmd /c \"(echo print 'line1'&& echo print 'line2')\" | %s l --stdin", xmake), "line1[\r\n]+line2" ) - test_shell( + _test_shell( t, "cmd_multi_semicolon", string.format("cmd /c echo \"print('semi1'); print('semi2')\" | %s l --stdin", xmake), "semi1[\r\n]+semi2" ) - -- Test powershell (if available) - local pwsh = "powershell" - try({ - function() - if os.exec("pwsh -v") == 0 then - pwsh = "pwsh" - end - end, - }) - test_shell( - t, - "pwsh_single", - string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), - "hello_pwsh" - ) - test_shell( - t, - "pwsh_calc", - string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s l --stdin"', pwsh, xmake), - "2" - ) - test_shell( + else + -- Linux/MacOS + _test_shell(t, "sh_single", string.format("echo \"print('hello_sh')\" | %s l --stdin", xmake), "hello_sh") + _test_shell(t, "sh_calc", string.format('echo "local f = 1+1; print(f)" | %s l --stdin', xmake), "2") + _test_shell( t, - "pwsh_main", - string.format('%s -c "echo \\"function main() print(\'in_pwsh_main\') end\\" | %s"', pwsh, run_stdin), - "in_pwsh_main" + "sh_main", + string.format("echo \"function main() print('in_sh_main') end\" | %s l --stdin", xmake), + "in_sh_main" ) - test_shell( + _test_shell( t, - "pwsh_multi", - string.format('%s -c "echo \\"print(\'pline1\')\\" \\"print(\'pline2\')\\" | %s"', pwsh, run_stdin), - "pline1[\r\n]+pline2" + "sh_multi", + string.format("printf \"print('shell_line1')\\nprint('shell_line2')\" | %s l --stdin", xmake), + "shell_line1[\r\n]+shell_line2" ) - else - -- Linux/MacOS - local pwsh = "" - try({ - function() - os.iorun("pwsh -v") - pwsh = "pwsh" - end, - }) - if pwsh == "" then - try({ - function() - os.iorun("powershell -v") - pwsh = "powershell" - end, - }) - end + end +end - if pwsh ~= "" then - test_shell( +function test_powershell(t) + local xmake, run_stdin = _get_xmake_and_run_stdin() + local pwsh = find_tool("powershell") + if pwsh then + pwsh = pwsh.program + if is_host("windows") then + _test_shell( + t, + "pwsh_single", + string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), + "hello_pwsh" + ) + _test_shell( + t, + "pwsh_calc", + string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s l --stdin"', pwsh, xmake), + "2" + ) + _test_shell( + t, + "pwsh_main", + string.format('%s -c "echo \\"function main() print(\'in_pwsh_main\') end\\" | %s"', pwsh, run_stdin), + "in_pwsh_main" + ) + _test_shell( + t, + "pwsh_multi", + string.format('%s -c "echo \\"print(\'pline1\')\\" \\"print(\'pline2\')\\" | %s"', pwsh, run_stdin), + "pline1[\r\n]+pline2" + ) + else + _test_shell( t, "pwsh_single", string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s"', pwsh, run_stdin), "hello_pwsh" ) - test_shell( + _test_shell( t, "pwsh_calc", string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s"', pwsh, run_stdin), "2" ) - test_shell( + _test_shell( t, "pwsh_main", string.format('%s -c "echo \\"function main() print(\'in_pwsh_main\') end\\" | %s"', pwsh, run_stdin), "in_pwsh_main" ) - test_shell( + _test_shell( t, "pwsh_multi", string.format('%s -c "echo \\"print(\'pline1\')\\" \\"print(\'pline2\')\\" | %s"', pwsh, run_stdin), "pline1[\r\n]+pline2" ) end - - test_shell(t, "sh_single", string.format("echo \"print('hello_sh')\" | %s l --stdin", xmake), "hello_sh") - test_shell(t, "sh_calc", string.format('echo "local f = 1+1; print(f)" | %s l --stdin', xmake), "2") - test_shell( - t, - "sh_main", - string.format("echo \"function main() print('in_sh_main') end\" | %s l --stdin", xmake), - "in_sh_main" - ) - test_shell( - t, - "sh_multi", - string.format("printf \"print('shell_line1')\\nprint('shell_line2')\" | %s l --stdin", xmake), - "shell_line1[\r\n]+shell_line2" - ) end end -- cgit v1.3.1 From 0c76b46b5bd1250edd45493f6839c60d592724ca Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 2 Feb 2026 22:43:31 +0800 Subject: Update test.lua --- tests/modules/stdin/test.lua | 5 ----- 1 file changed, 5 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index db37a0806..dd781e28b 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -34,12 +34,7 @@ end function _get_xmake_and_run_stdin() local xmake = path.translate(os.programfile()) - local is_ape = binutils.format(xmake) == "ape" local run_stdin = string.format('"%s" l --stdin', xmake) - -- Fix pwsh and cosmocc "exec format error" for MacOS - if is_ape and not is_host("windows") then - run_stdin = string.format("sh -c ' \"%s\" l --stdin '", xmake) - end return xmake, run_stdin end -- cgit v1.3.1 From 2e1c785938ae107734f4e4f1c36e57feae71aca6 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 2 Feb 2026 23:47:42 +0800 Subject: Update test.lua --- tests/modules/stdin/test.lua | 156 ++++++++++--------------------------------- 1 file changed, 36 insertions(+), 120 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index dd781e28b..6c9d49587 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -1,135 +1,51 @@ import("core.base.binutils") import("lib.detect.find_tool") -function _test_shell(t, name, cmd, expect) - local outfile = os.tmpfile() - local errfile = os.tmpfile() - local full_cmd = string.format('%s > "%s" 2> "%s"', cmd, outfile, errfile) - local ret = -1 - try({ - function() - if not is_host("windows") then - ret = os.execv("sh", { "-c", full_cmd }) - else - ret = os.exec(full_cmd) - end - end, - }) - local out = "" - if os.isfile(outfile) then - out = io.readfile(outfile) - if out and out:find("\0", 1, true) then - out = out:gsub("\0", "") - end +function _run_sh(t, name, cmd, expect) + if not is_host("windows") then + local xmake = path.translate(os.programfile()) + local run_stdin = string.format('"%s" l --stdin', xmake) + local outdata = os.iorunv("sh", {"-c", cmd .. " | " .. run_stdin}) or "" + t:are_equal(outdata:trim(), expect) end - local err = "" - if os.isfile(errfile) then - err = io.readfile(errfile) +end + +function _run_cmd(t, name, cmd, expect) + if is_host("windows") then + local xmake = path.translate(os.programfile()) + local run_stdin = string.format('"%s" l --stdin', xmake) + local outdata = os.iorunv("cmd", {"/c", cmd .. " | " .. run_stdin}) or "" + t:are_equal(outdata:trim(), expect) end - local passed = out:find(expect) - t:require(passed) - os.tryrm(outfile) - os.tryrm(errfile) end -function _get_xmake_and_run_stdin() +function _run_pwsh(t, name, cmd, expect) local xmake = path.translate(os.programfile()) local run_stdin = string.format('"%s" l --stdin', xmake) - return xmake, run_stdin + local pwsh = find_tool("powershell") + if pwsh then + local outdata = os.iorunv(pwsh.program, {"-c", cmd .. " | " .. run_stdin}) or "" + t:are_equal(outdata:trim(), expect) + end end function test_sh(t) - local xmake, run_stdin = _get_xmake_and_run_stdin() - if is_host("windows") then - -- Test cmd - _test_shell(t, "cmd_single", string.format("cmd /c echo print 'hello_cmd' | %s l --stdin", xmake), "hello_cmd") - _test_shell(t, "cmd_calc", string.format("cmd /c echo local f = 1+1; print^(f^) | %s l --stdin", xmake), "2") - _test_shell( - t, - "cmd_multi_lines", - string.format("cmd /c \"(echo print 'line1'&& echo print 'line2')\" | %s l --stdin", xmake), - "line1[\r\n]+line2" - ) - _test_shell( - t, - "cmd_multi_semicolon", - string.format("cmd /c echo \"print('semi1'); print('semi2')\" | %s l --stdin", xmake), - "semi1[\r\n]+semi2" - ) - else - -- Linux/MacOS - _test_shell(t, "sh_single", string.format("echo \"print('hello_sh')\" | %s l --stdin", xmake), "hello_sh") - _test_shell(t, "sh_calc", string.format('echo "local f = 1+1; print(f)" | %s l --stdin', xmake), "2") - _test_shell( - t, - "sh_main", - string.format("echo \"function main() print('in_sh_main') end\" | %s l --stdin", xmake), - "in_sh_main" - ) - _test_shell( - t, - "sh_multi", - string.format("printf \"print('shell_line1')\\nprint('shell_line2')\" | %s l --stdin", xmake), - "shell_line1[\r\n]+shell_line2" - ) - end + _run_sh(t, "sh_single", "echo \"print('hello_sh')\"", "hello_sh") + _run_sh(t, "sh_calc", 'echo "local f = 1+1; print(f)"', "2") + _run_sh(t, "sh_main", "echo \"function main() print('in_sh_main') end\"", "in_sh_main") + _run_sh(t, "sh_multi", "printf \"print('shell_line1')\\nprint('shell_line2')\"", "shell_line1\nshell_line2") end -function test_powershell(t) - local xmake, run_stdin = _get_xmake_and_run_stdin() - local pwsh = find_tool("powershell") - if pwsh then - pwsh = pwsh.program - if is_host("windows") then - _test_shell( - t, - "pwsh_single", - string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s l --stdin"', pwsh, xmake), - "hello_pwsh" - ) - _test_shell( - t, - "pwsh_calc", - string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s l --stdin"', pwsh, xmake), - "2" - ) - _test_shell( - t, - "pwsh_main", - string.format('%s -c "echo \\"function main() print(\'in_pwsh_main\') end\\" | %s"', pwsh, run_stdin), - "in_pwsh_main" - ) - _test_shell( - t, - "pwsh_multi", - string.format('%s -c "echo \\"print(\'pline1\')\\" \\"print(\'pline2\')\\" | %s"', pwsh, run_stdin), - "pline1[\r\n]+pline2" - ) - else - _test_shell( - t, - "pwsh_single", - string.format('%s -c "echo \\"print(\'hello_pwsh\')\\" | %s"', pwsh, run_stdin), - "hello_pwsh" - ) - _test_shell( - t, - "pwsh_calc", - string.format('%s -c "echo \\"local f = 1+1; print(f)\\" | %s"', pwsh, run_stdin), - "2" - ) - _test_shell( - t, - "pwsh_main", - string.format('%s -c "echo \\"function main() print(\'in_pwsh_main\') end\\" | %s"', pwsh, run_stdin), - "in_pwsh_main" - ) - _test_shell( - t, - "pwsh_multi", - string.format('%s -c "echo \\"print(\'pline1\')\\" \\"print(\'pline2\')\\" | %s"', pwsh, run_stdin), - "pline1[\r\n]+pline2" - ) - end - end +function test_cmd(t) + _run_cmd(t, "cmd_single", "echo print 'hello_cmd'", "hello_cmd") + _run_cmd(t, "cmd_calc", "echo local f = 1+1; print^(f^)", "2") + _run_cmd(t, "cmd_multi_lines", "(echo print 'line1'&& echo print 'line2')", "line1\r\nline2") + _run_cmd(t, "cmd_multi_semicolon", "echo \"print('semi1'); print('semi2')\"", "semi1\r\nsemi2") +end + +function test_pwsh(t) + _run_pwsh(t, "pwsh_single", "echo \"print('hello_pwsh')\"", "hello_pwsh") + _run_pwsh(t, "pwsh_calc", "echo \"local f = 1+1; print(f)\"", "2") + _run_pwsh(t, "pwsh_main", "echo \"function main() print('in_pwsh_main') end\"", "in_pwsh_main") + _run_pwsh(t, "pwsh_multi", "echo \"print('pline1')\"; echo \"print('pline2')\"", "pline1\r\npline2") end -- cgit v1.3.1 From 1e9e8d25b8180d05ac8c99073f31cd6b085dc4a9 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 2 Feb 2026 23:49:48 +0800 Subject: Update test.lua --- tests/modules/stdin/test.lua | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 6c9d49587..351955f71 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -1,4 +1,3 @@ -import("core.base.binutils") import("lib.detect.find_tool") function _run_sh(t, name, cmd, expect) @@ -20,12 +19,14 @@ function _run_cmd(t, name, cmd, expect) end function _run_pwsh(t, name, cmd, expect) - local xmake = path.translate(os.programfile()) - local run_stdin = string.format('"%s" l --stdin', xmake) - local pwsh = find_tool("powershell") - if pwsh then - local outdata = os.iorunv(pwsh.program, {"-c", cmd .. " | " .. run_stdin}) or "" - t:are_equal(outdata:trim(), expect) + if is_host("windows") then + local xmake = path.translate(os.programfile()) + local run_stdin = string.format('"%s" l --stdin', xmake) + local pwsh = find_tool("powershell") + if pwsh then + local outdata = os.iorunv(pwsh.program, {"-c", cmd .. " | " .. run_stdin}) or "" + t:are_equal(outdata:trim(), expect) + end end end @@ -47,5 +48,5 @@ function test_pwsh(t) _run_pwsh(t, "pwsh_single", "echo \"print('hello_pwsh')\"", "hello_pwsh") _run_pwsh(t, "pwsh_calc", "echo \"local f = 1+1; print(f)\"", "2") _run_pwsh(t, "pwsh_main", "echo \"function main() print('in_pwsh_main') end\"", "in_pwsh_main") - _run_pwsh(t, "pwsh_multi", "echo \"print('pline1')\"; echo \"print('pline2')\"", "pline1\r\npline2") + _run_pwsh(t, "pwsh_multi", "echo \"print('pline1')`nprint('pline2')\"", "pline1\r\npline2") end -- cgit v1.3.1 From 2ddc34d3ecfe4b3cdbf8370fdbcc4c46f61f54d0 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 2 Feb 2026 23:51:44 +0800 Subject: Fix shell and command test cases for output consistency --- tests/modules/stdin/test.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 351955f71..d739e9bab 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -32,14 +32,14 @@ end function test_sh(t) _run_sh(t, "sh_single", "echo \"print('hello_sh')\"", "hello_sh") - _run_sh(t, "sh_calc", 'echo "local f = 1+1; print(f)"', "2") + _run_sh(t, "sh_calc", "echo \"local f = 1+1; print(f)\"", "2") _run_sh(t, "sh_main", "echo \"function main() print('in_sh_main') end\"", "in_sh_main") _run_sh(t, "sh_multi", "printf \"print('shell_line1')\\nprint('shell_line2')\"", "shell_line1\nshell_line2") end function test_cmd(t) _run_cmd(t, "cmd_single", "echo print 'hello_cmd'", "hello_cmd") - _run_cmd(t, "cmd_calc", "echo local f = 1+1; print^(f^)", "2") + _run_cmd(t, "cmd_calc", "echo local f = 1+1; print(f)", "2") _run_cmd(t, "cmd_multi_lines", "(echo print 'line1'&& echo print 'line2')", "line1\r\nline2") _run_cmd(t, "cmd_multi_semicolon", "echo \"print('semi1'); print('semi2')\"", "semi1\r\nsemi2") end @@ -48,5 +48,5 @@ function test_pwsh(t) _run_pwsh(t, "pwsh_single", "echo \"print('hello_pwsh')\"", "hello_pwsh") _run_pwsh(t, "pwsh_calc", "echo \"local f = 1+1; print(f)\"", "2") _run_pwsh(t, "pwsh_main", "echo \"function main() print('in_pwsh_main') end\"", "in_pwsh_main") - _run_pwsh(t, "pwsh_multi", "echo \"print('pline1')`nprint('pline2')\"", "pline1\r\npline2") + _run_pwsh(t, "pwsh_multi", "echo \"print('pline1')\"; echo \"print('pline2')\"", "pline1\r\npline2") end -- cgit v1.3.1 From 7d1615632117405b5078aef5a3ecdb6dd5d0edf0 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 3 Feb 2026 10:03:26 +0800 Subject: Update test.lua --- tests/modules/stdin/test.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index d739e9bab..60e8ca1e5 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -12,7 +12,7 @@ end function _run_cmd(t, name, cmd, expect) if is_host("windows") then local xmake = path.translate(os.programfile()) - local run_stdin = string.format('"%s" l --stdin', xmake) + local run_stdin = string.format('%s l --stdin', xmake) local outdata = os.iorunv("cmd", {"/c", cmd .. " | " .. run_stdin}) or "" t:are_equal(outdata:trim(), expect) end @@ -21,7 +21,7 @@ end function _run_pwsh(t, name, cmd, expect) if is_host("windows") then local xmake = path.translate(os.programfile()) - local run_stdin = string.format('"%s" l --stdin', xmake) + local run_stdin = string.format('%s l --stdin', xmake) local pwsh = find_tool("powershell") if pwsh then local outdata = os.iorunv(pwsh.program, {"-c", cmd .. " | " .. run_stdin}) or "" @@ -40,13 +40,13 @@ end function test_cmd(t) _run_cmd(t, "cmd_single", "echo print 'hello_cmd'", "hello_cmd") _run_cmd(t, "cmd_calc", "echo local f = 1+1; print(f)", "2") - _run_cmd(t, "cmd_multi_lines", "(echo print 'line1'&& echo print 'line2')", "line1\r\nline2") - _run_cmd(t, "cmd_multi_semicolon", "echo \"print('semi1'); print('semi2')\"", "semi1\r\nsemi2") + _run_cmd(t, "cmd_multi_lines", "(echo print 'line1' && echo print 'line2')", "line1\nline2") + _run_cmd(t, "cmd_multi_semicolon", "echo print('semi1'); print('semi2')", "semi1\nsemi2") end function test_pwsh(t) _run_pwsh(t, "pwsh_single", "echo \"print('hello_pwsh')\"", "hello_pwsh") _run_pwsh(t, "pwsh_calc", "echo \"local f = 1+1; print(f)\"", "2") _run_pwsh(t, "pwsh_main", "echo \"function main() print('in_pwsh_main') end\"", "in_pwsh_main") - _run_pwsh(t, "pwsh_multi", "echo \"print('pline1')\"; echo \"print('pline2')\"", "pline1\r\npline2") + _run_pwsh(t, "pwsh_multi", "echo \"print('pline1')\"; echo \"print('pline2')\"", "pline1\npline2") end -- cgit v1.3.1 From b72e271e4ca2b59170819295eaa972ce149096b2 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 3 Feb 2026 10:17:09 +0800 Subject: Fix syntax for pwsh_multi test case --- tests/modules/stdin/test.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/modules/stdin/test.lua') diff --git a/tests/modules/stdin/test.lua b/tests/modules/stdin/test.lua index 60e8ca1e5..323ba6f81 100644 --- a/tests/modules/stdin/test.lua +++ b/tests/modules/stdin/test.lua @@ -48,5 +48,5 @@ function test_pwsh(t) _run_pwsh(t, "pwsh_single", "echo \"print('hello_pwsh')\"", "hello_pwsh") _run_pwsh(t, "pwsh_calc", "echo \"local f = 1+1; print(f)\"", "2") _run_pwsh(t, "pwsh_main", "echo \"function main() print('in_pwsh_main') end\"", "in_pwsh_main") - _run_pwsh(t, "pwsh_multi", "echo \"print('pline1')\"; echo \"print('pline2')\"", "pline1\npline2") + _run_pwsh(t, "pwsh_multi", "echo \"print('pline1')\" \"print('pline2')\"", "pline1\npline2") end -- cgit v1.3.1