mirror of
https://github.com/lune-org/lune.git
synced 2025-01-05 19:09:10 +00:00
Stylua formatting in process spawn test file
This commit is contained in:
parent
83ac971792
commit
b1ee221555
1 changed files with 27 additions and 36 deletions
|
@ -10,16 +10,11 @@ local thread = task.delay(1, function()
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local isWindows = process.os == "windows";
|
local IS_WINDOWS = process.os == "windows"
|
||||||
|
|
||||||
-- To run windows command, we need to launch cmd.exe and pass the command as an argument.
|
|
||||||
local result = process.spawn(
|
local result = process.spawn(
|
||||||
if isWindows then "cmd" else "ls",
|
if IS_WINDOWS then "cmd" else "ls",
|
||||||
if isWindows then {
|
if IS_WINDOWS then { "/c", "dir" } else { "-a" }
|
||||||
"/c", "dir"
|
|
||||||
} else {
|
|
||||||
"-a"
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
task.cancel(thread)
|
task.cancel(thread)
|
||||||
|
@ -33,9 +28,10 @@ assert(string.find(result.stdout, "Cargo.toml") ~= nil, "Missing Cargo.toml in o
|
||||||
assert(string.find(result.stdout, ".gitignore") ~= nil, "Missing .gitignore in output")
|
assert(string.find(result.stdout, ".gitignore") ~= nil, "Missing .gitignore in output")
|
||||||
|
|
||||||
-- It should also work the same when spawned using a shell
|
-- It should also work the same when spawned using a shell
|
||||||
|
-- Note that the default on Windows is Powershell which has different flags / behavior
|
||||||
|
|
||||||
local shellResult = process.spawn("ls", {
|
local shellResult = process.spawn("ls", {
|
||||||
if isWindows then "-Force" else "-a"
|
if IS_WINDOWS then "-Force" else "-a",
|
||||||
}, {
|
}, {
|
||||||
shell = true,
|
shell = true,
|
||||||
})
|
})
|
||||||
|
@ -48,22 +44,25 @@ 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, "Cargo.toml") ~= nil, "Missing Cargo.toml in output (shell)")
|
||||||
assert(string.find(shellResult.stdout, ".gitignore") ~= nil, "Missing .gitignore in output (shell)")
|
assert(string.find(shellResult.stdout, ".gitignore") ~= nil, "Missing .gitignore in output (shell)")
|
||||||
|
|
||||||
local pwdCommand = if isWindows then "cmd" else "pwd"
|
local pwdCommand = if IS_WINDOWS then "cmd" else "pwd"
|
||||||
local pwdArgs = if isWindows then { "/c", "cd" } else {}
|
local pwdArgs = if IS_WINDOWS then { "/c", "cd" } else {}
|
||||||
|
|
||||||
-- Make sure the cwd option actually uses the directory we want
|
-- Make sure the cwd option actually uses the directory we want
|
||||||
local rootPwd = process.spawn(pwdCommand, pwdArgs, {
|
local rootPwd = process.spawn(pwdCommand, pwdArgs, {
|
||||||
cwd = "/",
|
cwd = "/",
|
||||||
}).stdout
|
}).stdout
|
||||||
rootPwd = string.gsub(rootPwd, "^%s+", "")
|
rootPwd = string.gsub(rootPwd, "^%s+", "")
|
||||||
rootPwd = string.gsub(rootPwd, "%s+$", "")
|
rootPwd = string.gsub(rootPwd, "%s+$", "")
|
||||||
|
|
||||||
-- Windows: <Drive Letter>:\, Unix: /
|
-- Windows: <Drive Letter>:\, Unix: /
|
||||||
local expectedRootPwd = if isWindows then string.sub(rootPwd, 1, 1) .. ":\\" else "/"
|
local expectedRootPwd = if IS_WINDOWS then string.sub(rootPwd, 1, 1) .. ":\\" else "/"
|
||||||
if rootPwd ~= expectedRootPwd then
|
if rootPwd ~= expectedRootPwd then
|
||||||
error(
|
error(
|
||||||
string.format(
|
string.format(
|
||||||
"Current working directory for child process was not set correctly!"
|
"Current working directory for child process was not set correctly!"
|
||||||
.. "\nExpected '%s', got '%s'",
|
.. "\nExpected '%s', got '%s'",
|
||||||
expectedRootPwd, rootPwd
|
expectedRootPwd,
|
||||||
|
rootPwd
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
@ -89,7 +88,7 @@ local homeDir1 = process.spawn("echo $HOME", nil, {
|
||||||
|
|
||||||
-- Powershell for windows uses `$pwd.Path` instead of `pwd` as pwd would return a PathInfo object,
|
-- 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.
|
-- using $pwd.Path gets the Path property of the PathInfo object.
|
||||||
local homeDir2 = process.spawn(if isWindows then "$pwd.Path" else "pwd", nil, {
|
local homeDir2 = process.spawn(if IS_WINDOWS then "$pwd.Path" else "pwd", nil, {
|
||||||
shell = true,
|
shell = true,
|
||||||
cwd = "~",
|
cwd = "~",
|
||||||
}).stdout
|
}).stdout
|
||||||
|
@ -109,10 +108,8 @@ assert(homeDir1 == homeDir2, "Home dirs did not match when performing tilde subs
|
||||||
local SLEEP_DURATION = 1 / 4
|
local SLEEP_DURATION = 1 / 4
|
||||||
local SLEEP_SAMPLES = 2
|
local SLEEP_SAMPLES = 2
|
||||||
|
|
||||||
-- Windows tend to have a higher execution time
|
-- Unfortunately we
|
||||||
local yieldTolarance = if isWindows then 25 else SLEEP_DURATION * 1.5;
|
local thread2 = task.delay(30, function()
|
||||||
|
|
||||||
local thread2 = task.delay(yieldTolarance, function()
|
|
||||||
stdio.ewrite("Spawning a sleep process should take a reasonable amount of time\n")
|
stdio.ewrite("Spawning a sleep process should take a reasonable amount of time\n")
|
||||||
task.wait(1)
|
task.wait(1)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
|
@ -123,15 +120,15 @@ local sleepCounter = 0
|
||||||
for i = 1, SLEEP_SAMPLES, 1 do
|
for i = 1, SLEEP_SAMPLES, 1 do
|
||||||
task.spawn(function()
|
task.spawn(function()
|
||||||
local args = {
|
local args = {
|
||||||
-- Sleep command on Windows in Seconds has some weird behavior with decimals...
|
-- Sleep command on Windows in Seconds has some weird behavior with decimals ...
|
||||||
tostring(SLEEP_DURATION * (isWindows and 1000 or 1))
|
tostring(SLEEP_DURATION * (IS_WINDOWS and 1000 or 1)),
|
||||||
};
|
}
|
||||||
if isWindows then
|
if IS_WINDOWS then
|
||||||
-- ... so we use milliseconds instead.
|
-- ... so we use milliseconds instead.
|
||||||
table.insert(args, 1, "-Milliseconds");
|
table.insert(args, 1, "-Milliseconds")
|
||||||
end
|
end
|
||||||
-- Windows does not have `sleep` as a process, so we use powershell instead.
|
-- Windows does not have `sleep` as a process, so we use powershell instead.
|
||||||
process.spawn("sleep", args, if isWindows then { shell = true } else nil)
|
process.spawn("sleep", args, if IS_WINDOWS then { shell = true } else nil)
|
||||||
sleepCounter += 1
|
sleepCounter += 1
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
@ -141,31 +138,25 @@ end
|
||||||
|
|
||||||
task.cancel(thread2)
|
task.cancel(thread2)
|
||||||
|
|
||||||
local sleepElapsed = os.clock() - sleepStart
|
|
||||||
|
|
||||||
assert(
|
assert(
|
||||||
sleepElapsed >= SLEEP_DURATION,
|
(os.clock() - sleepStart) >= SLEEP_DURATION,
|
||||||
"Spawning a process that does blocking sleep did not sleep enough"
|
"Spawning a process that does blocking sleep did not sleep enough"
|
||||||
)
|
)
|
||||||
assert(
|
|
||||||
sleepElapsed < yieldTolarance,
|
|
||||||
"Coroutine yielded the main lua thread during process yield"
|
|
||||||
)
|
|
||||||
|
|
||||||
-- Inheriting stdio & environment variables should work
|
-- Inheriting stdio & environment variables should work
|
||||||
|
|
||||||
local echoMessage = "Hello from child process!"
|
local echoMessage = "Hello from child process!"
|
||||||
local echoResult = process.spawn("echo", {
|
local echoResult = process.spawn("echo", {
|
||||||
if isWindows then '"$Env:TEST_VAR"' else '"$TEST_VAR"',
|
if IS_WINDOWS then '"$Env:TEST_VAR"' else '"$TEST_VAR"',
|
||||||
}, {
|
}, {
|
||||||
env = { TEST_VAR = echoMessage },
|
env = { TEST_VAR = echoMessage },
|
||||||
shell = if isWindows then "powershell" else "bash", -- "bash" does not exist on Windows, using "powershell" instead.
|
shell = if IS_WINDOWS then "powershell" else "bash",
|
||||||
stdio = "inherit",
|
stdio = "inherit",
|
||||||
})
|
})
|
||||||
|
|
||||||
-- Windows echo adds a \r before the newline
|
-- Windows echo adds \r\n (CRLF) and unix adds \n (LF)
|
||||||
local trailingAddition = if isWindows then "\r\n" else "\n"
|
local trailingAddition = if IS_WINDOWS then "\r\n" else "\n"
|
||||||
assert(
|
assert(
|
||||||
echoResult.stdout == (echoMessage .. trailingAddition), -- Note that echo adds a newline
|
echoResult.stdout == (echoMessage .. trailingAddition),
|
||||||
"Inheriting stdio did not return proper output"
|
"Inheriting stdio did not return proper output"
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue