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.

PR-URL: https://github.com/nodejs/node/pull/60556
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Joyee Cheung 2025-11-07 18:47:33 +01:00 committed by GitHub
parent 572cc6fa55
commit 7c85846cd9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -86,8 +86,22 @@ function expectSyncExit(caller, spawnArgs, {
console.error(`${tag} status = ${child.status}, signal = ${child.signal}`);
const error = new Error(`${failures.join('\n')}`);
if (spawnArgs[2]) {
error.options = spawnArgs[2];
if (typeof spawnArgs[2] === 'object' && spawnArgs[2] !== null) {
const envInOptions = spawnArgs[2].env;
// If the env is overridden in the spawn options, include it in the error
// object for easier debugging.
if (typeof envInOptions === 'object' && envInOptions !== null && envInOptions !== process.env) {
// Only include the environment variables that are different from
// the current process.env to avoid cluttering the output.
error.options = { ...spawnArgs[2], env: {} };
for (const key of Object.keys(envInOptions)) {
if (envInOptions[key] !== process.env[key]) {
error.options.env[key] = spawnArgs[2].env[key];
}
}
} else {
error.options = spawnArgs[2];
}
}
let command = spawnArgs[0];
if (Array.isArray(spawnArgs[1])) {