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
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
TweaksSindre Sorhusbefore adoption698a1b8
line 1why, one step back1import assert from 'node:assert/strict';100% test coverage (#1018)ehmickybefore adoption9f65b86
line 2why, one step back2import {Buffer} from 'node:buffer';Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
line 3why, one step back3import {writeFile, rm} from 'node:fs/promises';TweaksSindre Sorhusbefore adoption698a1b8
line 4why, one step back4import test from 'node:test';Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
line 5why, one step back5import tempfile from 'tempfile';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 6–34why, one step back6import {execa, execaSync} from '../../index.js';7import {setFixtureDirectory} from '../helpers/fixtures-directory.js';8import {getStdio} from '../helpers/stdio.js';9import {10 foobarString,11 foobarUppercase,12 foobarHex,13 foobarUint8Array,14 foobarBuffer,15 foobarObject,16 foobarObjectString,17} from '../helpers/input.js';18import {generatorsMap} from '../helpers/map.js';1920setFixtureDirectory();2122const getInputObjectMode = (objectMode, addNoopTransform, type) => objectMode23 ? {24 input: [foobarObject],25 generators: generatorsMap[type].addNoop(generatorsMap[type].serialize(objectMode), addNoopTransform, objectMode),26 output: foobarObjectString,27 }28 : {29 input: foobarUint8Array,30 generators: generatorsMap[type].addNoop(generatorsMap[type].uppercase(objectMode), addNoopTransform, objectMode),31 output: foobarUppercase,32 };3334// eslint-disable-next-line max-paramsTweaksSindre Sorhusbefore adoption698a1b8
line 35why, one step back35const testGeneratorInput = async (fdNumber, objectMode, addNoopTransform, type, execaMethod) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 36–37why, one step back36 const {input, generators, output} = getInputObjectMode(objectMode, addNoopTransform, type);37 const {stdout} = await execaMethod('stdin-fd.js', [`${fdNumber}`], getStdio(fdNumber, [input, ...generators]));TweaksSindre Sorhusbefore adoption698a1b8
line 38why, one step back38 assert.equal(stdout, output);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 39–40why, one step back39};40TweaksSindre Sorhusbefore adoption698a1b8
lines 41–68why, one step back41test('Can use generators with result.stdin', () => testGeneratorInput(0, false, false, 'generator', execa));42test('Can use generators with result.stdio[*] as input', () => testGeneratorInput(3, false, false, 'generator', execa));43test('Can use generators with result.stdin, objectMode', () => testGeneratorInput(0, true, false, 'generator', execa));44test('Can use generators with result.stdio[*] as input, objectMode', () => testGeneratorInput(3, true, false, 'generator', execa));45test('Can use generators with result.stdin, noop transform', () => testGeneratorInput(0, false, true, 'generator', execa));46test('Can use generators with result.stdio[*] as input, noop transform', () => testGeneratorInput(3, false, true, 'generator', execa));47test('Can use generators with result.stdin, objectMode, noop transform', () => testGeneratorInput(0, true, true, 'generator', execa));48test('Can use generators with result.stdio[*] as input, objectMode, noop transform', () => testGeneratorInput(3, true, true, 'generator', execa));49test('Can use generators with result.stdin, sync', () => testGeneratorInput(0, false, false, 'generator', execaSync));50test('Can use generators with result.stdin, objectMode, sync', () => testGeneratorInput(0, true, false, 'generator', execaSync));51test('Can use generators with result.stdin, noop transform, sync', () => testGeneratorInput(0, false, true, 'generator', execaSync));52test('Can use generators with result.stdin, objectMode, noop transform, sync', () => testGeneratorInput(0, true, true, 'generator', execaSync));53test('Can use duplexes with result.stdin', () => testGeneratorInput(0, false, false, 'duplex', execa));54test('Can use duplexes with result.stdio[*] as input', () => testGeneratorInput(3, false, false, 'duplex', execa));55test('Can use duplexes with result.stdin, objectMode', () => testGeneratorInput(0, true, false, 'duplex', execa));56test('Can use duplexes with result.stdio[*] as input, objectMode', () => testGeneratorInput(3, true, false, 'duplex', execa));57test('Can use duplexes with result.stdin, noop transform', () => testGeneratorInput(0, false, true, 'duplex', execa));58test('Can use duplexes with result.stdio[*] as input, noop transform', () => testGeneratorInput(3, false, true, 'duplex', execa));59test('Can use duplexes with result.stdin, objectMode, noop transform', () => testGeneratorInput(0, true, true, 'duplex', execa));60test('Can use duplexes with result.stdio[*] as input, objectMode, noop transform', () => testGeneratorInput(3, true, true, 'duplex', execa));61test('Can use webTransforms with result.stdin', () => testGeneratorInput(0, false, false, 'webTransform', execa));62test('Can use webTransforms with result.stdio[*] as input', () => testGeneratorInput(3, false, false, 'webTransform', execa));63test('Can use webTransforms with result.stdin, objectMode', () => testGeneratorInput(0, true, false, 'webTransform', execa));64test('Can use webTransforms with result.stdio[*] as input, objectMode', () => testGeneratorInput(3, true, false, 'webTransform', execa));65test('Can use webTransforms with result.stdin, noop transform', () => testGeneratorInput(0, false, true, 'webTransform', execa));66test('Can use webTransforms with result.stdio[*] as input, noop transform', () => testGeneratorInput(3, false, true, 'webTransform', execa));67test('Can use webTransforms with result.stdin, objectMode, noop transform', () => testGeneratorInput(0, true, true, 'webTransform', execa));68test('Can use webTransforms with result.stdio[*] as input, objectMode, noop transform', () => testGeneratorInput(3, true, true, 'webTransform', execa));100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 69–70why, one step back6970// eslint-disable-next-line max-paramsTweaksSindre Sorhusbefore adoption698a1b8
line 71why, one step back71const testGeneratorInputPipe = async (useShortcutProperty, objectMode, addNoopTransform, type, input) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 72–77why, one step back72 const {generators, output} = getInputObjectMode(objectMode, addNoopTransform, type);73 const subprocess = execa('stdin-fd.js', ['0'], getStdio(0, generators));74 const stream = useShortcutProperty ? subprocess.stdin : subprocess.stdio[0];75 stream.end(...input);76 const {stdout} = await subprocess;77 const expectedOutput = input[1] === 'utf16le' ? Buffer.from(output, input[1]).toString() : output;TweaksSindre Sorhusbefore adoption698a1b8
line 78why, one step back78 assert.equal(stdout, expectedOutput);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 79–80why, one step back79};80TweaksSindre Sorhusbefore adoption698a1b8
lines 81–140why, one step back81test('Can use generators with subprocess.stdio[0] and default encoding', () => testGeneratorInputPipe(false, false, false, 'generator', [foobarString, 'utf8']));82test('Can use generators with subprocess.stdin and default encoding', () => testGeneratorInputPipe(true, false, false, 'generator', [foobarString, 'utf8']));83test('Can use generators with subprocess.stdio[0] and encoding "utf16le"', () => testGeneratorInputPipe(false, false, false, 'generator', [foobarString, 'utf16le']));84test('Can use generators with subprocess.stdin and encoding "utf16le"', () => testGeneratorInputPipe(true, false, false, 'generator', [foobarString, 'utf16le']));85test('Can use generators with subprocess.stdio[0] and encoding "buffer"', () => testGeneratorInputPipe(false, false, false, 'generator', [foobarBuffer, 'buffer']));86test('Can use generators with subprocess.stdin and encoding "buffer"', () => testGeneratorInputPipe(true, false, false, 'generator', [foobarBuffer, 'buffer']));87test('Can use generators with subprocess.stdio[0] and encoding "hex"', () => testGeneratorInputPipe(false, false, false, 'generator', [foobarHex, 'hex']));88test('Can use generators with subprocess.stdin and encoding "hex"', () => testGeneratorInputPipe(true, false, false, 'generator', [foobarHex, 'hex']));89test('Can use generators with subprocess.stdio[0], objectMode', () => testGeneratorInputPipe(false, true, false, 'generator', [foobarObject]));90test('Can use generators with subprocess.stdin, objectMode', () => testGeneratorInputPipe(true, true, false, 'generator', [foobarObject]));91test('Can use generators with subprocess.stdio[0] and default encoding, noop transform', () => testGeneratorInputPipe(false, false, true, 'generator', [foobarString, 'utf8']));92test('Can use generators with subprocess.stdin and default encoding, noop transform', () => testGeneratorInputPipe(true, false, true, 'generator', [foobarString, 'utf8']));93test('Can use generators with subprocess.stdio[0] and encoding "utf16le", noop transform', () => testGeneratorInputPipe(false, false, true, 'generator', [foobarString, 'utf16le']));94test('Can use generators with subprocess.stdin and encoding "utf16le", noop transform', () => testGeneratorInputPipe(true, false, true, 'generator', [foobarString, 'utf16le']));95test('Can use generators with subprocess.stdio[0] and encoding "buffer", noop transform', () => testGeneratorInputPipe(false, false, true, 'generator', [foobarBuffer, 'buffer']));96test('Can use generators with subprocess.stdin and encoding "buffer", noop transform', () => testGeneratorInputPipe(true, false, true, 'generator', [foobarBuffer, 'buffer']));97test('Can use generators with subprocess.stdio[0] and encoding "hex", noop transform', () => testGeneratorInputPipe(false, false, true, 'generator', [foobarHex, 'hex']));98test('Can use generators with subprocess.stdin and encoding "hex", noop transform', () => testGeneratorInputPipe(true, false, true, 'generator', [foobarHex, 'hex']));99test('Can use generators with subprocess.stdio[0], objectMode, noop transform', () => testGeneratorInputPipe(false, true, true, 'generator', [foobarObject]));100test('Can use generators with subprocess.stdin, objectMode, noop transform', () => testGeneratorInputPipe(true, true, true, 'generator', [foobarObject]));101test('Can use duplexes with subprocess.stdio[0] and default encoding', () => testGeneratorInputPipe(false, false, false, 'duplex', [foobarString, 'utf8']));102test('Can use duplexes with subprocess.stdin and default encoding', () => testGeneratorInputPipe(true, false, false, 'duplex', [foobarString, 'utf8']));103test('Can use duplexes with subprocess.stdio[0] and encoding "utf16le"', () => testGeneratorInputPipe(false, false, false, 'duplex', [foobarString, 'utf16le']));104test('Can use duplexes with subprocess.stdin and encoding "utf16le"', () => testGeneratorInputPipe(true, false, false, 'duplex', [foobarString, 'utf16le']));105test('Can use duplexes with subprocess.stdio[0] and encoding "buffer"', () => testGeneratorInputPipe(false, false, false, 'duplex', [foobarBuffer, 'buffer']));106test('Can use duplexes with subprocess.stdin and encoding "buffer"', () => testGeneratorInputPipe(true, false, false, 'duplex', [foobarBuffer, 'buffer']));107test('Can use duplexes with subprocess.stdio[0] and encoding "hex"', () => testGeneratorInputPipe(false, false, false, 'duplex', [foobarHex, 'hex']));108test('Can use duplexes with subprocess.stdin and encoding "hex"', () => testGeneratorInputPipe(true, false, false, 'duplex', [foobarHex, 'hex']));109test('Can use duplexes with subprocess.stdio[0], objectMode', () => testGeneratorInputPipe(false, true, false, 'duplex', [foobarObject]));110test('Can use duplexes with subprocess.stdin, objectMode', () => testGeneratorInputPipe(true, true, false, 'duplex', [foobarObject]));111test('Can use duplexes with subprocess.stdio[0] and default encoding, noop transform', () => testGeneratorInputPipe(false, false, true, 'duplex', [foobarString, 'utf8']));112test('Can use duplexes with subprocess.stdin and default encoding, noop transform', () => testGeneratorInputPipe(true, false, true, 'duplex', [foobarString, 'utf8']));113test('Can use duplexes with subprocess.stdio[0] and encoding "utf16le", noop transform', () => testGeneratorInputPipe(false, false, true, 'duplex', [foobarString, 'utf16le']));114test('Can use duplexes with subprocess.stdin and encoding "utf16le", noop transform', () => testGeneratorInputPipe(true, false, true, 'duplex', [foobarString, 'utf16le']));115test('Can use duplexes with subprocess.stdio[0] and encoding "buffer", noop transform', () => testGeneratorInputPipe(false, false, true, 'duplex', [foobarBuffer, 'buffer']));116test('Can use duplexes with subprocess.stdin and encoding "buffer", noop transform', () => testGeneratorInputPipe(true, false, true, 'duplex', [foobarBuffer, 'buffer']));117test('Can use duplexes with subprocess.stdio[0] and encoding "hex", noop transform', () => testGeneratorInputPipe(false, false, true, 'duplex', [foobarHex, 'hex']));118test('Can use duplexes with subprocess.stdin and encoding "hex", noop transform', () => testGeneratorInputPipe(true, false, true, 'duplex', [foobarHex, 'hex']));119test('Can use duplexes with subprocess.stdio[0], objectMode, noop transform', () => testGeneratorInputPipe(false, true, true, 'duplex', [foobarObject]));120test('Can use duplexes with subprocess.stdin, objectMode, noop transform', () => testGeneratorInputPipe(true, true, true, 'duplex', [foobarObject]));121test('Can use webTransforms with subprocess.stdio[0] and default encoding', () => testGeneratorInputPipe(false, false, false, 'webTransform', [foobarString, 'utf8']));122test('Can use webTransforms with subprocess.stdin and default encoding', () => testGeneratorInputPipe(true, false, false, 'webTransform', [foobarString, 'utf8']));123test('Can use webTransforms with subprocess.stdio[0] and encoding "utf16le"', () => testGeneratorInputPipe(false, false, false, 'webTransform', [foobarString, 'utf16le']));124test('Can use webTransforms with subprocess.stdin and encoding "utf16le"', () => testGeneratorInputPipe(true, false, false, 'webTransform', [foobarString, 'utf16le']));125test('Can use webTransforms with subprocess.stdio[0] and encoding "buffer"', () => testGeneratorInputPipe(false, false, false, 'webTransform', [foobarBuffer, 'buffer']));126test('Can use webTransforms with subprocess.stdin and encoding "buffer"', () => testGeneratorInputPipe(true, false, false, 'webTransform', [foobarBuffer, 'buffer']));127test('Can use webTransforms with subprocess.stdio[0] and encoding "hex"', () => testGeneratorInputPipe(false, false, false, 'webTransform', [foobarHex, 'hex']));128test('Can use webTransforms with subprocess.stdin and encoding "hex"', () => testGeneratorInputPipe(true, false, false, 'webTransform', [foobarHex, 'hex']));129test('Can use webTransforms with subprocess.stdio[0], objectMode', () => testGeneratorInputPipe(false, true, false, 'webTransform', [foobarObject]));130test('Can use webTransforms with subprocess.stdin, objectMode', () => testGeneratorInputPipe(true, true, false, 'webTransform', [foobarObject]));131test('Can use webTransforms with subprocess.stdio[0] and default encoding, noop transform', () => testGeneratorInputPipe(false, false, true, 'webTransform', [foobarString, 'utf8']));132test('Can use webTransforms with subprocess.stdin and default encoding, noop transform', () => testGeneratorInputPipe(true, false, true, 'webTransform', [foobarString, 'utf8']));133test('Can use webTransforms with subprocess.stdio[0] and encoding "utf16le", noop transform', () => testGeneratorInputPipe(false, false, true, 'webTransform', [foobarString, 'utf16le']));134test('Can use webTransforms with subprocess.stdin and encoding "utf16le", noop transform', () => testGeneratorInputPipe(true, false, true, 'webTransform', [foobarString, 'utf16le']));135test('Can use webTransforms with subprocess.stdio[0] and encoding "buffer", noop transform', () => testGeneratorInputPipe(false, false, true, 'webTransform', [foobarBuffer, 'buffer']));136test('Can use webTransforms with subprocess.stdin and encoding "buffer", noop transform', () => testGeneratorInputPipe(true, false, true, 'webTransform', [foobarBuffer, 'buffer']));137test('Can use webTransforms with subprocess.stdio[0] and encoding "hex", noop transform', () => testGeneratorInputPipe(false, false, true, 'webTransform', [foobarHex, 'hex']));138test('Can use webTransforms with subprocess.stdin and encoding "hex", noop transform', () => testGeneratorInputPipe(true, false, true, 'webTransform', [foobarHex, 'hex']));139test('Can use webTransforms with subprocess.stdio[0], objectMode, noop transform', () => testGeneratorInputPipe(false, true, true, 'webTransform', [foobarObject]));140test('Can use webTransforms with subprocess.stdin, objectMode, noop transform', () => testGeneratorInputPipe(true, true, true, 'webTransform', [foobarObject]));100% test coverage (#1018)ehmickybefore adoption9f65b86
line 141why, one step back141TweaksSindre Sorhusbefore adoption698a1b8
line 142why, one step back142const testGeneratorStdioInputPipe = async (objectMode, addNoopTransform, type) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 143–146why, one step back143 const {input, generators, output} = getInputObjectMode(objectMode, addNoopTransform, type);144 const subprocess = execa('stdin-fd.js', ['3'], getStdio(3, [[], ...generators]));145 subprocess.stdio[3].write(Array.isArray(input) ? input[0] : input);146 const {stdout} = await subprocess;TweaksSindre Sorhusbefore adoption698a1b8
line 147why, one step back147 assert.equal(stdout, output);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 148–149why, one step back148};149TweaksSindre Sorhusbefore adoption698a1b8
lines 150–161why, one step back150test('Can use generators with subprocess.stdio[*] as input', () => testGeneratorStdioInputPipe(false, false, 'generator'));151test('Can use generators with subprocess.stdio[*] as input, objectMode', () => testGeneratorStdioInputPipe(true, false, 'generator'));152test('Can use generators with subprocess.stdio[*] as input, noop transform', () => testGeneratorStdioInputPipe(false, true, 'generator'));153test('Can use generators with subprocess.stdio[*] as input, objectMode, noop transform', () => testGeneratorStdioInputPipe(true, true, 'generator'));154test('Can use duplexes with subprocess.stdio[*] as input', () => testGeneratorStdioInputPipe(false, false, 'duplex'));155test('Can use duplexes with subprocess.stdio[*] as input, objectMode', () => testGeneratorStdioInputPipe(true, false, 'duplex'));156test('Can use duplexes with subprocess.stdio[*] as input, noop transform', () => testGeneratorStdioInputPipe(false, true, 'duplex'));157test('Can use duplexes with subprocess.stdio[*] as input, objectMode, noop transform', () => testGeneratorStdioInputPipe(true, true, 'duplex'));158test('Can use webTransforms with subprocess.stdio[*] as input', () => testGeneratorStdioInputPipe(false, false, 'webTransform'));159test('Can use webTransforms with subprocess.stdio[*] as input, objectMode', () => testGeneratorStdioInputPipe(true, false, 'webTransform'));160test('Can use webTransforms with subprocess.stdio[*] as input, noop transform', () => testGeneratorStdioInputPipe(false, true, 'webTransform'));161test('Can use webTransforms with subprocess.stdio[*] as input, objectMode, noop transform', () => testGeneratorStdioInputPipe(true, true, 'webTransform'));Fix `stdin` iterables and input transforms edge casesSindre Sorhusbefore adoption6925cc3
lines 162–204why, one step back162163// Like with the output, an empty input is not passed as a chunk to binary transforms, which do not split lines164const testEmptyInput = async (options, execaMethod) => {165 const chunks = [];166 const {stdout} = await execaMethod('stdin.js', {167 ...options,168 stdin: {169 * transform(chunk) {170 chunks.push(chunk);171 yield chunk;172 },173 binary: true,174 },175 });176 assert.equal(stdout, '');177 assert.deepEqual(chunks, []);178};179180test('Binary input generators are not called with an empty "input" string', () => testEmptyInput({input: ''}, execa));181test('Binary input generators are not called with an empty "input" Uint8Array', () => testEmptyInput({input: new Uint8Array()}, execa));182test('Binary input generators are not called with an empty "input" string, sync', () => testEmptyInput({input: ''}, execaSync));183test('Binary input generators are not called with an empty "input" Uint8Array, sync', () => testEmptyInput({input: new Uint8Array()}, execaSync));184185const testEmptyInputFile = async execaMethod => {186 const filePath = tempfile();187 await writeFile(filePath, '');188 try {189 await testEmptyInput({inputFile: filePath}, execaMethod);190 } finally {191 await rm(filePath);192 }193};194195test('Binary input generators are not called with an empty "inputFile"', () => testEmptyInputFile(execa));196test('Binary input generators are not called with an empty "inputFile", sync', () => testEmptyInputFile(execaSync));197198const testEmptyInheritedInput = async isSync => {199 const {stdout} = await execa('nested-input-transform-chunks.js', [isSync ? 'sync' : 'async'], {input: ''});200 assert.equal(stdout, '0');201};202203test('Binary input generators are not called with an empty inherited stdin', () => testEmptyInheritedInput(false));204test('Binary input generators are not called with an empty inherited stdin, sync', () => testEmptyInheritedInput(true));