Fix using multiple `stdin` values (#661)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 61a1623 · Browse files · 3 files·+26 added−13 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+26 added−13 removed
- tests:+14 added−10 removed
- other:+12 added−3 removed
Provenance
- Commit
4d8f9ca- Tree
f9795c8- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
61a1623
3 files changed+26 added−13 removed
modified:lib/stdio/async.js+12 added-3 removed
123456713 unmodified lines212223242526272829263031323334353637383039404142436 unmodified lines50515253455455import {createReadStream, createWriteStream} from 'node:fs';import {Readable, Writable} from 'node:stream';import mergeStreams from '@sindresorhus/merge-streams';import {handleInput} from './handle.js';
// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async modeexport const handleInputAsync = options => handleInput(addPropertiesAsync, options);13 unmodified lines },};
// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in async mode// When multiple input streams are used, we merge them to ensure the output stream ends only once each input stream has endedexport const pipeOutputAsync = (spawned, stdioStreams) => { const inputStreamsGroups = {};
for (const stdioStream of stdioStreams) { pipeStdioOption(spawned.stdio[stdioStream.index], stdioStream); pipeStdioOption(spawned.stdio[stdioStream.index], stdioStream, inputStreamsGroups); }
for (const [index, inputStreams] of Object.entries(inputStreamsGroups)) { const value = inputStreams.length === 1 ? inputStreams[0] : mergeStreams(inputStreams); value.pipe(spawned.stdio[index]); }};
const pipeStdioOption = (childStream, {type, value, direction}) => {const pipeStdioOption = (childStream, {type, value, direction, index}, inputStreamsGroups) => { if (type === 'native') { return; }
6 unmodified lines childStream.end(value); return; }
value.pipe(childStream); inputStreamsGroups[index] = [...(inputStreamsGroups[index] ?? []), value];};modified:test/stdio/array.js+6 added-10 removed
1 import {readFile, writeFile, rm} from 'node:fs/promises';2 import process from 'node:process';3-import {PassThrough} from 'node:stream';3 import test from 'ava';4 import tempfile from 'tempfile';5 import {execa, execaSync} from '../../index.js';6 import {getStdinOption, getStdoutOption, getStderrOption, getStdioOption} from '../helpers/stdio.js';7+import {stringGenerator} from '../helpers/generator.js';8 import {setFixtureDir} from '../helpers/fixtures-dir.js';9 10 setFixtureDir();11 111 unmodified lines123 124 test('stdio[*] default direction is output', testAmbiguousDirection, execa);125 test('stdio[*] default direction is output - sync', testAmbiguousDirection, execaSync);126 127-const testAmbiguousMultiple = async (t, stdioOptions) => {127+const testAmbiguousMultiple = async (t, fixtureName, getOptions) => {128 const filePath = tempfile();129 await writeFile(filePath, 'foobar');130- const {stdout} = await execa('stdin-fd3.js', getStdioOption([filePath, ...stdioOptions]));131- t.is(stdout, 'foobar');130+ const {stdout} = await execa(fixtureName, getOptions([filePath, stringGenerator()]));131+ t.is(stdout, 'foobarfoobar');132 await rm(filePath);133 };134 135-test('stdio[*] ambiguous direction is influenced by other values like ReadableStream', testAmbiguousMultiple, [new ReadableStream()]);136-test('stdio[*] ambiguous direction is influenced by other values like 0', testAmbiguousMultiple, [0]);137-test('stdio[*] ambiguous direction is influenced by other values like process.stdin', testAmbiguousMultiple, [process.stdin]);138-test('stdio[*] Duplex has an ambiguous direction like ReadableStream', testAmbiguousMultiple, [new PassThrough(), new ReadableStream()]);139-test('stdio[*] Duplex has an ambiguous direction like 0', testAmbiguousMultiple, [new PassThrough(), 0]);140-test('stdio[*] Duplex has an ambiguous direction like process.stdin', testAmbiguousMultiple, [new PassThrough(), process.stdin]);135+test('stdin ambiguous direction is influenced by other values', testAmbiguousMultiple, 'stdin.js', getStdinOption);136+test('stdio[*] ambiguous direction is influenced by other values', testAmbiguousMultiple, 'stdin-fd3.js', getStdioOption);137 138 const testRedirectInput = async (t, stdioOption, index, fixtureName) => {139 const {stdout} = await execa('nested-stdio.js', [JSON.stringify(stdioOption), String(index), fixtureName], {input: 'foobar'});140 t.is(stdout, 'foobar');125 unmodified linesmodified:test/stdio/iterable.js+8 added
59 unmodified lines60616263646566676869707159 unmodified lines } finally { abort(); }});
const testMultipleIterable = async (t, fixtureName, getOptions) => { const {stdout} = await execa(fixtureName, getOptions([stringGenerator(), asyncGenerator()])); t.is(stdout, 'foobarfoobar');};
test('stdin option can be multiple iterables', testMultipleIterable, 'stdin.js', getStdinOption);test('stdio[*] option can be multiple iterables', testMultipleIterable, 'stdin-fd3.js', getStdioOption);