2023-10-06 02:53:29 +01:00
|
|
|
local process = require("@lune/process")
|
|
|
|
|
|
|
|
local IS_WINDOWS = process.os == "windows"
|
|
|
|
|
|
|
|
local pwdCommand = if IS_WINDOWS then "cmd" else "pwd"
|
|
|
|
local pwdArgs = if IS_WINDOWS then { "/c", "cd" } else {}
|
|
|
|
|
|
|
|
-- Make sure the cwd option actually uses the directory we want
|
2024-10-16 20:48:12 +01:00
|
|
|
local rootPwd = process.exec(pwdCommand, pwdArgs, {
|
2023-10-06 02:53:29 +01:00
|
|
|
cwd = "/",
|
|
|
|
}).stdout
|
|
|
|
rootPwd = string.gsub(rootPwd, "^%s+", "")
|
|
|
|
rootPwd = string.gsub(rootPwd, "%s+$", "")
|
|
|
|
|
|
|
|
-- Windows: <Drive Letter>:\, Unix: /
|
|
|
|
local expectedRootPwd = if IS_WINDOWS then string.sub(rootPwd, 1, 1) .. ":\\" else "/"
|
|
|
|
if rootPwd ~= expectedRootPwd then
|
|
|
|
error(
|
|
|
|
string.format(
|
|
|
|
"Current working directory for child process was not set correctly!"
|
|
|
|
.. "\nExpected '%s', got '%s'",
|
|
|
|
expectedRootPwd,
|
|
|
|
rootPwd
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Setting cwd should not change the cwd of this process
|
|
|
|
|
2024-10-16 20:48:12 +01:00
|
|
|
local pwdBefore = process.exec(pwdCommand, pwdArgs).stdout
|
|
|
|
process.exec("ls", {}, {
|
2023-10-06 02:53:29 +01:00
|
|
|
cwd = "/",
|
|
|
|
shell = true,
|
|
|
|
})
|
2024-10-16 20:48:12 +01:00
|
|
|
local pwdAfter = process.exec(pwdCommand, pwdArgs).stdout
|
2023-10-06 02:53:29 +01:00
|
|
|
assert(pwdBefore == pwdAfter, "Current working directory changed after running child process")
|
|
|
|
|
|
|
|
-- Setting the cwd on a child process should properly
|
|
|
|
-- replace any leading ~ with the users real home dir
|
|
|
|
|
2024-10-16 20:48:12 +01:00
|
|
|
local homeDir1 = process.exec("echo $HOME", nil, {
|
2023-10-06 02:53:29 +01:00
|
|
|
shell = true,
|
|
|
|
}).stdout
|
|
|
|
|
|
|
|
-- NOTE: 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
|
2024-10-16 20:48:12 +01:00
|
|
|
local homeDir2 = process.exec(if IS_WINDOWS then "$pwd.Path" else "pwd", nil, {
|
2023-10-06 02:53:29 +01: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")
|