Skip to content

Commit 99566ae

Browse files
joyeecheungtargos
authored andcommittedOct 2, 2024
test: improve output of child process utilities
- 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>

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed
 

Diff for: ‎test/common/child_process.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@ function checkOutput(str, check) {
6060
return { passed: true };
6161
}
6262

63-
function expectSyncExit(child, {
63+
function expectSyncExit(caller, spawnArgs, {
6464
status,
6565
signal,
6666
stderr: stderrCheck,
6767
stdout: stdoutCheck,
6868
trim = false,
6969
}) {
70+
const child = spawnSync(...spawnArgs);
7071
const failures = [];
7172
let stderrStr, stdoutStr;
7273
if (status !== undefined && child.status !== status) {
@@ -83,7 +84,18 @@ function expectSyncExit(child, {
8384
console.error(`${tag} --- stdout ---`);
8485
console.error(stdoutStr === undefined ? child.stdout.toString() : stdoutStr);
8586
console.error(`${tag} status = ${child.status}, signal = ${child.signal}`);
86-
throw new Error(`${failures.join('\n')}`);
87+
88+
const error = new Error(`${failures.join('\n')}`);
89+
if (spawnArgs[2]) {
90+
error.options = spawnArgs[2];
91+
}
92+
let command = spawnArgs[0];
93+
if (Array.isArray(spawnArgs[1])) {
94+
command += ' ' + spawnArgs[1].join(' ');
95+
}
96+
error.command = command;
97+
Error.captureStackTrace(error, caller);
98+
throw error;
8799
}
88100

89101
// If status and signal are not matching expectations, fail early.
@@ -114,21 +126,19 @@ function expectSyncExit(child, {
114126
function spawnSyncAndExit(...args) {
115127
const spawnArgs = args.slice(0, args.length - 1);
116128
const expectations = args[args.length - 1];
117-
const child = spawnSync(...spawnArgs);
118-
return expectSyncExit(child, expectations);
129+
return expectSyncExit(spawnSyncAndExit, spawnArgs, expectations);
119130
}
120131

121132
function spawnSyncAndExitWithoutError(...args) {
122-
return expectSyncExit(spawnSync(...args), {
133+
return expectSyncExit(spawnSyncAndExitWithoutError, [...args], {
123134
status: 0,
124135
signal: null,
125136
});
126137
}
127138

128139
function spawnSyncAndAssert(...args) {
129140
const expectations = args.pop();
130-
const child = spawnSync(...args);
131-
return expectSyncExit(child, {
141+
return expectSyncExit(spawnSyncAndAssert, [...args], {
132142
status: 0,
133143
signal: null,
134144
...expectations,

0 commit comments

Comments
 (0)
Please sign in to comment.