Skip to content

Commit f77409a

Browse files
authored
Prevent exit before stdout is drained (#4983)
* Fix error handling in CLI tests * Ensure process.stdout is closed before exiting
1 parent 7423175 commit f77409a

File tree

2 files changed

+54
-64
lines changed

2 files changed

+54
-64
lines changed

cli/cli.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ if (command.help || (process.argv.length <= 2 && process.stdin.isTTY)) {
2121
} catch {
2222
// do nothing
2323
}
24-
25-
run(command).then(() => process.exit(0));
24+
run(command).then(() => {
25+
process.stdout.on('finish', () => process.exit(0));
26+
process.stdout.end();
27+
});
2628
}

test/cli/index.js

Lines changed: 50 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -58,88 +58,76 @@ async function runTest(config, command) {
5858
killSignal: 'SIGKILL'
5959
},
6060
async (error, code, stderr) => {
61-
if (config.after) {
62-
await config.after(error, code, stderr);
63-
}
64-
if (error && !error.killed) {
65-
if (config.error) {
66-
if (!config.error(error)) {
67-
return resolve();
61+
try {
62+
if (config.after) {
63+
await config.after(error, code, stderr);
64+
}
65+
if (error && !error.killed) {
66+
if (config.error) {
67+
if (!config.error(error)) {
68+
return resolve();
69+
}
70+
} else {
71+
return reject(error);
6872
}
69-
} else {
70-
return reject(error);
7173
}
72-
}
73-
if (childProcess.signalCode === 'SIGKILL') {
74-
return reject(new Error('Test aborted due to timeout.'));
75-
}
74+
if (childProcess.signalCode === 'SIGKILL') {
75+
return reject(new Error('Test aborted due to timeout.'));
76+
}
7677

77-
if ('stderr' in config) {
78-
const shouldContinue = config.stderr(stderr);
79-
if (!shouldContinue) return resolve();
80-
} else if (stderr) {
81-
console.error(stderr);
82-
}
78+
if ('stderr' in config) {
79+
const shouldContinue = config.stderr(stderr);
80+
if (!shouldContinue) return resolve();
81+
} else if (stderr) {
82+
console.error(stderr);
83+
}
8384

84-
let unintendedError;
85+
let unintendedError;
8586

86-
if (config.execute) {
87-
try {
88-
const function_ = new Function('require', 'module', 'exports', 'assert', code);
89-
const module = {
90-
exports: {}
91-
};
92-
function_(require, module, module.exports, assert);
87+
if (config.execute) {
88+
try {
89+
const function_ = new Function('require', 'module', 'exports', 'assert', code);
90+
const module = {
91+
exports: {}
92+
};
93+
function_(require, module, module.exports, assert);
9394

94-
if (config.error) {
95-
unintendedError = new Error('Expected an error while executing output');
96-
}
95+
if (config.error) {
96+
unintendedError = new Error('Expected an error while executing output');
97+
}
9798

98-
if (config.exports) {
99-
await config.exports(module.exports);
99+
if (config.exports) {
100+
config.exports(module.exports);
101+
}
102+
} catch (error) {
103+
if (config.error) {
104+
config.error(error);
105+
} else {
106+
unintendedError = error;
107+
}
100108
}
101-
} catch (error) {
102-
if (config.error) {
103-
config.error(error);
104-
} else {
105-
unintendedError = error;
106-
}
107-
}
108109

109-
if (config.show || unintendedError) {
110-
console.log(code + '\n\n\n');
111-
}
110+
if (config.show || unintendedError) {
111+
console.log(code + '\n\n\n');
112+
}
112113

113-
if (config.solo) console.groupEnd();
114+
if (config.solo) console.groupEnd();
114115

115-
return unintendedError ? reject(unintendedError) : resolve();
116-
}
117-
if (config.result) {
118-
try {
116+
return unintendedError ? reject(unintendedError) : resolve();
117+
}
118+
if (config.result) {
119119
config.result(code);
120120
return resolve();
121-
} catch (error) {
122-
return reject(error);
123121
}
124-
}
125-
if (config.test) {
126-
try {
122+
if (config.test) {
127123
config.test();
128124
return resolve();
129-
} catch (error) {
130-
return reject(error);
131125
}
132-
}
133-
if (existsSync('_expected') && statSync('_expected').isDirectory()) {
134-
try {
126+
if (existsSync('_expected') && statSync('_expected').isDirectory()) {
135127
assertDirectoriesAreEqual('_actual', '_expected');
136128
return resolve();
137-
} catch (error) {
138-
return reject(error);
139129
}
140-
}
141-
const expected = readFileSync('_expected.js', 'utf8');
142-
try {
130+
const expected = readFileSync('_expected.js', 'utf8');
143131
assert.equal(normaliseOutput(code), normaliseOutput(expected));
144132
return resolve();
145133
} catch (error) {

0 commit comments

Comments
 (0)