Skip to content

test: improve test error message logs #23694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this wasn't actually done before? Creating the error with the stack trace at this point is the primary change. Otherwise the stack trace is always just the child_process exit/error stack which is useless.

const error = new Error();

// Return log info about the current process status
function envDump() {
return [
`ENV:${JSON.stringify(spawnOptions.env, null, 2)}`,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also changed but is a bit hard to see. It now outputs the env of the process being spawned, not the parent process - spawnOptions.env isntead of process.env. I think that is far more useful and probably the original intention?

`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`,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note both the code (formerly error) and the envDump() were not here previously.

);
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