generator.js
This file
Path
test/stdio/generator.js
653 lines · 38.2 KiB · JavaScript
Last changed by
Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf
History of this fileTake it
Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 1–3why, one step back1import {Buffer} from 'node:buffer';2import {readFile, writeFile, rm} from 'node:fs/promises';3import {getDefaultHighWaterMark, PassThrough} from 'node:stream';Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf
line 4why, one step back4import {setTimeout, setImmediate} from 'node:timers/promises';Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 5why, one step back5import test from 'ava';Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 6why, one step back6import getStream, {getStreamAsArray} from 'get-stream';Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 7–10why, one step back7import tempfile from 'tempfile';8import {execa, execaSync} from '../../index.js';9import {setFixtureDir} from '../helpers/fixtures-dir.js';10import {getStdio} from '../helpers/stdio.js';Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 11–12why, one step back11import {foobarString, foobarUint8Array, foobarBuffer, foobarObject, foobarObjectString} from '../helpers/input.js';12import {serializeGenerator, noopGenerator, getOutputsGenerator, getOutputGenerator, outputObjectGenerator} from '../helpers/generator.js';Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 13–15why, one step back1314setFixtureDir();15Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 16–18why, one step back16const textEncoder = new TextEncoder();17const textDecoder = new TextDecoder();18Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 19why, one step back19const foobarUppercase = foobarString.toUpperCase();Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 20why, one step back20const foobarHex = foobarBuffer.toString('hex');Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 21why, one step back21Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 22–24why, one step back22const uppercaseGenerator = async function * (lines) {23 for await (const line of lines) {24 yield line.toUpperCase();Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 25–27why, one step back25 }26};27Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 28–31why, one step back28const uppercaseBufferGenerator = async function * (lines) {29 for await (const line of lines) {30 yield textDecoder.decode(line).toUpperCase();31 }Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 32–33why, one step back32};33Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 34–36why, one step back34const getInputObjectMode = objectMode => objectMode35 ? {input: [foobarObject], generator: serializeGenerator, output: foobarObjectString}36 : {input: foobarUint8Array, generator: uppercaseGenerator, output: foobarUppercase};Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 37why, one step back37Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 38–57why, one step back38const getOutputObjectMode = objectMode => objectMode39 ? {generator: outputObjectGenerator, output: [foobarObject], getStreamMethod: getStreamAsArray}40 : {generator: uppercaseGenerator, output: foobarUppercase, getStreamMethod: getStream};4142const testGeneratorInput = async (t, index, objectMode) => {43 const {input, generator, output} = getInputObjectMode(objectMode);44 const {stdout} = await execa('stdin-fd.js', [`${index}`], getStdio(index, [input, generator]));45 t.is(stdout, output);46};4748test('Can use generators with result.stdin', testGeneratorInput, 0, false);49test('Can use generators with result.stdio[*] as input', testGeneratorInput, 3, false);50test('Can use generators with result.stdin, objectMode', testGeneratorInput, 0, true);51test('Can use generators with result.stdio[*] as input, objectMode', testGeneratorInput, 3, true);5253const testGeneratorInputPipe = async (t, useShortcutProperty, objectMode, input) => {54 const {generator, output} = getInputObjectMode(objectMode);55 const childProcess = execa('stdin-fd.js', ['0'], getStdio(0, generator));56 const stream = useShortcutProperty ? childProcess.stdin : childProcess.stdio[0];57 stream.end(...input);Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 58why, one step back58 const {stdout} = await childProcess;Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 59why, one step back59 t.is(stdout, output);Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 60–61why, one step back60};61Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 62–69why, one step back62test('Can use generators with childProcess.stdio[0] and default encoding', testGeneratorInputPipe, false, false, [foobarString, 'utf8']);63test('Can use generators with childProcess.stdin and default encoding', testGeneratorInputPipe, true, false, [foobarString, 'utf8']);64test('Can use generators with childProcess.stdio[0] and encoding "buffer"', testGeneratorInputPipe, false, false, [foobarBuffer, 'buffer']);65test('Can use generators with childProcess.stdin and encoding "buffer"', testGeneratorInputPipe, true, false, [foobarBuffer, 'buffer']);66test('Can use generators with childProcess.stdio[0] and encoding "hex"', testGeneratorInputPipe, false, false, [foobarHex, 'hex']);67test('Can use generators with childProcess.stdin and encoding "hex"', testGeneratorInputPipe, true, false, [foobarHex, 'hex']);68test('Can use generators with childProcess.stdio[0], objectMode', testGeneratorInputPipe, false, true, [foobarObject]);69test('Can use generators with childProcess.stdin, objectMode', testGeneratorInputPipe, true, true, [foobarObject]);Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 70why, one step back70Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 71–74why, one step back71const testGeneratorStdioInputPipe = async (t, objectMode) => {72 const {input, generator, output} = getInputObjectMode(objectMode);73 const childProcess = execa('stdin-fd.js', ['3'], getStdio(3, [new Uint8Array(), generator]));74 childProcess.stdio[3].write(Array.isArray(input) ? input[0] : input);Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 75why, one step back75 const {stdout} = await childProcess;Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 76–77why, one step back76 t.is(stdout, output);77};Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 78why, one step back78Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 79–103why, one step back79test('Can use generators with childProcess.stdio[*] as input', testGeneratorStdioInputPipe, false);80test('Can use generators with childProcess.stdio[*] as input, objectMode', testGeneratorStdioInputPipe, true);8182const testGeneratorReturn = async (t, index, generators, fixtureName) => {83 await t.throwsAsync(84 execa(fixtureName, [`${index}`], getStdio(index, generators)),85 {message: /a string or an Uint8Array/},86 );87};8889const inputObjectGenerators = [foobarUint8Array, getOutputGenerator(foobarObject, false), serializeGenerator];90const lastInputObjectGenerators = [foobarUint8Array, getOutputGenerator(foobarObject, true)];91const invalidOutputObjectGenerator = getOutputGenerator(foobarObject, false);9293test('Generators with result.stdin cannot return an object if not in objectMode', testGeneratorReturn, 0, inputObjectGenerators, 'stdin-fd.js');94test('Generators with result.stdio[*] as input cannot return an object if not in objectMode', testGeneratorReturn, 3, inputObjectGenerators, 'stdin-fd.js');95test('The last generator with result.stdin cannot return an object even in objectMode', testGeneratorReturn, 0, lastInputObjectGenerators, 'stdin-fd.js');96test('The last generator with result.stdio[*] as input cannot return an object even in objectMode', testGeneratorReturn, 3, lastInputObjectGenerators, 'stdin-fd.js');97test('Generators with result.stdout cannot return an object if not in objectMode', testGeneratorReturn, 1, invalidOutputObjectGenerator, 'noop-fd.js');98test('Generators with result.stderr cannot return an object if not in objectMode', testGeneratorReturn, 2, invalidOutputObjectGenerator, 'noop-fd.js');99test('Generators with result.stdio[*] as output cannot return an object if not in objectMode', testGeneratorReturn, 3, invalidOutputObjectGenerator, 'noop-fd.js');100101// eslint-disable-next-line max-params102const testGeneratorOutput = async (t, index, reject, useShortcutProperty, objectMode) => {103 const {generator, output} = getOutputObjectMode(objectMode);Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 104why, one step back104 const fixtureName = reject ? 'noop-fd.js' : 'noop-fail.js';Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 105why, one step back105 const {stdout, stderr, stdio} = await execa(fixtureName, [`${index}`, foobarString], {...getStdio(index, generator), reject});Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 106why, one step back106 const result = useShortcutProperty ? [stdout, stderr][index - 1] : stdio[index];Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 107why, one step back107 t.deepEqual(result, output);Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 108–109why, one step back108};109Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 110–129why, one step back110test('Can use generators with result.stdio[1]', testGeneratorOutput, 1, true, false, false);111test('Can use generators with result.stdout', testGeneratorOutput, 1, true, true, false);112test('Can use generators with result.stdio[2]', testGeneratorOutput, 2, true, false, false);113test('Can use generators with result.stderr', testGeneratorOutput, 2, true, true, false);114test('Can use generators with result.stdio[*] as output', testGeneratorOutput, 3, true, false, false);115test('Can use generators with error.stdio[1]', testGeneratorOutput, 1, false, false, false);116test('Can use generators with error.stdout', testGeneratorOutput, 1, false, true, false);117test('Can use generators with error.stdio[2]', testGeneratorOutput, 2, false, false, false);118test('Can use generators with error.stderr', testGeneratorOutput, 2, false, true, false);119test('Can use generators with error.stdio[*] as output', testGeneratorOutput, 3, false, false, false);120test('Can use generators with result.stdio[1], objectMode', testGeneratorOutput, 1, true, false, true);121test('Can use generators with result.stdout, objectMode', testGeneratorOutput, 1, true, true, true);122test('Can use generators with result.stdio[2], objectMode', testGeneratorOutput, 2, true, false, true);123test('Can use generators with result.stderr, objectMode', testGeneratorOutput, 2, true, true, true);124test('Can use generators with result.stdio[*] as output, objectMode', testGeneratorOutput, 3, true, false, true);125test('Can use generators with error.stdio[1], objectMode', testGeneratorOutput, 1, false, false, true);126test('Can use generators with error.stdout, objectMode', testGeneratorOutput, 1, false, true, true);127test('Can use generators with error.stdio[2], objectMode', testGeneratorOutput, 2, false, false, true);128test('Can use generators with error.stderr, objectMode', testGeneratorOutput, 2, false, true, true);129test('Can use generators with error.stdio[*] as output, objectMode', testGeneratorOutput, 3, false, false, true);Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 130why, one step back130Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 131–133why, one step back131const testGeneratorOutputPipe = async (t, index, useShortcutProperty, objectMode) => {132 const {generator, output, getStreamMethod} = getOutputObjectMode(objectMode);133 const childProcess = execa('noop-fd.js', [`${index}`, foobarString], {...getStdio(index, generator), buffer: false});Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 134why, one step back134 const stream = useShortcutProperty ? [childProcess.stdout, childProcess.stderr][index - 1] : childProcess.stdio[index];Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 135–136why, one step back135 const [result] = await Promise.all([getStreamMethod(stream), childProcess]);136 t.deepEqual(result, output);Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 137–138why, one step back137};138Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 139–148why, one step back139test('Can use generators with childProcess.stdio[1]', testGeneratorOutputPipe, 1, false, false);140test('Can use generators with childProcess.stdout', testGeneratorOutputPipe, 1, true, false);141test('Can use generators with childProcess.stdio[2]', testGeneratorOutputPipe, 2, false, false);142test('Can use generators with childProcess.stderr', testGeneratorOutputPipe, 2, true, false);143test('Can use generators with childProcess.stdio[*] as output', testGeneratorOutputPipe, 3, false, false);144test('Can use generators with childProcess.stdio[1], objectMode', testGeneratorOutputPipe, 1, false, true);145test('Can use generators with childProcess.stdout, objectMode', testGeneratorOutputPipe, 1, true, true);146test('Can use generators with childProcess.stdio[2], objectMode', testGeneratorOutputPipe, 2, false, true);147test('Can use generators with childProcess.stderr, objectMode', testGeneratorOutputPipe, 2, true, true);148test('Can use generators with childProcess.stdio[*] as output, objectMode', testGeneratorOutputPipe, 3, false, true);Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 149why, one step back149Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 150–182why, one step back150const getAllStdioOption = (stdioOption, encoding, objectMode) => {151 if (stdioOption) {152 return 'pipe';153 }154155 if (objectMode) {156 return outputObjectGenerator;157 }158159 return encoding === 'buffer' ? uppercaseBufferGenerator : uppercaseGenerator;160};161162const getStdoutStderrOutput = (output, stdioOption, encoding, objectMode) => {163 if (objectMode && !stdioOption) {164 return [foobarObject];165 }166167 const stdioOutput = stdioOption ? output : output.toUpperCase();168 return encoding === 'buffer' ? textEncoder.encode(stdioOutput) : stdioOutput;169};170171const getAllOutput = (stdoutOutput, stderrOutput, encoding, objectMode) => {172 if (objectMode) {173 return [stdoutOutput, stderrOutput].flat();174 }175176 return encoding === 'buffer'177 ? new Uint8Array([...stdoutOutput, ...stderrOutput])178 : `${stdoutOutput}${stderrOutput}`;179};180181// eslint-disable-next-line max-params182const testGeneratorAll = async (t, reject, encoding, objectMode, stdoutOption, stderrOption) => {Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 183why, one step back183 const fixtureName = reject ? 'all.js' : 'all-fail.js';Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 184–198why, one step back184 const {stdout, stderr, all} = await execa(fixtureName, {185 all: true,186 reject,187 stdout: getAllStdioOption(stdoutOption, encoding, objectMode),188 stderr: getAllStdioOption(stderrOption, encoding, objectMode),189 encoding,190 stripFinalNewline: false,191 });192193 const stdoutOutput = getStdoutStderrOutput('stdout\n', stdoutOption, encoding, objectMode);194 t.deepEqual(stdout, stdoutOutput);195 const stderrOutput = getStdoutStderrOutput('stderr\n', stderrOption, encoding, objectMode);196 t.deepEqual(stderr, stderrOutput);197 const allOutput = getAllOutput(stdoutOutput, stderrOutput, encoding, objectMode);198 t.deepEqual(all, allOutput);Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 199–200why, one step back199};200Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 201–224why, one step back201test('Can use generators with result.all = transform + transform', testGeneratorAll, true, 'utf8', false, false, false);202test('Can use generators with error.all = transform + transform', testGeneratorAll, false, 'utf8', false, false, false);203test('Can use generators with result.all = transform + transform, encoding "buffer"', testGeneratorAll, true, 'buffer', false, false, false);204test('Can use generators with error.all = transform + transform, encoding "buffer"', testGeneratorAll, false, 'buffer', false, false, false);205test('Can use generators with result.all = transform + pipe', testGeneratorAll, true, 'utf8', false, false, true);206test('Can use generators with error.all = transform + pipe', testGeneratorAll, false, 'utf8', false, false, true);207test('Can use generators with result.all = transform + pipe, encoding "buffer"', testGeneratorAll, true, 'buffer', false, false, true);208test('Can use generators with error.all = transform + pipe, encoding "buffer"', testGeneratorAll, false, 'buffer', false, false, true);209test('Can use generators with result.all = pipe + transform', testGeneratorAll, true, 'utf8', false, true, false);210test('Can use generators with error.all = pipe + transform', testGeneratorAll, false, 'utf8', false, true, false);211test('Can use generators with result.all = pipe + transform, encoding "buffer"', testGeneratorAll, true, 'buffer', false, true, false);212test('Can use generators with error.all = pipe + transform, encoding "buffer"', testGeneratorAll, false, 'buffer', false, true, false);213test('Can use generators with result.all = transform + transform, objectMode', testGeneratorAll, true, 'utf8', true, false, false);214test('Can use generators with error.all = transform + transform, objectMode', testGeneratorAll, false, 'utf8', true, false, false);215test('Can use generators with result.all = transform + transform, objectMode, encoding "buffer"', testGeneratorAll, true, 'buffer', true, false, false);216test('Can use generators with error.all = transform + transform, objectMode, encoding "buffer"', testGeneratorAll, false, 'buffer', true, false, false);217test('Can use generators with result.all = transform + pipe, objectMode', testGeneratorAll, true, 'utf8', true, false, true);218test('Can use generators with error.all = transform + pipe, objectMode', testGeneratorAll, false, 'utf8', true, false, true);219test('Can use generators with result.all = transform + pipe, objectMode, encoding "buffer"', testGeneratorAll, true, 'buffer', true, false, true);220test('Can use generators with error.all = transform + pipe, objectMode, encoding "buffer"', testGeneratorAll, false, 'buffer', true, false, true);221test('Can use generators with result.all = pipe + transform, objectMode', testGeneratorAll, true, 'utf8', true, true, false);222test('Can use generators with error.all = pipe + transform, objectMode', testGeneratorAll, false, 'utf8', true, true, false);223test('Can use generators with result.all = pipe + transform, objectMode, encoding "buffer"', testGeneratorAll, true, 'buffer', true, true, false);224test('Can use generators with error.all = pipe + transform, objectMode, encoding "buffer"', testGeneratorAll, false, 'buffer', true, true, false);Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 225–232why, one step back225226test('Can use generators with input option', async t => {227 const {stdout} = await execa('stdin-fd.js', ['0'], {stdin: uppercaseGenerator, input: foobarUint8Array});228 t.is(stdout, foobarUppercase);229});230231const syncGenerator = function * () {};232Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 233why, one step back233const testInvalidGenerator = (t, index, stdioOption) => {Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 234why, one step back234 t.throws(() => {Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 235why, one step back235 execa('empty.js', getStdio(index, stdioOption));Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 236–238why, one step back236 }, {message: /asynchronous generator/});237};238Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 239–250why, one step back239test('Cannot use sync generators with stdin', testInvalidGenerator, 0, syncGenerator);240test('Cannot use sync generators with stdout', testInvalidGenerator, 1, syncGenerator);241test('Cannot use sync generators with stderr', testInvalidGenerator, 2, syncGenerator);242test('Cannot use sync generators with stdio[*]', testInvalidGenerator, 3, syncGenerator);243test('Cannot use sync generators with stdin, with options', testInvalidGenerator, 0, {transform: syncGenerator});244test('Cannot use sync generators with stdout, with options', testInvalidGenerator, 1, {transform: syncGenerator});245test('Cannot use sync generators with stderr, with options', testInvalidGenerator, 2, {transform: syncGenerator});246test('Cannot use sync generators with stdio[*], with options', testInvalidGenerator, 3, {transform: syncGenerator});247test('Cannot use invalid "transform" with stdin', testInvalidGenerator, 0, {transform: true});248test('Cannot use invalid "transform" with stdout', testInvalidGenerator, 1, {transform: true});249test('Cannot use invalid "transform" with stderr', testInvalidGenerator, 2, {transform: true});250test('Cannot use invalid "transform" with stdio[*]', testInvalidGenerator, 3, {transform: true});Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 251why, one step back251Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 252why, one step back252const testInvalidBinary = (t, index, optionName) => {Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 253why, one step back253 t.throws(() => {Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 254why, one step back254 execa('empty.js', getStdio(index, {transform: uppercaseGenerator, [optionName]: 'true'}));Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 255–257why, one step back255 }, {message: /a boolean/});256};257Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 258–265why, one step back258test('Cannot use invalid "binary" with stdin', testInvalidBinary, 0, 'binary');259test('Cannot use invalid "binary" with stdout', testInvalidBinary, 1, 'binary');260test('Cannot use invalid "binary" with stderr', testInvalidBinary, 2, 'binary');261test('Cannot use invalid "binary" with stdio[*]', testInvalidBinary, 3, 'binary');262test('Cannot use invalid "objectMode" with stdin', testInvalidBinary, 0, 'objectMode');263test('Cannot use invalid "objectMode" with stdout', testInvalidBinary, 1, 'objectMode');264test('Cannot use invalid "objectMode" with stderr', testInvalidBinary, 2, 'objectMode');265test('Cannot use invalid "objectMode" with stdio[*]', testInvalidBinary, 3, 'objectMode');Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 266why, one step back266Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 267–279why, one step back267const testSyncMethods = (t, index) => {268 t.throws(() => {269 execaSync('empty.js', getStdio(index, uppercaseGenerator));270 }, {message: /cannot be a generator/});271};272273test('Cannot use generators with sync methods and stdin', testSyncMethods, 0);274test('Cannot use generators with sync methods and stdout', testSyncMethods, 1);275test('Cannot use generators with sync methods and stderr', testSyncMethods, 2);276test('Cannot use generators with sync methods and stdio[*]', testSyncMethods, 3);277278const repeatHighWaterMark = 10;279Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 280why, one step back280const writerGenerator = async function * (lines) {Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 281why, one step back281 // eslint-disable-next-line no-unused-varsLine-wise transforms (#699)ehmickybefore adoption6dd804f
line 282why, one step back282 for await (const line of lines) {Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 283why, one step back283 for (let index = 0; index < getDefaultHighWaterMark() * repeatHighWaterMark; index += 1) {Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 284why, one step back284 yield '\n';Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 285–289why, one step back285 }286 }287};288289const passThroughGenerator = async function * (chunks) {Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 290–293why, one step back290 yield * chunks;291};292293const getLengthGenerator = async function * (chunks) {Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 294–298why, one step back294 for await (const chunk of chunks) {295 yield `${chunk.length}`;296 }297};298Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 299–307why, one step back299const testHighWaterMark = async (t, passThrough, binary, objectMode) => {300 const {stdout} = await execa('noop.js', {301 stdout: [302 ...(objectMode ? [outputObjectGenerator] : []),303 writerGenerator,304 ...(passThrough ? [{transform: passThroughGenerator, binary}] : []),305 {transform: getLengthGenerator, binary: true},306 ],307 });Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 308why, one step back308 t.is(stdout, `${getDefaultHighWaterMark()}`.repeat(repeatHighWaterMark));Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 309why, one step back309};Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 310why, one step back310Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 311–314why, one step back311test('Stream respects highWaterMark, no passThrough', testHighWaterMark, false, false, false);312test('Stream respects highWaterMark, line-wise passThrough', testHighWaterMark, true, false, false);313test('Stream respects highWaterMark, binary passThrough', testHighWaterMark, true, true, false);314test('Stream respects highWaterMark, objectMode as input but not output', testHighWaterMark, false, false, true);Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 315why, one step back315Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 316–323why, one step back316const getTypeofGenerator = objectMode => ({317 async * transform(lines) {318 for await (const line of lines) {319 yield Object.prototype.toString.call(line);320 }321 },322 objectMode,323});Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 324why, one step back324Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 325–327why, one step back325// eslint-disable-next-line max-params326const testGeneratorFirstEncoding = async (t, input, encoding, output, objectMode) => {327 const childProcess = execa('stdin.js', {stdin: getTypeofGenerator(objectMode), encoding});Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 328–329why, one step back328 childProcess.stdin.end(input);329 const {stdout} = await childProcess;Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 330–331why, one step back330 const result = Buffer.from(stdout, encoding).toString();331 t.is(result, output);Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 332–333why, one step back332};333Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 334–344why, one step back334test('First generator argument is string with default encoding, with string writes', testGeneratorFirstEncoding, foobarString, 'utf8', '[object String]', false);335test('First generator argument is string with default encoding, with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'utf8', '[object String]', false);336test('First generator argument is string with default encoding, with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'utf8', '[object String]', false);337test('First generator argument is Uint8Array with encoding "buffer", with string writes', testGeneratorFirstEncoding, foobarString, 'buffer', '[object Uint8Array]', false);338test('First generator argument is Uint8Array with encoding "buffer", with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'buffer', '[object Uint8Array]', false);339test('First generator argument is Uint8Array with encoding "buffer", with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'buffer', '[object Uint8Array]', false);340test('First generator argument is Uint8Array with encoding "hex", with string writes', testGeneratorFirstEncoding, foobarString, 'hex', '[object String]', false);341test('First generator argument is Uint8Array with encoding "hex", with Buffer writes', testGeneratorFirstEncoding, foobarBuffer, 'hex', '[object String]', false);342test('First generator argument is Uint8Array with encoding "hex", with Uint8Array writes', testGeneratorFirstEncoding, foobarUint8Array, 'hex', '[object String]', false);343test('First generator argument can be string with objectMode', testGeneratorFirstEncoding, foobarString, 'utf8', '[object String]', true);344test('First generator argument can be objects with objectMode', testGeneratorFirstEncoding, foobarObject, 'utf8', '[object Object]', true);Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 345why, one step back345Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 346–351why, one step back346const testEncodingIgnored = async (t, encoding) => {347 const input = Buffer.from(foobarString).toString(encoding);348 const childProcess = execa('stdin.js', {stdin: noopGenerator(true)});349 childProcess.stdin.end(input, encoding);350 const {stdout} = await childProcess;351 t.is(stdout, input);Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 352–353why, one step back352};353Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 354–370why, one step back354test('Write call encoding "utf8" is ignored with objectMode', testEncodingIgnored, 'utf8');355test('Write call encoding "utf16le" is ignored with objectMode', testEncodingIgnored, 'utf16le');356test('Write call encoding "hex" is ignored with objectMode', testEncodingIgnored, 'hex');357test('Write call encoding "base64" is ignored with objectMode', testEncodingIgnored, 'base64');358359// eslint-disable-next-line max-params360const testGeneratorNextEncoding = async (t, input, encoding, firstObjectMode, secondObjectMode, expectedType) => {361 const {stdout} = await execa('noop.js', ['other'], {362 stdout: [363 getOutputGenerator(input, firstObjectMode),364 getTypeofGenerator(secondObjectMode),365 ],366 encoding,367 });368 const typeofChunk = Array.isArray(stdout) ? stdout[0] : stdout;369 const output = Buffer.from(typeofChunk, encoding === 'buffer' ? undefined : encoding).toString();370 t.is(output, `[object ${expectedType}]`);Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 371–372why, one step back371};372Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 373–395why, one step back373test('Next generator argument is string with default encoding, with string writes', testGeneratorNextEncoding, foobarString, 'utf8', false, false, 'String');374test('Next generator argument is string with default encoding, with string writes, objectMode first', testGeneratorNextEncoding, foobarString, 'utf8', true, false, 'String');375test('Next generator argument is string with default encoding, with string writes, objectMode both', testGeneratorNextEncoding, foobarString, 'utf8', true, true, 'String');376test('Next generator argument is string with default encoding, with Buffer writes', testGeneratorNextEncoding, foobarBuffer, 'utf8', false, false, 'String');377test('Next generator argument is string with default encoding, with Buffer writes, objectMode first', testGeneratorNextEncoding, foobarBuffer, 'utf8', true, false, 'String');378test('Next generator argument is string with default encoding, with Buffer writes, objectMode both', testGeneratorNextEncoding, foobarBuffer, 'utf8', true, true, 'String');379test('Next generator argument is string with default encoding, with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'utf8', false, false, 'String');380test('Next generator argument is string with default encoding, with Uint8Array writes, objectMode first', testGeneratorNextEncoding, foobarUint8Array, 'utf8', true, false, 'String');381test('Next generator argument is string with default encoding, with Uint8Array writes, objectMode both', testGeneratorNextEncoding, foobarUint8Array, 'utf8', true, true, 'String');382test('Next generator argument is Uint8Array with encoding "buffer", with string writes', testGeneratorNextEncoding, foobarString, 'buffer', false, false, 'Uint8Array');383test('Next generator argument is Uint8Array with encoding "buffer", with string writes, objectMode first', testGeneratorNextEncoding, foobarString, 'buffer', true, false, 'Uint8Array');384test('Next generator argument is Uint8Array with encoding "buffer", with string writes, objectMode both', testGeneratorNextEncoding, foobarString, 'buffer', true, true, 'Uint8Array');385test('Next generator argument is Uint8Array with encoding "buffer", with Buffer writes', testGeneratorNextEncoding, foobarBuffer, 'buffer', false, false, 'Uint8Array');386test('Next generator argument is Uint8Array with encoding "buffer", with Buffer writes, objectMode first', testGeneratorNextEncoding, foobarBuffer, 'buffer', true, false, 'Uint8Array');387test('Next generator argument is Uint8Array with encoding "buffer", with Buffer writes, objectMode both', testGeneratorNextEncoding, foobarBuffer, 'buffer', true, true, 'Uint8Array');388test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'buffer', false, false, 'Uint8Array');389test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode first', testGeneratorNextEncoding, foobarUint8Array, 'buffer', true, false, 'Uint8Array');390test('Next generator argument is Uint8Array with encoding "buffer", with Uint8Array writes, objectMode both', testGeneratorNextEncoding, foobarUint8Array, 'buffer', true, true, 'Uint8Array');391test('Next generator argument is Uint8Array with encoding "hex", with string writes', testGeneratorNextEncoding, foobarString, 'hex', false, false, 'String');392test('Next generator argument is Uint8Array with encoding "hex", with Buffer writes', testGeneratorNextEncoding, foobarBuffer, 'hex', false, false, 'String');393test('Next generator argument is Uint8Array with encoding "hex", with Uint8Array writes', testGeneratorNextEncoding, foobarUint8Array, 'hex', false, false, 'String');394test('Next generator argument is object with default encoding, with object writes, objectMode first', testGeneratorNextEncoding, foobarObject, 'utf8', true, false, 'Object');395test('Next generator argument is object with default encoding, with object writes, objectMode both', testGeneratorNextEncoding, foobarObject, 'utf8', true, true, 'Object');Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 396why, one step back396Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 397–415why, one step back397const testFirstOutputGeneratorArgument = async (t, index) => {398 const {stdio} = await execa('noop-fd.js', [`${index}`], getStdio(index, getTypeofGenerator(true)));399 t.deepEqual(stdio[index], ['[object String]']);400};401402test('The first generator with result.stdout does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 1);403test('The first generator with result.stderr does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 2);404test('The first generator with result.stdio[*] does not receive an object argument even in objectMode', testFirstOutputGeneratorArgument, 3);405406// eslint-disable-next-line max-params407const testGeneratorReturnType = async (t, input, encoding, reject, objectMode) => {408 const fixtureName = reject ? 'noop-fd.js' : 'noop-fail.js';409 const {stdout} = await execa(fixtureName, ['1', 'other'], {410 stdout: getOutputGenerator(input, objectMode),411 encoding,412 reject,413 });414 const typeofChunk = Array.isArray(stdout) ? stdout[0] : stdout;415 const output = Buffer.from(typeofChunk, encoding === 'buffer' ? undefined : encoding).toString();Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 416–418why, one step back416 t.is(output, foobarString);417};418Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 419–442why, one step back419test('Generator can return string with default encoding', testGeneratorReturnType, foobarString, 'utf8', true, false);420test('Generator can return Uint8Array with default encoding', testGeneratorReturnType, foobarUint8Array, 'utf8', true, false);421test('Generator can return string with encoding "buffer"', testGeneratorReturnType, foobarString, 'buffer', true, false);422test('Generator can return Uint8Array with encoding "buffer"', testGeneratorReturnType, foobarUint8Array, 'buffer', true, false);423test('Generator can return string with encoding "hex"', testGeneratorReturnType, foobarString, 'hex', true, false);424test('Generator can return Uint8Array with encoding "hex"', testGeneratorReturnType, foobarUint8Array, 'hex', true, false);425test('Generator can return string with default encoding, failure', testGeneratorReturnType, foobarString, 'utf8', false, false);426test('Generator can return Uint8Array with default encoding, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, false);427test('Generator can return string with encoding "buffer", failure', testGeneratorReturnType, foobarString, 'buffer', false, false);428test('Generator can return Uint8Array with encoding "buffer", failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, false);429test('Generator can return string with encoding "hex", failure', testGeneratorReturnType, foobarString, 'hex', false, false);430test('Generator can return Uint8Array with encoding "hex", failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, false);431test('Generator can return string with default encoding, objectMode', testGeneratorReturnType, foobarString, 'utf8', true, true);432test('Generator can return Uint8Array with default encoding, objectMode', testGeneratorReturnType, foobarUint8Array, 'utf8', true, true);433test('Generator can return string with encoding "buffer", objectMode', testGeneratorReturnType, foobarString, 'buffer', true, true);434test('Generator can return Uint8Array with encoding "buffer", objectMode', testGeneratorReturnType, foobarUint8Array, 'buffer', true, true);435test('Generator can return string with encoding "hex", objectMode', testGeneratorReturnType, foobarString, 'hex', true, true);436test('Generator can return Uint8Array with encoding "hex", objectMode', testGeneratorReturnType, foobarUint8Array, 'hex', true, true);437test('Generator can return string with default encoding, objectMode, failure', testGeneratorReturnType, foobarString, 'utf8', false, true);438test('Generator can return Uint8Array with default encoding, objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'utf8', false, true);439test('Generator can return string with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarString, 'buffer', false, true);440test('Generator can return Uint8Array with encoding "buffer", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'buffer', false, true);441test('Generator can return string with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarString, 'hex', false, true);442test('Generator can return Uint8Array with encoding "hex", objectMode, failure', testGeneratorReturnType, foobarUint8Array, 'hex', false, true);Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 443–445why, one step back443444const multibyteChar = '\u{1F984}';445const multibyteString = `${multibyteChar}${multibyteChar}`;Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 446why, one step back446const multibyteUint8Array = textEncoder.encode(multibyteString);Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 447–449why, one step back447const breakingLength = multibyteUint8Array.length * 0.75;448const brokenSymbol = '\uFFFD';449Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 450–451why, one step back450const testMultibyte = async (t, objectMode) => {451 const childProcess = execa('stdin.js', {stdin: noopGenerator(objectMode)});Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 452why, one step back452 childProcess.stdin.write(multibyteUint8Array.slice(0, breakingLength));Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf
line 453why, one step back453 await setImmediate();Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 454–456why, one step back454 childProcess.stdin.end(multibyteUint8Array.slice(breakingLength));455 const {stdout} = await childProcess;456 t.is(stdout, multibyteString);Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 457why, one step back457};Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 458why, one step back458Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 459–463why, one step back459test('Generator handles multibyte characters with Uint8Array', testMultibyte, false);460test('Generator handles multibyte characters with Uint8Array, objectMode', testMultibyte, true);461462const testMultibytePartial = async (t, objectMode) => {463 const {stdout} = await execa('stdin.js', {stdin: [multibyteUint8Array.slice(0, breakingLength), noopGenerator(objectMode)]});Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 464why, one step back464 t.is(stdout, `${multibyteChar}${brokenSymbol}`);Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 465why, one step back465};Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 466why, one step back466Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 467–469why, one step back467test('Generator handles partial multibyte characters with Uint8Array', testMultibytePartial, false);468test('Generator handles partial multibyte characters with Uint8Array, objectMode', testMultibytePartial, true);469Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 470why, one step back470// eslint-disable-next-line require-yieldLine-wise transforms (#699)ehmickybefore adoption6dd804f
line 471why, one step back471const noYieldGenerator = async function * (lines) {Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 472why, one step back472 // eslint-disable-next-line no-empty, no-unused-varsLine-wise transforms (#699)ehmickybefore adoption6dd804f
line 473why, one step back473 for await (const line of lines) {}Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 474–475why, one step back474};475Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 476–479why, one step back476const testNoYield = async (t, objectMode, output) => {477 const {stdout} = await execa('noop.js', {stdout: {transform: noYieldGenerator, objectMode}});478 t.deepEqual(stdout, output);479};Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 480why, one step back480Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 481–483why, one step back481test('Generator can filter by not calling yield', testNoYield, false, '');482test('Generator can filter by not calling yield, objectMode', testNoYield, true, []);483Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 484–486why, one step back484const prefix = '> ';485const suffix = ' <';486Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 487–488why, one step back487const multipleYieldGenerator = async function * (lines) {488 for await (const line of lines) {Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 489why, one step back489 yield prefix;Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf
line 490why, one step back490 await setImmediate();Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 491why, one step back491 yield line;Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf
line 492why, one step back492 await setImmediate();Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 493–553why, one step back493 yield suffix;494 }495};496497test('Generator can yield multiple times at different moments', async t => {498 const {stdout} = await execa('noop-fd.js', ['1', foobarString], {stdout: multipleYieldGenerator});499 t.is(stdout, `${prefix}${foobarString}${suffix}`);500});501502const testInputFile = async (t, getOptions, reversed) => {503 const filePath = tempfile();504 await writeFile(filePath, foobarString);505 const {stdin, ...options} = getOptions(filePath);506 const reversedStdin = reversed ? stdin.reverse() : stdin;507 const {stdout} = await execa('stdin-fd.js', ['0'], {...options, stdin: reversedStdin});508 t.is(stdout, foobarUppercase);509 await rm(filePath);510};511512test('Can use generators with a file as input', testInputFile, filePath => ({stdin: [{file: filePath}, uppercaseGenerator]}), false);513test('Can use generators with a file as input, reversed', testInputFile, filePath => ({stdin: [{file: filePath}, uppercaseGenerator]}), true);514test('Can use generators with inputFile option', testInputFile, filePath => ({inputFile: filePath, stdin: uppercaseGenerator}), false);515516const testOutputFile = async (t, reversed) => {517 const filePath = tempfile();518 const stdoutOption = [uppercaseGenerator, {file: filePath}];519 const reversedStdoutOption = reversed ? stdoutOption.reverse() : stdoutOption;520 const {stdout} = await execa('noop-fd.js', ['1'], {stdout: reversedStdoutOption});521 t.is(stdout, foobarUppercase);522 t.is(await readFile(filePath, 'utf8'), foobarUppercase);523 await rm(filePath);524};525526test('Can use generators with a file as output', testOutputFile, false);527test('Can use generators with a file as output, reversed', testOutputFile, true);528529test('Can use generators to a Writable stream', async t => {530 const passThrough = new PassThrough();531 const [{stdout}, streamOutput] = await Promise.all([532 execa('noop-fd.js', ['1', foobarString], {stdout: [uppercaseGenerator, passThrough]}),533 getStream(passThrough),534 ]);535 t.is(stdout, foobarUppercase);536 t.is(streamOutput, foobarUppercase);537});538539test('Can use generators from a Readable stream', async t => {540 const passThrough = new PassThrough();541 const childProcess = execa('stdin-fd.js', ['0'], {stdin: [passThrough, uppercaseGenerator]});542 passThrough.end(foobarString);543 const {stdout} = await childProcess;544 t.is(stdout, foobarUppercase);545});546547test('Can use generators with "inherit"', async t => {548 const {stdout} = await execa('nested-inherit.js');549 t.is(stdout, foobarUppercase);550});551552const casedSuffix = 'k';553Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 554–556why, one step back554const appendGenerator = async function * (lines) {555 for await (const line of lines) {556 yield `${line}${casedSuffix}`;Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 557–581why, one step back557 }558};559560const testAppendInput = async (t, reversed) => {561 const stdin = [foobarUint8Array, uppercaseGenerator, appendGenerator];562 const reversedStdin = reversed ? stdin.reverse() : stdin;563 const {stdout} = await execa('stdin-fd.js', ['0'], {stdin: reversedStdin});564 const reversedSuffix = reversed ? casedSuffix.toUpperCase() : casedSuffix;565 t.is(stdout, `${foobarUppercase}${reversedSuffix}`);566};567568test('Can use multiple generators as input', testAppendInput, false);569test('Can use multiple generators as input, reversed', testAppendInput, true);570571const testAppendOutput = async (t, reversed) => {572 const stdoutOption = [uppercaseGenerator, appendGenerator];573 const reversedStdoutOption = reversed ? stdoutOption.reverse() : stdoutOption;574 const {stdout} = await execa('noop-fd.js', ['1', foobarString], {stdout: reversedStdoutOption});575 const reversedSuffix = reversed ? casedSuffix.toUpperCase() : casedSuffix;576 t.is(stdout, `${foobarUppercase}${reversedSuffix}`);577};578579test('Can use multiple generators as output', testAppendOutput, false);580test('Can use multiple generators as output, reversed', testAppendOutput, true);581Add passing the same transform multiple times (#697)ehmickybefore adoption35ec8de
lines 582–586why, one step back582test('Can use multiple identical generators', async t => {583 const {stdout} = await execa('noop-fd.js', ['1', foobarString], {stdout: [appendGenerator, appendGenerator]});584 t.is(stdout, `${foobarString}${casedSuffix}${casedSuffix}`);585});586Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 587–590why, one step back587const maxBuffer = 10;588589test('Generators take "maxBuffer" into account', async t => {590 const bigString = '.'.repeat(maxBuffer);Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 591why, one step back591 const {stdout} = await execa('noop.js', {maxBuffer, stdout: getOutputGenerator(bigString, false)});Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 592–593why, one step back592 t.is(stdout, bigString);593Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 594–602why, one step back594 await t.throwsAsync(execa('noop.js', {maxBuffer, stdout: getOutputGenerator(`${bigString}.`, false)}));595});596597test('Generators take "maxBuffer" into account, objectMode', async t => {598 const bigArray = Array.from({length: maxBuffer});599 const {stdout} = await execa('noop.js', {maxBuffer, stdout: getOutputsGenerator(bigArray, true)});600 t.is(stdout.length, maxBuffer);601602 await t.throwsAsync(execa('noop.js', {maxBuffer, stdout: getOutputsGenerator([...bigArray, ''], true)}));Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 603–604why, one step back603});604Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 605–606why, one step back605const timeoutGenerator = async function * (timeout, lines) {606 for await (const line of lines) {Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 607why, one step back607 await setTimeout(timeout);Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 608why, one step back608 yield line;Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 609–617why, one step back609 }610};611612test('Generators are awaited on success', async t => {613 const {stdout} = await execa('noop-fd.js', ['1', foobarString], {maxBuffer, stdout: timeoutGenerator.bind(undefined, 1e3)});614 t.is(stdout, foobarString);615});616617// eslint-disable-next-line require-yieldLine-wise transforms (#699)ehmickybefore adoption6dd804f
line 618why, one step back618const throwingGenerator = async function * (lines) {Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 619why, one step back619 // eslint-disable-next-line no-unreachable-loopLine-wise transforms (#699)ehmickybefore adoption6dd804f
lines 620–621why, one step back620 for await (const line of lines) {621 throw new Error(`Generator error ${line}`);Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 622–632why, one step back622 }623};624625test('Generators errors make process fail', async t => {626 await t.throwsAsync(627 execa('noop-fd.js', ['1', foobarString], {stdout: throwingGenerator}),628 {message: /Generator error foobar/},629 );630});631632// eslint-disable-next-line require-yieldLine-wise transforms (#699)ehmickybefore adoption6dd804f
line 633why, one step back633const errorHandlerGenerator = async function * (state, lines) {Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 634–635why, one step back634 try {635 // eslint-disable-next-line no-unused-varsLine-wise transforms (#699)ehmickybefore adoption6dd804f
line 636why, one step back636 for await (const line of lines) {Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 637–650why, one step back637 await setTimeout(1e8);638 }639 } catch (error) {640 state.error = error;641 }642};643644test.serial('Process streams failures make generators throw', async t => {645 const state = {};646 const childProcess = execa('noop-fail.js', ['1'], {stdout: errorHandlerGenerator.bind(undefined, state)});647 const error = new Error('test');648 childProcess.stdout.emit('error', error);649 const thrownError = await t.throwsAsync(childProcess);650 t.is(error, thrownError);Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf
line 651why, one step back651 await setImmediate();Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 652–653why, one step back652 t.is(state.error.code, 'ABORT_ERR');653});