diff options
| author | ruki <[email protected]> | 2026-02-02 22:34:42 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-02-02 22:34:42 +0800 |
| commit | 7799a7947779a1c962e8c3256bcd615891f2a338 (patch) | |
| tree | 533813a587adaeb5bb33c322c3edae9e2ab40994 /tests/modules/stdin/test.lua | |
| parent | 3bf7d52392bd9489ce7e580120dec8dac7d7c29f (diff) | |
Update test.lua
Diffstat (limited to 'tests/modules/stdin/test.lua')
| -rw-r--r-- | tests/modules/stdin/test.lua | 134 |
1 files changed, 61 insertions, 73 deletions
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 |
