stdio.js
This file
Path
test/resolve/stdio.js
122 lines · 4.5 KiB · JavaScript
Last changed by
Upgrade xo (#980)ehmickybefore adoptionccfa412
History of this fileTake it
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–3why, one step back1import {setTimeout} from 'node:timers/promises';2import test from 'ava';3import {execa} from '../../index.js';Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 4–10why, one step back4import {setFixtureDirectory} from '../helpers/fixtures-directory.js';5import {6 fullStdio,7 getStdio,8 prematureClose,9 assertEpipe,10} from '../helpers/stdio.js';Rename files (#977)ehmickybefore adoption4b2e316
lines 11–12why, one step back11import {infiniteGenerator} from '../helpers/generator.js';12Upgrade xo (#980)ehmickybefore adoptionccfa412
line 13why, one step back13setFixtureDirectory();Rename files (#977)ehmickybefore adoption4b2e316
lines 14–122why, one step back1415const getStreamInputSubprocess = fdNumber => execa('stdin-fd.js', [`${fdNumber}`], fdNumber === 316 ? getStdio(3, [new Uint8Array(), infiniteGenerator()])17 : {});18const getStreamOutputSubprocess = fdNumber => execa('noop-repeat.js', [`${fdNumber}`], fdNumber === 3 ? fullStdio : {});1920const assertStreamInputError = (t, {exitCode, signal, isTerminated, failed}) => {21 t.is(exitCode, 0);22 t.is(signal, undefined);23 t.false(isTerminated);24 t.true(failed);25};2627const assertStreamOutputError = (t, fdNumber, {exitCode, signal, isTerminated, failed, stderr}) => {28 if (fdNumber !== 3) {29 t.is(exitCode, 1);30 }3132 t.is(signal, undefined);33 t.false(isTerminated);34 t.true(failed);3536 assertEpipe(t, stderr, fdNumber);37};3839const testStreamInputAbort = async (t, fdNumber) => {40 const subprocess = getStreamInputSubprocess(fdNumber);41 subprocess.stdio[fdNumber].destroy();42 const error = await t.throwsAsync(subprocess, prematureClose);43 assertStreamInputError(t, error);44};4546test('Aborting stdin should not make the subprocess exit', testStreamInputAbort, 0);47test('Aborting input stdio[*] should not make the subprocess exit', testStreamInputAbort, 3);4849const testStreamOutputAbort = async (t, fdNumber) => {50 const subprocess = getStreamOutputSubprocess(fdNumber);51 subprocess.stdio[fdNumber].destroy();52 const error = await t.throwsAsync(subprocess);53 assertStreamOutputError(t, fdNumber, error);54};5556test('Aborting stdout should not make the subprocess exit', testStreamOutputAbort, 1);57test('Aborting stderr should not make the subprocess exit', testStreamOutputAbort, 2);58test('Aborting output stdio[*] should not make the subprocess exit', testStreamOutputAbort, 3);5960const testStreamInputDestroy = async (t, fdNumber) => {61 const subprocess = getStreamInputSubprocess(fdNumber);62 const cause = new Error('test');63 subprocess.stdio[fdNumber].destroy(cause);64 const error = await t.throwsAsync(subprocess);65 t.is(error.cause, cause);66 assertStreamInputError(t, error);67};6869test('Destroying stdin should not make the subprocess exit', testStreamInputDestroy, 0);70test('Destroying input stdio[*] should not make the subprocess exit', testStreamInputDestroy, 3);7172const testStreamOutputDestroy = async (t, fdNumber) => {73 const subprocess = getStreamOutputSubprocess(fdNumber);74 const cause = new Error('test');75 subprocess.stdio[fdNumber].destroy(cause);76 const error = await t.throwsAsync(subprocess);77 t.is(error.cause, cause);78 assertStreamOutputError(t, fdNumber, error);79};8081test('Destroying stdout should not make the subprocess exit', testStreamOutputDestroy, 1);82test('Destroying stderr should not make the subprocess exit', testStreamOutputDestroy, 2);83test('Destroying output stdio[*] should not make the subprocess exit', testStreamOutputDestroy, 3);8485const testStreamInputError = async (t, fdNumber) => {86 const subprocess = getStreamInputSubprocess(fdNumber);87 const cause = new Error('test');88 const stream = subprocess.stdio[fdNumber];89 stream.emit('error', cause);90 stream.end();91 const error = await t.throwsAsync(subprocess);92 t.is(error.cause, cause);93 assertStreamInputError(t, error);94};9596test('Errors on stdin should not make the subprocess exit', testStreamInputError, 0);97test('Errors on input stdio[*] should not make the subprocess exit', testStreamInputError, 3);9899const testStreamOutputError = async (t, fdNumber) => {100 const subprocess = getStreamOutputSubprocess(fdNumber);101 const cause = new Error('test');102 const stream = subprocess.stdio[fdNumber];103 stream.emit('error', cause);104 const error = await t.throwsAsync(subprocess);105 t.is(error.cause, cause);106 assertStreamOutputError(t, fdNumber, error);107};108109test('Errors on stdout should make the subprocess exit', testStreamOutputError, 1);110test('Errors on stderr should make the subprocess exit', testStreamOutputError, 2);111test('Errors on output stdio[*] should make the subprocess exit', testStreamOutputError, 3);112113const testWaitOnStreamEnd = async (t, fdNumber) => {114 const subprocess = execa('stdin-fd.js', [`${fdNumber}`], fullStdio);115 await setTimeout(100);116 subprocess.stdio[fdNumber].end('foobar');117 const {stdout} = await subprocess;118 t.is(stdout, 'foobar');119};120121test('Subprocess waits on stdin before exiting', testWaitOnStreamEnd, 0);122test('Subprocess waits on stdio[*] before exiting', testWaitOnStreamEnd, 3);