From 41011537dff886df10f52d9b24b42e4de5d3043f Mon Sep 17 00:00:00 2001 From: Daniel Lando Date: Thu, 24 Oct 2024 09:26:55 +0200 Subject: [PATCH] chore: better process handling --- test/test.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/test.js b/test/test.js index 2a834265..bdf5e910 100644 --- a/test/test.js +++ b/test/test.js @@ -102,13 +102,27 @@ function msToHumanDuration(ms) { return human.join(' '); } +/** @type {Array} */ +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 { @@ -127,6 +141,7 @@ function runTest(file) { }); process.on('error', (error) => { + removeProcess(); error.logOutput = `${error.message}\n${output.join('')}`; reject(error); }); @@ -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();