From 5507e2fa44cd164bce200a0829b37339db5e9cf2 Mon Sep 17 00:00:00 2001 From: Daeyeon Jeong Date: Sun, 11 Sep 2022 21:29:14 +0900 Subject: [PATCH] fixup: add test cases using process.exitCode Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com --- .../test-process-exit-code-validation.js | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/test/parallel/test-process-exit-code-validation.js b/test/parallel/test-process-exit-code-validation.js index 88cb73e271765c..9bb2149eed8868 100644 --- a/test/parallel/test-process-exit-code-validation.js +++ b/test/parallel/test-process-exit-code-validation.js @@ -2,8 +2,7 @@ require('../common'); -const args = [ - // invaild code +const invalids = [ { code: '', expected: 1, @@ -48,7 +47,8 @@ const args = [ code: NaN, expected: 1, }, - // vaild code +]; +const valids = [ { code: undefined, expected: 0, @@ -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); + } }