Skip to content
Myr

array.js

This file

Path

test/stdio/array.js

265 lines · 15.4 KiB · JavaScript

Last changed by

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

History of this file

Take it

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

lines 1–6why, 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';6import {getStdinOption, getStdoutOption, getStderrOption, getStdioOption} 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–126why, one step back
8import {setFixtureDir} from '../helpers/fixtures-dir.js';
910setFixtureDir();1112const testEmptyArray = (t, getOptions, optionName, execaMethod) => {13 t.throws(() => {14 execaMethod('noop.js', getOptions([]));15 }, {message: `The \`${optionName}\` option must not be an empty array.`});16};1718test('Cannot pass an empty array to stdin', testEmptyArray, getStdinOption, 'stdin', execa);19test('Cannot pass an empty array to stdout', testEmptyArray, getStdoutOption, 'stdout', execa);20test('Cannot pass an empty array to stderr', testEmptyArray, getStderrOption, 'stderr', execa);21test('Cannot pass an empty array to stdio[*]', testEmptyArray, getStdioOption, 'stdio[3]', execa);22test('Cannot pass an empty array to stdin - sync', testEmptyArray, getStdinOption, 'stdin', execaSync);23test('Cannot pass an empty array to stdout - sync', testEmptyArray, getStdoutOption, 'stdout', execaSync);24test('Cannot pass an empty array to stderr - sync', testEmptyArray, getStderrOption, 'stderr', execaSync);25test('Cannot pass an empty array to stdio[*] - sync', testEmptyArray, getStdioOption, 'stdio[3]', execaSync);2627const testNoPipeOption = async (t, stdioOption, streamName) => {28 const childProcess = execa('empty.js', {[streamName]: stdioOption});29 t.is(childProcess[streamName], null);30 await execa;31};3233test('stdin can be "ignore"', testNoPipeOption, 'ignore', 'stdin');34test('stdin can be ["ignore"]', testNoPipeOption, ['ignore'], 'stdin');35test('stdin can be ["ignore", "ignore"]', testNoPipeOption, ['ignore', 'ignore'], 'stdin');36test('stdin can be "ipc"', testNoPipeOption, 'ipc', 'stdin');37test('stdin can be ["ipc"]', testNoPipeOption, ['ipc'], 'stdin');38test('stdin can be "inherit"', testNoPipeOption, 'inherit', 'stdin');39test('stdin can be ["inherit"]', testNoPipeOption, ['inherit'], 'stdin');40test('stdin can be 0', testNoPipeOption, 0, 'stdin');41test('stdin can be [0]', testNoPipeOption, [0], 'stdin');42test('stdout can be "ignore"', testNoPipeOption, 'ignore', 'stdout');43test('stdout can be ["ignore"]', testNoPipeOption, ['ignore'], 'stdout');44test('stdout can be ["ignore", "ignore"]', testNoPipeOption, ['ignore', 'ignore'], 'stdout');45test('stdout can be "ipc"', testNoPipeOption, 'ipc', 'stdout');46test('stdout can be ["ipc"]', testNoPipeOption, ['ipc'], 'stdout');47test('stdout can be "inherit"', testNoPipeOption, 'inherit', 'stdout');48test('stdout can be ["inherit"]', testNoPipeOption, ['inherit'], 'stdout');49test('stdout can be 1', testNoPipeOption, 1, 'stdout');50test('stdout can be [1]', testNoPipeOption, [1], 'stdout');51test('stderr can be "ignore"', testNoPipeOption, 'ignore', 'stderr');52test('stderr can be ["ignore"]', testNoPipeOption, ['ignore'], 'stderr');53test('stderr can be ["ignore", "ignore"]', testNoPipeOption, ['ignore', 'ignore'], 'stderr');54test('stderr can be "ipc"', testNoPipeOption, 'ipc', 'stderr');55test('stderr can be ["ipc"]', testNoPipeOption, ['ipc'], 'stderr');56test('stderr can be "inherit"', testNoPipeOption, 'inherit', 'stderr');57test('stderr can be ["inherit"]', testNoPipeOption, ['inherit'], 'stderr');58test('stderr can be 2', testNoPipeOption, 2, 'stderr');59test('stderr can be [2]', testNoPipeOption, [2], 'stderr');6061const testNoPipeStdioOption = async (t, stdioOption) => {62 const childProcess = execa('empty.js', {stdio: ['pipe', 'pipe', 'pipe', stdioOption]});63 t.is(childProcess.stdio[3], null);64 await execa;65};6667test('stdio[*] can be "ignore"', testNoPipeStdioOption, 'ignore');68test('stdio[*] can be ["ignore"]', testNoPipeStdioOption, ['ignore']);69test('stdio[*] can be ["ignore", "ignore"]', testNoPipeStdioOption, ['ignore', 'ignore']);70test('stdio[*] can be "ipc"', testNoPipeStdioOption, 'ipc');71test('stdio[*] can be ["ipc"]', testNoPipeStdioOption, ['ipc']);72test('stdio[*] can be "inherit"', testNoPipeStdioOption, 'inherit');73test('stdio[*] can be ["inherit"]', testNoPipeStdioOption, ['inherit']);74test('stdio[*] can be 3', testNoPipeStdioOption, 3);75test('stdio[*] can be [3]', testNoPipeStdioOption, [3]);7677const testInvalidArrayValue = (t, invalidStdio, getOptions, execaMethod) => {78 t.throws(() => {79 execaMethod('noop.js', getOptions(['pipe', invalidStdio]));80 }, {message: /must not include/});81};8283test('Cannot pass "ignore" and another value to stdin', testInvalidArrayValue, 'ignore', getStdinOption, execa);84test('Cannot pass "ignore" and another value to stdout', testInvalidArrayValue, 'ignore', getStdoutOption, execa);85test('Cannot pass "ignore" and another value to stderr', testInvalidArrayValue, 'ignore', getStderrOption, execa);86test('Cannot pass "ignore" and another value to stdio[*]', testInvalidArrayValue, 'ignore', getStdioOption, execa);87test('Cannot pass "ignore" and another value to stdin - sync', testInvalidArrayValue, 'ignore', getStdinOption, execaSync);88test('Cannot pass "ignore" and another value to stdout - sync', testInvalidArrayValue, 'ignore', getStdoutOption, execaSync);89test('Cannot pass "ignore" and another value to stderr - sync', testInvalidArrayValue, 'ignore', getStderrOption, execaSync);90test('Cannot pass "ignore" and another value to stdio[*] - sync', testInvalidArrayValue, 'ignore', getStdioOption, execaSync);91test('Cannot pass "ipc" and another value to stdin', testInvalidArrayValue, 'ipc', getStdinOption, execa);92test('Cannot pass "ipc" and another value to stdout', testInvalidArrayValue, 'ipc', getStdoutOption, execa);93test('Cannot pass "ipc" and another value to stderr', testInvalidArrayValue, 'ipc', getStderrOption, execa);94test('Cannot pass "ipc" and another value to stdio[*]', testInvalidArrayValue, 'ipc', getStdioOption, execa);95test('Cannot pass "ipc" and another value to stdin - sync', testInvalidArrayValue, 'ipc', getStdinOption, execaSync);96test('Cannot pass "ipc" and another value to stdout - sync', testInvalidArrayValue, 'ipc', getStdoutOption, execaSync);97test('Cannot pass "ipc" and another value to stderr - sync', testInvalidArrayValue, 'ipc', getStderrOption, execaSync);98test('Cannot pass "ipc" and another value to stdio[*] - sync', testInvalidArrayValue, 'ipc', getStdioOption, execaSync);99100const testInputOutput = (t, stdioOption, execaMethod) => {101 t.throws(() => {102 execaMethod('noop.js', getStdioOption([new ReadableStream(), stdioOption]));103 }, {message: /readable and writable/});104};105106test('Cannot pass both readable and writable values to stdio[*] - WritableStream', testInputOutput, new WritableStream(), execa);107test('Cannot pass both readable and writable values to stdio[*] - 1', testInputOutput, 1, execa);108test('Cannot pass both readable and writable values to stdio[*] - 2', testInputOutput, 2, execa);109test('Cannot pass both readable and writable values to stdio[*] - process.stdout', testInputOutput, process.stdout, execa);110test('Cannot pass both readable and writable values to stdio[*] - process.stderr', testInputOutput, process.stderr, execa);111test('Cannot pass both readable and writable values to stdio[*] - WritableStream - sync', testInputOutput, new WritableStream(), execaSync);112test('Cannot pass both readable and writable values to stdio[*] - 1 - sync', testInputOutput, 1, execaSync);113test('Cannot pass both readable and writable values to stdio[*] - 2 - sync', testInputOutput, 2, execaSync);114test('Cannot pass both readable and writable values to stdio[*] - process.stdout - sync', testInputOutput, process.stdout, execaSync);115test('Cannot pass both readable and writable values to stdio[*] - process.stderr - sync', testInputOutput, process.stderr, execaSync);116117const testAmbiguousDirection = async (t, execaMethod) => {118 const [filePathOne, filePathTwo] = [tempfile(), tempfile()];119 await execaMethod('noop-fd3.js', ['foobar'], getStdioOption([filePathOne, filePathTwo]));120 t.deepEqual(await Promise.all([readFile(filePathOne, 'utf8'), readFile(filePathTwo, 'utf8')]), ['foobar\n', 'foobar\n']);121 await Promise.all([rm(filePathOne), rm(filePathTwo)]);122};123124test('stdio[*] default direction is output', testAmbiguousDirection, execa);125test('stdio[*] default direction is output - sync', testAmbiguousDirection, execaSync);126

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

