Skip to content
Myr

array.js

This file

Path

test/stdio/array.js

254 lines · 13.9 KiB · JavaScript

Last changed by

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

History of this file

Take it

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 1–5why, one step back
1import {readFile, writeFile, rm} from 'node:fs/promises';
2import process from 'node:process';3import test from 'ava';4import tempfile from 'tempfile';5import {execa, execaSync} from '../../index.js';

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 6why, one step back
6import {fullStdio, getStdio, STANDARD_STREAMS} from '../helpers/stdio.js';

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 7–10why, one step back
7import {setFixtureDir} from '../helpers/fixtures-dir.js';
89setFixtureDir();10

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 11why, one step back
11const testEmptyArray = (t, index, optionName, execaMethod) => {

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

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

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 13why, one step back
13 execaMethod('empty.js', getStdio(index, []));

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 14–16why, one step back
14 }, {message: `The \`${optionName}\` option must not be an empty array.`});
15};16

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 17–24why, one step back
17test('Cannot pass an empty array to stdin', testEmptyArray, 0, 'stdin', execa);
18test('Cannot pass an empty array to stdout', testEmptyArray, 1, 'stdout', execa);19test('Cannot pass an empty array to stderr', testEmptyArray, 2, 'stderr', execa);20test('Cannot pass an empty array to stdio[*]', testEmptyArray, 3, 'stdio[3]', execa);21test('Cannot pass an empty array to stdin - sync', testEmptyArray, 0, 'stdin', execaSync);22test('Cannot pass an empty array to stdout - sync', testEmptyArray, 1, 'stdout', execaSync);23test('Cannot pass an empty array to stderr - sync', testEmptyArray, 2, 'stderr', execaSync);24test('Cannot pass an empty array to stdio[*] - sync', testEmptyArray, 3, 'stdio[3]', execaSync);

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

