Add passing the same transform multiple times (#697)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent b6f9fa2 · Browse files · 2 files·+26 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+26 added−16 removed
- tests:+5 added−0 removed
- other:+21 added−16 removed
Provenance
- Commit
35ec8de- Tree
a031e38- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
b6f9fa2
2 files changed+26 added−16 removed
modified:lib/stdio/handle.js+21 added-16 removed
21 unmodified lines2223242526272829262728293031323334353637383940414243444535364647484950515253544445465556575859606162635364655556575859606667686936 unmodified lines21 unmodified lines// This is what users would expect.// For example, `stdout: ['ignore']` behaves the same as `stdout: 'ignore'`.const getStdioStreams = (stdioOption, index) => { const optionName = getOptionName(index); const stdioOptions = Array.isArray(stdioOption) ? [...new Set(stdioOption)] : [stdioOption]; const isStdioArray = stdioOptions.length > 1; validateStdioArray(stdioOptions, isStdioArray, optionName); return stdioOptions.map(stdioOption => getStdioStream({stdioOption, optionName, index, isStdioArray})); const stdioOptions = Array.isArray(stdioOption) ? stdioOption : [stdioOption]; const rawStdioStreams = stdioOptions.map(stdioOption => getStdioStream(stdioOption, optionName, index)); const stdioStreams = filterDuplicates(rawStdioStreams); const isStdioArray = stdioStreams.length > 1; validateStdioArray(stdioStreams, isStdioArray, optionName); return stdioStreams.map(stdioStream => handleNativeStream(stdioStream, isStdioArray));};
const getOptionName = index => KNOWN_OPTION_NAMES[index] ?? `stdio[${index}]`;const KNOWN_OPTION_NAMES = ['stdin', 'stdout', 'stderr'];
const getStdioStream = (stdioOption, optionName, index) => { const type = getStdioOptionType(stdioOption, optionName); return {type, value: stdioOption, optionName, index};};
const filterDuplicates = stdioStreams => stdioStreams.filter((stdioStreamOne, indexOne) => stdioStreams.every((stdioStreamTwo, indexTwo) => stdioStreamOne.value !== stdioStreamTwo.value || indexOne >= indexTwo || stdioStreamOne.type === 'generator'));
const validateStdioArray = (stdioOptions, isStdioArray, optionName) => { if (stdioOptions.length === 0) {const validateStdioArray = (stdioStreams, isStdioArray, optionName) => { if (stdioStreams.length === 0) { throw new TypeError(`The \`${optionName}\` option must not be an empty array.`); }
if (!isStdioArray) { return; }
for (const invalidStdioOption of INVALID_STDIO_ARRAY_OPTIONS) { if (stdioOptions.includes(invalidStdioOption)) { throw new Error(`The \`${optionName}\` option must not include \`${invalidStdioOption}\`.`); for (const {value, optionName} of stdioStreams) { if (INVALID_STDIO_ARRAY_OPTIONS.has(value)) { throw new Error(`The \`${optionName}\` option must not include \`${value}\`.`); } }};
// Using those `stdio` values together with others for the same stream does not make sense, so we make it fail.// However, we do allow it if the array has a single item.const INVALID_STDIO_ARRAY_OPTIONS = ['ignore', 'ipc'];const INVALID_STDIO_ARRAY_OPTIONS = new Set(['ignore', 'ipc']);
const getStdioStream = ({stdioOption, optionName, index, isStdioArray}) => { const type = getStdioOptionType(stdioOption, optionName); const stdioStream = {type, value: stdioOption, optionName, index}; return handleNativeStream(stdioStream, isStdioArray);};
const validateStreams = stdioStreams => { for (const stdioStream of stdioStreams) { validateFileStdio(stdioStream); }36 unmodified linesmodified:test/stdio/generator.js+5 added
337 unmodified lines338 339 test('Can use multiple generators as output', testAppendOutput, false);340 test('Can use multiple generators as output, reversed', testAppendOutput, true);341 342+test('Can use multiple identical generators', async t => {343+ const {stdout} = await execa('noop-fd.js', ['1', foobarString], {stdout: [appendGenerator, appendGenerator]});344+ t.is(stdout, `${foobarString}${casedSuffix}${casedSuffix}`);345+});346+347 const maxBuffer = 10;348 349 test('Generators take "maxBuffer" into account', async t => {350 const bigString = '.'.repeat(maxBuffer);55 unmodified lines