diff options
| author | Saikari <[email protected]> | 2026-02-01 16:03:52 +0300 |
|---|---|---|
| committer | Saikari <[email protected]> | 2026-02-01 16:03:52 +0300 |
| commit | 9d45443b6cf3a6bb2a0e0f6ab30347cb826c700d (patch) | |
| tree | 44328540eb869e797fb7d4472b6256f8afb8512e /tests/modules/stdin/test.lua | |
| parent | 0588ca91219e1ac614e234b7307941ec9b6df161 (diff) | |
stylua fmt?
Diffstat (limited to 'tests/modules/stdin/test.lua')
| -rw-r--r-- | tests/modules/stdin/test.lua | 216 |
1 files changed, 138 insertions, 78 deletions
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 |
