Skip to content

Commit

Permalink
Ensure errors doesn't result in failures during cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kraenhansen committed Mar 29, 2024
1 parent 40f0e22 commit e8467b4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
4 changes: 2 additions & 2 deletions packages/cli/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ describe("Mocha Remote CLI", () => {
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "mocha-remote-cli-test-"));
const outFile = path.join(tempDir, "out.txt");
expect(fs.existsSync(outFile)).equals(false, "Expected the outfile to be missing");
const cliProcess = spawnCli("--port", "0", "--context", `endlessLoop,outFile=${outFile}`, "--", "tsx", "src/test/hanging-client.ts");
const cliProcess = spawnCli("--port", "0", "--exit-on-error", "--context", `endlessLoop,outFile=${outFile}`, "--", "tsx", "src/test/hanging-client.ts");

const outFileContent = await waitForFile(outFile);
const success = cliProcess.kill("SIGINT"); // Interrupt Mocha Remote CLI
Expand All @@ -175,7 +175,7 @@ describe("Mocha Remote CLI", () => {
const outFile = path.join(tempDir, "out.txt");
expect(fs.existsSync(outFile)).equals(false, "Expected the outfile to be missing");
// Using spawnCli because it ignores stdout, which makes the spawnSync hang for some reason
const cliProcess = spawnCli("--port", "0", "--context", `outFile=${outFile}`, "--", "tsx", "src/test/hanging-client.ts");
const cliProcess = spawnCli("--port", "0", "--exit-on-error", "--context", `outFile=${outFile}`, "--", "tsx", "src/test/hanging-client.ts");
await new Promise<void>(resolve => cliProcess.once("close", resolve));

expect(cliProcess.exitCode).equals(0, "Expected signal of the mocha-remote cli to remain a success");
Expand Down
26 changes: 12 additions & 14 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ function cleanup() {
cleanupTasks.clear();
// Execute a chain of promises
tasks.reduce((previous, task) => previous.then(task), Promise.resolve()).catch(err => {
// eslint-disable-next-line no-console
console.error(`Failed while cleaning up: ${err}`);
/* eslint-disable-next-line no-console */
console.error(chalk.red("ERROR"), `Failed to clean up: ${err.message}`);
});
}

Expand Down Expand Up @@ -80,11 +80,9 @@ type ServerOptions = {

// Start the server
export async function startServer({ log, server, command, exitOnError }: ServerOptions): Promise<void> {
cleanupTasks.add(async () => {
if (server.listening) {
await server.stop();
log("🧹 Stopped the server");
}
cleanupTasks.add(() => {
// Ensure that errors during cleanup doesn't result in a failure
exitOnError = false;
});

server.on('started', server => {
Expand Down Expand Up @@ -139,15 +137,15 @@ export async function startServer({ log, server, command, exitOnError }: ServerO
}
});

await server.start();
// User stopping the process in a terminal
process.on("SIGINT", () => {
server.stop().then(undefined, err => {
/* eslint-disable-next-line no-console */
console.error(chalk.red("ERROR"), `Failed to stop server: ${err.message}`);
});
cleanupTasks.add(async () => {
if (server.listening) {
await server.stop();
log("🧹 Stopped the server");
}
});

await server.start();

if (command && command.length > 0) {
const [commandName, ...args] = command;
const commandProcess = cp.spawn(commandName, args, {
Expand Down

0 comments on commit e8467b4

Please sign in to comment.