stdio.js
This file
Path
test/resolve/stdio.js
134 lines · 5.5 KiB · JavaScript
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
TweaksSindre Sorhusbefore adoption698a1b8
line 1why, one step back1import assert from 'node:assert/strict';100% test coverage (#1018)ehmickybefore adoption9f65b86
line 2why, one step back2import {setTimeout} from 'node:timers/promises';TweaksSindre Sorhusbefore adoption698a1b8
lines 3–5why, one step back3import 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 back6import {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 : {});21TweaksSindre Sorhusbefore adoption698a1b8
lines 22–26why, one step back22const 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 back27};28TweaksSindre Sorhusbefore adoption698a1b8
line 29why, one step back29const assertStreamOutputError = (fdNumber, {exitCode, signal, isTerminated, failed, stderr}) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
line 30why, one step back30 if (fdNumber !== 3) {TweaksSindre Sorhusbefore adoption698a1b8
line 31why, one step back31 assert.equal(exitCode, 1);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 32–33why, one step back32 }33TweaksSindre Sorhusbefore adoption698a1b8
lines 34–36why, one step back34 assert.equal(signal, undefined);35 assert.equal(isTerminated, false);36 assert.equal(failed, true);100% test coverage (#1018)ehmickybefore adoption9f65b86
line 37why, one step back37TweaksSindre Sorhusbefore adoption698a1b8
line 38why, one step back38 assertEpipe(stderr, fdNumber);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 39–40why, one step back39};40TweaksSindre Sorhusbefore adoption698a1b8
line 41why, one step back41const testStreamInputAbort = async fdNumber => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 42–43why, one step back42 const subprocess = getStreamInputSubprocess(fdNumber);43 subprocess.stdio[fdNumber].destroy();TweaksSindre Sorhusbefore adoption698a1b8
lines 44–45why, one step back44 const error = await assertRejects(subprocess, prematureClose);45 assertStreamInputError(error);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 46–47why, one step back46};47TweaksSindre Sorhusbefore adoption698a1b8
lines 48–49why, one step back48test('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 back50TweaksSindre Sorhusbefore adoption698a1b8
line 51why, one step back51const testStreamOutputAbort = async fdNumber => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 52–53why, one step back52 const subprocess = getStreamOutputSubprocess(fdNumber);53 subprocess.stdio[fdNumber].destroy();TweaksSindre Sorhusbefore adoption698a1b8
lines 54–55why, one step back54 const error = await assertRejects(subprocess);55 assertStreamOutputError(fdNumber, error);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 56–57why, one step back56};57TweaksSindre Sorhusbefore adoption698a1b8
lines 58–60why, one step back58test('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 back61TweaksSindre Sorhusbefore adoption698a1b8
line 62why, one step back62const testStreamInputDestroy = async fdNumber => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 63–65why, one step back63 const subprocess = getStreamInputSubprocess(fdNumber);64 const cause = new Error('test');65 subprocess.stdio[fdNumber].destroy(cause);TweaksSindre Sorhusbefore adoption698a1b8
lines 66–68why, one step back66 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 back69};70TweaksSindre Sorhusbefore adoption698a1b8
lines 71–72why, one step back71test('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 back73TweaksSindre Sorhusbefore adoption698a1b8
line 74why, one step back74const testStreamOutputDestroy = async fdNumber => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 75–77why, one step back75 const subprocess = getStreamOutputSubprocess(fdNumber);76 const cause = new Error('test');77 subprocess.stdio[fdNumber].destroy(cause);TweaksSindre Sorhusbefore adoption698a1b8
lines 78–80why, one step back78 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 back81};82TweaksSindre Sorhusbefore adoption698a1b8
lines 83–85why, one step back83test('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 back86TweaksSindre Sorhusbefore adoption698a1b8
line 87why, one step back87const testStreamInputError = async fdNumber => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 88–92why, one step back88 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 back93 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 back96};97TweaksSindre Sorhusbefore adoption698a1b8
lines 98–99why, one step back98test('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 back100TweaksSindre Sorhusbefore adoption698a1b8
line 101why, one step back101const testStreamOutputError = async fdNumber => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 102–105why, one step back102 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 back106 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 back109};110TweaksSindre Sorhusbefore adoption698a1b8
lines 111–113why, one step back111test('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 back114TweaksSindre Sorhusbefore adoption698a1b8
line 115why, one step back115const testWaitOnStreamEnd = async fdNumber => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 116–119why, one step back116 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 back120 assert.equal(stdout, 'foobar');100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 121–122why, one step back121};122TweaksSindre Sorhusbefore adoption698a1b8
lines 123–134why, one step back123test('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));