Skip to content
Myr

subprocess.js

This file

Path

test/stream/subprocess.js

117 lines · 4.5 KiB · JavaScript

Last changed by

Refactor tests (#934)ehmickybefore adoption52c4bde

History of this file

Take it

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 1–4why, one step back
1import {setTimeout} from 'node:timers/promises';
2import test from 'ava';3import {execa} from '../../index.js';4import {setFixtureDir} from '../helpers/fixtures-dir.js';

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

line 5why, one step back
5import {fullStdio, getStdio, prematureClose, assertEpipe} from '../helpers/stdio.js';

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 6–10why, one step back
6import {infiniteGenerator} from '../helpers/generator.js';
78setFixtureDir();910const getStreamInputSubprocess = fdNumber => execa('stdin-fd.js', [`${fdNumber}`], fdNumber === 3

Refactor tests (#934)ehmickybefore adoption52c4bde

line 11why, one step back
11 ? getStdio(3, [new Uint8Array(), infiniteGenerator()])

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 12–30why, one step back
12 : {});
13const getStreamOutputSubprocess = fdNumber => execa('noop-repeat.js', [`${fdNumber}`], fdNumber === 3 ? fullStdio : {});1415const assertStreamInputError = (t, {exitCode, signal, isTerminated, failed}) => {16 t.is(exitCode, 0);17 t.is(signal, undefined);18 t.false(isTerminated);19 t.true(failed);20};2122const assertStreamOutputError = (t, fdNumber, {exitCode, signal, isTerminated, failed, stderr}) => {23 if (fdNumber !== 3) {24 t.is(exitCode, 1);25 }2627 t.is(signal, undefined);28 t.false(isTerminated);29 t.true(failed);30

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

line 31why, one step back
31 assertEpipe(t, stderr, fdNumber);

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 32–56why, one step back
32};
3334const testStreamInputAbort = async (t, fdNumber) => {35 const subprocess = getStreamInputSubprocess(fdNumber);36 subprocess.stdio[fdNumber].destroy();37 const error = await t.throwsAsync(subprocess, prematureClose);38 assertStreamInputError(t, error);39};4041test('Aborting stdin should not make the subprocess exit', testStreamInputAbort, 0);42test('Aborting input stdio[*] should not make the subprocess exit', testStreamInputAbort, 3);4344const testStreamOutputAbort = async (t, fdNumber) => {45 const subprocess = getStreamOutputSubprocess(fdNumber);46 subprocess.stdio[fdNumber].destroy();47 const error = await t.throwsAsync(subprocess);48 assertStreamOutputError(t, fdNumber, error);49};5051test('Aborting stdout should not make the subprocess exit', testStreamOutputAbort, 1);52test('Aborting stderr should not make the subprocess exit', testStreamOutputAbort, 2);53test('Aborting output stdio[*] should not make the subprocess exit', testStreamOutputAbort, 3);5455const testStreamInputDestroy = async (t, fdNumber) => {56 const subprocess = getStreamInputSubprocess(fdNumber);

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 57–60why, one step back
57 const cause = new Error('test');
58 subprocess.stdio[fdNumber].destroy(cause);59 const error = await t.throwsAsync(subprocess);60 t.is(error.cause, cause);

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 61–68why, one step back
61 assertStreamInputError(t, error);
62};6364test('Destroying stdin should not make the subprocess exit', testStreamInputDestroy, 0);65test('Destroying input stdio[*] should not make the subprocess exit', testStreamInputDestroy, 3);6667const testStreamOutputDestroy = async (t, fdNumber) => {68 const subprocess = getStreamOutputSubprocess(fdNumber);

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 69–72why, one step back
69 const cause = new Error('test');
70 subprocess.stdio[fdNumber].destroy(cause);71 const error = await t.throwsAsync(subprocess);72 t.is(error.cause, cause);

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 73–81why, one step back
73 assertStreamOutputError(t, fdNumber, error);
74};7576test('Destroying stdout should not make the subprocess exit', testStreamOutputDestroy, 1);77test('Destroying stderr should not make the subprocess exit', testStreamOutputDestroy, 2);78test('Destroying output stdio[*] should not make the subprocess exit', testStreamOutputDestroy, 3);7980const testStreamInputError = async (t, fdNumber) => {81 const subprocess = getStreamInputSubprocess(fdNumber);

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

line 82why, one step back
82 const cause = new Error('test');

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 83why, one step back
83 const stream = subprocess.stdio[fdNumber];

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

line 84why, one step back
84 stream.emit('error', cause);

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 85why, one step back
85 stream.end();

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 86–87why, one step back
86 const error = await t.throwsAsync(subprocess);
87 t.is(error.cause, cause);

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 88–95why, one step back
88 assertStreamInputError(t, error);
89};9091test('Errors on stdin should not make the subprocess exit', testStreamInputError, 0);92test('Errors on input stdio[*] should not make the subprocess exit', testStreamInputError, 3);9394const testStreamOutputError = async (t, fdNumber) => {95 const subprocess = getStreamOutputSubprocess(fdNumber);

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

line 96why, one step back
96 const cause = new Error('test');

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

line 97why, one step back
97 const stream = subprocess.stdio[fdNumber];

Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815

lines 98–100why, one step back
98 stream.emit('error', cause);
99 const error = await t.throwsAsync(subprocess);100 t.is(error.cause, cause);

Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343

lines 101–117why, one step back
101 assertStreamOutputError(t, fdNumber, error);
102};103104test('Errors on stdout should not make the subprocess exit', testStreamOutputError, 1);105test('Errors on stderr should not make the subprocess exit', testStreamOutputError, 2);106test('Errors on output stdio[*] should not make the subprocess exit', testStreamOutputError, 3);107108const testWaitOnStreamEnd = async (t, fdNumber) => {109 const subprocess = execa('stdin-fd.js', [`${fdNumber}`], fullStdio);110 await setTimeout(100);111 subprocess.stdio[fdNumber].end('foobar');112 const {stdout} = await subprocess;113 t.is(stdout, 'foobar');114};115116test('Subprocess waits on stdin before exiting', testWaitOnStreamEnd, 0);117test('Subprocess waits on stdio[*] before exiting', testWaitOnStreamEnd, 3);