node/test/parallel/test-child-process-validate-stdio.js
Denys Otrishko c66e6471e7
lib: remove ERR_INVALID_OPT_VALUE and ERR_INVALID_OPT_VALUE_ENCODING
This will be a start to generalize all argument validation
errors. As currently we throw ARG/OPT, OUT_OF_RANGE, and other more
specific errors.
The OPT errors didn't bring much to the errors as it's just another
variant of ARG error which is sometimes more confusing (some of our code
used OPT errors to denote just argument validation errors presumably
because of similarity of OPT to 'option' and not 'options-object')
and they don't specify the name of the options object where the invalid
value is located. Much better approach would be to just specify path
to the invalid value in the name of the value as it is done in this PR
(i.e. 'options.format', 'options.publicKey.type' etc)

Also since this decreases a variety of errors we have it'd be easier to
reuse validation code across the codebase.

Refs: https://github.com/nodejs/node/pull/31251
Refs: https://github.com/nodejs/node/pull/34070#discussion_r467251009
Signed-off-by: Denys Otrishko <shishugi@gmail.com>

PR-URL: https://github.com/nodejs/node/pull/34682
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
2020-09-11 17:54:42 +03:00

62 lines
1.8 KiB
JavaScript

'use strict';
// Flags: --expose-internals
const common = require('../common');
const assert = require('assert');
const getValidStdio = require('internal/child_process').getValidStdio;
const expectedError = { code: 'ERR_INVALID_ARG_VALUE', name: 'TypeError' };
// Should throw if string and not ignore, pipe, or inherit
assert.throws(() => getValidStdio('foo'), expectedError);
// Should throw if not a string or array
assert.throws(() => getValidStdio(600), expectedError);
// Should populate stdio with undefined if len < 3
{
const stdio1 = [];
const result = getValidStdio(stdio1, false);
assert.strictEqual(stdio1.length, 3);
assert.strictEqual(result.hasOwnProperty('stdio'), true);
assert.strictEqual(result.hasOwnProperty('ipc'), true);
assert.strictEqual(result.hasOwnProperty('ipcFd'), true);
}
// Should throw if stdio has ipc and sync is true
const stdio2 = ['ipc', 'ipc', 'ipc'];
assert.throws(() => getValidStdio(stdio2, true),
{ code: 'ERR_IPC_SYNC_FORK', name: 'Error' }
);
// Should throw if stdio is not a valid input
{
const stdio = ['foo'];
assert.throws(() => getValidStdio(stdio, false),
{ code: 'ERR_INVALID_SYNC_FORK_INPUT', name: 'TypeError' }
);
}
// Should throw if stdio is not a valid option
{
const stdio = [{ foo: 'bar' }];
assert.throws(() => getValidStdio(stdio), expectedError);
}
if (common.isMainThread) {
const stdio3 = [process.stdin, process.stdout, process.stderr];
const result = getValidStdio(stdio3, false);
assert.deepStrictEqual(result, {
stdio: [
{ type: 'fd', fd: 0 },
{ type: 'fd', fd: 1 },
{ type: 'fd', fd: 2 }
],
ipc: undefined,
ipcFd: undefined
});
} else {
common.printSkipMessage(
'stdio is not associated with file descriptors in Workers');
}