Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/stdio/handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ const getFileDescriptor = ({stdioOption, fdNumber, options, isSync}) => {
// For example, `stdout: ['ignore']` behaves the same as `stdout: 'ignore'`.
const initializeStdioItems = ({stdioOption, fdNumber, options, optionName}) => {
const values = Array.isArray(stdioOption) ? stdioOption : [stdioOption];
const inputStdioItems = handleInputOptions(options, fdNumber);
const initialStdioItems = [
...values.map(value => initializeStdioItem(value, optionName)),
...handleInputOptions(options, fdNumber),
...omitInheritedStdin(values.map(value => initializeStdioItem(value, optionName)), inputStdioItems),
...inputStdioItems,
];

const stdioItems = filterDuplicates(initialStdioItems);
Expand All @@ -73,6 +74,14 @@ const initializeStdioItems = ({stdioOption, fdNumber, options, optionName}) => {
return {stdioItems, isStdioArray};
};

const omitInheritedStdin = (stdioItems, inputStdioItems) => inputStdioItems.length > 0 && isInheritedStdinOnly(stdioItems)
? []
: stdioItems;

const isInheritedStdinOnly = stdioItems => stdioItems.length === 1
&& stdioItems[0].type === 'native'
&& stdioItems[0].value === 'inherit';

const initializeStdioItem = (value, optionName) => ({
type: getStdioItemType(value, optionName),
value,
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/nested-input-inherit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env node
import 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);
15 changes: 15 additions & 0 deletions test/io/input-option.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
runScriptSync,
} from '../helpers/run.js';
import {
foobarString,
foobarUint8Array,
foobarBuffer,
foobarArrayBuffer,
Expand All @@ -32,6 +33,20 @@ test('input option can be a Buffer - sync', testInput, foobarBuffer, runExecaSyn
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});
Expand Down
Loading