2023-05-14 21:16:58 +01:00
|
|
|
local process = require("@lune/process")
|
2023-08-21 00:54:34 +01:00
|
|
|
local stdio = require("@lune/stdio")
|
2023-06-08 10:21:00 +01:00
|
|
|
local task = require("@lune/task")
|
2023-05-14 21:16:58 +01:00
|
|
|
|
2023-08-21 00:54:34 +01:00
|
|
|
-- Spawning a child process should work, with options
|
|
|
|
|
|
|
|
local thread = task.delay(1, function()
|
|
|
|
stdio.ewrite("Spawning a process should take a reasonable amount of time\n")
|
|
|
|
task.wait(1)
|
|
|
|
process.exit(1)
|
|
|
|
end)
|
2023-01-28 04:46:07 +00:00
|
|
|
|
2023-01-21 04:40:31 +00:00
|
|
|
local result = process.spawn("ls", {
|
|
|
|
"-a",
|
|
|
|
})
|
|
|
|
|
2023-08-21 00:54:34 +01:00
|
|
|
task.cancel(thread)
|
|
|
|
|
2023-01-21 04:40:31 +00:00
|
|
|
assert(result.ok, "Failed to spawn child process")
|
|
|
|
|
|
|
|
assert(result.stderr == "", "Stderr was not empty")
|
|
|
|
assert(result.stdout ~= "", "Stdout was empty")
|
|
|
|
|
|
|
|
assert(string.find(result.stdout, "Cargo.toml") ~= nil, "Missing Cargo.toml in output")
|
|
|
|
assert(string.find(result.stdout, ".gitignore") ~= nil, "Missing .gitignore in output")
|
2023-01-28 04:46:07 +00:00
|
|
|
|
|
|
|
-- It should also work the same when spawned using a shell
|
|
|
|
|
|
|
|
local shellResult = process.spawn("ls", {
|
|
|
|
"-a",
|
|
|
|
}, {
|
|
|
|
shell = true,
|
|
|
|
})
|
|
|
|
|
|
|
|
assert(shellResult.ok, "Failed to spawn child process (shell)")
|
|
|
|
|
|
|
|
assert(shellResult.stderr == "", "Stderr was not empty (shell)")
|
|
|
|
assert(shellResult.stdout ~= "", "Stdout was empty (shell)")
|
|
|
|
|
|
|
|
assert(string.find(shellResult.stdout, "Cargo.toml") ~= nil, "Missing Cargo.toml in output (shell)")
|
|
|
|
assert(string.find(shellResult.stdout, ".gitignore") ~= nil, "Missing .gitignore in output (shell)")
|
|
|
|
|
|
|
|
-- Make sure the cwd option actually uses the directory we want
|
|
|
|
local rootPwd = process.spawn("pwd", {}, {
|
|
|
|
cwd = "/",
|
|
|
|
}).stdout
|
|
|
|
rootPwd = string.gsub(rootPwd, "^%s+", "")
|
|
|
|
rootPwd = string.gsub(rootPwd, "%s+$", "")
|
|
|
|
if rootPwd ~= "/" then
|
|
|
|
error(
|
|
|
|
string.format(
|
|
|
|
"Current working directory for child process was not set correctly!"
|
|
|
|
.. "\nExpected '/', got '%s'",
|
|
|
|
rootPwd
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Setting cwd should not change the cwd of this process
|
|
|
|
|
2023-02-09 21:23:22 +00:00
|
|
|
local pwdBefore = process.spawn("pwd").stdout
|
2023-01-28 04:46:07 +00:00
|
|
|
process.spawn("ls", {}, {
|
|
|
|
cwd = "/",
|
|
|
|
shell = true,
|
|
|
|
})
|
2023-02-09 21:23:22 +00:00
|
|
|
local pwdAfter = process.spawn("pwd").stdout
|
|
|
|
assert(pwdBefore == pwdAfter, "Current working directory changed after running child process")
|
2023-01-28 04:46:07 +00:00
|
|
|
|
2023-02-09 21:59:17 +00:00
|
|
|
--[[
|
|
|
|
Setting the cwd on a child process should properly
|
|
|
|
replace any leading ~ with the users real home dir
|
|
|
|
]]
|
2023-01-28 04:46:07 +00:00
|
|
|
|
2023-02-09 21:59:17 +00:00
|
|
|
local homeDir1 = process.spawn("echo $HOME", nil, {
|
|
|
|
shell = true,
|
|
|
|
}).stdout
|
|
|
|
local homeDir2 = process.spawn("pwd", nil, {
|
|
|
|
shell = true,
|
|
|
|
cwd = "~",
|
|
|
|
}).stdout
|
|
|
|
|
|
|
|
assert(#homeDir1 > 0, "Home dir from echo was empty")
|
|
|
|
assert(#homeDir2 > 0, "Home dir from pwd was empty")
|
|
|
|
assert(homeDir1 == homeDir2, "Home dirs did not match when performing tilde substitution")
|
2023-02-09 21:23:22 +00:00
|
|
|
|
|
|
|
--[[
|
|
|
|
Spawning a process should not block any lua thread(s)
|
|
|
|
|
|
|
|
We test this by sleeping more than once concurrently
|
|
|
|
and then ensuring that the total time slept is more
|
|
|
|
than a single sleep but also less than 1.5 sleeps
|
|
|
|
]]
|
|
|
|
|
|
|
|
local SLEEP_DURATION = 1 / 4
|
|
|
|
local SLEEP_SAMPLES = 2
|
|
|
|
|
2023-08-21 00:54:34 +01:00
|
|
|
local thread2 = task.delay(SLEEP_DURATION * 1.5, function()
|
|
|
|
stdio.ewrite("Spawning a sleep process should take a reasonable amount of time\n")
|
|
|
|
task.wait(1)
|
|
|
|
process.exit(1)
|
|
|
|
end)
|
|
|
|
|
2023-02-09 21:23:22 +00:00
|
|
|
local sleepStart = os.clock()
|
|
|
|
local sleepCounter = 0
|
|
|
|
for i = 1, SLEEP_SAMPLES, 1 do
|
|
|
|
task.spawn(function()
|
|
|
|
process.spawn("sleep", { tostring(SLEEP_DURATION) })
|
|
|
|
sleepCounter += 1
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
while sleepCounter < SLEEP_SAMPLES do
|
|
|
|
task.wait()
|
|
|
|
end
|
|
|
|
|
2023-08-21 00:54:34 +01:00
|
|
|
task.cancel(thread2)
|
|
|
|
|
2023-02-09 21:23:22 +00:00
|
|
|
local sleepElapsed = os.clock() - sleepStart
|
|
|
|
assert(
|
2023-08-21 00:54:34 +01:00
|
|
|
sleepElapsed >= SLEEP_DURATION,
|
|
|
|
"Spawning a process that does blocking sleep did not sleep enough"
|
|
|
|
)
|
|
|
|
assert(
|
|
|
|
sleepElapsed < SLEEP_DURATION * 1.5,
|
2023-02-09 21:23:22 +00:00
|
|
|
"Coroutine yielded the main lua thread during process yield"
|
|
|
|
)
|
2023-02-09 21:59:17 +00:00
|
|
|
|
|
|
|
-- Inheriting stdio & environment variables should work
|
|
|
|
|
|
|
|
local echoMessage = "Hello from child process!"
|
|
|
|
local echoResult = process.spawn("echo", {
|
|
|
|
'"$TEST_VAR"',
|
|
|
|
}, {
|
|
|
|
env = { TEST_VAR = echoMessage },
|
|
|
|
shell = "bash",
|
|
|
|
stdio = "inherit",
|
|
|
|
})
|
|
|
|
assert(
|
|
|
|
echoResult.stdout == (echoMessage .. "\n"), -- Note that echo adds a newline
|
|
|
|
"Inheriting stdio did not return proper output"
|
|
|
|
)
|