summaryrefslogtreecommitdiff
path: root/tests/modules/stdin/test.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2026-02-02 23:47:42 +0800
committerGitHub <[email protected]>2026-02-02 23:47:42 +0800
commit2e1c785938ae107734f4e4f1c36e57feae71aca6 (patch)
treed2a2d48f92534c600fc70a3406dcda3295ea53d2 /tests/modules/stdin/test.lua
parent0c76b46b5bd1250edd45493f6839c60d592724ca (diff)
Update test.lua
Diffstat (limited to 'tests/modules/stdin/test.lua')
-rw-r--r--tests/modules/stdin/test.lua156
1 files changed, 36 insertions, 120 deletions
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