line 25why, one step back
25

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 26–28why, one step back
26const testNoPipeOption = async (t, stdioOption, index) => {
27 const childProcess = execa('empty.js', getStdio(index, stdioOption));28 t.is(childProcess.stdio[index], null);

Fix typo in a test (#662)ehmickybefore adoption710ecd5

line 29why, one step back
29 await childProcess;

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 30–31why, one step back
30};
31

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 32–67why, one step back
32test('stdin can be "ignore"', testNoPipeOption, 'ignore', 0);
33test('stdin can be ["ignore"]', testNoPipeOption, ['ignore'], 0);34test('stdin can be ["ignore", "ignore"]', testNoPipeOption, ['ignore', 'ignore'], 0);35test('stdin can be "ipc"', testNoPipeOption, 'ipc', 0);36test('stdin can be ["ipc"]', testNoPipeOption, ['ipc'], 0);37test('stdin can be "inherit"', testNoPipeOption, 'inherit', 0);38test('stdin can be ["inherit"]', testNoPipeOption, ['inherit'], 0);39test('stdin can be 0', testNoPipeOption, 0, 0);40test('stdin can be [0]', testNoPipeOption, [0], 0);41test('stdout can be "ignore"', testNoPipeOption, 'ignore', 1);42test('stdout can be ["ignore"]', testNoPipeOption, ['ignore'], 1);43test('stdout can be ["ignore", "ignore"]', testNoPipeOption, ['ignore', 'ignore'], 1);44test('stdout can be "ipc"', testNoPipeOption, 'ipc', 1);45test('stdout can be ["ipc"]', testNoPipeOption, ['ipc'], 1);46test('stdout can be "inherit"', testNoPipeOption, 'inherit', 1);47test('stdout can be ["inherit"]', testNoPipeOption, ['inherit'], 1);48test('stdout can be 1', testNoPipeOption, 1, 1);49test('stdout can be [1]', testNoPipeOption, [1], 1);50test('stderr can be "ignore"', testNoPipeOption, 'ignore', 2);51test('stderr can be ["ignore"]', testNoPipeOption, ['ignore'], 2);52test('stderr can be ["ignore", "ignore"]', testNoPipeOption, ['ignore', 'ignore'], 2);53test('stderr can be "ipc"', testNoPipeOption, 'ipc', 2);54test('stderr can be ["ipc"]', testNoPipeOption, ['ipc'], 2);55test('stderr can be "inherit"', testNoPipeOption, 'inherit', 2);56test('stderr can be ["inherit"]', testNoPipeOption, ['inherit'], 2);57test('stderr can be 2', testNoPipeOption, 2, 2);58test('stderr can be [2]', testNoPipeOption, [2], 2);59test('stdio[*] can be "ignore"', testNoPipeOption, 'ignore', 3);60test('stdio[*] can be ["ignore"]', testNoPipeOption, ['ignore'], 3);61test('stdio[*] can be ["ignore", "ignore"]', testNoPipeOption, ['ignore', 'ignore'], 3);62test('stdio[*] can be "ipc"', testNoPipeOption, 'ipc', 3);63test('stdio[*] can be ["ipc"]', testNoPipeOption, ['ipc'], 3);64test('stdio[*] can be "inherit"', testNoPipeOption, 'inherit', 3);65test('stdio[*] can be ["inherit"]', testNoPipeOption, ['inherit'], 3);66test('stdio[*] can be 3', testNoPipeOption, 3, 3);67test('stdio[*] can be [3]', testNoPipeOption, [3], 3);

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

line 68why, one step back
68

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 69why, one step back
69const testInvalidArrayValue = (t, invalidStdio, index, execaMethod) => {

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

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

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 71why, one step back
71 execaMethod('empty.js', getStdio(index, ['pipe', invalidStdio]));

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 72–74why, one step back
72 }, {message: /must not include/});
73};74

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 75–90why, one step back
75test('Cannot pass "ignore" and another value to stdin', testInvalidArrayValue, 'ignore', 0, execa);
76test('Cannot pass "ignore" and another value to stdout', testInvalidArrayValue, 'ignore', 1, execa);77test('Cannot pass "ignore" and another value to stderr', testInvalidArrayValue, 'ignore', 2, execa);78test('Cannot pass "ignore" and another value to stdio[*]', testInvalidArrayValue, 'ignore', 3, execa);79test('Cannot pass "ignore" and another value to stdin - sync', testInvalidArrayValue, 'ignore', 0, execaSync);80test('Cannot pass "ignore" and another value to stdout - sync', testInvalidArrayValue, 'ignore', 1, execaSync);81test('Cannot pass "ignore" and another value to stderr - sync', testInvalidArrayValue, 'ignore', 2, execaSync);82test('Cannot pass "ignore" and another value to stdio[*] - sync', testInvalidArrayValue, 'ignore', 3, execaSync);83test('Cannot pass "ipc" and another value to stdin', testInvalidArrayValue, 'ipc', 0, execa);84test('Cannot pass "ipc" and another value to stdout', testInvalidArrayValue, 'ipc', 1, execa);85test('Cannot pass "ipc" and another value to stderr', testInvalidArrayValue, 'ipc', 2, execa);86test('Cannot pass "ipc" and another value to stdio[*]', testInvalidArrayValue, 'ipc', 3, execa);87test('Cannot pass "ipc" and another value to stdin - sync', testInvalidArrayValue, 'ipc', 0, execaSync);88test('Cannot pass "ipc" and another value to stdout - sync', testInvalidArrayValue, 'ipc', 1, execaSync);89test('Cannot pass "ipc" and another value to stderr - sync', testInvalidArrayValue, 'ipc', 2, execaSync);90test('Cannot pass "ipc" and another value to stdio[*] - sync', testInvalidArrayValue, 'ipc', 3, execaSync);

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 91–93why, one step back
91
92const testInputOutput = (t, stdioOption, execaMethod) => {93 t.throws(() => {

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 94why, one step back
94 execaMethod('empty.js', getStdio(3, [new ReadableStream(), stdioOption]));

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 95–110why, one step back
95 }, {message: /readable and writable/});
96};9798test('Cannot pass both readable and writable values to stdio[*] - WritableStream', testInputOutput, new WritableStream(), execa);99test('Cannot pass both readable and writable values to stdio[*] - 1', testInputOutput, 1, execa);100test('Cannot pass both readable and writable values to stdio[*] - 2', testInputOutput, 2, execa);101test('Cannot pass both readable and writable values to stdio[*] - process.stdout', testInputOutput, process.stdout, execa);102test('Cannot pass both readable and writable values to stdio[*] - process.stderr', testInputOutput, process.stderr, execa);103test('Cannot pass both readable and writable values to stdio[*] - WritableStream - sync', testInputOutput, new WritableStream(), execaSync);104test('Cannot pass both readable and writable values to stdio[*] - 1 - sync', testInputOutput, 1, execaSync);105test('Cannot pass both readable and writable values to stdio[*] - 2 - sync', testInputOutput, 2, execaSync);106test('Cannot pass both readable and writable values to stdio[*] - process.stdout - sync', testInputOutput, process.stdout, execaSync);107test('Cannot pass both readable and writable values to stdio[*] - process.stderr - sync', testInputOutput, process.stderr, execaSync);108109const testAmbiguousDirection = async (t, execaMethod) => {110 const [filePathOne, filePathTwo] = [tempfile(), tempfile()];

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 111–115why, one step back
111 await execaMethod('noop-fd.js', ['3', 'foobar'], getStdio(3, [{file: filePathOne}, {file: filePathTwo}]));
112 t.deepEqual(113 await Promise.all([readFile(filePathOne, 'utf8'), readFile(filePathTwo, 'utf8')]),114 ['foobar', 'foobar'],115 );

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 116–121why, one step back
116 await Promise.all([rm(filePathOne), rm(filePathTwo)]);
117};118119test('stdio[*] default direction is output', testAmbiguousDirection, execa);120test('stdio[*] default direction is output - sync', testAmbiguousDirection, execaSync);121

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 122why, one step back
122const testAmbiguousMultiple = async (t, index) => {

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 123–124why, one step back
123 const filePath = tempfile();
124 await writeFile(filePath, 'foobar');

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 125why, one step back
125 const {stdout} = await execa('stdin-fd.js', [`${index}`], getStdio(index, [{file: filePath}, ['foo', 'bar']]));

Fix using multiple `stdin` values (#661)ehmickybefore adoption4d8f9ca

line 126why, one step back
126 t.is(stdout, 'foobarfoobar');

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 127–129why, one step back
127 await rm(filePath);
128};129

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 130–131why, one step back
130test('stdin ambiguous direction is influenced by other values', testAmbiguousMultiple, 0);
131test('stdio[*] ambiguous direction is influenced by other values', testAmbiguousMultiple, 3);

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

line 132why, one step back
132

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 133–139why, one step back
133const testRedirect = async (t, stdioOption, index, isInput) => {
134 const {fixtureName, ...options} = isInput135 ? {fixtureName: 'stdin-fd.js', input: 'foobar'}136 : {fixtureName: 'noop-fd.js'};137 const {stdio} = await execa('nested-stdio.js', [JSON.stringify(stdioOption), `${index}`, fixtureName, 'foobar'], options);138 const resultIndex = isStderrDescriptor(stdioOption) ? 2 : 1;139 t.is(stdio[resultIndex], 'foobar');

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 140–141why, one step back
140};
141

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 142–144why, one step back
142const isStderrDescriptor = stdioOption => stdioOption === 2
143 || stdioOption === 'stderr'144 || (Array.isArray(stdioOption) && isStderrDescriptor(stdioOption[0]));

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

line 145why, one step back
145

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 146–175why, one step back
146test.serial('stdio[*] can be 0', testRedirect, 0, 3, true);
147test.serial('stdio[*] can be [0]', testRedirect, [0], 3, true);148test.serial('stdio[*] can be [0, "pipe"]', testRedirect, [0, 'pipe'], 3, true);149test.serial('stdio[*] can be process.stdin', testRedirect, 'stdin', 3, true);150test.serial('stdio[*] can be [process.stdin]', testRedirect, ['stdin'], 3, true);151test.serial('stdio[*] can be [process.stdin, "pipe"]', testRedirect, ['stdin', 'pipe'], 3, true);152test('stdout can be 2', testRedirect, 2, 1, false);153test('stdout can be [2]', testRedirect, [2], 1, false);154test('stdout can be [2, "pipe"]', testRedirect, [2, 'pipe'], 1, false);155test('stdout can be process.stderr', testRedirect, 'stderr', 1, false);156test('stdout can be [process.stderr]', testRedirect, ['stderr'], 1, false);157test('stdout can be [process.stderr, "pipe"]', testRedirect, ['stderr', 'pipe'], 1, false);158test('stderr can be 1', testRedirect, 1, 2, false);159test('stderr can be [1]', testRedirect, [1], 2, false);160test('stderr can be [1, "pipe"]', testRedirect, [1, 'pipe'], 2, false);161test('stderr can be process.stdout', testRedirect, 'stdout', 2, false);162test('stderr can be [process.stdout]', testRedirect, ['stdout'], 2, false);163test('stderr can be [process.stdout, "pipe"]', testRedirect, ['stdout', 'pipe'], 2, false);164test('stdio[*] can be 1', testRedirect, 1, 3, false);165test('stdio[*] can be [1]', testRedirect, [1], 3, false);166test('stdio[*] can be [1, "pipe"]', testRedirect, [1, 'pipe'], 3, false);167test('stdio[*] can be 2', testRedirect, 2, 3, false);168test('stdio[*] can be [2]', testRedirect, [2], 3, false);169test('stdio[*] can be [2, "pipe"]', testRedirect, [2, 'pipe'], 3, false);170test('stdio[*] can be process.stdout', testRedirect, 'stdout', 3, false);171test('stdio[*] can be [process.stdout]', testRedirect, ['stdout'], 3, false);172test('stdio[*] can be [process.stdout, "pipe"]', testRedirect, ['stdout', 'pipe'], 3, false);173test('stdio[*] can be process.stderr', testRedirect, 'stderr', 3, false);174test('stdio[*] can be [process.stderr]', testRedirect, ['stderr'], 3, false);175test('stdio[*] can be [process.stderr, "pipe"]', testRedirect, ['stderr', 'pipe'], 3, false);

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 176–202why, one step back
176
177const testInheritStdin = async (t, stdin) => {178 const {stdout} = await execa('nested-multiple-stdin.js', [JSON.stringify(stdin)], {input: 'foobar'});179 t.is(stdout, 'foobarfoobar');180};181182test('stdin can be ["inherit", "pipe"]', testInheritStdin, ['inherit', 'pipe']);183test('stdin can be [0, "pipe"]', testInheritStdin, [0, 'pipe']);184185const testInheritStdout = async (t, stdout) => {186 const result = await execa('nested-multiple-stdout.js', [JSON.stringify(stdout)]);187 t.is(result.stdout, 'foobar');188 t.is(result.stderr, 'nested foobar');189};190191test('stdout can be ["inherit", "pipe"]', testInheritStdout, ['inherit', 'pipe']);192test('stdout can be [1, "pipe"]', testInheritStdout, [1, 'pipe']);193194const testInheritStderr = async (t, stderr) => {195 const result = await execa('nested-multiple-stderr.js', [JSON.stringify(stderr)]);196 t.is(result.stdout, 'nested foobar');197 t.is(result.stderr, 'foobar');198};199200test('stderr can be ["inherit", "pipe"]', testInheritStderr, ['inherit', 'pipe']);201test('stderr can be [2, "pipe"]', testInheritStderr, [2, 'pipe']);202

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 203–204why, one step back
203const testOverflowStream = async (t, index, stdioOption) => {
204 const {stdout} = await execa('nested.js', [JSON.stringify(getStdio(index, stdioOption)), 'empty.js'], fullStdio);

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 205–208why, one step back
205 t.is(stdout, '');
206};207208if (process.platform === 'linux') {

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 209–216why, one step back
209 test('stdin can use 4+', testOverflowStream, 0, 4);
210 test('stdin can use [4+]', testOverflowStream, 0, [4]);211 test('stdout can use 4+', testOverflowStream, 1, 4);212 test('stdout can use [4+]', testOverflowStream, 1, [4]);213 test('stderr can use 4+', testOverflowStream, 2, 4);214 test('stderr can use [4+]', testOverflowStream, 2, [4]);215 test('stdio[*] can use 4+', testOverflowStream, 3, 4);216 test('stdio[*] can use [4+]', testOverflowStream, 3, [4]);

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 217–218why, one step back
217}
218

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 219–220why, one step back
219test('stdio[*] can use "inherit"', testOverflowStream, 3, 'inherit');
220test('stdio[*] can use ["inherit"]', testOverflowStream, 3, ['inherit']);

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

line 221why, one step back
221

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 222why, one step back
222const testOverflowStreamArray = (t, index, stdioOption) => {

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

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

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

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

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 225–227why, one step back
225 }, {message: /no such standard stream/});
226};227

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 228–232why, one step back
228test('stdin cannot use 4+ and another value', testOverflowStreamArray, 0, [4, 'pipe']);
229test('stdout cannot use 4+ and another value', testOverflowStreamArray, 1, [4, 'pipe']);230test('stderr cannot use 4+ and another value', testOverflowStreamArray, 2, [4, 'pipe']);231test('stdio[*] cannot use 4+ and another value', testOverflowStreamArray, 3, [4, 'pipe']);232test('stdio[*] cannot use "inherit" and another value', testOverflowStreamArray, 3, ['inherit', 'pipe']);

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

