Avoid spawning a shell when running node in test (#155)

`execSync` by default spawns a shell to exec the specified command. And,
without specifying an absolute path to the node binary, it will be
searched in `PATH` where yarn prepends a temporary directory containing
a shell script called **node** that wraps the original node binary with
exec, resulting in implicitly chaining a shell again.
On some Linux distros, the default non-interactive shell, such as the
Dash shell of Debian, silently discards dashes (`-`) in names of
environment variables. But GitHub Actions allows dashes in names of
input variables which are then converted to environment variables in the
form of `INPUT_<VARIABLE_NAME>`.
So the commit fix the error-prone test code by using execFileSync, which
by default exec directly without spawning a shell, to run node with its
absolute path.
This commit is contained in:
Hung-I Wang 2020-11-20 19:28:46 +08:00 committed by GitHub
parent 2ba3761c6b
commit 2525b376e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,9 +19,10 @@ test('wait 500 ms', async () => {
// shows how the runner will run a javascript action with env / stdout protocol
test('test runs', () => {
process.env['INPUT_MILLISECONDS'] = '500'
const np = process.execPath
const ip = path.join(__dirname, '..', 'lib', 'main.js')
const options: cp.ExecSyncOptions = {
const options: cp.ExecFileSyncOptions = {
env: process.env
}
console.log(cp.execSync(`node ${ip}`, options).toString())
console.log(cp.execFileSync(np, [ip], options).toString())
})