Skip to content
Myr

Allow using the `input`, `inputFile` and `stdin` options together (#666)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent 66d0efb · Browse files · 10 files+80 added−109 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

10 files+80 added−109 removed

  • tests:+29 added−47 removed
  • other:+51 added−62 removed

10 files+80 added−109 removed

modified:lib/stdio/async.js+4 added-8 removed
modified:lib/stdio/handle.js+15 added-13 removed
modified:lib/stdio/input.js+15 added-30 removed
modified:lib/stdio/sync.js+10 added-4 removed
22 unmodified lines
23};2425const addPropertiesSync = {26 input: {27 filePath: ({value}) => ({value: readFileSync(value), type: 'stringOrBuffer'}),27 filePath: ({value}) => ({value: readFileSync(value, 'utf8'), type: 'stringOrBuffer'}),28 webStream: forbiddenIfSync,29 nodeStream: forbiddenIfSync,30 iterable: forbiddenIfSync,31 native: forbiddenIfStreamSync,
6 unmodified lines
38 },39};4041const addInputOptionSync = (stdioStreams, options) => {42 const inputValue = stdioStreams.find(({type}) => type === 'stringOrBuffer')?.value;43 if (inputValue !== undefined) {44 options.input = inputValue;42 const inputs = stdioStreams.filter(({type}) => type === 'stringOrBuffer');43 if (inputs.length === 0) {44 return;45 }4647 options.input = inputs.length === 148 ? inputs[0].value49 : inputs.map(({value}) => serializeInput(value)).join('');50};5152const serializeInput = value => typeof value === 'string' ? value : new TextDecoder().decode(value);5354// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, after spawning, in sync mode55export const pipeOutputSync = (stdioStreams, result) => {56 if (result.output === null) {57 return;
16 unmodified lines
modified:lib/stream.js+1 added-1 removed
60 unmodified lines
61// eslint-disable-next-line unicorn/text-encoding-identifier-case62const isUtf8Encoding = encoding => encoding === 'utf8' || encoding === 'utf-8';6364// Retrieve streams created by the `std*` options65const getCustomStreams = stdioStreams => stdioStreams.filter(({type}) => type !== 'native' && type !== 'stringOrBuffer');65const getCustomStreams = stdioStreams => stdioStreams.filter(({type}) => type !== 'native');6667// Some `stdout`/`stderr` options create a stream, e.g. when passing a file path.68// The `.pipe()` method automatically ends that stream when `childProcess.stdout|stderr` ends.69// This makes sure we want for the completion of those streams, in order to catch any error.
78 unmodified lines
modified:test/helpers/stdio.js-1 removed
modified:test/stdio/file-path.js+28 added-5 removed
8 unmodified lines
9import {getStdinOption, getStdoutOption, getStderrOption, getStdioOption, getInputFileOption, getScriptSync, identity} from '../helpers/stdio.js';1011setFixtureDir();1213const textEncoder = new TextEncoder();14const binaryFoobar = textEncoder.encode('foobar');15const nonFileUrl = new URL('https://example.com');1617const getRelativePath = filePath => relative('.', filePath);18
151 unmodified lines
170171test('inputFile can be set with $', testInputFileScript, identity);172test('inputFile can be set with $.sync', testInputFileScript, getScriptSync);173172test('inputFile option cannot be set when stdin is set', t => {173 t.throws(() => {174 execa('stdin.js', {inputFile: '', stdin: 'ignore'});175 }, {message: /`inputFile` and `stdin` options/});176});174const testMultipleInputs = async (t, allGetOptions, execaMethod) => {175 const filePath = tempfile();176 await writeFile(filePath, 'foobar');177 const options = Object.assign({}, ...allGetOptions.map(getOptions => getOptions(filePath)));178 const {stdout} = await execaMethod('stdin.js', options);179 t.is(stdout, 'foobar'.repeat(allGetOptions.length));180 await rm(filePath);181};182183const getStringInput = () => ({input: 'foobar'});184const getBinaryInput = () => ({input: binaryFoobar});185186test('input String and inputFile can be both set', testMultipleInputs, [getInputFileOption, getStringInput], execa);187test('input String and stdin can be both set', testMultipleInputs, [getStdinOption, getStringInput], execa);188test('input Uint8Array and inputFile can be both set', testMultipleInputs, [getInputFileOption, getBinaryInput], execa);189test('input Uint8Array and stdin can be both set', testMultipleInputs, [getStdinOption, getBinaryInput], execa);190test('stdin and inputFile can be both set', testMultipleInputs, [getStdinOption, getInputFileOption], execa);191test('input String, stdin and inputFile can be all set', testMultipleInputs, [getInputFileOption, getStdinOption, getStringInput], execa);192test('input Uint8Array, stdin and inputFile can be all set', testMultipleInputs, [getInputFileOption, getStdinOption, getBinaryInput], execa);193test('input String and inputFile can be both set - sync', testMultipleInputs, [getInputFileOption, getStringInput], execaSync);194test('input String and stdin can be both set - sync', testMultipleInputs, [getStdinOption, getStringInput], execaSync);195test('input Uint8Array and inputFile can be both set - sync', testMultipleInputs, [getInputFileOption, getBinaryInput], execaSync);196test('input Uint8Array and stdin can be both set - sync', testMultipleInputs, [getStdinOption, getBinaryInput], execaSync);197test('stdin and inputFile can be both set - sync', testMultipleInputs, [getStdinOption, getInputFileOption], execaSync);198test('input String, stdin and inputFile can be all set - sync', testMultipleInputs, [getInputFileOption, getStdinOption, getStringInput], execaSync);199test('input Uint8Array, stdin and inputFile can be all set - sync', testMultipleInputs, [getInputFileOption, getStdinOption, getBinaryInput], execaSync);
modified:test/stdio/input.js+1 added-41 removed
1import {Readable} from 'node:stream';2import {pathToFileURL} from 'node:url';1import test from 'ava';2import {execa, execaSync, $} from '../../index.js';3import {setFixtureDir} from '../helpers/fixtures-dir.js';6import {getStdinOption, getPlainStdioOption, getScriptSync, identity} from '../helpers/stdio.js';7import {stringGenerator} from '../helpers/generator.js';4import {getScriptSync, identity} from '../helpers/stdio.js';56setFixtureDir();78const textEncoder = new TextEncoder();9const binaryFoobar = textEncoder.encode('foobar');1014const testInputOptionError = (t, stdin, inputName) => {15 t.throws(() => {16 execa('stdin.js', {stdin, [inputName]: 'foobar'});17 }, {message: new RegExp(`\`${inputName}\` and \`stdin\` options`)});18};1920test('stdin option cannot be an iterable when "input" is used', testInputOptionError, stringGenerator(), 'input');21test('stdin option cannot be an iterable when "inputFile" is used', testInputOptionError, stringGenerator(), 'inputFile');22test('stdin option cannot be a file URL when "input" is used', testInputOptionError, pathToFileURL('unknown'), 'input');23test('stdin option cannot be a file URL when "inputFile" is used', testInputOptionError, pathToFileURL('unknown'), 'inputFile');24test('stdin option cannot be a file path when "input" is used', testInputOptionError, './unknown', 'input');25test('stdin option cannot be a file path when "inputFile" is used', testInputOptionError, './unknown', 'inputFile');26test('stdin option cannot be a ReadableStream when "input" is used', testInputOptionError, new ReadableStream(), 'input');27test('stdin option cannot be a ReadableStream when "inputFile" is used', testInputOptionError, new ReadableStream(), 'inputFile');2811const testInput = async (t, input, execaMethod) => {12 const {stdout} = await execaMethod('stdin.js', {input});13 t.is(stdout, 'foobar');14};
9 unmodified lines
24};2526test('input option can be used with $', testInputScript, identity);27test('input option can be used with $.sync', testInputScript, getScriptSync);4647const testInputWithStdinError = (t, input, getOptions, execaMethod) => {48 t.throws(() => {49 execaMethod('stdin.js', {input, ...getOptions('ignore')});50 }, {message: /`input` and `stdin` options/});51};5253test('input option cannot be a String when stdin is set', testInputWithStdinError, 'foobar', getStdinOption, execa);54test('input option cannot be a String when stdio is set', testInputWithStdinError, 'foobar', getPlainStdioOption, execa);55test('input option cannot be a String when stdin is set - sync', testInputWithStdinError, 'foobar', getStdinOption, execaSync);56test('input option cannot be a String when stdio is set - sync', testInputWithStdinError, 'foobar', getPlainStdioOption, execaSync);57test('input option cannot be a Node.js Readable when stdin is set', testInputWithStdinError, new Readable(), getStdinOption, execa);58test('input option cannot be a Node.js Readable when stdio is set', testInputWithStdinError, new Readable(), getPlainStdioOption, execa);5960const testInputAndInputFile = async (t, execaMethod) => {61 t.throws(() => execaMethod('stdin.js', {inputFile: '', input: ''}), {62 message: /cannot be both set/,63 });64};6566test('inputFile and input cannot be both set', testInputAndInputFile, execa);67test('inputFile and input cannot be both set - sync', testInputAndInputFile, execaSync);
modified:index.d.ts+3 added-3 removed
117 unmodified lines
118 - `'inherit'`: Re-use the current process' `stdin`.119 - an integer: Re-use a specific file descriptor from the current process.120 - a Node.js `Readable` stream.121122 Unless either the synchronous methods, the `input` option or the `inputFile` option is used, the value can also be a:122 Unless the synchronous methods are used, the value can also be a:123 - file path. If relative, it must start with `.`.124 - file URL.125 - web [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).126 - [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol) or [`AsyncIterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols)
13 unmodified lines
140 - `'inherit'`: Re-use the current process' `stdout`.141 - an integer: Re-use a specific file descriptor from the current process.142 - a Node.js `Writable` stream.143144 Unless either synchronous methods, the value can also be a:144 Unless the synchronous methods are used, the value can also be a:145 - file path. If relative, it must start with `.`.146 - file URL.147 - web [`WritableStream`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream).148
12 unmodified lines
161 - `'inherit'`: Re-use the current process' `stderr`.162 - an integer: Re-use a specific file descriptor from the current process.163 - a Node.js `Writable` stream.164165 Unless either synchronous methods, the value can also be a:165 Unless the synchronous methods are used, the value can also be a:166 - file path. If relative, it must start with `.`.167 - file URL.168 - web [`WritableStream`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream).169
913 unmodified lines
modified:readme.md+3 added-3 removed
579 unmodified lines
580- `'inherit'`: Re-use the current process' `stdin`.581- an integer: Re-use a specific file descriptor from the current process.582- a [Node.js `Readable` stream](#redirect-a-nodejs-stream-fromto-stdinstdoutstderr).583584Unless either the [synchronous methods](#execasyncfile-arguments-options), the [`input` option](#input) or the [`inputFile` option](#inputfile) is used, the value can also be a:584Unless the [synchronous methods](#execasyncfile-arguments-options) are used, the value can also be a:585- file path. If relative, it must start with `.`.586- file URL.587- web [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).588- [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol) or [`AsyncIterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols)
13 unmodified lines
602- `'inherit'`: Re-use the current process' `stdout`.603- an integer: Re-use a specific file descriptor from the current process.604- a [Node.js `Writable` stream](#redirect-a-nodejs-stream-fromto-stdinstdoutstderr).605606Unless either [synchronous methods](#execasyncfile-arguments-options), the value can also be a:606Unless the [synchronous methods](#execasyncfile-arguments-options) are used, the value can also be a:607- file path. If relative, it must start with `.`.608- file URL.609- web [`WritableStream`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream).610
12 unmodified lines
623- `'inherit'`: Re-use the current process' `stderr`.624- an integer: Re-use a specific file descriptor from the current process.625- a [Node.js `Writable` stream](#redirect-a-nodejs-stream-fromto-stdinstdoutstderr).626627Unless either [synchronous methods](#execasyncfile-arguments-options), the value can also be a:627Unless the [synchronous methods](#execasyncfile-arguments-options) are used, the value can also be a:628- file path. If relative, it must start with `.`.629- file URL.630- web [`WritableStream`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream).631
298 unmodified lines