(caller, spawnArgs, {
status,
signal,
stderr: stderrCheck,
stdout: stdoutCheck,
trim = false,
})
| 61 | } |
| 62 | |
| 63 | function expectSyncExit(caller, spawnArgs, { |
| 64 | status, |
| 65 | signal, |
| 66 | stderr: stderrCheck, |
| 67 | stdout: stdoutCheck, |
| 68 | trim = false, |
| 69 | }) { |
| 70 | const child = spawnSync(...spawnArgs); |
| 71 | const failures = []; |
| 72 | let stderrStr, stdoutStr; |
| 73 | if (status !== undefined && child.status !== status) { |
| 74 | failures.push(`- process terminated with status ${child.status}, expected ${status}`); |
| 75 | } |
| 76 | if (signal !== undefined && child.signal !== signal) { |
| 77 | failures.push(`- process terminated with signal ${child.signal}, expected ${signal}`); |
| 78 | } |
| 79 | |
| 80 | function logAndThrow() { |
| 81 | const tag = `[process ${child.pid}]:`; |
| 82 | console.error(`${tag} --- stderr ---`); |
| 83 | console.error(stderrStr === undefined ? child.stderr.toString() : stderrStr); |
| 84 | console.error(`${tag} --- stdout ---`); |
| 85 | console.error(stdoutStr === undefined ? child.stdout.toString() : stdoutStr); |
| 86 | console.error(`${tag} status = ${child.status}, signal = ${child.signal}`); |
| 87 | |
| 88 | const error = new Error(`${failures.join('\n')}`); |
| 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 | } |
| 105 | } |
| 106 | let command = spawnArgs[0]; |
| 107 | if (Array.isArray(spawnArgs[1])) { |
| 108 | command += ' ' + spawnArgs[1].join(' '); |
| 109 | } |
| 110 | error.command = command; |
| 111 | Error.captureStackTrace(error, caller); |
| 112 | throw error; |
| 113 | } |
| 114 | |
| 115 | // If status and signal are not matching expectations, fail early. |
| 116 | if (failures.length !== 0) { |
| 117 | logAndThrow(); |
| 118 | } |
| 119 | |
| 120 | if (stderrCheck !== undefined) { |
no test coverage detected
searching dependent graphs…