Skip to content

Commit

Permalink
test: improve output of child process utilities
Browse files Browse the repository at this point in the history
- Display command and options when it fails
- Keep the caller line at the top of the stack trace.

PR-URL: #54622
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
  • Loading branch information
joyeecheung authored and aduh95 committed Sep 12, 2024
1 parent 754baa4 commit fdb6511
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions test/common/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ function checkOutput(str, check) {
return { passed: true };
}

function expectSyncExit(child, {
function expectSyncExit(caller, spawnArgs, {
status,
signal,
stderr: stderrCheck,
stdout: stdoutCheck,
trim = false,
}) {
const child = spawnSync(...spawnArgs);
const failures = [];
let stderrStr, stdoutStr;
if (status !== undefined && child.status !== status) {
Expand All @@ -83,7 +84,18 @@ function expectSyncExit(child, {
console.error(`${tag} --- stdout ---`);
console.error(stdoutStr === undefined ? child.stdout.toString() : stdoutStr);
console.error(`${tag} status = ${child.status}, signal = ${child.signal}`);
throw new Error(`${failures.join('\n')}`);

const error = new Error(`${failures.join('\n')}`);
if (spawnArgs[2]) {
error.options = spawnArgs[2];
}
let command = spawnArgs[0];
if (Array.isArray(spawnArgs[1])) {
command += ' ' + spawnArgs[1].join(' ');
}
error.command = command;
Error.captureStackTrace(error, caller);
throw error;
}

// If status and signal are not matching expectations, fail early.
Expand Down Expand Up @@ -114,21 +126,19 @@ function expectSyncExit(child, {
function spawnSyncAndExit(...args) {
const spawnArgs = args.slice(0, args.length - 1);
const expectations = args[args.length - 1];
const child = spawnSync(...spawnArgs);
return expectSyncExit(child, expectations);
return expectSyncExit(spawnSyncAndExit, spawnArgs, expectations);
}

function spawnSyncAndExitWithoutError(...args) {
return expectSyncExit(spawnSync(...args), {
return expectSyncExit(spawnSyncAndExitWithoutError, [...args], {
status: 0,
signal: null,
});
}

function spawnSyncAndAssert(...args) {
const expectations = args.pop();
const child = spawnSync(...args);
return expectSyncExit(child, {
return expectSyncExit(spawnSyncAndAssert, [...args], {
status: 0,
signal: null,
...expectations,
Expand Down

0 comments on commit fdb6511

Please sign in to comment.