chore(tests): add new process.spawn tests

This commit is contained in:
Erica Marigold 2024-06-09 22:24:32 +05:30
parent 6a2f5061d5
commit d9cc71e512
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1
6 changed files with 55 additions and 4 deletions

View file

@ -4,7 +4,7 @@ local task = require("@lune/task")
local IS_WINDOWS = process.os == "windows"
-- Spawning a process should not block any lua thread(s)
-- Executing a command should not block any lua thread(s)
local SLEEP_DURATION = 1 / 4
local SLEEP_SAMPLES = 2

View file

@ -2,7 +2,7 @@ local process = require("@lune/process")
local stdio = require("@lune/stdio")
local task = require("@lune/task")
-- Spawning a child process should work, with options
-- Executing a comamnd should work, with options
local thread = task.delay(1, function()
stdio.ewrite("Spawning a process should take a reasonable amount of time\n")

View file

@ -1,7 +1,7 @@
local process = require("@lune/process")
-- Spawning a child process for a non-existent
-- program should not panic, but should error
-- Executing a non existent command as a child process
-- should not panic, but should error
local success = pcall(process.exec, "someProgramThatDoesNotExist")
assert(not success, "Spawned a non-existent program")

View file

@ -0,0 +1,18 @@
local process = require("@lune/process")
-- Spawning a child process should not block the main thread
local SAMPLES = 400
for _ = 1, SAMPLES do
local start = os.time()
local child = process.spawn("echo", { "hello, world" })
assert(child ~= nil, "Failed to spawn child process")
local delta = os.time() - start
assert(
delta <= 1,
`Spawning a child process should not block the main thread, process.spawn took {delta}s to return it should return immediately`
)
end

View file

@ -0,0 +1,15 @@
local process = require("@lune/process")
-- The exit code of an child process should be correct
local randomExitCode = math.random(1, 255)
local isOk = randomExitCode == 0
local child = process.spawn("exit", { tostring(randomExitCode) }, { shell = true })
local status = child.status()
assert(
status.code == randomExitCode,
`Child process exited with wrong exit code, expected {randomExitCode}`
)
assert(status.ok == isOk, `Child status should be {if status.ok then "ok" else "not ok"}`)

View file

@ -0,0 +1,18 @@
local process = require("@lune/process")
-- Should be able to write and read from child process streams
local msg = "hello, world"
local catChild = process.spawn("cat")
catChild.stdin:write(msg)
assert(
msg == buffer.tostring(catChild.stdout:read(#msg)),
"Failed to write to stdin or read from stdout of child process"
)
local echoChild = process.spawn("echo", { msg, ">>/dev/stderr" }, { shell = true })
assert(
msg == buffer.tostring(echoChild.stderr:read(#msg)),
"Failed to read from stderr of child process"
)