summaryrefslogtreecommitdiff
path: root/tests/modules/stdin/test.lua
diff options
context:
space:
mode:
authorSaikari <[email protected]>2026-02-02 14:07:20 +0300
committerSaikari <[email protected]>2026-02-02 14:07:20 +0300
commit93d38be02f934c234dd6e62f5fd6cfd25d4d59c4 (patch)
tree96581ff9a394320bb4f1068def57bf9b1adb3c19 /tests/modules/stdin/test.lua
parent313d34ffc104c8f9371ad3d94ad633112d2430bc (diff)
refactor: enhance test_shell function for improved command execution and output handling
Diffstat (limited to 'tests/modules/stdin/test.lua')
-rw-r--r--tests/modules/stdin/test.lua93
1 files changed, 48 insertions, 45 deletions
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"