Skip to content
Myr

array.js

This file

Path

test/stdio/array.js

255 lines · 14.0 KiB · JavaScript

Last changed by

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

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';

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

line 7why, one step back
7import {stringGenerator} from '../helpers/generator.js';

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

lines 8–11why, one step back
8import {setFixtureDir} from '../helpers/fixtures-dir.js';
910setFixtureDir();11

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

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

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

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

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

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

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

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

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

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

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

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

Fix typo in a test (#662)ehmickybefore adoption710ecd5

line 30why, one step back
30 await childProcess;

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

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

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

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

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

line 69why, one step back
69

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

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

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

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

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

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

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

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

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

lines 76–91why, one step back
76test('Cannot pass "ignore" and another value to stdin', testInvalidArrayValue, 'ignore', 0, execa);
77test('Cannot pass "ignore" and another value to stdout', testInvalidArrayValue, 'ignore', 1, execa);78test('Cannot pass "ignore" and another value to stderr', testInvalidArrayValue, 'ignore', 2, execa);79test('Cannot pass "ignore" and another value to stdio[*]', testInvalidArrayValue, 'ignore', 3, execa);80test('Cannot pass "ignore" and another value to stdin - sync', testInvalidArrayValue, 'ignore', 0, execaSync);81test('Cannot pass "ignore" and another value to stdout - sync', testInvalidArrayValue, 'ignore', 1, execaSync);82test('Cannot pass "ignore" and another value to stderr - sync', testInvalidArrayValue, 'ignore', 2, execaSync);83test('Cannot pass "ignore" and another value to stdio[*] - sync', testInvalidArrayValue, 'ignore', 3, execaSync);84test('Cannot pass "ipc" and another value to stdin', testInvalidArrayValue, 'ipc', 0, execa);85test('Cannot pass "ipc" and another value to stdout', testInvalidArrayValue, 'ipc', 1, execa);86test('Cannot pass "ipc" and another value to stderr', testInvalidArrayValue, 'ipc', 2, execa);87test('Cannot pass "ipc" and another value to stdio[*]', testInvalidArrayValue, 'ipc', 3, execa);88test('Cannot pass "ipc" and another value to stdin - sync', testInvalidArrayValue, 'ipc', 0, execaSync);89test('Cannot pass "ipc" and another value to stdout - sync', testInvalidArrayValue, 'ipc', 1, execaSync);90test('Cannot pass "ipc" and another value to stderr - sync', testInvalidArrayValue, 'ipc', 2, execaSync);91test('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 92–94why, one step back
92
93const testInputOutput = (t, stdioOption, execaMethod) => {94 t.throws(() => {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

line 133why, one step back
133

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

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

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

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

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

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

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

line 146why, one step back
146

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

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

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

lines 177–203why, one step back
177
178const testInheritStdin = async (t, stdin) => {179 const {stdout} = await execa('nested-multiple-stdin.js', [JSON.stringify(stdin)], {input: 'foobar'});180 t.is(stdout, 'foobarfoobar');181};182183test('stdin can be ["inherit", "pipe"]', testInheritStdin, ['inherit', 'pipe']);184test('stdin can be [0, "pipe"]', testInheritStdin, [0, 'pipe']);185186const testInheritStdout = async (t, stdout) => {187 const result = await execa('nested-multiple-stdout.js', [JSON.stringify(stdout)]);188 t.is(result.stdout, 'foobar');189 t.is(result.stderr, 'nested foobar');190};191192test('stdout can be ["inherit", "pipe"]', testInheritStdout, ['inherit', 'pipe']);193test('stdout can be [1, "pipe"]', testInheritStdout, [1, 'pipe']);194195const testInheritStderr = async (t, stderr) => {196 const result = await execa('nested-multiple-stderr.js', [JSON.stringify(stderr)]);197 t.is(result.stdout, 'nested foobar');198 t.is(result.stderr, 'foobar');199};200201test('stderr can be ["inherit", "pipe"]', testInheritStderr, ['inherit', 'pipe']);202test('stderr can be [2, "pipe"]', testInheritStderr, [2, 'pipe']);203

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

lines 204–205why, one step back
204const testOverflowStream = async (t, index, stdioOption) => {
205 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 206–209why, one step back
206 t.is(stdout, '');
207};208209if (process.platform === 'linux') {

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

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

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

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

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

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

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

line 222why, one step back
222

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

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

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

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

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

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

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

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

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

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

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

line 234why, one step back
234

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

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

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

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

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

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

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

line 244why, one step back
244

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

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

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

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

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

line 247why, one step back
247 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 248–249why, one step back
248 {message: /timed out/},
249 );

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

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

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

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

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

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