-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
process: add
code
validation in process.exit()
This commit validates the `code` in `process.exit([code])`. According to the API doc, the `code` argument must be an integer value. However, the current implementation accepts various types of values and internally converts them into integer values. Also, the actual meaningful code range is 0 - 255, but it doesn't validate the range. Signed-off-by: Daeyeon Jeong [email protected]
- Loading branch information
Showing
5 changed files
with
62 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const { strictEqual } = require('node:assert'); | ||
|
||
const isParent = () => process.argv[2] === undefined; | ||
const invalid_type_samples = ['', '0', {}, [], true, false]; | ||
const out_of_range_samples = [-1, 256]; | ||
const args = [...invalid_type_samples, ...out_of_range_samples]; | ||
|
||
if (isParent()) { | ||
const { spawn } = require('node:child_process'); | ||
|
||
// Test the samples. | ||
strictEqual( | ||
invalid_type_samples.every( | ||
(v) => typeof v !== 'number' && v !== null && v !== undefined, | ||
), | ||
true, | ||
); | ||
|
||
strictEqual( | ||
out_of_range_samples.every( | ||
(v) => typeof v == 'number' && (v < 0 || v > 255), | ||
), | ||
true, | ||
); | ||
|
||
// Test process.exit() with the samepls. | ||
const node = process.execPath; | ||
const test = (arg, expectedCode) => { | ||
spawn(node, [__filename, arg]).on('exit', (code) => { | ||
strictEqual( | ||
code, | ||
expectedCode, | ||
`args[${arg}] - actual: ${code}, expected: ${expectedCode}`, | ||
); | ||
}); | ||
}; | ||
|
||
for (const index of args.keys()) { | ||
test(index, 1); | ||
} | ||
} else { | ||
const i = parseInt(process.argv[2]); | ||
if (Number.isNaN(i)) { | ||
return process.exit(100); | ||
} | ||
process.exit(args[i]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters