Skip to content
Myr

stdio.js

This file

Path

test/resolve/stdio.js

134 lines · 5.5 KiB · JavaScript

Last changed by

TweaksSindre Sorhusbefore adoption698a1b8

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

TweaksSindre Sorhusbefore adoption698a1b8

line 1why, one step back
1import assert from 'node:assert/strict';

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 2why, one step back
2import {setTimeout} from 'node:timers/promises';

TweaksSindre Sorhusbefore adoption698a1b8

lines 3–5why, one step back
3import test from 'node:test';
4import {assertRejects} from '../helpers/assert.js';5import {execa, execaSync} from '../../index.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 6–21why, one step back
6import {setFixtureDirectory} from '../helpers/fixtures-directory.js';
7import {8 fullStdio,9 getStdio,10 prematureClose,11 assertEpipe,12} from '../helpers/stdio.js';13import {infiniteGenerator} from '../helpers/generator.js';1415setFixtureDirectory();1617const getStreamInputSubprocess = fdNumber => execa('stdin-fd.js', [`${fdNumber}`], fdNumber === 318 ? getStdio(3, [new Uint8Array(), infiniteGenerator()])19 : {});20const getStreamOutputSubprocess = fdNumber => execa('noop-repeat.js', [`${fdNumber}`], fdNumber === 3 ? fullStdio : {});21

TweaksSindre Sorhusbefore adoption698a1b8

lines 22–26why, one step back
22const assertStreamInputError = ({exitCode, signal, isTerminated, failed}) => {
23 assert.equal(exitCode, 0);24 assert.equal(signal, undefined);25 assert.equal(isTerminated, false);26 assert.equal(failed, true);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 27–28why, one step back
27};
28

TweaksSindre Sorhusbefore adoption698a1b8

line 29why, one step back
29const assertStreamOutputError = (fdNumber, {exitCode, signal, isTerminated, failed, stderr}) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 30why, one step back
30 if (fdNumber !== 3) {

TweaksSindre Sorhusbefore adoption698a1b8

line 31why, one step back
31 assert.equal(exitCode, 1);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 32–33why, one step back
32 }
33

TweaksSindre Sorhusbefore adoption698a1b8

lines 34–36why, one step back
34 assert.equal(signal, undefined);
35 assert.equal(isTerminated, false);36 assert.equal(failed, true);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 37why, one step back
37

TweaksSindre Sorhusbefore adoption698a1b8

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

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 39–40why, one step back
39};
40

TweaksSindre Sorhusbefore adoption698a1b8

line 41why, one step back
41const testStreamInputAbort = async fdNumber => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 42–43why, one step back
42 const subprocess = getStreamInputSubprocess(fdNumber);
43 subprocess.stdio[fdNumber].destroy();

TweaksSindre Sorhusbefore adoption698a1b8

lines 44–45why, one step back
44 const error = await assertRejects(subprocess, prematureClose);
45 assertStreamInputError(error);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 46–47why, one step back
46};
47

TweaksSindre Sorhusbefore adoption698a1b8

lines 48–49why, one step back
48test('Aborting stdin should not make the subprocess exit', () => testStreamInputAbort(0));
49test('Aborting input stdio[*] should not make the subprocess exit', () => testStreamInputAbort(3));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 50why, one step back
50

TweaksSindre Sorhusbefore adoption698a1b8

line 51why, one step back
51const testStreamOutputAbort = async fdNumber => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 52–53why, one step back
52 const subprocess = getStreamOutputSubprocess(fdNumber);
53 subprocess.stdio[fdNumber].destroy();

TweaksSindre Sorhusbefore adoption698a1b8

lines 54–55why, one step back
54 const error = await assertRejects(subprocess);
55 assertStreamOutputError(fdNumber, error);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 56–57why, one step back
56};
57

TweaksSindre Sorhusbefore adoption698a1b8

lines 58–60why, one step back
58test('Aborting stdout should not make the subprocess exit', () => testStreamOutputAbort(1));
59test('Aborting stderr should not make the subprocess exit', () => testStreamOutputAbort(2));60test('Aborting output stdio[*] should not make the subprocess exit', () => testStreamOutputAbort(3));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 61why, one step back
61

TweaksSindre Sorhusbefore adoption698a1b8

line 62why, one step back
62const testStreamInputDestroy = async fdNumber => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 63–65why, one step back
63 const subprocess = getStreamInputSubprocess(fdNumber);
64 const cause = new Error('test');65 subprocess.stdio[fdNumber].destroy(cause);

TweaksSindre Sorhusbefore adoption698a1b8

lines 66–68why, one step back
66 const error = await assertRejects(subprocess);
67 assert.equal(error.cause, cause);68 assertStreamInputError(error);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 69–70why, one step back
69};
70

TweaksSindre Sorhusbefore adoption698a1b8

lines 71–72why, one step back
71test('Destroying stdin should not make the subprocess exit', () => testStreamInputDestroy(0));
72test('Destroying input stdio[*] should not make the subprocess exit', () => testStreamInputDestroy(3));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 73why, one step back
73

TweaksSindre Sorhusbefore adoption698a1b8

line 74why, one step back
74const testStreamOutputDestroy = async fdNumber => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 75–77why, one step back
75 const subprocess = getStreamOutputSubprocess(fdNumber);
76 const cause = new Error('test');77 subprocess.stdio[fdNumber].destroy(cause);

TweaksSindre Sorhusbefore adoption698a1b8

lines 78–80why, one step back
78 const error = await assertRejects(subprocess);
79 assert.equal(error.cause, cause);80 assertStreamOutputError(fdNumber, error);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 81–82why, one step back
81};
82

TweaksSindre Sorhusbefore adoption698a1b8

lines 83–85why, one step back
83test('Destroying stdout should not make the subprocess exit', () => testStreamOutputDestroy(1));
84test('Destroying stderr should not make the subprocess exit', () => testStreamOutputDestroy(2));85test('Destroying output stdio[*] should not make the subprocess exit', () => testStreamOutputDestroy(3));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 86why, one step back
86

TweaksSindre Sorhusbefore adoption698a1b8

line 87why, one step back
87const testStreamInputError = async fdNumber => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 88–92why, one step back
88 const subprocess = getStreamInputSubprocess(fdNumber);
89 const cause = new Error('test');90 const stream = subprocess.stdio[fdNumber];91 stream.emit('error', cause);92 stream.end();

TweaksSindre Sorhusbefore adoption698a1b8

lines 93–95why, one step back
93 const error = await assertRejects(subprocess);
94 assert.equal(error.cause, cause);95 assertStreamInputError(error);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 96–97why, one step back
96};
97

TweaksSindre Sorhusbefore adoption698a1b8

lines 98–99why, one step back
98test('Errors on stdin should not make the subprocess exit', () => testStreamInputError(0));
99test('Errors on input stdio[*] should not make the subprocess exit', () => testStreamInputError(3));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 100why, one step back
100

TweaksSindre Sorhusbefore adoption698a1b8

line 101why, one step back
101const testStreamOutputError = async fdNumber => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 102–105why, one step back
102 const subprocess = getStreamOutputSubprocess(fdNumber);
103 const cause = new Error('test');104 const stream = subprocess.stdio[fdNumber];105 stream.emit('error', cause);

TweaksSindre Sorhusbefore adoption698a1b8

lines 106–108why, one step back
106 const error = await assertRejects(subprocess);
107 assert.equal(error.cause, cause);108 assertStreamOutputError(fdNumber, error);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 109–110why, one step back
109};
110

TweaksSindre Sorhusbefore adoption698a1b8

lines 111–113why, one step back
111test('Errors on stdout should make the subprocess exit', () => testStreamOutputError(1));
112test('Errors on stderr should make the subprocess exit', () => testStreamOutputError(2));113test('Errors on output stdio[*] should make the subprocess exit', () => testStreamOutputError(3));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 114why, one step back
114

TweaksSindre Sorhusbefore adoption698a1b8

line 115why, one step back
115const testWaitOnStreamEnd = async fdNumber => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 116–119why, one step back
116 const subprocess = execa('stdin-fd.js', [`${fdNumber}`], fullStdio);
117 await setTimeout(100);118 subprocess.stdio[fdNumber].end('foobar');119 const {stdout} = await subprocess;

TweaksSindre Sorhusbefore adoption698a1b8

line 120why, one step back
120 assert.equal(stdout, 'foobar');

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 121–122why, one step back
121};
122

TweaksSindre Sorhusbefore adoption698a1b8

lines 123–134why, one step back
123test('Subprocess waits on stdin before exiting', () => testWaitOnStreamEnd(0));
124test('Subprocess waits on stdio[*] before exiting', () => testWaitOnStreamEnd(3));125126// A failed spawn has no output at all, so `result.stdio` must still have one entry per127// file descriptor, the same as a successful spawn and the same as asynchronous methods.128const testFailedSpawnStdioLength = async (stdio, expectedLength, execaMethod) => {129 const {stdio: stdioResult} = await execaMethod('unknown-file.js', {stdio, reject: false});130 assert.equal(stdioResult.length, expectedLength);131};132133test('result.stdio has one entry per file descriptor when the spawn fails', () => testFailedSpawnStdioLength(fullStdio.stdio, 4, execa));134test('result.stdio has one entry per file descriptor when the spawn fails, sync', () => testFailedSpawnStdioLength(fullStdio.stdio, 4, execaSync));