handle-invalid.js
This file
Path
test/stdio/handle-invalid.js
126 lines · 8.0 KiB · JavaScript
Last changed by
Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127import assert from 'node:assert/strict';import {open, rm} from 'node:fs/promises';import test from 'node:test';import tempfile from 'tempfile';import {assertThrows} from '../helpers/assert.js';import {execa, execaSync} from '../../index.js';import {getStdio} from '../helpers/stdio.js';import {setFixtureDirectory} from '../helpers/fixtures-directory.js';
setFixtureDirectory();
const testEmptyArray = (fdNumber, optionName, execaMethod) => { assertThrows(() => { execaMethod('empty.js', getStdio(fdNumber, [])); }, {message: `The \`${optionName}\` option must not be an empty array.`});};
test('Cannot pass an empty array to stdin', () => testEmptyArray(0, 'stdin', execa));test('Cannot pass an empty array to stdout', () => testEmptyArray(1, 'stdout', execa));test('Cannot pass an empty array to stderr', () => testEmptyArray(2, 'stderr', execa));test('Cannot pass an empty array to stdio[*]', () => testEmptyArray(3, 'stdio[3]', execa));test('Cannot pass an empty array to stdin - sync', () => testEmptyArray(0, 'stdin', execaSync));test('Cannot pass an empty array to stdout - sync', () => testEmptyArray(1, 'stdout', execaSync));test('Cannot pass an empty array to stderr - sync', () => testEmptyArray(2, 'stderr', execaSync));test('Cannot pass an empty array to stdio[*] - sync', () => testEmptyArray(3, 'stdio[3]', execaSync));
const testEmptyArrayBufferFalse = (fdNumber, optionName, execaMethod) => { assertThrows(() => { execaMethod('empty.js', {...getStdio(fdNumber, []), buffer: false}); }, {message: `The \`${optionName}\` option must not be an empty array.`});};
test('Cannot pass an empty array to stdout with buffer: false', () => testEmptyArrayBufferFalse(1, 'stdout', execa));test('Cannot pass an empty array to stderr with buffer: false', () => testEmptyArrayBufferFalse(2, 'stderr', execa));test('Cannot pass an empty array to stdout with buffer: false - sync', () => testEmptyArrayBufferFalse(1, 'stdout', execaSync));test('Cannot pass an empty array to stderr with buffer: false - sync', () => testEmptyArrayBufferFalse(2, 'stderr', execaSync));
const testInvalidValueSync = (fdNumber, stdioOption) => { const {message} = assertThrows(() => { execaSync('empty.js', getStdio(fdNumber, stdioOption)); }); assert.ok(message.includes(`cannot be "${stdioOption}" with synchronous methods`));};
test('stdin cannot be "overlapped", sync', () => testInvalidValueSync(0, 'overlapped'));test('stdout cannot be "overlapped", sync', () => testInvalidValueSync(1, 'overlapped'));test('stderr cannot be "overlapped", sync', () => testInvalidValueSync(2, 'overlapped'));test('stdio[*] cannot be "overlapped", sync', () => testInvalidValueSync(3, 'overlapped'));
const testInvalidArrayValue = (invalidStdio, fdNumber, execaMethod) => { assertThrows(() => { execaMethod('empty.js', getStdio(fdNumber, ['pipe', invalidStdio])); }, {message: /must not include/});};
test('Cannot pass "ignore" and another value to stdin', () => testInvalidArrayValue('ignore', 0, execa));test('Cannot pass "ignore" and another value to stdout', () => testInvalidArrayValue('ignore', 1, execa));test('Cannot pass "ignore" and another value to stderr', () => testInvalidArrayValue('ignore', 2, execa));test('Cannot pass "ignore" and another value to stdio[*]', () => testInvalidArrayValue('ignore', 3, execa));test('Cannot pass "ignore" and another value to stdin - sync', () => testInvalidArrayValue('ignore', 0, execaSync));test('Cannot pass "ignore" and another value to stdout - sync', () => testInvalidArrayValue('ignore', 1, execaSync));test('Cannot pass "ignore" and another value to stderr - sync', () => testInvalidArrayValue('ignore', 2, execaSync));test('Cannot pass "ignore" and another value to stdio[*] - sync', () => testInvalidArrayValue('ignore', 3, execaSync));
// The `stdio: 'ipc'` value (raw `child_process` syntax) was replaced by the `ipc: true` optionconst testIpcStdioOption = (fdNumber, stdioOption, execaMethod) => { assertThrows(() => { execaMethod('empty.js', getStdio(fdNumber, stdioOption)); }, {message: /The `ipc: true` option must be used instead/});};
test('stdin cannot be "ipc"', () => testIpcStdioOption(0, 'ipc', execa));test('stdout cannot be "ipc"', () => testIpcStdioOption(1, 'ipc', execa));test('stderr cannot be "ipc"', () => testIpcStdioOption(2, 'ipc', execa));test('stdio[*] cannot be "ipc"', () => testIpcStdioOption(3, 'ipc', execa));test('stdin cannot be "ipc" - sync', () => testIpcStdioOption(0, 'ipc', execaSync));test('stdout cannot be "ipc" - sync', () => testIpcStdioOption(1, 'ipc', execaSync));test('stderr cannot be "ipc" - sync', () => testIpcStdioOption(2, 'ipc', execaSync));test('stdio[*] cannot be "ipc" - sync', () => testIpcStdioOption(3, 'ipc', execaSync));test('stdio cannot be "ipc"', () => testIpcStdioOption('stdio', 'ipc', execa));test('stdio cannot be "ipc" - sync', () => testIpcStdioOption('stdio', 'ipc', execaSync));test('stdio[*] cannot be ["ipc"]', () => testIpcStdioOption(3, ['ipc'], execa));test('stdio[*] cannot be ["ipc"] - sync', () => testIpcStdioOption(3, ['ipc'], execaSync));test('stdio[*] cannot be {value: "ipc"}', () => testIpcStdioOption(3, {value: 'ipc'}, execa));test('stdio[*] cannot be {value: "ipc"} - sync', () => testIpcStdioOption(3, {value: 'ipc'}, execaSync));test('Cannot pass "ipc" and another value to stdio[*]', () => testIpcStdioOption(3, ['pipe', 'ipc'], execa));test('Cannot pass "ipc" and another value to stdio[*] - sync', () => testIpcStdioOption(3, ['pipe', 'ipc'], execaSync));test('Cannot pass {value: "ipc"} and another value to stdio[*]', () => testIpcStdioOption(3, ['pipe', {value: 'ipc'}], execa));test('Cannot pass {value: "ipc"} and another value to stdio[*] - sync', () => testIpcStdioOption(3, ['pipe', {value: 'ipc'}], execaSync));
// A file descriptor must be a non-negative integer. Without this, `node:child_process` aborts the current process with a fatal assertion when spawning.const testInvalidFileDescriptor = (fdNumber, optionName, stdioOption, execaMethod) => { assertThrows(() => { execaMethod('empty.js', getStdio(fdNumber, stdioOption)); }, {message: `The \`${optionName}\` option must be a non-negative 32-bit integer: got ${stdioOption}.`});};
test('Cannot pass a fractional file descriptor to stdin', () => testInvalidFileDescriptor(0, 'stdin', 1.5, execa));test('Cannot pass NaN as a file descriptor to stdin', () => testInvalidFileDescriptor(0, 'stdin', NaN, execa));test('Cannot pass Infinity as a file descriptor to stdout', () => testInvalidFileDescriptor(1, 'stdout', Infinity, execa));test('Cannot pass a too large file descriptor to stderr', () => testInvalidFileDescriptor(2, 'stderr', 2 ** 32, execa));test('Cannot pass a negative file descriptor to stdio[*]', () => testInvalidFileDescriptor(3, 'stdio[3]', -1, execa));test('Cannot pass a fractional file descriptor to stdout', () => testInvalidFileDescriptor(1, 'stdout', 1.5, execaSync));test('Cannot pass a negative file descriptor to stdin', () => testInvalidFileDescriptor(0, 'stdin', -1, execaSync));
const testUnknownNativeValue = (invalidStdio, fdNumber, execaMethod) => { const {message} = assertThrows(() => { execaMethod('empty.js', getStdio(fdNumber, ['pipe', invalidStdio])); }, {instanceOf: TypeError}); assert.ok(message.includes(`option is invalid: ${String(invalidStdio)} (${typeof invalidStdio})`));};
test('Cannot pass "true" and another value to stdout', () => testUnknownNativeValue(true, 1, execa));test('Cannot pass a plain object and another value to stdout', () => testUnknownNativeValue({}, 1, execa));test('Cannot pass "false" and another value to stdin', () => testUnknownNativeValue(false, 0, execa));test('Cannot pass "true" and another value to stdout - sync', () => testUnknownNativeValue(true, 1, execaSync));
test('Cannot pass a file handle and another value to stdout', async () => { const filePath = tempfile(); const fileHandle = await open(filePath, 'w'); assertThrows(() => { execa('empty.js', getStdio(1, ['pipe', fileHandle])); }, {instanceOf: TypeError, message: /`stdout` option cannot combine file handles with other values/}); await fileHandle.close(); await rm(filePath);});