Skip to content

Commit 99566ae

Browse files
joyeecheungtargos
authored andcommitted
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>
1 parent 87f78a3 commit 99566ae

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

‎test/common/child_process.js‎

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

63-
functionexpectSyncExit(child,{
63+
functionexpectSyncExit(caller,spawnArgs,{
6464
status,
6565
signal,
6666
stderr: stderrCheck,
6767
stdout: stdoutCheck,
6868
trim =false,
6969
}){
70+
constchild=spawnSync(...spawnArgs);
7071
constfailures=[];
7172
letstderrStr,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-
thrownewError(`${failures.join('\n')}`);
87+
88+
consterror=newError(`${failures.join('\n')}`);
89+
if(spawnArgs[2]){
90+
error.options=spawnArgs[2];
91+
}
92+
letcommand=spawnArgs[0];
93+
if(Array.isArray(spawnArgs[1])){
94+
command+=' '+spawnArgs[1].join(' ');
95+
}
96+
error.command=command;
97+
Error.captureStackTrace(error,caller);
98+
throwerror;
8799
}
88100

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

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

128139
functionspawnSyncAndAssert(...args){
129140
constexpectations=args.pop();
130-
constchild=spawnSync(...args);
131-
returnexpectSyncExit(child,{
141+
returnexpectSyncExit(spawnSyncAndAssert,[...args],{
132142
status: 0,
133143
signal: null,
134144
...expectations,

0 commit comments

Comments
 (0)