Skip to content

Commit

Permalink
fixup: add test cases using process.exitCode
Browse files Browse the repository at this point in the history
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com
  • Loading branch information
daeyeon committed Sep 11, 2022
1 parent 7551f03 commit 5507e2f
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions test/parallel/test-process-exit-code-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

require('../common');

const args = [
// invaild code
const invalids = [
{
code: '',
expected: 1,
Expand Down Expand Up @@ -48,7 +47,8 @@ const args = [
code: NaN,
expected: 1,
},
// vaild code
];
const valids = [
{
code: undefined,
expected: 0,
Expand All @@ -70,32 +70,52 @@ const args = [
expected: 2,
},
];
const args = [...invalids, ...valids];

if (process.argv[2] === undefined) {
const { spawn } = require('node:child_process');
const { inspect, debuglog } = require('node:util');
const { strictEqual } = require('node:assert');
const { throws, strictEqual } = require('node:assert');

const debug = debuglog('test');
const node = process.execPath;
const test = (index) => {
spawn(node, [__filename, index]).on('exit', (code) => {
debug(`actual: ${code}, ${inspect(args[index])}`);
const test = (index, useProcessExitCode) => {
spawn(node, [__filename, index, useProcessExitCode]).on('exit', (code) => {
debug(`actual: ${code}, ${inspect(args[index])} ${!!useProcessExitCode}`);
strictEqual(
code,
args[index].expected,
`actual: ${code}, ${inspect(args[index])}`,
`actual: ${code}, ${inspect(args[index])}`
);
});
};

// Check process.exitCode
for (const arg of invalids) {
debug(`invaild code: ${inspect(arg.code)}`);
throws(() => (process.exitCode = arg.code), Error);
}
for (const arg of valids) {
debug(`vaild code: ${inspect(arg.code)}`);
process.exitCode = arg.code;
}

// Check process.exit([code])
for (const index of args.keys()) {
test(index);
test(index, true);
}
} else {
const index = parseInt(process.argv[2]);
const useProcessExitCode = process.argv[3] !== 'undefined';
if (Number.isNaN(index)) {
return process.exit(100);
}
process.exit(args[index].code);

if (useProcessExitCode) {
process.exitCode = args[index].code;
process.exit();
} else {
process.exit(args[index].code);
}
}

0 comments on commit 5507e2f

Please sign in to comment.