Fix input with inherited stdin (#1232)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Puneet Dixit · committed by GitHub · parent f3a2e84 · Browse files · 3 files·+43 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+43 added−2 removed
- tests:+32 added−0 removed
- other:+11 added−2 removed
Provenance
- Commit
3ed0544- Tree
4360893- Author
Puneet Dixit <puneetdixit4321@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
f3a2e84
3 files changed+43 added−2 removed
modified:lib/stdio/handle.js+11 added-2 removed
59 unmodified lines60 // This is what users would expect.61 // For example, `stdout: ['ignore']` behaves the same as `stdout: 'ignore'`.62 const initializeStdioItems = ({stdioOption, fdNumber, options, optionName}) => {63 const values = Array.isArray(stdioOption) ? stdioOption : [stdioOption];64+ const inputStdioItems = handleInputOptions(options, fdNumber);65 const initialStdioItems = [65- ...values.map(value => initializeStdioItem(value, optionName)),66- ...handleInputOptions(options, fdNumber),66+ ...omitInheritedStdin(values.map(value => initializeStdioItem(value, optionName)), inputStdioItems),67+ ...inputStdioItems,68 ];69 70 const stdioItems = filterDuplicates(initialStdioItems);71 const isStdioArray = stdioItems.length > 1;1 unmodified line73 validateStreams(stdioItems);74 return {stdioItems, isStdioArray};75 };76 77+const omitInheritedStdin = (stdioItems, inputStdioItems) => inputStdioItems.length > 0 && isInheritedStdinOnly(stdioItems)78+ ? []79+ : stdioItems;80+81+const isInheritedStdinOnly = stdioItems => stdioItems.length === 182+ && stdioItems[0].type === 'native'83+ && stdioItems[0].value === 'inherit';84+85 const initializeStdioItem = (value, optionName) => ({86 type: getStdioItemType(value, optionName),87 value,88 optionName,135 unmodified linesadded:test/fixtures/nested-input-inherit.js+17 added
1234567891011121314151617#!/usr/bin/env nodeimport process from 'node:process';import {execa, execaSync} from '../../index.js';import {foobarString} from '../helpers/input.js';
const [optionName, stdioOptionString, isSyncString] = process.argv.slice(2);const execaMethod = isSyncString === 'true' ? execaSync : execa;const options = { input: foobarString, [optionName]: JSON.parse(stdioOptionString),};
if (optionName === 'stdin') { options.stdout = 'inherit';}
await execaMethod('stdin.js', options);modified:test/io/input-option.js+15 added
7 unmodified lines891011121314151615 unmodified lines3233343536373839404142434445464748495051525316 unmodified lines7 unmodified lines runScript, runScriptSync,} from '../helpers/run.js';import { foobarString, foobarUint8Array, foobarBuffer, foobarArrayBuffer, foobarUint16Array,15 unmodified linestest('input option can be a Buffer - sync', testInput, foobarBuffer, runExecaSync);test('input option can be used with $', testInput, 'foobar', runScript);test('input option can be used with $.sync', testInput, 'foobar', runScriptSync);
const testInputIgnoresInheritedStdin = async (t, optionName, stdioOption, isSync) => { const {stdout} = await execa('nested-input-inherit.js', [optionName, JSON.stringify(stdioOption), `${isSync}`], {input: foobarString}); t.is(stdout, foobarString);};
test('input option ignores stdin "inherit"', testInputIgnoresInheritedStdin, 'stdin', 'inherit', false);test('input option ignores stdin ["inherit"]', testInputIgnoresInheritedStdin, 'stdin', ['inherit'], false);test('input option ignores stdio "inherit"', testInputIgnoresInheritedStdin, 'stdio', 'inherit', false);test('input option ignores stdio[0] "inherit"', testInputIgnoresInheritedStdin, 'stdio', ['inherit', 'inherit', 'pipe'], false);test.serial('input option ignores stdin "inherit" - sync', testInputIgnoresInheritedStdin, 'stdin', 'inherit', true);test.serial('input option ignores stdin ["inherit"] - sync', testInputIgnoresInheritedStdin, 'stdin', ['inherit'], true);test.serial('input option ignores stdio "inherit" - sync', testInputIgnoresInheritedStdin, 'stdio', 'inherit', true);test.serial('input option ignores stdio[0] "inherit" - sync', testInputIgnoresInheritedStdin, 'stdio', ['inherit', 'inherit', 'pipe'], true);
const testInvalidInput = async (t, input, execaMethod) => { t.throws(() => { execaMethod('empty.js', {input}); }, {message: /a string, a Uint8Array/});16 unmodified lines