line 233why, one step back
233

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 234–235why, one step back
234const testOverlapped = async (t, index) => {
235 const {stdout} = await execa('noop.js', ['foobar'], getStdio(index, ['overlapped', 'pipe']));

Allow `stdin`/`stdout`/`stderr` to be an array (#643)ehmickybefore adoption7efd50c

lines 236–238why, one step back
236 t.is(stdout, 'foobar');
237};238

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 239–242why, one step back
239test('stdin can be ["overlapped", "pipe"]', testOverlapped, 0);
240test('stdout can be ["overlapped", "pipe"]', testOverlapped, 1);241test('stderr can be ["overlapped", "pipe"]', testOverlapped, 2);242test('stdio[*] can be ["overlapped", "pipe"]', testOverlapped, 3);

Fix Node.js streams not being cleaned up on process error (#660)ehmickybefore adoption61a1623

line 243why, one step back
243

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 244why, one step back
244const testDestroyStandard = async (t, index) => {

Fix Node.js streams not being cleaned up on process error (#660)ehmickybefore adoption61a1623

line 245why, one step back
245 await t.throwsAsync(

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 246why, one step back
246 execa('forever.js', {...getStdio(index, [STANDARD_STREAMS[index], 'pipe']), timeout: 1}),

Fix Node.js streams not being cleaned up on process error (#660)ehmickybefore adoption61a1623

lines 247–248why, one step back
247 {message: /timed out/},
248 );

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 249why, one step back
249 t.false(STANDARD_STREAMS[index].destroyed);

Fix Node.js streams not being cleaned up on process error (#660)ehmickybefore adoption61a1623

lines 250–251why, one step back
250};
251

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 252–254why, one step back
252test('Does not destroy process.stdin on errors', testDestroyStandard, 0);
253test('Does not destroy process.stdout on errors', testDestroyStandard, 1);254test('Does not destroy process.stderr on errors', testDestroyStandard, 2);