Skip to content
Myr

no-buffer.js

This file

Path

test/stream/no-buffer.js

162 lines · 8.6 KiB · JavaScript

Last changed by

Add support for `buffer: false` to `execaSync()` (#953)ehmickybefore adoptiond0052ac

History of this file

Take it

Split files (#837)ehmickybefore adoption672c2d7

lines 1–3why, one step back
1import {once} from 'node:events';
2import test from 'ava';3import getStream from 'get-stream';

Add support for `buffer: false` to `execaSync()` (#953)ehmickybefore adoptiond0052ac

line 4why, one step back
4import {execa, execaSync} from '../../index.js';

Split files (#837)ehmickybefore adoption672c2d7

lines 5–6why, one step back
5import {setFixtureDir} from '../helpers/fixtures-dir.js';
6import {fullStdio, getStdio} from '../helpers/stdio.js';

Add support for `buffer: false` to `execaSync()` (#953)ehmickybefore adoptiond0052ac

lines 7–8why, one step back
7import {foobarString, foobarUppercase, foobarUppercaseUint8Array} from '../helpers/input.js';
8import {resultGenerator, uppercaseGenerator, uppercaseBufferGenerator} from '../helpers/generator.js';

Split files (#837)ehmickybefore adoption672c2d7

lines 9–11why, one step back
9
10setFixtureDir();11

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

lines 12–13why, one step back
12const testLateStream = async (t, fdNumber, all) => {
13 const subprocess = execa('noop-fd-ipc.js', [`${fdNumber}`, foobarString], {...getStdio(4, 'ipc', 4), buffer: false, all});

Split files (#837)ehmickybefore adoption672c2d7

lines 14–15why, one step back
14 await once(subprocess, 'message');
15 const [output, allOutput] = await Promise.all([

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 16why, one step back
16 getStream(subprocess.stdio[fdNumber]),

Split files (#837)ehmickybefore adoption672c2d7

lines 17–38why, one step back
17 all ? getStream(subprocess.all) : undefined,
18 subprocess,19 ]);2021 t.is(output, '');2223 if (all) {24 t.is(allOutput, '');25 }26};2728test('Lacks some data when stdout is read too late `buffer` set to `false`', testLateStream, 1, false);29test('Lacks some data when stderr is read too late `buffer` set to `false`', testLateStream, 2, false);30test('Lacks some data when stdio[*] is read too late `buffer` set to `false`', testLateStream, 3, false);31test('Lacks some data when all is read too late `buffer` set to `false`', testLateStream, 1, true);3233const getFirstDataEvent = async stream => {34 const [output] = await once(stream, 'data');35 return output.toString();36};3738// eslint-disable-next-line max-params

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

lines 39–40why, one step back
39const testIterationBuffer = async (t, fdNumber, buffer, useDataEvents, all) => {
40 const subprocess = execa('noop-fd.js', [`${fdNumber}`, foobarString], {...fullStdio, buffer, all});

Split files (#837)ehmickybefore adoption672c2d7

lines 41–43why, one step back
41 const getOutput = useDataEvents ? getFirstDataEvent : getStream;
42 const [result, output, allOutput] = await Promise.all([43 subprocess,

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 44why, one step back
44 getOutput(subprocess.stdio[fdNumber]),

Split files (#837)ehmickybefore adoption672c2d7

lines 45–47why, one step back
45 all ? getOutput(subprocess.all) : undefined,
46 ]);47

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 48why, one step back
48 const expectedResult = buffer ? foobarString : undefined;

Split files (#837)ehmickybefore adoption672c2d7

line 49why, one step back
49

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 50why, one step back
50 t.is(result.stdio[fdNumber], expectedResult);

Upgrade `get-stream` (#908)ehmickybefore adoption3ecd447

line 51why, one step back
51 t.is(output, foobarString);

Split files (#837)ehmickybefore adoption672c2d7

lines 52–53why, one step back
52
53 if (all) {

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 54why, one step back
54 t.is(result.all, expectedResult);

Upgrade `get-stream` (#908)ehmickybefore adoption3ecd447

line 55why, one step back
55 t.is(allOutput, foobarString);

Split files (#837)ehmickybefore adoption672c2d7

lines 56–62why, one step back
56 }
57};5859test('Can iterate stdout when `buffer` set to `false`', testIterationBuffer, 1, false, false, false);60test('Can iterate stderr when `buffer` set to `false`', testIterationBuffer, 2, false, false, false);61test('Can iterate stdio[*] when `buffer` set to `false`', testIterationBuffer, 3, false, false, false);62test('Can iterate all when `buffer` set to `false`', testIterationBuffer, 1, false, false, true);

Upgrade `get-stream` (#908)ehmickybefore adoption3ecd447

lines 63–66why, one step back
63test('Can iterate stdout when `buffer` set to `true`', testIterationBuffer, 1, true, false, false);
64test('Can iterate stderr when `buffer` set to `true`', testIterationBuffer, 2, true, false, false);65test('Can iterate stdio[*] when `buffer` set to `true`', testIterationBuffer, 3, true, false, false);66test('Can iterate all when `buffer` set to `true`', testIterationBuffer, 1, true, false, true);

Split files (#837)ehmickybefore adoption672c2d7

lines 67–75why, one step back
67test('Can listen to `data` events on stdout when `buffer` set to `false`', testIterationBuffer, 1, false, true, false);
68test('Can listen to `data` events on stderr when `buffer` set to `false`', testIterationBuffer, 2, false, true, false);69test('Can listen to `data` events on stdio[*] when `buffer` set to `false`', testIterationBuffer, 3, false, true, false);70test('Can listen to `data` events on all when `buffer` set to `false`', testIterationBuffer, 1, false, true, true);71test('Can listen to `data` events on stdout when `buffer` set to `true`', testIterationBuffer, 1, true, true, false);72test('Can listen to `data` events on stderr when `buffer` set to `true`', testIterationBuffer, 2, true, true, false);73test('Can listen to `data` events on stdio[*] when `buffer` set to `true`', testIterationBuffer, 3, true, true, false);74test('Can listen to `data` events on all when `buffer` set to `true`', testIterationBuffer, 1, true, true, true);75

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

lines 76–78why, one step back
76const testNoBufferStreamError = async (t, fdNumber, all) => {
77 const subprocess = execa('noop-fd.js', [`${fdNumber}`], {...fullStdio, buffer: false, all});78 const stream = all ? subprocess.all : subprocess.stdio[fdNumber];

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 79–81why, one step back
79 const cause = new Error('test');
80 stream.destroy(cause);81 t.like(await t.throwsAsync(subprocess), {cause});

Split files (#837)ehmickybefore adoption672c2d7

lines 82–88why, one step back
82};
8384test('Listen to stdout errors even when `buffer` is `false`', testNoBufferStreamError, 1, false);85test('Listen to stderr errors even when `buffer` is `false`', testNoBufferStreamError, 2, false);86test('Listen to stdio[*] errors even when `buffer` is `false`', testNoBufferStreamError, 3, false);87test('Listen to all errors even when `buffer` is `false`', testNoBufferStreamError, 1, true);88

Add support for `buffer: false` to `execaSync()` (#953)ehmickybefore adoptiond0052ac

lines 89–92why, one step back
89const testNoOutput = async (t, stdioOption, execaMethod) => {
90 const {stdout} = await execaMethod('noop.js', {stdout: stdioOption, buffer: false});91 t.is(stdout, undefined);92};

Split files (#837)ehmickybefore adoption672c2d7

line 93why, one step back
93

Add support for `buffer: false` to `execaSync()` (#953)ehmickybefore adoptiond0052ac

lines 94–110why, one step back
94test('buffer: false does not return output', testNoOutput, 'pipe', execa);
95test('buffer: false does not return output, stdout undefined', testNoOutput, undefined, execa);96test('buffer: false does not return output, stdout null', testNoOutput, null, execa);97test('buffer: false does not return output, stdout ["pipe"]', testNoOutput, ['pipe'], execa);98test('buffer: false does not return output, stdout [undefined]', testNoOutput, [undefined], execa);99test('buffer: false does not return output, stdout [null]', testNoOutput, [null], execa);100test('buffer: false does not return output, stdout ["pipe", undefined]', testNoOutput, ['pipe', undefined], execa);101test('buffer: false does not return output, sync', testNoOutput, 'pipe', execaSync);102test('buffer: false does not return output, stdout undefined, sync', testNoOutput, undefined, execaSync);103test('buffer: false does not return output, stdout null, sync', testNoOutput, null, execaSync);104test('buffer: false does not return output, stdout ["pipe"], sync', testNoOutput, ['pipe'], execaSync);105test('buffer: false does not return output, stdout [undefined], sync', testNoOutput, [undefined], execaSync);106test('buffer: false does not return output, stdout [null], sync', testNoOutput, [null], execaSync);107test('buffer: false does not return output, stdout ["pipe", undefined], sync', testNoOutput, ['pipe', undefined], execaSync);108109const testNoOutputFail = async (t, execaMethod) => {110 const {exitCode, stdout} = await execaMethod('fail.js', {buffer: false, reject: false});

Split files (#837)ehmickybefore adoption672c2d7

line 111why, one step back
111 t.is(exitCode, 2);

Add support for `buffer: false` to `execaSync()` (#953)ehmickybefore adoptiond0052ac

lines 112–147why, one step back
112 t.is(stdout, undefined);
113};114115test('buffer: false does not return output, failure', testNoOutputFail, execa);116test('buffer: false does not return output, failure, sync', testNoOutputFail, execaSync);117118const testTransform = async (t, objectMode, execaMethod) => {119 const lines = [];120 const {stdout} = await execaMethod('noop.js', {121 buffer: false,122 stdout: [uppercaseGenerator(objectMode), resultGenerator(lines)(objectMode)],123 });124 t.is(stdout, undefined);125 t.deepEqual(lines, [foobarUppercase]);126};127128test('buffer: false still runs transforms', testTransform, false, execa);129test('buffer: false still runs transforms, objectMode', testTransform, true, execa);130test('buffer: false still runs transforms, sync', testTransform, false, execaSync);131test('buffer: false still runs transforms, objectMode, sync', testTransform, true, execaSync);132133const testTransformBinary = async (t, objectMode, execaMethod) => {134 const lines = [];135 const {stdout} = await execaMethod('noop-fd.js', ['1', foobarString], {136 buffer: false,137 stdout: [uppercaseBufferGenerator(objectMode, true), resultGenerator(lines)(objectMode)],138 encoding: 'buffer',139 });140 t.is(stdout, undefined);141 t.deepEqual(lines, [foobarUppercaseUint8Array]);142};143144test('buffer: false still runs transforms, encoding "buffer"', testTransformBinary, false, execa);145test('buffer: false still runs transforms, encoding "buffer", objectMode', testTransformBinary, true, execa);146test('buffer: false still runs transforms, encoding "buffer", sync', testTransformBinary, false, execaSync);147test('buffer: false still runs transforms, encoding "buffer", objectMode, sync', testTransformBinary, true, execaSync);

Split files (#837)ehmickybefore adoption672c2d7

line 148why, one step back
148

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 149why, one step back
149const testStreamEnd = async (t, fdNumber, buffer) => {

Split files (#837)ehmickybefore adoption672c2d7

lines 150–152why, one step back
150 const subprocess = execa('wrong command', {...fullStdio, buffer});
151 await Promise.all([152 t.throwsAsync(subprocess, {message: /wrong command/}),

Rename `index` to `fdNumber` (#862)ehmickybefore adoption295ef40

line 153why, one step back
153 once(subprocess.stdio[fdNumber], 'end'),

Split files (#837)ehmickybefore adoption672c2d7

lines 154–162why, one step back
154 ]);
155};156157test('buffer: false > emits end event on stdout when promise is rejected', testStreamEnd, 1, false);158test('buffer: false > emits end event on stderr when promise is rejected', testStreamEnd, 2, false);159test('buffer: false > emits end event on stdio[*] when promise is rejected', testStreamEnd, 3, false);160test('buffer: true > emits end event on stdout when promise is rejected', testStreamEnd, 1, true);161test('buffer: true > emits end event on stderr when promise is rejected', testStreamEnd, 2, true);162test('buffer: true > emits end event on stdio[*] when promise is rejected', testStreamEnd, 3, true);