line 127why, one step back
127const testAmbiguousMultiple = async (t, fixtureName, getOptions) => {

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

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

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

lines 130–131why, one step back
130 const {stdout} = await execa(fixtureName, getOptions([filePath, stringGenerator()]));
131 t.is(stdout, 'foobarfoobar');

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

lines 132–134why, one step back
132 await rm(filePath);
133};134

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

lines 135–136why, one step back
135test('stdin ambiguous direction is influenced by other values', testAmbiguousMultiple, 'stdin.js', getStdinOption);
136test('stdio[*] ambiguous direction is influenced by other values', testAmbiguousMultiple, 'stdin-fd3.js', getStdioOption);

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

lines 137–253why, one step back
137
138const testRedirectInput = async (t, stdioOption, index, fixtureName) => {139 const {stdout} = await execa('nested-stdio.js', [JSON.stringify(stdioOption), String(index), fixtureName], {input: 'foobar'});140 t.is(stdout, 'foobar');141};142143test.serial('stdio[*] can be 0', testRedirectInput, 0, 3, 'stdin-fd3.js');144test.serial('stdio[*] can be [0]', testRedirectInput, [0], 3, 'stdin-fd3.js');145test.serial('stdio[*] can be [0, "pipe"]', testRedirectInput, [0, 'pipe'], 3, 'stdin-fd3.js');146test.serial('stdio[*] can be process.stdin', testRedirectInput, 'stdin', 3, 'stdin-fd3.js');147test.serial('stdio[*] can be [process.stdin]', testRedirectInput, ['stdin'], 3, 'stdin-fd3.js');148test.serial('stdio[*] can be [process.stdin, "pipe"]', testRedirectInput, ['stdin', 'pipe'], 3, 'stdin-fd3.js');149150const OUTPUT_DESCRIPTOR_FIXTURES = ['noop.js', 'noop-err.js', 'noop-fd3.js'];151152const isStdoutDescriptor = stdioOption => stdioOption === 1153 || stdioOption === 'stdout'154 || (Array.isArray(stdioOption) && isStdoutDescriptor(stdioOption[0]));155156const testRedirectOutput = async (t, stdioOption, index) => {157 const fixtureName = OUTPUT_DESCRIPTOR_FIXTURES[index - 1];158 const result = await execa('nested-stdio.js', [JSON.stringify(stdioOption), String(index), fixtureName, 'foobar']);159 const streamName = isStdoutDescriptor(stdioOption) ? 'stdout' : 'stderr';160 t.is(result[streamName], 'foobar');161};162163test('stdout can be 2', testRedirectOutput, 2, 1);164test('stdout can be [2]', testRedirectOutput, [2], 1);165test('stdout can be [2, "pipe"]', testRedirectOutput, [2, 'pipe'], 1);166test('stdout can be process.stderr', testRedirectOutput, 'stderr', 1);167test('stdout can be [process.stderr]', testRedirectOutput, ['stderr'], 1);168test('stdout can be [process.stderr, "pipe"]', testRedirectOutput, ['stderr', 'pipe'], 1);169test('stderr can be 1', testRedirectOutput, 1, 2);170test('stderr can be [1]', testRedirectOutput, [1], 2);171test('stderr can be [1, "pipe"]', testRedirectOutput, [1, 'pipe'], 2);172test('stderr can be process.stdout', testRedirectOutput, 'stdout', 2);173test('stderr can be [process.stdout]', testRedirectOutput, ['stdout'], 2);174test('stderr can be [process.stdout, "pipe"]', testRedirectOutput, ['stdout', 'pipe'], 2);175test('stdio[*] can be 1', testRedirectOutput, 1, 3);176test('stdio[*] can be [1]', testRedirectOutput, [1], 3);177test('stdio[*] can be [1, "pipe"]', testRedirectOutput, [1, 'pipe'], 3);178test('stdio[*] can be 2', testRedirectOutput, 2, 3);179test('stdio[*] can be [2]', testRedirectOutput, [2], 3);180test('stdio[*] can be [2, "pipe"]', testRedirectOutput, [2, 'pipe'], 3);181test('stdio[*] can be process.stdout', testRedirectOutput, 'stdout', 3);182test('stdio[*] can be [process.stdout]', testRedirectOutput, ['stdout'], 3);183test('stdio[*] can be [process.stdout, "pipe"]', testRedirectOutput, ['stdout', 'pipe'], 3);184test('stdio[*] can be process.stderr', testRedirectOutput, 'stderr', 3);185test('stdio[*] can be [process.stderr]', testRedirectOutput, ['stderr'], 3);186test('stdio[*] can be [process.stderr, "pipe"]', testRedirectOutput, ['stderr', 'pipe'], 3);187188const testInheritStdin = async (t, stdin) => {189 const {stdout} = await execa('nested-multiple-stdin.js', [JSON.stringify(stdin)], {input: 'foobar'});190 t.is(stdout, 'foobarfoobar');191};192193test('stdin can be ["inherit", "pipe"]', testInheritStdin, ['inherit', 'pipe']);194test('stdin can be [0, "pipe"]', testInheritStdin, [0, 'pipe']);195196const testInheritStdout = async (t, stdout) => {197 const result = await execa('nested-multiple-stdout.js', [JSON.stringify(stdout)]);198 t.is(result.stdout, 'foobar');199 t.is(result.stderr, 'nested foobar');200};201202test('stdout can be ["inherit", "pipe"]', testInheritStdout, ['inherit', 'pipe']);203test('stdout can be [1, "pipe"]', testInheritStdout, [1, 'pipe']);204205const testInheritStderr = async (t, stderr) => {206 const result = await execa('nested-multiple-stderr.js', [JSON.stringify(stderr)]);207 t.is(result.stdout, 'nested foobar');208 t.is(result.stderr, 'foobar');209};210211test('stderr can be ["inherit", "pipe"]', testInheritStderr, ['inherit', 'pipe']);212test('stderr can be [2, "pipe"]', testInheritStderr, [2, 'pipe']);213214const testOverflowStream = async (t, stdio) => {215 const {stdout} = await execa('nested.js', [JSON.stringify({stdio}), 'empty.js'], {stdio: ['pipe', 'pipe', 'pipe', 'pipe']});216 t.is(stdout, '');217};218219if (process.platform === 'linux') {220 test('stdin can use 4+', testOverflowStream, [4, 'pipe', 'pipe', 'pipe']);221 test('stdin can use [4+]', testOverflowStream, [[4], 'pipe', 'pipe', 'pipe']);222 test('stdout can use 4+', testOverflowStream, ['pipe', 4, 'pipe', 'pipe']);223 test('stdout can use [4+]', testOverflowStream, ['pipe', [4], 'pipe', 'pipe']);224 test('stderr can use 4+', testOverflowStream, ['pipe', 'pipe', 4, 'pipe']);225 test('stderr can use [4+]', testOverflowStream, ['pipe', 'pipe', [4], 'pipe']);226 test('stdio[*] can use 4+', testOverflowStream, ['pipe', 'pipe', 'pipe', 4]);227 test('stdio[*] can use [4+]', testOverflowStream, ['pipe', 'pipe', 'pipe', [4]]);228}229230test('stdio[*] can use "inherit"', testOverflowStream, ['pipe', 'pipe', 'pipe', 'inherit']);231test('stdio[*] can use ["inherit"]', testOverflowStream, ['pipe', 'pipe', 'pipe', ['inherit']]);232233const testOverflowStreamArray = (t, stdio) => {234 t.throws(() => {235 execa('noop.js', {stdio});236 }, {message: /no such standard stream/});237};238239test('stdin cannot use 4+ and another value', testOverflowStreamArray, [[4, 'pipe'], 'pipe', 'pipe', 'pipe']);240test('stdout cannot use 4+ and another value', testOverflowStreamArray, ['pipe', [4, 'pipe'], 'pipe', 'pipe']);241test('stderr cannot use 4+ and another value', testOverflowStreamArray, ['pipe', 'pipe', [4, 'pipe'], 'pipe']);242test('stdio[*] cannot use 4+ and another value', testOverflowStreamArray, ['pipe', 'pipe', 'pipe', [4, 'pipe']]);243test('stdio[*] cannot use "inherit" and another value', testOverflowStreamArray, ['pipe', 'pipe', 'pipe', ['inherit', 'pipe']]);244245const testOverlapped = async (t, getOptions) => {246 const {stdout} = await execa('noop.js', ['foobar'], getOptions(['overlapped', 'pipe']));247 t.is(stdout, 'foobar');248};249250test('stdin can be ["overlapped", "pipe"]', testOverlapped, getStdinOption);251test('stdout can be ["overlapped", "pipe"]', testOverlapped, getStdoutOption);252test('stderr can be ["overlapped", "pipe"]', testOverlapped, getStderrOption);253test('stdio[*] can be ["overlapped", "pipe"]', testOverlapped, getStdioOption);

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

lines 254–265why, one step back
254
255const testDestroyStandard = async (t, optionName) => {256 await t.throwsAsync(257 execa('forever.js', {timeout: 1, [optionName]: [process[optionName], 'pipe']}),258 {message: /timed out/},259 );260 t.false(process[optionName].destroyed);261};262263test('Does not destroy process.stdin on errors', testDestroyStandard, 'stdin');264test('Does not destroy process.stdout on errors', testDestroyStandard, 'stdout');265test('Does not destroy process.stderr on errors', testDestroyStandard, 'stderr');