Skip to content

Commit

Permalink
chore: better process handling
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando committed Oct 24, 2024
1 parent f46c16c commit 4101153
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,27 @@ function msToHumanDuration(ms) {
return human.join(' ');
}

/** @type {Array<import('child_process').ChildProcessWithoutNullStreams>} */
const activeProcesses = [];

function runTest(file) {
return new Promise((resolve, reject) => {
const process = spawn('node', [path.basename(file), target], {
cwd: path.dirname(file),
stdio: 'pipe',
});

activeProcesses.push(process);

const removeProcess = () => {
const index = activeProcesses.indexOf(process);
if (index !== -1) {
activeProcesses.splice(index, 1);
}
};

process.on('close', (code) => {
removeProcess();
if (code !== 0) {
reject(new Error(`Process exited with code ${code}`));
} else {
Expand All @@ -127,6 +141,7 @@ function runTest(file) {
});

process.on('error', (error) => {
removeProcess();
error.logOutput = `${error.message}\n${output.join('')}`;
reject(error);
});
Expand Down Expand Up @@ -209,4 +224,13 @@ async function run() {
}
}

function cleanup() {
for (const process of activeProcesses) {
process.kill();
}
}

process.on('SIGINT', cleanup);
process.on('SIGTERM', cleanup);

run();

0 comments on commit 4101153

Please sign in to comment.