Skip to content
Myr

native.js

This file

Path

test/stdio/native.js

235 lines · 15.2 KiB · JavaScript

Last changed by

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

History of this file

Take it

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

line 1why, one step back
1import {readFile, rm} from 'node:fs/promises';

Split files (#837)ehmickybefore adoption672c2d7

lines 2–3why, one step back
2import {platform} from 'node:process';
3import test from 'ava';

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

line 4why, one step back
4import tempfile from 'tempfile';

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

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

Split files (#837)ehmickybefore adoption672c2d7

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

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

line 8why, one step back
8import {foobarString} from '../helpers/input.js';

Split files (#837)ehmickybefore adoption672c2d7

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

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

lines 12–18why, one step back
12const isLinux = platform === 'linux';
13const isWindows = platform === 'win32';1415const getFixtureName = isSync => isSync ? 'nested-sync.js' : 'nested.js';1617// eslint-disable-next-line max-params18const testRedirect = async (t, stdioOption, fdNumber, isInput, isSync) => {

Split files (#837)ehmickybefore adoption672c2d7

line 19why, one step back
19 const {fixtureName, ...options} = isInput

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

line 20why, one step back
20 ? {fixtureName: 'stdin-fd.js', input: foobarString}

Split files (#837)ehmickybefore adoption672c2d7

line 21why, one step back
21 : {fixtureName: 'noop-fd.js'};

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

line 22why, one step back
22 const {stdio} = await execa('nested-stdio.js', [JSON.stringify(stdioOption), `${fdNumber}`, `${isSync}`, fixtureName, foobarString], options);

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

line 23why, one step back
23 const resultFdNumber = isStderrDescriptor(stdioOption) ? 2 : 1;

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

line 24why, one step back
24 t.is(stdio[resultFdNumber], foobarString);

Split files (#837)ehmickybefore adoption672c2d7

lines 25–30why, one step back
25};
2627const isStderrDescriptor = stdioOption => stdioOption === 228 || stdioOption === 'stderr'29 || (Array.isArray(stdioOption) && isStderrDescriptor(stdioOption[0]));30

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

lines 31–84why, one step back
31test.serial('stdio[*] can be 0', testRedirect, 0, 3, true, false);
32test.serial('stdio[*] can be [0]', testRedirect, [0], 3, true, false);33test.serial('stdio[*] can be [0, "pipe"]', testRedirect, [0, 'pipe'], 3, true, false);34test.serial('stdio[*] can be process.stdin', testRedirect, 'stdin', 3, true, false);35test.serial('stdio[*] can be [process.stdin]', testRedirect, ['stdin'], 3, true, false);36test.serial('stdio[*] can be [process.stdin, "pipe"]', testRedirect, ['stdin', 'pipe'], 3, true, false);37test('stdout can be 2', testRedirect, 2, 1, false, false);38test('stdout can be [2]', testRedirect, [2], 1, false, false);39test('stdout can be [2, "pipe"]', testRedirect, [2, 'pipe'], 1, false, false);40test('stdout can be process.stderr', testRedirect, 'stderr', 1, false, false);41test('stdout can be [process.stderr]', testRedirect, ['stderr'], 1, false, false);42test('stdout can be [process.stderr, "pipe"]', testRedirect, ['stderr', 'pipe'], 1, false, false);43test('stderr can be 1', testRedirect, 1, 2, false, false);44test('stderr can be [1]', testRedirect, [1], 2, false, false);45test('stderr can be [1, "pipe"]', testRedirect, [1, 'pipe'], 2, false, false);46test('stderr can be process.stdout', testRedirect, 'stdout', 2, false, false);47test('stderr can be [process.stdout]', testRedirect, ['stdout'], 2, false, false);48test('stderr can be [process.stdout, "pipe"]', testRedirect, ['stdout', 'pipe'], 2, false, false);49test('stdio[*] can be 1', testRedirect, 1, 3, false, false);50test('stdio[*] can be [1]', testRedirect, [1], 3, false, false);51test('stdio[*] can be [1, "pipe"]', testRedirect, [1, 'pipe'], 3, false, false);52test('stdio[*] can be 2', testRedirect, 2, 3, false, false);53test('stdio[*] can be [2]', testRedirect, [2], 3, false, false);54test('stdio[*] can be [2, "pipe"]', testRedirect, [2, 'pipe'], 3, false, false);55test('stdio[*] can be process.stdout', testRedirect, 'stdout', 3, false, false);56test('stdio[*] can be [process.stdout]', testRedirect, ['stdout'], 3, false, false);57test('stdio[*] can be [process.stdout, "pipe"]', testRedirect, ['stdout', 'pipe'], 3, false, false);58test('stdio[*] can be process.stderr', testRedirect, 'stderr', 3, false, false);59test('stdio[*] can be [process.stderr]', testRedirect, ['stderr'], 3, false, false);60test('stdio[*] can be [process.stderr, "pipe"]', testRedirect, ['stderr', 'pipe'], 3, false, false);61test('stdout can be 2, sync', testRedirect, 2, 1, false, true);62test('stdout can be [2], sync', testRedirect, [2], 1, false, true);63test('stdout can be [2, "pipe"], sync', testRedirect, [2, 'pipe'], 1, false, true);64test('stdout can be process.stderr, sync', testRedirect, 'stderr', 1, false, true);65test('stdout can be [process.stderr], sync', testRedirect, ['stderr'], 1, false, true);66test('stdout can be [process.stderr, "pipe"], sync', testRedirect, ['stderr', 'pipe'], 1, false, true);67test('stderr can be 1, sync', testRedirect, 1, 2, false, true);68test('stderr can be [1], sync', testRedirect, [1], 2, false, true);69test('stderr can be [1, "pipe"], sync', testRedirect, [1, 'pipe'], 2, false, true);70test('stderr can be process.stdout, sync', testRedirect, 'stdout', 2, false, true);71test('stderr can be [process.stdout], sync', testRedirect, ['stdout'], 2, false, true);72test('stderr can be [process.stdout, "pipe"], sync', testRedirect, ['stdout', 'pipe'], 2, false, true);73test('stdio[*] can be 1, sync', testRedirect, 1, 3, false, true);74test('stdio[*] can be [1], sync', testRedirect, [1], 3, false, true);75test('stdio[*] can be [1, "pipe"], sync', testRedirect, [1, 'pipe'], 3, false, true);76test('stdio[*] can be 2, sync', testRedirect, 2, 3, false, true);77test('stdio[*] can be [2], sync', testRedirect, [2], 3, false, true);78test('stdio[*] can be [2, "pipe"], sync', testRedirect, [2, 'pipe'], 3, false, true);79test('stdio[*] can be process.stdout, sync', testRedirect, 'stdout', 3, false, true);80test('stdio[*] can be [process.stdout], sync', testRedirect, ['stdout'], 3, false, true);81test('stdio[*] can be [process.stdout, "pipe"], sync', testRedirect, ['stdout', 'pipe'], 3, false, true);82test('stdio[*] can be process.stderr, sync', testRedirect, 'stderr', 3, false, true);83test('stdio[*] can be [process.stderr], sync', testRedirect, ['stderr'], 3, false, true);84test('stdio[*] can be [process.stderr, "pipe"], sync', testRedirect, ['stderr', 'pipe'], 3, false, true);

Split files (#837)ehmickybefore adoption672c2d7

line 85why, one step back
85

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

lines 86–87why, one step back
86const testInheritStdin = async (t, stdioOption, isSync) => {
87 const {stdout} = await execa('nested-multiple-stdin.js', [JSON.stringify(stdioOption), `${isSync}`], {input: foobarString});

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

line 88why, one step back
88 t.is(stdout, `${foobarString}${foobarString}`);

Split files (#837)ehmickybefore adoption672c2d7

lines 89–90why, one step back
89};
90

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

lines 91–96why, one step back
91test('stdin can be ["inherit", "pipe"]', testInheritStdin, ['inherit', 'pipe'], false);
92test('stdin can be [0, "pipe"]', testInheritStdin, [0, 'pipe'], false);93test('stdin can be [process.stdin, "pipe"]', testInheritStdin, ['stdin', 'pipe'], false);94test.serial('stdin can be ["inherit", "pipe"], sync', testInheritStdin, ['inherit', 'pipe'], true);95test.serial('stdin can be [0, "pipe"], sync', testInheritStdin, [0, 'pipe'], true);96test.serial('stdin can be [process.stdin, "pipe"], sync', testInheritStdin, ['stdin', 'pipe'], true);

Split files (#837)ehmickybefore adoption672c2d7

line 97why, one step back
97

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

lines 98–102why, one step back
98// eslint-disable-next-line max-params
99const testInheritStdioOutput = async (t, fdNumber, outerFdNumber, stdioOption, isSync, encoding) => {100 const {stdio} = await execa('nested-multiple-stdio-output.js', [JSON.stringify(stdioOption), `${fdNumber}`, `${outerFdNumber}`, `${isSync}`, encoding], fullStdio);101 t.is(stdio[fdNumber], foobarString);102 t.is(stdio[outerFdNumber], `nested ${foobarString}`);

Split files (#837)ehmickybefore adoption672c2d7

lines 103–104why, one step back
103};
104

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

lines 105–132why, one step back
105test('stdout can be ["inherit", "pipe"]', testInheritStdioOutput, 1, 2, ['inherit', 'pipe'], false, 'utf8');
106test('stdout can be [1, "pipe"]', testInheritStdioOutput, 1, 2, [1, 'pipe'], false, 'utf8');107test('stdout can be [process.stdout, "pipe"]', testInheritStdioOutput, 1, 2, ['stdout', 'pipe'], false, 'utf8');108test('stderr can be ["inherit", "pipe"]', testInheritStdioOutput, 2, 1, ['inherit', 'pipe'], false, 'utf8');109test('stderr can be [2, "pipe"]', testInheritStdioOutput, 2, 1, [2, 'pipe'], false, 'utf8');110test('stderr can be [process.stderr, "pipe"]', testInheritStdioOutput, 2, 1, ['stderr', 'pipe'], false, 'utf8');111test('stdout can be ["inherit", "pipe"], encoding "buffer"', testInheritStdioOutput, 1, 2, ['inherit', 'pipe'], false, 'buffer');112test('stdout can be [1, "pipe"], encoding "buffer"', testInheritStdioOutput, 1, 2, [1, 'pipe'], false, 'buffer');113test('stdout can be [process.stdout, "pipe"], encoding "buffer"', testInheritStdioOutput, 1, 2, ['stdout', 'pipe'], false, 'buffer');114test('stderr can be ["inherit", "pipe"], encoding "buffer"', testInheritStdioOutput, 2, 1, ['inherit', 'pipe'], false, 'buffer');115test('stderr can be [2, "pipe"], encoding "buffer"', testInheritStdioOutput, 2, 1, [2, 'pipe'], false, 'buffer');116test('stderr can be [process.stderr, "pipe"], encoding "buffer"', testInheritStdioOutput, 2, 1, ['stderr', 'pipe'], false, 'buffer');117test('stdout can be ["inherit", "pipe"], sync', testInheritStdioOutput, 1, 2, ['inherit', 'pipe'], true, 'utf8');118test('stdout can be [1, "pipe"], sync', testInheritStdioOutput, 1, 2, [1, 'pipe'], true, 'utf8');119test('stdout can be [process.stdout, "pipe"], sync', testInheritStdioOutput, 1, 2, ['stdout', 'pipe'], true, 'utf8');120test('stderr can be ["inherit", "pipe"], sync', testInheritStdioOutput, 2, 1, ['inherit', 'pipe'], true, 'utf8');121test('stderr can be [2, "pipe"], sync', testInheritStdioOutput, 2, 1, [2, 'pipe'], true, 'utf8');122test('stderr can be [process.stderr, "pipe"], sync', testInheritStdioOutput, 2, 1, ['stderr', 'pipe'], true, 'utf8');123test('stdio[*] output can be ["inherit", "pipe"], sync', testInheritStdioOutput, 3, 1, ['inherit', 'pipe'], true, 'utf8');124test('stdio[*] output can be [3, "pipe"], sync', testInheritStdioOutput, 3, 1, [3, 'pipe'], true, 'utf8');125test('stdout can be ["inherit", "pipe"], encoding "buffer", sync', testInheritStdioOutput, 1, 2, ['inherit', 'pipe'], true, 'buffer');126test('stdout can be [1, "pipe"], encoding "buffer", sync', testInheritStdioOutput, 1, 2, [1, 'pipe'], true, 'buffer');127test('stdout can be [process.stdout, "pipe"], encoding "buffer", sync', testInheritStdioOutput, 1, 2, ['stdout', 'pipe'], true, 'buffer');128test('stderr can be ["inherit", "pipe"], encoding "buffer", sync', testInheritStdioOutput, 2, 1, ['inherit', 'pipe'], true, 'buffer');129test('stderr can be [2, "pipe"], encoding "buffer", sync', testInheritStdioOutput, 2, 1, [2, 'pipe'], true, 'buffer');130test('stderr can be [process.stderr, "pipe"], encoding "buffer", sync', testInheritStdioOutput, 2, 1, ['stderr', 'pipe'], true, 'buffer');131test('stdio[*] output can be ["inherit", "pipe"], encoding "buffer", sync', testInheritStdioOutput, 3, 1, ['inherit', 'pipe'], true, 'buffer');132test('stdio[*] output can be [3, "pipe"], encoding "buffer", sync', testInheritStdioOutput, 3, 1, [3, 'pipe'], true, 'buffer');

Split files (#837)ehmickybefore adoption672c2d7

line 133why, one step back
133

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

lines 134–136why, one step back
134const testFd3InheritOutput = async (t, stdioOption, isSync) => {
135 const {stdio} = await execa(getFixtureName(isSync), [JSON.stringify(getStdio(3, stdioOption)), 'noop-fd.js', '3', foobarString], fullStdio);136 t.is(stdio[3], foobarString);

Split files (#837)ehmickybefore adoption672c2d7

lines 137–138why, one step back
137};
138

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

lines 139–142why, one step back
139test('stdio[*] output can use "inherit"', testFd3InheritOutput, 'inherit', false);
140test('stdio[*] output can use ["inherit"]', testFd3InheritOutput, ['inherit'], false);141test('stdio[*] output can use "inherit", sync', testFd3InheritOutput, 'inherit', true);142test('stdio[*] output can use ["inherit"], sync', testFd3InheritOutput, ['inherit'], true);

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

line 143why, one step back
143

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

line 144why, one step back
144const testInheritNoBuffer = async (t, stdioOption, isSync) => {

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

line 145why, one step back
145 const filePath = tempfile();

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

line 146why, one step back
146 await execa(getFixtureName(isSync), [JSON.stringify({stdin: stdioOption, buffer: false}), 'nested-write.js', filePath, foobarString], {input: foobarString});

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

lines 147–150why, one step back
147 t.is(await readFile(filePath, 'utf8'), `${foobarString} ${foobarString}`);
148 await rm(filePath);149};150

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

lines 151–154why, one step back
151test('stdin can be ["inherit", "pipe"], buffer: false', testInheritNoBuffer, ['inherit', 'pipe'], false);
152test('stdin can be [0, "pipe"], buffer: false', testInheritNoBuffer, [0, 'pipe'], false);153test.serial('stdin can be ["inherit", "pipe"], buffer: false, sync', testInheritNoBuffer, ['inherit', 'pipe'], true);154test.serial('stdin can be [0, "pipe"], buffer: false, sync', testInheritNoBuffer, [0, 'pipe'], true);

Split files (#837)ehmickybefore adoption672c2d7

line 155why, one step back
155

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

lines 156–160why, one step back
156if (isLinux) {
157 const testOverflowStream = async (t, fdNumber, stdioOption, isSync) => {158 const {stdout} = await execa(getFixtureName(isSync), [JSON.stringify(getStdio(fdNumber, stdioOption)), 'empty.js'], fullStdio);159 t.is(stdout, '');160 };

Split files (#837)ehmickybefore adoption672c2d7

line 161why, one step back
161

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

lines 162–177why, one step back
162 test('stdin can use 4+', testOverflowStream, 0, 4, false);
163 test('stdin can use [4+]', testOverflowStream, 0, [4], false);164 test('stdout can use 4+', testOverflowStream, 1, 4, false);165 test('stdout can use [4+]', testOverflowStream, 1, [4], false);166 test('stderr can use 4+', testOverflowStream, 2, 4, false);167 test('stderr can use [4+]', testOverflowStream, 2, [4], false);168 test('stdio[*] can use 4+', testOverflowStream, 3, 4, false);169 test('stdio[*] can use [4+]', testOverflowStream, 3, [4], false);170 test('stdin can use 4+, sync', testOverflowStream, 0, 4, true);171 test('stdin can use [4+], sync', testOverflowStream, 0, [4], true);172 test('stdout can use 4+, sync', testOverflowStream, 1, 4, true);173 test('stdout can use [4+], sync', testOverflowStream, 1, [4], true);174 test('stderr can use 4+, sync', testOverflowStream, 2, 4, true);175 test('stderr can use [4+], sync', testOverflowStream, 2, [4], true);176 test('stdio[*] can use 4+, sync', testOverflowStream, 3, 4, true);177 test('stdio[*] can use [4+], sync', testOverflowStream, 3, [4], true);

Split files (#837)ehmickybefore adoption672c2d7

lines 178–179why, one step back
178}
179

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

line 180why, one step back
180const testOverflowStreamArray = (t, fdNumber, stdioOption) => {

Split files (#837)ehmickybefore adoption672c2d7

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

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

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

Split files (#837)ehmickybefore adoption672c2d7

lines 183–190why, one step back
183 }, {message: /no such standard stream/});
184};185186test('stdin cannot use 4+ and another value', testOverflowStreamArray, 0, [4, 'pipe']);187test('stdout cannot use 4+ and another value', testOverflowStreamArray, 1, [4, 'pipe']);188test('stderr cannot use 4+ and another value', testOverflowStreamArray, 2, [4, 'pipe']);189test('stdio[*] cannot use 4+ and another value', testOverflowStreamArray, 3, [4, 'pipe']);190test('stdio[*] cannot use "inherit" and another value', testOverflowStreamArray, 3, ['inherit', 'pipe']);

Add support for `std*: ['pipe', 'inherit']` with `execaSync()` (#954)ehmickybefore adoptionc15edbc

lines 191–235why, one step back
191
192const getInvalidFdCode = () => {193 if (isLinux) {194 return 'EINVAL';195 }196197 return isWindows ? 'EBADF' : 'ENXIO';198};199200const testOverflowStreamArraySync = (t, fdNumber) => {201 t.throws(() => {202 execaSync('noop-fd.js', [fdNumber, foobarString], getStdio(fdNumber, [4, 'pipe']));203 }, {code: getInvalidFdCode()});204};205206test('stdout cannot use 4+ and another value, sync', testOverflowStreamArraySync, 1);207test('stderr cannot use 4+ and another value, sync', testOverflowStreamArraySync, 2);208test('stdio[*] cannot use 4+ and another value, sync', testOverflowStreamArraySync, 3);209210test('stdin can use ["inherit", "pipe"] in a TTY', async t => {211 const stdioOption = [['inherit', 'pipe'], 'inherit', 'pipe'];212 const {stdout} = await execa('nested-sync-tty.js', [JSON.stringify({stdio: stdioOption}), 'false', 'stdin-fd.js', '0'], {input: foobarString});213 t.is(stdout, foobarString);214});215216const testNoTtyInput = async (t, fdNumber, optionName) => {217 const stdioOption = ['pipe', 'inherit', 'pipe'];218 stdioOption[fdNumber] = [[''], 'inherit', 'pipe'];219 const {message} = await t.throwsAsync(execa('nested-sync-tty.js', [JSON.stringify({stdio: stdioOption}), 'true', 'stdin-fd.js', `${fdNumber}`], fullStdio));220 t.true(message.includes(`The \`${optionName}: 'inherit'\` option is invalid: it cannot be a TTY`));221};222223test('stdin cannot use ["inherit", "pipe"] in a TTY, sync', testNoTtyInput, 0, 'stdin');224test('stdio[*] input cannot use ["inherit", "pipe"] in a TTY, sync', testNoTtyInput, 3, 'stdio[3]');225226const testTtyOutput = async (t, fdNumber, isSync) => {227 const {stdio} = await execa('nested-sync-tty.js', [JSON.stringify(getStdio(fdNumber, ['inherit', 'pipe'])), `${isSync}`, 'noop-fd.js', `${fdNumber}`, foobarString], fullStdio);228 t.is(stdio[fdNumber], foobarString);229};230231test('stdout can use ["inherit", "pipe"] in a TTY', testTtyOutput, 1, false);232test('stderr can use ["inherit", "pipe"] in a TTY', testTtyOutput, 2, false);233test('stdout can use ["inherit", "pipe"] in a TTY, sync', testTtyOutput, 1, true);234test('stderr can use ["inherit", "pipe"] in a TTY, sync', testTtyOutput, 2, true);235test('stdio[*] output can use ["inherit", "pipe"] in a TTY, sync', testTtyOutput, 3, true);