From 7aa0136e7528ee24c265cc0638d23e994c7134c2 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sun, 2 Nov 2025 23:02:52 +0100 Subject: [PATCH] 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. --- test/common/child_process.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/test/common/child_process.js b/test/common/child_process.js index 6c2bc6c9614af3..d8c957bdf09ce4 100644 --- a/test/common/child_process.js +++ b/test/common/child_process.js @@ -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])) {