generator-input.js
This file
Path
test/transform/generator-input.js
204 lines · 18.9 KiB · JavaScript
Last changed by
Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205import assert from 'node:assert/strict';import {Buffer} from 'node:buffer';import {writeFile, rm} from 'node:fs/promises';import test from 'node:test';import tempfile from 'tempfile';import {execa, execaSync} from '../../index.js';import {setFixtureDirectory} from '../helpers/fixtures-directory.js';import {getStdio} from '../helpers/stdio.js';import { foobarString, foobarUppercase, foobarHex, foobarUint8Array, foobarBuffer, foobarObject, foobarObjectString,} from '../helpers/input.js';import {generatorsMap} from '../helpers/map.js';
setFixtureDirectory();
const getInputObjectMode = (objectMode, addNoopTransform, type) => objectMode ? { input: [foobarObject], generators: generatorsMap[type].addNoop(generatorsMap[type].serialize(objectMode), addNoopTransform, objectMode), output: foobarObjectString, } : { input: foobarUint8Array, generators: generatorsMap[type].addNoop(generatorsMap[type].uppercase(objectMode), addNoopTransform, objectMode), output: foobarUppercase, };
// eslint-disable-next-line max-paramsconst testGeneratorInput = async (fdNumber, objectMode, addNoopTransform, type, execaMethod) => { const {input, generators, output} = getInputObjectMode(objectMode, addNoopTransform, type); const {stdout} = await execaMethod('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [input, ...generators])); assert.equal(stdout, output);};
test('Can use generators with result.stdin', () => testGeneratorInput(0, false, false, 'generator', execa));test('Can use generators with result.stdio[*] as input', () => testGeneratorInput(3, false, false, 'generator', execa));test('Can use generators with result.stdin, objectMode', () => testGeneratorInput(0, true, false, 'generator', execa));test('Can use generators with result.stdio[*] as input, objectMode', () => testGeneratorInput(3, true, false, 'generator', execa));test('Can use generators with result.stdin, noop transform', () => testGeneratorInput(0, false, true, 'generator', execa));test('Can use generators with result.stdio[*] as input, noop transform', () => testGeneratorInput(3, false, true, 'generator', execa));test('Can use generators with result.stdin, objectMode, noop transform', () => testGeneratorInput(0, true, true, 'generator', execa));test('Can use generators with result.stdio[*] as input, objectMode, noop transform', () => testGeneratorInput(3, true, true, 'generator', execa));test('Can use generators with result.stdin, sync', () => testGeneratorInput(0, false, false, 'generator', execaSync));test('Can use generators with result.stdin, objectMode, sync', () => testGeneratorInput(0, true, false, 'generator', execaSync));test('Can use generators with result.stdin, noop transform, sync', () => testGeneratorInput(0, false, true, 'generator', execaSync));test('Can use generators with result.stdin, objectMode, noop transform, sync', () => testGeneratorInput(0, true, true, 'generator', execaSync));test('Can use duplexes with result.stdin', () => testGeneratorInput(0, false, false, 'duplex', execa));test('Can use duplexes with result.stdio[*] as input', () => testGeneratorInput(3, false, false, 'duplex', execa));test('Can use duplexes with result.stdin, objectMode', () => testGeneratorInput(0, true, false, 'duplex', execa));test('Can use duplexes with result.stdio[*] as input, objectMode', () => testGeneratorInput(3, true, false, 'duplex', execa));test('Can use duplexes with result.stdin, noop transform', () => testGeneratorInput(0, false, true, 'duplex', execa));test('Can use duplexes with result.stdio[*] as input, noop transform', () => testGeneratorInput(3, false, true, 'duplex', execa));test('Can use duplexes with result.stdin, objectMode, noop transform', () => testGeneratorInput(0, true, true, 'duplex', execa));test('Can use duplexes with result.stdio[*] as input, objectMode, noop transform', () => testGeneratorInput(3, true, true, 'duplex', execa));test('Can use webTransforms with result.stdin', () => testGeneratorInput(0, false, false, 'webTransform', execa));test('Can use webTransforms with result.stdio[*] as input', () => testGeneratorInput(3, false, false, 'webTransform', execa));test('Can use webTransforms with result.stdin, objectMode', () => testGeneratorInput(0, true, false, 'webTransform', execa));test('Can use webTransforms with result.stdio[*] as input, objectMode', () => testGeneratorInput(3, true, false, 'webTransform', execa));test('Can use webTransforms with result.stdin, noop transform', () => testGeneratorInput(0, false, true, 'webTransform', execa));test('Can use webTransforms with result.stdio[*] as input, noop transform', () => testGeneratorInput(3, false, true, 'webTransform', execa));test('Can use webTransforms with result.stdin, objectMode, noop transform', () => testGeneratorInput(0, true, true, 'webTransform', execa));test('Can use webTransforms with result.stdio[*] as input, objectMode, noop transform', () => testGeneratorInput(3, true, true, 'webTransform', execa));
// eslint-disable-next-line max-paramsconst testGeneratorInputPipe = async (useShortcutProperty, objectMode, addNoopTransform, type, input) => { const {generators, output} = getInputObjectMode(objectMode, addNoopTransform, type); const subprocess = execa('stdin-fd.js', ['0'], getStdio(0, generators)); const stream = useShortcutProperty ? subprocess.stdin : subprocess.stdio[0]; stream.end(...input); const {stdout} = await subprocess; const expectedOutput = input[1] === 'utf16le' ? Buffer.from(output, input[1]).toString() : output; assert.equal(stdout, expectedOutput);};
test('Can use generators with subprocess.stdio[0] and default encoding', () => testGeneratorInputPipe(false, false, false, 'generator', [foobarString, 'utf8']));test('Can use generators with subprocess.stdin and default encoding', () => testGeneratorInputPipe(true, false, false, 'generator', [foobarString, 'utf8']));test('Can use generators with subprocess.stdio[0] and encoding "utf16le"', () => testGeneratorInputPipe(false, false, false, 'generator', [foobarString, 'utf16le']));test('Can use generators with subprocess.stdin and encoding "utf16le"', () => testGeneratorInputPipe(true, false, false, 'generator', [foobarString, 'utf16le']));test('Can use generators with subprocess.stdio[0] and encoding "buffer"', () => testGeneratorInputPipe(false, false, false, 'generator', [foobarBuffer, 'buffer']));test('Can use generators with subprocess.stdin and encoding "buffer"', () => testGeneratorInputPipe(true, false, false, 'generator', [foobarBuffer, 'buffer']));test('Can use generators with subprocess.stdio[0] and encoding "hex"', () => testGeneratorInputPipe(false, false, false, 'generator', [foobarHex, 'hex']));test('Can use generators with subprocess.stdin and encoding "hex"', () => testGeneratorInputPipe(true, false, false, 'generator', [foobarHex, 'hex']));test('Can use generators with subprocess.stdio[0], objectMode', () => testGeneratorInputPipe(false, true, false, 'generator', [foobarObject]));test('Can use generators with subprocess.stdin, objectMode', () => testGeneratorInputPipe(true, true, false, 'generator', [foobarObject]));test('Can use generators with subprocess.stdio[0] and default encoding, noop transform', () => testGeneratorInputPipe(false, false, true, 'generator', [foobarString, 'utf8']));test('Can use generators with subprocess.stdin and default encoding, noop transform', () => testGeneratorInputPipe(true, false, true, 'generator', [foobarString, 'utf8']));test('Can use generators with subprocess.stdio[0] and encoding "utf16le", noop transform', () => testGeneratorInputPipe(false, false, true, 'generator', [foobarString, 'utf16le']));test('Can use generators with subprocess.stdin and encoding "utf16le", noop transform', () => testGeneratorInputPipe(true, false, true, 'generator', [foobarString, 'utf16le']));test('Can use generators with subprocess.stdio[0] and encoding "buffer", noop transform', () => testGeneratorInputPipe(false, false, true, 'generator', [foobarBuffer, 'buffer']));test('Can use generators with subprocess.stdin and encoding "buffer", noop transform', () => testGeneratorInputPipe(true, false, true, 'generator', [foobarBuffer, 'buffer']));test('Can use generators with subprocess.stdio[0] and encoding "hex", noop transform', () => testGeneratorInputPipe(false, false, true, 'generator', [foobarHex, 'hex']));test('Can use generators with subprocess.stdin and encoding "hex", noop transform', () => testGeneratorInputPipe(true, false, true, 'generator', [foobarHex, 'hex']));test('Can use generators with subprocess.stdio[0], objectMode, noop transform', () => testGeneratorInputPipe(false, true, true, 'generator', [foobarObject]));test('Can use generators with subprocess.stdin, objectMode, noop transform', () => testGeneratorInputPipe(true, true, true, 'generator', [foobarObject]));test('Can use duplexes with subprocess.stdio[0] and default encoding', () => testGeneratorInputPipe(false, false, false, 'duplex', [foobarString, 'utf8']));test('Can use duplexes with subprocess.stdin and default encoding', () => testGeneratorInputPipe(true, false, false, 'duplex', [foobarString, 'utf8']));test('Can use duplexes with subprocess.stdio[0] and encoding "utf16le"', () => testGeneratorInputPipe(false, false, false, 'duplex', [foobarString, 'utf16le']));test('Can use duplexes with subprocess.stdin and encoding "utf16le"', () => testGeneratorInputPipe(true, false, false, 'duplex', [foobarString, 'utf16le']));test('Can use duplexes with subprocess.stdio[0] and encoding "buffer"', () => testGeneratorInputPipe(false, false, false, 'duplex', [foobarBuffer, 'buffer']));test('Can use duplexes with subprocess.stdin and encoding "buffer"', () => testGeneratorInputPipe(true, false, false, 'duplex', [foobarBuffer, 'buffer']));test('Can use duplexes with subprocess.stdio[0] and encoding "hex"', () => testGeneratorInputPipe(false, false, false, 'duplex', [foobarHex, 'hex']));test('Can use duplexes with subprocess.stdin and encoding "hex"', () => testGeneratorInputPipe(true, false, false, 'duplex', [foobarHex, 'hex']));test('Can use duplexes with subprocess.stdio[0], objectMode', () => testGeneratorInputPipe(false, true, false, 'duplex', [foobarObject]));test('Can use duplexes with subprocess.stdin, objectMode', () => testGeneratorInputPipe(true, true, false, 'duplex', [foobarObject]));test('Can use duplexes with subprocess.stdio[0] and default encoding, noop transform', () => testGeneratorInputPipe(false, false, true, 'duplex', [foobarString, 'utf8']));test('Can use duplexes with subprocess.stdin and default encoding, noop transform', () => testGeneratorInputPipe(true, false, true, 'duplex', [foobarString, 'utf8']));test('Can use duplexes with subprocess.stdio[0] and encoding "utf16le", noop transform', () => testGeneratorInputPipe(false, false, true, 'duplex', [foobarString, 'utf16le']));test('Can use duplexes with subprocess.stdin and encoding "utf16le", noop transform', () => testGeneratorInputPipe(true, false, true, 'duplex', [foobarString, 'utf16le']));test('Can use duplexes with subprocess.stdio[0] and encoding "buffer", noop transform', () => testGeneratorInputPipe(false, false, true, 'duplex', [foobarBuffer, 'buffer']));test('Can use duplexes with subprocess.stdin and encoding "buffer", noop transform', () => testGeneratorInputPipe(true, false, true, 'duplex', [foobarBuffer, 'buffer']));test('Can use duplexes with subprocess.stdio[0] and encoding "hex", noop transform', () => testGeneratorInputPipe(false, false, true, 'duplex', [foobarHex, 'hex']));test('Can use duplexes with subprocess.stdin and encoding "hex", noop transform', () => testGeneratorInputPipe(true, false, true, 'duplex', [foobarHex, 'hex']));test('Can use duplexes with subprocess.stdio[0], objectMode, noop transform', () => testGeneratorInputPipe(false, true, true, 'duplex', [foobarObject]));test('Can use duplexes with subprocess.stdin, objectMode, noop transform', () => testGeneratorInputPipe(true, true, true, 'duplex', [foobarObject]));test('Can use webTransforms with subprocess.stdio[0] and default encoding', () => testGeneratorInputPipe(false, false, false, 'webTransform', [foobarString, 'utf8']));test('Can use webTransforms with subprocess.stdin and default encoding', () => testGeneratorInputPipe(true, false, false, 'webTransform', [foobarString, 'utf8']));test('Can use webTransforms with subprocess.stdio[0] and encoding "utf16le"', () => testGeneratorInputPipe(false, false, false, 'webTransform', [foobarString, 'utf16le']));test('Can use webTransforms with subprocess.stdin and encoding "utf16le"', () => testGeneratorInputPipe(true, false, false, 'webTransform', [foobarString, 'utf16le']));test('Can use webTransforms with subprocess.stdio[0] and encoding "buffer"', () => testGeneratorInputPipe(false, false, false, 'webTransform', [foobarBuffer, 'buffer']));test('Can use webTransforms with subprocess.stdin and encoding "buffer"', () => testGeneratorInputPipe(true, false, false, 'webTransform', [foobarBuffer, 'buffer']));test('Can use webTransforms with subprocess.stdio[0] and encoding "hex"', () => testGeneratorInputPipe(false, false, false, 'webTransform', [foobarHex, 'hex']));test('Can use webTransforms with subprocess.stdin and encoding "hex"', () => testGeneratorInputPipe(true, false, false, 'webTransform', [foobarHex, 'hex']));test('Can use webTransforms with subprocess.stdio[0], objectMode', () => testGeneratorInputPipe(false, true, false, 'webTransform', [foobarObject]));test('Can use webTransforms with subprocess.stdin, objectMode', () => testGeneratorInputPipe(true, true, false, 'webTransform', [foobarObject]));test('Can use webTransforms with subprocess.stdio[0] and default encoding, noop transform', () => testGeneratorInputPipe(false, false, true, 'webTransform', [foobarString, 'utf8']));test('Can use webTransforms with subprocess.stdin and default encoding, noop transform', () => testGeneratorInputPipe(true, false, true, 'webTransform', [foobarString, 'utf8']));test('Can use webTransforms with subprocess.stdio[0] and encoding "utf16le", noop transform', () => testGeneratorInputPipe(false, false, true, 'webTransform', [foobarString, 'utf16le']));test('Can use webTransforms with subprocess.stdin and encoding "utf16le", noop transform', () => testGeneratorInputPipe(true, false, true, 'webTransform', [foobarString, 'utf16le']));test('Can use webTransforms with subprocess.stdio[0] and encoding "buffer", noop transform', () => testGeneratorInputPipe(false, false, true, 'webTransform', [foobarBuffer, 'buffer']));test('Can use webTransforms with subprocess.stdin and encoding "buffer", noop transform', () => testGeneratorInputPipe(true, false, true, 'webTransform', [foobarBuffer, 'buffer']));test('Can use webTransforms with subprocess.stdio[0] and encoding "hex", noop transform', () => testGeneratorInputPipe(false, false, true, 'webTransform', [foobarHex, 'hex']));test('Can use webTransforms with subprocess.stdin and encoding "hex", noop transform', () => testGeneratorInputPipe(true, false, true, 'webTransform', [foobarHex, 'hex']));test('Can use webTransforms with subprocess.stdio[0], objectMode, noop transform', () => testGeneratorInputPipe(false, true, true, 'webTransform', [foobarObject]));test('Can use webTransforms with subprocess.stdin, objectMode, noop transform', () => testGeneratorInputPipe(true, true, true, 'webTransform', [foobarObject]));
const testGeneratorStdioInputPipe = async (objectMode, addNoopTransform, type) => { const {input, generators, output} = getInputObjectMode(objectMode, addNoopTransform, type); const subprocess = execa('stdin-fd.js', ['3'], getStdio(3, [[], ...generators])); subprocess.stdio[3].write(Array.isArray(input) ? input[0] : input); const {stdout} = await subprocess; assert.equal(stdout, output);};
test('Can use generators with subprocess.stdio[*] as input', () => testGeneratorStdioInputPipe(false, false, 'generator'));test('Can use generators with subprocess.stdio[*] as input, objectMode', () => testGeneratorStdioInputPipe(true, false, 'generator'));test('Can use generators with subprocess.stdio[*] as input, noop transform', () => testGeneratorStdioInputPipe(false, true, 'generator'));test('Can use generators with subprocess.stdio[*] as input, objectMode, noop transform', () => testGeneratorStdioInputPipe(true, true, 'generator'));test('Can use duplexes with subprocess.stdio[*] as input', () => testGeneratorStdioInputPipe(false, false, 'duplex'));test('Can use duplexes with subprocess.stdio[*] as input, objectMode', () => testGeneratorStdioInputPipe(true, false, 'duplex'));test('Can use duplexes with subprocess.stdio[*] as input, noop transform', () => testGeneratorStdioInputPipe(false, true, 'duplex'));test('Can use duplexes with subprocess.stdio[*] as input, objectMode, noop transform', () => testGeneratorStdioInputPipe(true, true, 'duplex'));test('Can use webTransforms with subprocess.stdio[*] as input', () => testGeneratorStdioInputPipe(false, false, 'webTransform'));test('Can use webTransforms with subprocess.stdio[*] as input, objectMode', () => testGeneratorStdioInputPipe(true, false, 'webTransform'));test('Can use webTransforms with subprocess.stdio[*] as input, noop transform', () => testGeneratorStdioInputPipe(false, true, 'webTransform'));test('Can use webTransforms with subprocess.stdio[*] as input, objectMode, noop transform', () => testGeneratorStdioInputPipe(true, true, 'webTransform'));
// Like with the output, an empty input is not passed as a chunk to binary transforms, which do not split linesconst testEmptyInput = async (options, execaMethod) => { const chunks = []; const {stdout} = await execaMethod('stdin.js', { ...options, stdin: { * transform(chunk) { chunks.push(chunk); yield chunk; }, binary: true, }, }); assert.equal(stdout, ''); assert.deepEqual(chunks, []);};
test('Binary input generators are not called with an empty "input" string', () => testEmptyInput({input: ''}, execa));test('Binary input generators are not called with an empty "input" Uint8Array', () => testEmptyInput({input: new Uint8Array()}, execa));test('Binary input generators are not called with an empty "input" string, sync', () => testEmptyInput({input: ''}, execaSync));test('Binary input generators are not called with an empty "input" Uint8Array, sync', () => testEmptyInput({input: new Uint8Array()}, execaSync));
const testEmptyInputFile = async execaMethod => { const filePath = tempfile(); await writeFile(filePath, ''); try { await testEmptyInput({inputFile: filePath}, execaMethod); } finally { await rm(filePath); }};
test('Binary input generators are not called with an empty "inputFile"', () => testEmptyInputFile(execa));test('Binary input generators are not called with an empty "inputFile", sync', () => testEmptyInputFile(execaSync));
const testEmptyInheritedInput = async isSync => { const {stdout} = await execa('nested-input-transform-chunks.js', [isSync ? 'sync' : 'async'], {input: ''}); assert.equal(stdout, '0');};
test('Binary input generators are not called with an empty inherited stdin', () => testEmptyInheritedInput(false));test('Binary input generators are not called with an empty inherited stdin, sync', () => testEmptyInheritedInput(true));