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
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
TweaksSindre Sorhusbefore adoption698a1b8
line 1why, one step back1import assert from 'node:assert/strict';Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
line 2why, one step back2import {open, rm} from 'node:fs/promises';TweaksSindre Sorhusbefore adoption698a1b8
line 3why, one step back3import test from 'node:test';Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
line 4why, one step back4import tempfile from 'tempfile';TweaksSindre Sorhusbefore adoption698a1b8
line 5why, one step back5import {assertThrows} from '../helpers/assert.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 6–11why, one step back6import {execa, execaSync} from '../../index.js';7import {getStdio} from '../helpers/stdio.js';8import {setFixtureDirectory} from '../helpers/fixtures-directory.js';910setFixtureDirectory();11TweaksSindre Sorhusbefore adoption698a1b8
lines 12–13why, one step back12const testEmptyArray = (fdNumber, optionName, execaMethod) => {13 assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 14–17why, one step back14 execaMethod('empty.js', getStdio(fdNumber, []));15 }, {message: `The \`${optionName}\` option must not be an empty array.`});16};17TweaksSindre Sorhusbefore adoption698a1b8
lines 18–25why, one step back18test('Cannot pass an empty array to stdin', () => testEmptyArray(0, 'stdin', execa));19test('Cannot pass an empty array to stdout', () => testEmptyArray(1, 'stdout', execa));20test('Cannot pass an empty array to stderr', () => testEmptyArray(2, 'stderr', execa));21test('Cannot pass an empty array to stdio[*]', () => testEmptyArray(3, 'stdio[3]', execa));22test('Cannot pass an empty array to stdin - sync', () => testEmptyArray(0, 'stdin', execaSync));23test('Cannot pass an empty array to stdout - sync', () => testEmptyArray(1, 'stdout', execaSync));24test('Cannot pass an empty array to stderr - sync', () => testEmptyArray(2, 'stderr', execaSync));25test('Cannot pass an empty array to stdio[*] - sync', () => testEmptyArray(3, 'stdio[3]', execaSync));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 26why, one step back26TweaksSindre Sorhusbefore adoption698a1b8
lines 27–28why, one step back27const testEmptyArrayBufferFalse = (fdNumber, optionName, execaMethod) => {28 assertThrows(() => {Fix invalid `stdio` values being acceptedSindre Sorhusbefore adoptionc6bd387
lines 29–32why, one step back29 execaMethod('empty.js', {...getStdio(fdNumber, []), buffer: false});30 }, {message: `The \`${optionName}\` option must not be an empty array.`});31};32TweaksSindre Sorhusbefore adoption698a1b8
lines 33–36why, one step back33test('Cannot pass an empty array to stdout with buffer: false', () => testEmptyArrayBufferFalse(1, 'stdout', execa));34test('Cannot pass an empty array to stderr with buffer: false', () => testEmptyArrayBufferFalse(2, 'stderr', execa));35test('Cannot pass an empty array to stdout with buffer: false - sync', () => testEmptyArrayBufferFalse(1, 'stdout', execaSync));36test('Cannot pass an empty array to stderr with buffer: false - sync', () => testEmptyArrayBufferFalse(2, 'stderr', execaSync));Fix invalid `stdio` values being acceptedSindre Sorhusbefore adoptionc6bd387
line 37why, one step back37TweaksSindre Sorhusbefore adoption698a1b8
lines 38–39why, one step back38const testInvalidValueSync = (fdNumber, stdioOption) => {39 const {message} = assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 40–41why, one step back40 execaSync('empty.js', getStdio(fdNumber, stdioOption));41 });TweaksSindre Sorhusbefore adoption698a1b8
line 42why, one step back42 assert.ok(message.includes(`cannot be "${stdioOption}" with synchronous methods`));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 43–44why, one step back43};44TweaksSindre Sorhusbefore adoption698a1b8
lines 45–48why, one step back45test('stdin cannot be "overlapped", sync', () => testInvalidValueSync(0, 'overlapped'));46test('stdout cannot be "overlapped", sync', () => testInvalidValueSync(1, 'overlapped'));47test('stderr cannot be "overlapped", sync', () => testInvalidValueSync(2, 'overlapped'));48test('stdio[*] cannot be "overlapped", sync', () => testInvalidValueSync(3, 'overlapped'));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 49why, one step back49TweaksSindre Sorhusbefore adoption698a1b8
lines 50–51why, one step back50const testInvalidArrayValue = (invalidStdio, fdNumber, execaMethod) => {51 assertThrows(() => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 52–55why, one step back52 execaMethod('empty.js', getStdio(fdNumber, ['pipe', invalidStdio]));53 }, {message: /must not include/});54};55TweaksSindre Sorhusbefore adoption698a1b8
lines 56–63why, one step back56test('Cannot pass "ignore" and another value to stdin', () => testInvalidArrayValue('ignore', 0, execa));57test('Cannot pass "ignore" and another value to stdout', () => testInvalidArrayValue('ignore', 1, execa));58test('Cannot pass "ignore" and another value to stderr', () => testInvalidArrayValue('ignore', 2, execa));59test('Cannot pass "ignore" and another value to stdio[*]', () => testInvalidArrayValue('ignore', 3, execa));60test('Cannot pass "ignore" and another value to stdin - sync', () => testInvalidArrayValue('ignore', 0, execaSync));61test('Cannot pass "ignore" and another value to stdout - sync', () => testInvalidArrayValue('ignore', 1, execaSync));62test('Cannot pass "ignore" and another value to stderr - sync', () => testInvalidArrayValue('ignore', 2, execaSync));63test('Cannot pass "ignore" and another value to stdio[*] - sync', () => testInvalidArrayValue('ignore', 3, execaSync));Remove the old `stdio: [..., 'ipc']` syntax (#1245)Sindre Sorhusbefore adoptiondcf611c
lines 64–65why, one step back6465// The `stdio: 'ipc'` value (raw `child_process` syntax) was replaced by the `ipc: true` optionTweaksSindre Sorhusbefore adoption698a1b8
lines 66–67why, one step back66const testIpcStdioOption = (fdNumber, stdioOption, execaMethod) => {67 assertThrows(() => {Remove the old `stdio: [..., 'ipc']` syntax (#1245)Sindre Sorhusbefore adoptiondcf611c
lines 68–71why, one step back68 execaMethod('empty.js', getStdio(fdNumber, stdioOption));69 }, {message: /The `ipc: true` option must be used instead/});70};71TweaksSindre Sorhusbefore adoption698a1b8
lines 72–89why, one step back72test('stdin cannot be "ipc"', () => testIpcStdioOption(0, 'ipc', execa));73test('stdout cannot be "ipc"', () => testIpcStdioOption(1, 'ipc', execa));74test('stderr cannot be "ipc"', () => testIpcStdioOption(2, 'ipc', execa));75test('stdio[*] cannot be "ipc"', () => testIpcStdioOption(3, 'ipc', execa));76test('stdin cannot be "ipc" - sync', () => testIpcStdioOption(0, 'ipc', execaSync));77test('stdout cannot be "ipc" - sync', () => testIpcStdioOption(1, 'ipc', execaSync));78test('stderr cannot be "ipc" - sync', () => testIpcStdioOption(2, 'ipc', execaSync));79test('stdio[*] cannot be "ipc" - sync', () => testIpcStdioOption(3, 'ipc', execaSync));80test('stdio cannot be "ipc"', () => testIpcStdioOption('stdio', 'ipc', execa));81test('stdio cannot be "ipc" - sync', () => testIpcStdioOption('stdio', 'ipc', execaSync));82test('stdio[*] cannot be ["ipc"]', () => testIpcStdioOption(3, ['ipc'], execa));83test('stdio[*] cannot be ["ipc"] - sync', () => testIpcStdioOption(3, ['ipc'], execaSync));84test('stdio[*] cannot be {value: "ipc"}', () => testIpcStdioOption(3, {value: 'ipc'}, execa));85test('stdio[*] cannot be {value: "ipc"} - sync', () => testIpcStdioOption(3, {value: 'ipc'}, execaSync));86test('Cannot pass "ipc" and another value to stdio[*]', () => testIpcStdioOption(3, ['pipe', 'ipc'], execa));87test('Cannot pass "ipc" and another value to stdio[*] - sync', () => testIpcStdioOption(3, ['pipe', 'ipc'], execaSync));88test('Cannot pass {value: "ipc"} and another value to stdio[*]', () => testIpcStdioOption(3, ['pipe', {value: 'ipc'}], execa));89test('Cannot pass {value: "ipc"} and another value to stdio[*] - sync', () => testIpcStdioOption(3, ['pipe', {value: 'ipc'}], execaSync));Fix invalid `stdio` values being acceptedSindre Sorhusbefore adoptionc6bd387
lines 90–91why, one step back9091// A file descriptor must be a non-negative integer. Without this, `node:child_process` aborts the current process with a fatal assertion when spawning.TweaksSindre Sorhusbefore adoption698a1b8
lines 92–93why, one step back92const testInvalidFileDescriptor = (fdNumber, optionName, stdioOption, execaMethod) => {93 assertThrows(() => {Fix invalid `stdio` values being acceptedSindre Sorhusbefore adoptionc6bd387
lines 94–97why, one step back94 execaMethod('empty.js', getStdio(fdNumber, stdioOption));95 }, {message: `The \`${optionName}\` option must be a non-negative 32-bit integer: got ${stdioOption}.`});96};97TweaksSindre Sorhusbefore adoption698a1b8
lines 98–104why, one step back98test('Cannot pass a fractional file descriptor to stdin', () => testInvalidFileDescriptor(0, 'stdin', 1.5, execa));99test('Cannot pass NaN as a file descriptor to stdin', () => testInvalidFileDescriptor(0, 'stdin', NaN, execa));100test('Cannot pass Infinity as a file descriptor to stdout', () => testInvalidFileDescriptor(1, 'stdout', Infinity, execa));101test('Cannot pass a too large file descriptor to stderr', () => testInvalidFileDescriptor(2, 'stderr', 2 ** 32, execa));102test('Cannot pass a negative file descriptor to stdio[*]', () => testInvalidFileDescriptor(3, 'stdio[3]', -1, execa));103test('Cannot pass a fractional file descriptor to stdout', () => testInvalidFileDescriptor(1, 'stdout', 1.5, execaSync));104test('Cannot pass a negative file descriptor to stdin', () => testInvalidFileDescriptor(0, 'stdin', -1, execaSync));Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
lines 105–126why, one step back105106const testUnknownNativeValue = (invalidStdio, fdNumber, execaMethod) => {107 const {message} = assertThrows(() => {108 execaMethod('empty.js', getStdio(fdNumber, ['pipe', invalidStdio]));109 }, {instanceOf: TypeError});110 assert.ok(message.includes(`option is invalid: ${String(invalidStdio)} (${typeof invalidStdio})`));111};112113test('Cannot pass "true" and another value to stdout', () => testUnknownNativeValue(true, 1, execa));114test('Cannot pass a plain object and another value to stdout', () => testUnknownNativeValue({}, 1, execa));115test('Cannot pass "false" and another value to stdin', () => testUnknownNativeValue(false, 0, execa));116test('Cannot pass "true" and another value to stdout - sync', () => testUnknownNativeValue(true, 1, execaSync));117118test('Cannot pass a file handle and another value to stdout', async () => {119 const filePath = tempfile();120 const fileHandle = await open(filePath, 'w');121 assertThrows(() => {122 execa('empty.js', getStdio(1, ['pipe', fileHandle]));123 }, {instanceOf: TypeError, message: /`stdout` option cannot combine file handles with other values/});124 await fileHandle.close();125 await rm(filePath);126});