Skip to content

Commit 7aa0136

Browse files
committed
test: only show overridden env in child process failures
Previously when the child process helpers are used to print information about the failed expectations and the env of the child process was overridden, it printed the entire env object, which may be too much if the caller does something like { env: { ENV: 'var', ...process.env } } (which tend to be always the case for specifying env because we need to copy the process.env for dynamic library loading in the CI). This updates it to only show the env vars that differ from process.env for a cleaner log in the case of failure.
1 parent fdcf4d9 commit 7aa0136

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

test/common/child_process.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,22 @@ function expectSyncExit(caller, spawnArgs, {
8686
console.error(`${tag} status = ${child.status}, signal = ${child.signal}`);
8787

8888
const error = new Error(`${failures.join('\n')}`);
89-
if (spawnArgs[2]) {
90-
error.options = spawnArgs[2];
89+
if (typeof spawnArgs[2] === 'object' && spawnArgs[2] !== null) {
90+
const envInOptions = spawnArgs[2].env;
91+
// If the env is overridden in the spawn options, include it in the error
92+
// object for easier debugging.
93+
if (typeof envInOptions === 'object' && envInOptions !== null && envInOptions !== process.env) {
94+
// Only include the environment variables that are different from
95+
// the current process.env to avoid cluttering the output.
96+
error.options = { ...spawnArgs[2], env: {} };
97+
for (const key of Object.keys(envInOptions)) {
98+
if (envInOptions[key] !== process.env[key]) {
99+
error.options.env[key] = spawnArgs[2].env[key];
100+
}
101+
}
102+
} else {
103+
error.options = spawnArgs[2];
104+
}
91105
}
92106
let command = spawnArgs[0];
93107
if (Array.isArray(spawnArgs[1])) {

0 commit comments

Comments
 (0)