Skip to content

Commit

Permalink
test: improve test error message logs
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard authored and clydin committed Aug 9, 2022
1 parent 167887d commit 9171543
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
44 changes: 25 additions & 19 deletions tests/legacy-cli/e2e/utils/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,39 +96,42 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
_processes.push(childProcess);

// Create the error here so the stack shows who called this function.
const error = new Error();

// Return log info about the current process status
function envDump() {
return [
`ENV:${JSON.stringify(spawnOptions.env, null, 2)}`,
`STDOUT:\n${stdout}`,
`STDERR:\n${stderr}`,
].join('\n\n');
}

return new Promise((resolve, reject) => {
return new Promise<ProcessOutput>((resolve, reject) => {
let matched = false;

childProcess.on('exit', (error: any) => {
childProcess.on('exit', (code: number) => {
_processes = _processes.filter((p) => p !== childProcess);

if (options.waitForMatch && !matched) {
error = `Output didn't match '${options.waitForMatch}'.`;
reject(
`Process output didn't match - "${cmd} ${args.join(' ')}": '${
options.waitForMatch
}': ${code}...\n\n${envDump()}\n`,
);
return;
}

if (!error) {
if (!code) {
resolve({ stdout, stderr });
return;
}

reject(
new Error(
`Running "${cmd} ${args.join(' ')}" returned error. ${error}...\n\nENV:${JSON.stringify(
process.env,
null,
2,
)}\n\nSTDOUT:\n${stdout}\n\nSTDERR:\n${stderr}\n`,
),
);
reject(`Process exit error - "${cmd} ${args.join(' ')}": ${code}...\n\n${envDump()}\n`);
});

childProcess.on('error', (err) => {
err.message += `${err}...\n\nENV:${JSON.stringify(
process.env,
null,
2,
)}\n\nSTDOUT:\n${stdout}\n\nSTDERR:\n${stderr}\n`;
reject(err);
reject(`Process error - "${cmd} ${args.join(' ')}": ${err}...\n\n${envDump()}\n`);
});

if (options.waitForMatch) {
Expand All @@ -154,6 +157,9 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<Proce
childProcess.stdin!.write(options.stdin);
childProcess.stdin!.end();
}
}).catch((err) => {
error.message = err.toString();
return Promise.reject(error);
});
}

Expand Down
3 changes: 2 additions & 1 deletion tests/legacy-cli/e2e/utils/test_process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const testFunction: () => Promise<void> | void =
try {
await testFunction();
} catch (e) {
console.error(e);
console.error('Test Process error', e);
console.error(`ENV:${JSON.stringify(process.env, null, 2)}`);
process.exitCode = -1;
} finally {
await killAllProcesses();
Expand Down

0 comments on commit 9171543

Please sign in to comment.