Skip to content
Myr

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 file

Take it

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 1–3why, one step back
1import {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 back
4import {setTimeout, setImmediate} from 'node:timers/promises';

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 5why, one step back
5import test from 'ava';

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 6why, one step back
6import getStream, {getStreamAsArray} from 'get-stream';

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 7–10why, one step back
7import 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 back
11import {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 back
13
14setFixtureDir();15

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 16–18why, one step back
16const textEncoder = new TextEncoder();
17const textDecoder = new TextDecoder();18

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 19why, one step back
19const foobarUppercase = foobarString.toUpperCase();

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 20why, one step back
20const foobarHex = foobarBuffer.toString('hex');

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 21why, one step back
21

Line-wise transforms (#699)ehmickybefore adoption6dd804f

lines 22–24why, one step back
22const 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 back
25 }
26};27

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 28–31why, one step back
28const 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 back
32};
33

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 34–36why, one step back
34const getInputObjectMode = objectMode => objectMode
35 ? {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 back
37

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 38–57why, one step back
38const getOutputObjectMode = objectMode => objectMode
39 ? {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 back
58 const {stdout} = await childProcess;

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 59why, one step back
59 t.is(stdout, output);

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 60–61why, one step back
60};
61

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 62–69why, one step back
62test('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 back
70

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 71–74why, one step back
71const 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 back
75 const {stdout} = await childProcess;

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 76–77why, one step back
76 t.is(stdout, output);
77};

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 78why, one step back
78

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 79–103why, one step back
79test('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 back
104 const fixtureName = reject ? 'noop-fd.js' : 'noop-fail.js';

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 105why, one step back
105 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 back
106 const result = useShortcutProperty ? [stdout, stderr][index - 1] : stdio[index];

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 107why, one step back
107 t.deepEqual(result, output);

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 108–109why, one step back
108};
109

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 110–129why, one step back
110test('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 back
130

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 131–133why, one step back
131const 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 back
134 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 back
135 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 back
137};
138

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 139–148why, one step back
139test('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 back
149

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 150–182why, one step back
150const 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 back
183 const fixtureName = reject ? 'all.js' : 'all-fail.js';

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 184–198why, one step back
184 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 back
199};
200

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 201–224why, one step back
201test('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 back
225
226test('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 * () {};232

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 233why, one step back
233const testInvalidGenerator = (t, index, stdioOption) => {

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 234why, one step back
234 t.throws(() => {

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 235why, one step back
235 execa('empty.js', getStdio(index, stdioOption));

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 236–238why, one step back
236 }, {message: /asynchronous generator/});
237};238

Line-wise transforms (#699)ehmickybefore adoption6dd804f

lines 239–250why, one step back
239test('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 back
251

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 252why, one step back
252const testInvalidBinary = (t, index, optionName) => {

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 253why, one step back
253 t.throws(() => {

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 254why, one step back
254 execa('empty.js', getStdio(index, {transform: uppercaseGenerator, [optionName]: 'true'}));

Line-wise transforms (#699)ehmickybefore adoption6dd804f

lines 255–257why, one step back
255 }, {message: /a boolean/});
256};257

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 258–265why, one step back
258test('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 back
266

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 267–279why, one step back
267const 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;279

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 280why, one step back
280const writerGenerator = async function * (lines) {

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 281why, one step back
281 // eslint-disable-next-line no-unused-vars

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 282why, one step back
282 for await (const line of lines) {

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 283why, one step back
283 for (let index = 0; index < getDefaultHighWaterMark() * repeatHighWaterMark; index += 1) {

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 284why, one step back
284 yield '\n';

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 285–289why, one step back
285 }
286 }287};288289const passThroughGenerator = async function * (chunks) {

Line-wise transforms (#699)ehmickybefore adoption6dd804f

lines 290–293why, one step back
290 yield * chunks;
291};292293const getLengthGenerator = async function * (chunks) {

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 294–298why, one step back
294 for await (const chunk of chunks) {
295 yield `${chunk.length}`;296 }297};298

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 299–307why, one step back
299const 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 back
308 t.is(stdout, `${getDefaultHighWaterMark()}`.repeat(repeatHighWaterMark));

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 309why, one step back
309};

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 310why, one step back
310

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 311–314why, one step back
311test('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 back
315

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 316–323why, one step back
316const 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 back
324

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 325–327why, one step back
325// eslint-disable-next-line max-params
326const 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 back
328 childProcess.stdin.end(input);
329 const {stdout} = await childProcess;

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 330–331why, one step back
330 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 back
332};
333

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 334–344why, one step back
334test('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 back
345

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 346–351why, one step back
346const 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 back
352};
353

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 354–370why, one step back
354test('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 back
371};
372

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 373–395why, one step back
373test('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 back
396

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 397–415why, one step back
397const 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 back
416 t.is(output, foobarString);
417};418

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 419–442why, one step back
419test('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 back
443
444const multibyteChar = '\u{1F984}';445const multibyteString = `${multibyteChar}${multibyteChar}`;

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 446why, one step back
446const multibyteUint8Array = textEncoder.encode(multibyteString);

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 447–449why, one step back
447const breakingLength = multibyteUint8Array.length * 0.75;
448const brokenSymbol = '\uFFFD';449

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 450–451why, one step back
450const 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 back
452 childProcess.stdin.write(multibyteUint8Array.slice(0, breakingLength));

Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf

line 453why, one step back
453 await setImmediate();

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 454–456why, one step back
454 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 back
457};

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 458why, one step back
458

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 459–463why, one step back
459test('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 back
464 t.is(stdout, `${multibyteChar}${brokenSymbol}`);

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 465why, one step back
465};

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 466why, one step back
466

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 467–469why, one step back
467test('Generator handles partial multibyte characters with Uint8Array', testMultibytePartial, false);
468test('Generator handles partial multibyte characters with Uint8Array, objectMode', testMultibytePartial, true);469

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 470why, one step back
470// eslint-disable-next-line require-yield

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 471why, one step back
471const noYieldGenerator = async function * (lines) {

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 472why, one step back
472 // eslint-disable-next-line no-empty, no-unused-vars

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 473why, one step back
473 for await (const line of lines) {}

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 474–475why, one step back
474};
475

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 476–479why, one step back
476const 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 back
480

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 481–483why, one step back
481test('Generator can filter by not calling yield', testNoYield, false, '');
482test('Generator can filter by not calling yield, objectMode', testNoYield, true, []);483

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 484–486why, one step back
484const prefix = '> ';
485const suffix = ' <';486

Line-wise transforms (#699)ehmickybefore adoption6dd804f

lines 487–488why, one step back
487const 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 back
489 yield prefix;

Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf

line 490why, one step back
490 await setImmediate();

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 491why, one step back
491 yield line;

Prefer `setImmediate()` over `setTimeout()` (#733)ehmickybefore adoption66d12bf

line 492why, one step back
492 await setImmediate();

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 493–553why, one step back
493 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';553

Line-wise transforms (#699)ehmickybefore adoption6dd804f

lines 554–556why, one step back
554const 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 back
557 }
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);581

Add passing the same transform multiple times (#697)ehmickybefore adoption35ec8de

lines 582–586why, one step back
582test('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});586

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 587–590why, one step back
587const 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 back
591 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 back
592 t.is(stdout, bigString);
593

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 594–602why, one step back
594 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 back
603});
604

Line-wise transforms (#699)ehmickybefore adoption6dd804f

lines 605–606why, one step back
605const 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 back
607 await setTimeout(timeout);

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 608why, one step back
608 yield line;

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 609–617why, one step back
609 }
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-yield

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 618why, one step back
618const throwingGenerator = async function * (lines) {

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

line 619why, one step back
619 // eslint-disable-next-line no-unreachable-loop

Line-wise transforms (#699)ehmickybefore adoption6dd804f

lines 620–621why, one step back
620 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 back
622 }
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-yield

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 633why, one step back
633const errorHandlerGenerator = async function * (state, lines) {

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 634–635why, one step back
634 try {
635 // eslint-disable-next-line no-unused-vars

Line-wise transforms (#699)ehmickybefore adoption6dd804f

line 636why, one step back
636 for await (const line of lines) {

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 637–650why, one step back
637 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 back
651 await setImmediate();

Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd

lines 652–653why, one step back
652 t.is(state.error.code, 'ABORT_ERR');
653});