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-09-25 20:20:01 +01:00
|
|
|
local IS_WINDOWS = process.os == "windows"
|
2023-09-25 20:14:29 +01:00
|
|
|
|
|
|
|
local result = process.spawn(
|
2023-09-25 20:20:01 +01:00
|
|
|
if IS_WINDOWS then "cmd" else "ls",
|
|
|
|
if IS_WINDOWS then { "/c", "dir" } else { "-a" }
|
2023-09-25 20:14:29 +01:00
|
|
|
)
|
2023-01-21 04:40:31 +00:00
|
|
|
|
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
|
2023-09-25 20:20:01 +01:00
|
|
|
-- Note that the default on Windows is Powershell which has different flags / behavior
|
2023-01-28 04:46:07 +00:00
|
|
|
|
|
|
|
local shellResult = process.spawn("ls", {
|
2023-09-25 20:20:01 +01:00
|
|
|
if IS_WINDOWS then "-Force" else "-a",
|
2023-01-28 04:46:07 +00:00
|
|
|
}, {
|
|
|
|
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)")
|
|
|
|
|
2023-09-25 20:20:01 +01:00
|
|
|
local pwdCommand = if IS_WINDOWS then "cmd" else "pwd"
|
|
|
|
local pwdArgs = if IS_WINDOWS then { "/c", "cd" } else {}
|
|
|
|
|
2023-01-28 04:46:07 +00:00
|
|
|
-- Make sure the cwd option actually uses the directory we want
|
2023-09-25 20:14:29 +01:00
|
|
|
local rootPwd = process.spawn(pwdCommand, pwdArgs, {
|
2023-01-28 04:46:07 +00:00
|
|
|
cwd = "/",
|
|
|
|
}).stdout
|
|
|
|
rootPwd = string.gsub(rootPwd, "^%s+", "")
|
|
|
|
rootPwd = string.gsub(rootPwd, "%s+$", "")
|
2023-09-25 20:20:01 +01:00
|
|
|
|
2023-09-25 20:14:29 +01:00
|
|
|
-- Windows: <Drive Letter>:\, Unix: /
|
2023-09-25 20:20:01 +01:00
|
|
|
local expectedRootPwd = if IS_WINDOWS then string.sub(rootPwd, 1, 1) .. ":\\" else "/"
|
2023-09-25 20:14:29 +01:00
|
|
|
if rootPwd ~= expectedRootPwd then
|
2023-01-28 04:46:07 +00:00
|
|
|
error(
|
|
|
|
string.format(
|
|
|
|
"Current working directory for child process was not set correctly!"
|
2023-09-25 20:14:29 +01:00
|
|
|
.. "\nExpected '%s', got '%s'",
|
2023-09-25 20:20:01 +01:00
|
|
|
expectedRootPwd,
|
|
|
|
rootPwd
|
2023-01-28 04:46:07 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Setting cwd should not change the cwd of this process
|
|
|
|
|
2023-09-25 20:14:29 +01:00
|
|
|
local pwdBefore = process.spawn(pwdCommand, pwdArgs).stdout
|
2023-01-28 04:46:07 +00:00
|
|
|
process.spawn("ls", {}, {
|
|
|
|
cwd = "/",
|
|
|
|
shell = true,
|
|
|
|
})
|
2023-09-25 20:14:29 +01:00
|
|
|
local pwdAfter = process.spawn(pwdCommand, pwdArgs).stdout
|
2023-02-09 21:23:22 +00:00
|
|
|
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
|
2023-09-25 20:14:29 +01:00
|
|
|
|
|
|
|
-- Powershell for windows uses `$pwd.Path` instead of `pwd` as pwd would return a PathInfo object,
|
|
|
|
-- using $pwd.Path gets the Path property of the PathInfo object.
|
2023-09-25 20:20:01 +01:00
|
|
|
local homeDir2 = process.spawn(if IS_WINDOWS then "$pwd.Path" else "pwd", nil, {
|
2023-02-09 21:59:17 +00:00
|
|
|
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-09-25 20:20:01 +01:00
|
|
|
-- Unfortunately we
|
|
|
|
local thread2 = task.delay(30, function()
|
2023-08-21 00:54:34 +01:00
|
|
|
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()
|
2023-09-25 20:14:29 +01:00
|
|
|
local args = {
|
2023-09-25 20:20:01 +01:00
|
|
|
-- Sleep command on Windows in Seconds has some weird behavior with decimals ...
|
|
|
|
tostring(SLEEP_DURATION * (IS_WINDOWS and 1000 or 1)),
|
|
|
|
}
|
|
|
|
if IS_WINDOWS then
|
2023-09-25 20:14:29 +01:00
|
|
|
-- ... so we use milliseconds instead.
|
2023-09-25 20:20:01 +01:00
|
|
|
table.insert(args, 1, "-Milliseconds")
|
2023-09-25 20:14:29 +01:00
|
|
|
end
|
|
|
|
-- Windows does not have `sleep` as a process, so we use powershell instead.
|
2023-09-25 20:20:01 +01:00
|
|
|
process.spawn("sleep", args, if IS_WINDOWS then { shell = true } else nil)
|
2023-02-09 21:23:22 +00:00
|
|
|
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
|
|
|
assert(
|
2023-09-25 20:20:01 +01:00
|
|
|
(os.clock() - sleepStart) >= SLEEP_DURATION,
|
2023-08-21 00:54:34 +01:00
|
|
|
"Spawning a process that does blocking sleep did not sleep enough"
|
|
|
|
)
|
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", {
|
2023-09-25 20:20:01 +01:00
|
|
|
if IS_WINDOWS then '"$Env:TEST_VAR"' else '"$TEST_VAR"',
|
2023-02-09 21:59:17 +00:00
|
|
|
}, {
|
|
|
|
env = { TEST_VAR = echoMessage },
|
2023-09-25 20:20:01 +01:00
|
|
|
shell = if IS_WINDOWS then "powershell" else "bash",
|
2023-02-09 21:59:17 +00:00
|
|
|
stdio = "inherit",
|
|
|
|
})
|
2023-09-25 20:14:29 +01:00
|
|
|
|
2023-09-25 20:20:01 +01:00
|
|
|
-- Windows echo adds \r\n (CRLF) and unix adds \n (LF)
|
|
|
|
local trailingAddition = if IS_WINDOWS then "\r\n" else "\n"
|
2023-02-09 21:59:17 +00:00
|
|
|
assert(
|
2023-09-25 20:20:01 +01:00
|
|
|
echoResult.stdout == (echoMessage .. trailingAddition),
|
2023-02-09 21:59:17 +00:00
|
|
|
"Inheriting stdio did not return proper output"
|
|
|
|
)
|