Skip to content

Commit

Permalink
Ensure full e2e test output is printed
Browse files Browse the repository at this point in the history
  • Loading branch information
davidje13 committed Jul 25, 2024
1 parent b31cab8 commit 1c8f639
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 33 deletions.
6 changes: 3 additions & 3 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions e2e/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 45 additions & 27 deletions scripts/helpers/proc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export async function exitWithCode(code, message) {
if (message) {
process.stderr.write(`\n${message}\n`);
}
// process.exit may lose stream data which has been buffered in NodeJS - wait for it all to be flushed before exiting
await Promise.all([
new Promise((resolve) => process.stdout.write('', resolve)),
new Promise((resolve) => process.stderr.write('', resolve)),
]);
process.exit(code);
}

Expand Down Expand Up @@ -60,16 +65,19 @@ export function propagateStreamWithPrefix(
target.write(data.subarray(begin));
target.write(suf);
} else {
data.copy(curLine, 0, begin);
curLineP = data.length - begin;
data.copy(curLine, curLineP, begin);
curLineP += data.length - begin;
}
});
stream.on('close', () => {
if (curLineP > 0) {
target.write(pre);
target.write(curLine.subarray(0, curLineP));
target.write(suf);
}
return new Promise((resolve) => {
stream.on('close', () => {
if (curLineP > 0) {
target.write(pre);
target.write(curLine.subarray(0, curLineP));
target.write(suf);
}
resolve();
});
});
}

Expand Down Expand Up @@ -128,6 +136,9 @@ export function waitForOutput(stream, search, timeout = 60 * 60 * 1000) {
}
});
stream.on('close', () => {
if (!byteSearch.length) {
resolve();
}
if (!found) {
clearTimeout(tm);
reject(new Error('closed'));
Expand Down Expand Up @@ -156,7 +167,7 @@ export function runTask({
outputMode = 'live',
beginMessage = '',
successMessage = '',
failureMessage = '',
failureMessage = `failure in ${outputPrefix || command}`,
exitOnFailure = true,
...options
}) {
Expand All @@ -175,24 +186,28 @@ export function runTask({
const proc = spawn(command, args, { ...options, stdio });
activeChildren.add(proc);
let printInfo = () => undefined;
let streamsClosed = Promise.resolve();
if (outputMode === 'live') {
if (outputPrefix) {
propagateStreamWithPrefix(
output,
proc.stdio[1],
outputPrefix,
prefixFormat,
);
propagateStreamWithPrefix(
output,
proc.stdio[2],
outputPrefix,
prefixFormat,
);
streamsClosed = Promise.all([
propagateStreamWithPrefix(
output,
proc.stdio[1],
outputPrefix,
prefixFormat,
),
propagateStreamWithPrefix(
output,
proc.stdio[2],
outputPrefix,
prefixFormat,
),
]);
}
} else {
const s1 = waitForOutput(proc.stdio[1], '');
const s2 = waitForOutput(proc.stdio[2], '');
streamsClosed = Promise.all([s1.promise, s2.promise]);
printInfo = () => {
output.write(`\nexit code: ${proc.exitCode}\n`);
const v1 = s1.getOutput();
Expand All @@ -207,12 +222,13 @@ export function runTask({
}
};
}
const wrappedResolve = (v) => {
const wrappedResolve = async (v) => {
activeChildren.delete(proc);
if (shuttingDown || handled) {
return;
}
handled = true;
await streamsClosed;
if (successMessage) {
output.write(`${successMessage}\n`);
}
Expand All @@ -221,15 +237,14 @@ export function runTask({
}
resolve(v);
};
const wrappedReject = (e) => {
const wrappedReject = async (e) => {
activeChildren.delete(proc);
if (shuttingDown || handled) {
return;
}
handled = true;
if (failureMessage) {
output.write(`${failureMessage}\n`);
}
await streamsClosed;
output.write(`${failureMessage} - ${e instanceof Error ? e.message : e}\n`);
if (outputMode === 'atomic' || outputMode === 'fail_atomic') {
printInfo();
}
Expand All @@ -239,7 +254,10 @@ export function runTask({
reject(e);
}
};
proc.on('error', wrappedReject);
proc.on('error', (e) => {
streamsClosed = Promise.resolve(); // process did not start, so do not wait for streams to close
wrappedReject(e);
});
proc.on('exit', handleExit(wrappedResolve, wrappedReject));
});
}
Expand Down
1 change: 1 addition & 0 deletions scripts/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ try {
cwd: join(basedir, 'e2e'),
env: { ...testEnv, SELENIUM_BROWSER: browser },
beginMessage: `E2E testing in ${browser}...`,
failureMessage: `E2E tests failed in ${browser}`,
outputPrefix: browser,
prefixFormat: format,
exitOnFailure: false,
Expand Down

0 comments on commit 1c8f639

Please sign in to comment.