Improve process exec stdin test

This commit is contained in:
Filip Tibell 2025-04-25 13:03:22 +02:00
parent b74568eea0
commit f6d2afb003
No known key found for this signature in database

View file

@ -1,19 +1,17 @@
local process = require("@lune/process") local process = require("@lune/process")
local IS_WINDOWS = process.os == "windows" local expected = "Hello from child process!"
-- Windows uses \r\n (CRLF) and unix uses \n (LF)
local echoTrail = if IS_WINDOWS then "\r\n" else "\n"
local echoMessage = "Hello from child process!"
-- When passing stdin to powershell on windows we must "accept" using the double newline -- When passing stdin to powershell on windows we must "accept" using the double newline
local result = if IS_WINDOWS local result = if process.os == "windows"
then process.exec("powershell", { "echo" }, { stdio = { stdin = echoMessage .. "\n\n" } }) then process.exec("powershell", { "echo" }, { stdio = { stdin = expected .. "\n\n" } })
else process.exec("xargs", { "echo" }, { stdio = { stdin = echoMessage } }) else process.exec("xargs", { "echo" }, { stdio = { stdin = expected } })
local resultStdout = if IS_WINDOWS local resultStdout = result.stdout
then string.sub(result.stdout, #result.stdout - #echoMessage - 1) resultStdout = string.gsub(resultStdout, "^%s+", "") -- Trim leading whitespace
else result.stdout resultStdout = string.gsub(resultStdout, "%s+$", "") -- Trim trailing whitespace
assert(resultStdout == echoMessage .. echoTrail, "Stdin passing did not return proper output") assert(
resultStdout == expected,
"Stdin passing did not return proper output!" .. `\nExpected: {expected}` .. `\nReceived: {resultStdout}`
)