Fix `stdio: [..., undefined]` option (#948)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent c5f9f09 · Browse files · 4 files·+66 added−21 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+66 added−21 removed
- tests:+55 added−18 removed
- other:+11 added−3 removed
Provenance
- Commit
96d2ef7- Tree
44b0bbc- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
c5f9f09
4 files changed+66 added−21 removed
modified:lib/stdio/forward.js+1 added-1 removed
1234456788 unmodified lines// Whether `subprocess.std*` will be setexport const willPipeFileDescriptor = stdioItems => PIPED_STDIO_VALUES.has(forwardStdio(stdioItems));
export const PIPED_STDIO_VALUES = new Set(['pipe', 'overlapped', undefined, null]);export const PIPED_STDIO_VALUES = new Set(['pipe', 'overlapped']);
// When the `std*: Iterable | WebStream | URL | filePath`, `input` or `inputFile` option is used, we pipe to `subprocess.std*`.// When the `std*: Array` option is used, we emulate some of the native values ('inherit', Node.js stream and file descriptor integer). To do so, we also need to pipe to `subprocess.std*`.// Therefore the `std*` options must be either `pipe` or `overlapped`. Other values do not set `subprocess.std*`.8 unmodified linesmodified:lib/stdio/option.js+10 added-2 removed
123455678915 unmodified lines2526272829293031323334353637383940import {STANDARD_STREAMS_ALIASES} from '../utils.js';
// Add support for `stdin`/`stdout`/`stderr` as an alias for `stdio`export const normalizeStdio = ({stdio, ipc, ...options}) => { const stdioArray = getStdioArray(stdio, options); const stdioArray = getStdioArray(stdio, options).map((stdioOption, fdNumber) => addDefaultValue(stdioOption, fdNumber)); return ipc && !stdioArray.includes('ipc') ? [...stdioArray, 'ipc'] : stdioArray;};15 unmodified lines throw new TypeError(`Expected \`stdio\` to be of type \`string\` or \`Array\`, got \`${typeof stdio}\``); }
const length = Math.max(stdio.length, STANDARD_STREAMS_ALIASES.length); return Array.from({length}, (value, fdNumber) => stdio[fdNumber]); return Array.from({length}, (_, fdNumber) => stdio[fdNumber]);};
const hasAlias = options => STANDARD_STREAMS_ALIASES.some(alias => options[alias] !== undefined);
const addDefaultValue = (stdioOption, fdNumber) => { if (stdioOption === null || stdioOption === undefined) { return fdNumber >= STANDARD_STREAMS_ALIASES.length ? 'ignore' : 'pipe'; }
return stdioOption;};modified:test/stdio/forward.js+32 added-7 removed
1234567889109101112131314151614151617181920212223242526272829303132333435363738394041import test from 'ava';import {execa} from '../../index.js';import {getStdio} from '../helpers/stdio.js';import {setFixtureDir} from '../helpers/fixtures-dir.js';import {foobarString, foobarUint8Array} from '../helpers/input.js';
setFixtureDir();
const testOverlapped = async (t, fdNumber) => { const {stdout} = await execa('noop.js', ['foobar'], getStdio(fdNumber, ['overlapped', 'pipe'])); t.is(stdout, 'foobar');const testInputOverlapped = async (t, fdNumber) => { const {stdout} = await execa('stdin-fd.js', [`${fdNumber}`, foobarString], getStdio(fdNumber, [foobarUint8Array, 'overlapped', 'pipe'])); t.is(stdout, foobarString);};
test('stdin can be ["overlapped", "pipe"]', testOverlapped, 0);test('stdout can be ["overlapped", "pipe"]', testOverlapped, 1);test('stderr can be ["overlapped", "pipe"]', testOverlapped, 2);test('stdio[*] can be ["overlapped", "pipe"]', testOverlapped, 3);test('stdin can be ["overlapped", "pipe"]', testInputOverlapped, 0);test('stdio[*] input can be ["overlapped", "pipe"]', testInputOverlapped, 3);
const testOutputOverlapped = async (t, fdNumber) => { const {stdio} = await execa('noop-fd.js', [`${fdNumber}`, foobarString], getStdio(fdNumber, ['overlapped', 'pipe'])); t.is(stdio[fdNumber], foobarString);};
test('stdout can be ["overlapped", "pipe"]', testOutputOverlapped, 1);test('stderr can be ["overlapped", "pipe"]', testOutputOverlapped, 2);test('stdio[*] output can be ["overlapped", "pipe"]', testOutputOverlapped, 3);
const testFd3Undefined = async (t, stdioOption, options) => { const subprocess = execa('empty.js', {...getStdio(3, stdioOption), ...options}); t.is(subprocess.stdio.length, 4); t.is(subprocess.stdio[3], null);
const {stdio} = await subprocess; t.is(stdio.length, 4); t.is(stdio[3], undefined);};
test('stdio[*] undefined means "ignore"', testFd3Undefined, undefined, {});test('stdio[*] null means "ignore"', testFd3Undefined, null, {});test('stdio[*] undefined means "ignore", "lines: true"', testFd3Undefined, undefined, {lines: true});test('stdio[*] null means "ignore", "lines: true"', testFd3Undefined, null, {lines: true});test('stdio[*] undefined means "ignore", "encoding: hex"', testFd3Undefined, undefined, {encoding: 'hex'});test('stdio[*] null means "ignore", "encoding: hex"', testFd3Undefined, null, {encoding: 'hex'});modified:test/stdio/option.js+23 added-11 removed
19 unmodified lines202122232425262728293031323334353637383926272829304041424332333444454647363738484950515253543 unmodified lines19 unmodified lines
test(stdioMacro, {stdio: 'inherit'}, ['inherit', 'inherit', 'inherit']);test(stdioMacro, {stdio: 'pipe'}, ['pipe', 'pipe', 'pipe']);test(stdioMacro, {stdio: 'ignore'}, ['ignore', 'ignore', 'ignore']);
test(stdioMacro, {}, ['pipe', 'pipe', 'pipe']);test(stdioMacro, {stdio: []}, ['pipe', 'pipe', 'pipe']);test(stdioMacro, {stdio: [0]}, [0, 'pipe', 'pipe']);test(stdioMacro, {stdio: [0, 1]}, [0, 1, 'pipe']);test(stdioMacro, {stdio: [0, 1, 2]}, [0, 1, 2]);test(stdioMacro, {stdio: [0, 1, 2, 3]}, [0, 1, 2, 3]);test(stdioMacro, {stdio: [undefined, 1, 2]}, ['pipe', 1, 2]);test(stdioMacro, {stdio: [null, 1, 2]}, ['pipe', 1, 2]);test(stdioMacro, {stdio: [0, undefined, 2]}, [0, 'pipe', 2]);test(stdioMacro, {stdio: [0, null, 2]}, [0, 'pipe', 2]);test(stdioMacro, {stdio: [0, 1, undefined]}, [0, 1, 'pipe']);test(stdioMacro, {stdio: [0, 1, null]}, [0, 1, 'pipe']);test(stdioMacro, {stdio: [0, 1, 2, undefined]}, [0, 1, 2, 'ignore']);test(stdioMacro, {stdio: [0, 1, 2, null]}, [0, 1, 2, 'ignore']);
test(stdioMacro, {}, [undefined, undefined, undefined]);test(stdioMacro, {stdio: []}, [undefined, undefined, undefined]);test(stdioMacro, {stdin: 'pipe'}, ['pipe', undefined, undefined]);test(stdioMacro, {stdout: 'ignore'}, [undefined, 'ignore', undefined]);test(stdioMacro, {stderr: 'inherit'}, [undefined, undefined, 'inherit']);test(stdioMacro, {stdin: 'pipe'}, ['pipe', 'pipe', 'pipe']);test(stdioMacro, {stdout: 'ignore'}, ['pipe', 'ignore', 'pipe']);test(stdioMacro, {stderr: 'inherit'}, ['pipe', 'pipe', 'inherit']);test(stdioMacro, {stdin: 'pipe', stdout: 'ignore', stderr: 'inherit'}, ['pipe', 'ignore', 'inherit']);test(stdioMacro, {stdin: 'pipe', stdout: 'ignore'}, ['pipe', 'ignore', undefined]);test(stdioMacro, {stdin: 'pipe', stderr: 'inherit'}, ['pipe', undefined, 'inherit']);test(stdioMacro, {stdout: 'ignore', stderr: 'inherit'}, [undefined, 'ignore', 'inherit']);test(stdioMacro, {stdin: 'pipe', stdout: 'ignore'}, ['pipe', 'ignore', 'pipe']);test(stdioMacro, {stdin: 'pipe', stderr: 'inherit'}, ['pipe', 'pipe', 'inherit']);test(stdioMacro, {stdout: 'ignore', stderr: 'inherit'}, ['pipe', 'ignore', 'inherit']);test(stdioMacro, {stdin: 0, stdout: 1, stderr: 2}, [0, 1, 2]);test(stdioMacro, {stdin: 0, stdout: 1}, [0, 1, undefined]);test(stdioMacro, {stdin: 0, stderr: 2}, [0, undefined, 2]);test(stdioMacro, {stdout: 1, stderr: 2}, [undefined, 1, 2]);test(stdioMacro, {stdin: 0, stdout: 1}, [0, 1, 'pipe']);test(stdioMacro, {stdin: 0, stderr: 2}, [0, 'pipe', 2]);test(stdioMacro, {stdout: 1, stderr: 2}, ['pipe', 1, 2]);
test(stdioMacro, {stdio: {foo: 'bar'}}, new TypeError('Expected `stdio` to be of type `string` or `Array`, got `object`'));
test(stdioMacro, {stdin: 'inherit', stdio: 'pipe'}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));3 unmodified lines