Skip to content

Commit

Permalink
Prevent exit before stdout is drained (#4983)
Browse files Browse the repository at this point in the history
* Fix error handling in CLI tests

* Ensure process.stdout is closed before exiting
  • Loading branch information
lukastaegert authored May 9, 2023
1 parent 7423175 commit f77409a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 64 deletions.
6 changes: 4 additions & 2 deletions cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ if (command.help || (process.argv.length <= 2 && process.stdin.isTTY)) {
} catch {
// do nothing
}

run(command).then(() => process.exit(0));
run(command).then(() => {
process.stdout.on('finish', () => process.exit(0));
process.stdout.end();
});
}
112 changes: 50 additions & 62 deletions test/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,88 +58,76 @@ async function runTest(config, command) {
killSignal: 'SIGKILL'
},
async (error, code, stderr) => {
if (config.after) {
await config.after(error, code, stderr);
}
if (error && !error.killed) {
if (config.error) {
if (!config.error(error)) {
return resolve();
try {
if (config.after) {
await config.after(error, code, stderr);
}
if (error && !error.killed) {
if (config.error) {
if (!config.error(error)) {
return resolve();
}
} else {
return reject(error);
}
} else {
return reject(error);
}
}
if (childProcess.signalCode === 'SIGKILL') {
return reject(new Error('Test aborted due to timeout.'));
}
if (childProcess.signalCode === 'SIGKILL') {
return reject(new Error('Test aborted due to timeout.'));
}

if ('stderr' in config) {
const shouldContinue = config.stderr(stderr);
if (!shouldContinue) return resolve();
} else if (stderr) {
console.error(stderr);
}
if ('stderr' in config) {
const shouldContinue = config.stderr(stderr);
if (!shouldContinue) return resolve();
} else if (stderr) {
console.error(stderr);
}

let unintendedError;
let unintendedError;

if (config.execute) {
try {
const function_ = new Function('require', 'module', 'exports', 'assert', code);
const module = {
exports: {}
};
function_(require, module, module.exports, assert);
if (config.execute) {
try {
const function_ = new Function('require', 'module', 'exports', 'assert', code);
const module = {
exports: {}
};
function_(require, module, module.exports, assert);

if (config.error) {
unintendedError = new Error('Expected an error while executing output');
}
if (config.error) {
unintendedError = new Error('Expected an error while executing output');
}

if (config.exports) {
await config.exports(module.exports);
if (config.exports) {
config.exports(module.exports);
}
} catch (error) {
if (config.error) {
config.error(error);
} else {
unintendedError = error;
}
}
} catch (error) {
if (config.error) {
config.error(error);
} else {
unintendedError = error;
}
}

if (config.show || unintendedError) {
console.log(code + '\n\n\n');
}
if (config.show || unintendedError) {
console.log(code + '\n\n\n');
}

if (config.solo) console.groupEnd();
if (config.solo) console.groupEnd();

return unintendedError ? reject(unintendedError) : resolve();
}
if (config.result) {
try {
return unintendedError ? reject(unintendedError) : resolve();
}
if (config.result) {
config.result(code);
return resolve();
} catch (error) {
return reject(error);
}
}
if (config.test) {
try {
if (config.test) {
config.test();
return resolve();
} catch (error) {
return reject(error);
}
}
if (existsSync('_expected') && statSync('_expected').isDirectory()) {
try {
if (existsSync('_expected') && statSync('_expected').isDirectory()) {
assertDirectoriesAreEqual('_actual', '_expected');
return resolve();
} catch (error) {
return reject(error);
}
}
const expected = readFileSync('_expected.js', 'utf8');
try {
const expected = readFileSync('_expected.js', 'utf8');
assert.equal(normaliseOutput(code), normaliseOutput(expected));
return resolve();
} catch (error) {
Expand Down

0 comments on commit f77409a

Please sign in to comment.