Skip to content
Myr

Fix stream error handling (#826)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent 4fb8e9a · Browse files · 11 files+459 added−138 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

11 files+459 added−138 removed

  • tests:+358 added−101 removed
  • other:+101 added−37 removed

11 files+459 added−138 removed

modified:lib/stdio/pipeline.js+6 added-1 removed
modified:lib/stream.js+34 added-36 removed
1import {once} from 'node:events';2import {finished} from 'node:stream/promises';2import {setImmediate} from 'node:timers/promises';3import getStream, {getStreamAsArrayBuffer, getStreamAsArray, MaxBufferError} from 'get-stream';4import {isStream} from 'is-stream';5import mergeStreams from '@sindresorhus/merge-streams';6import {waitForSuccessfulExit, throwOnTimeout, errorSignal} from './kill.js';7import {isStandardStream} from './stdio/utils.js';8import {generatorToDuplexStream} from './stdio/generator.js';9import {waitForStream, handleStreamError, isInputFileDescriptor} from './wait.js';1011// `all` interleaves `stdout` and `stderr`12export const makeAllStream = ({stdout, stderr}, {all}) => all && (stdout || stderr)13 ? mergeStreams([stdout, stderr].filter(Boolean))
5 unmodified lines
19const getBufferedData = async (streamPromise, encoding) => {20 try {21 return await streamPromise;22 } catch (error) {22 return error.bufferedData === undefined || Array.isArray(error.bufferedData)23 ? error.bufferedData24 : applyEncoding(error.bufferedData, encoding);23 return handleBufferedData(error, encoding);24 }25};2627const handleBufferedData = (error, encoding) => error.bufferedData === undefined || Array.isArray(error.bufferedData)28 ? error.bufferedData29 : applyEncoding(error.bufferedData, encoding);3031// Read the contents of `childProcess.std*` and|or wait for its completion29const waitForChildStreams = ({spawned, stdioStreamsGroups, encoding, buffer, maxBuffer, waitForStream}) => spawned.stdio.map((stream, index) => waitForChildStream({32const waitForChildStreams = ({spawned, encoding, buffer, maxBuffer, streamInfo}) => spawned.stdio.map((stream, index) => waitForChildStream({33 stream,34 spawned,32 direction: stdioStreamsGroups[index][0].direction,35 index,36 encoding,37 buffer,38 maxBuffer,36 waitForStream,39 streamInfo,40}));4142// Read the contents of `childProcess.all` and|or wait for its completion40const waitForAllStream = ({spawned, encoding, buffer, maxBuffer, waitForStream}) => waitForChildStream({43const waitForAllStream = ({spawned, encoding, buffer, maxBuffer, streamInfo}) => waitForChildStream({44 stream: getAllStream(spawned, encoding),45 spawned,43 direction: 'output',46 index: 1,47 encoding,48 buffer,49 maxBuffer: maxBuffer * 2,47 waitForStream,50 streamInfo,51});5253// When `childProcess.stdout` is in objectMode but not `childProcess.stderr` (or the opposite), we need to use both:54// - `getStreamAsArray()` for the chunks in objectMode, to return as an array without changing each chunk
11 unmodified lines
66 writableObjectMode: true,67 readableObjectMode: true,68};6967const waitForChildStream = async ({stream, spawned, direction, encoding, buffer, maxBuffer, waitForStream}) => {70const waitForChildStream = async ({stream, spawned, index, encoding, buffer, maxBuffer, streamInfo}) => {71 if (!stream) {72 return;73 }7472 if (direction === 'input') {73 await waitForStream(stream);75 if (isInputFileDescriptor(index, streamInfo.stdioStreamsGroups)) {76 await waitForStream(stream, index, streamInfo);77 return;78 }7980 if (!buffer) {81 await Promise.all([79 waitForStream(stream),82 waitForStream(stream, index, streamInfo),83 resumeStream(stream),84 ]);85 return;86 }
4 unmodified lines
91 if (error instanceof MaxBufferError) {92 spawned.kill();93 }9492 throw error;95 handleStreamError(error, index, streamInfo);96 return handleBufferedData(error, encoding);97 }98};99100// When using `buffer: false`, users need to read `childProcess.stdout|stderr|all` right away
19 unmodified lines
120const applyEncoding = (contents, encoding) => encoding === 'buffer' ? new Uint8Array(contents) : contents;121122// Transforms replace `childProcess.std*`, which means they are not exposed to users.123// However, we still want to wait for their completion.120const waitForOriginalStreams = (originalStreams, spawned, waitForStream) => originalStreams121 .filter((stream, index) => stream !== spawned.stdio[index])122 .map(stream => waitForStream(stream));124const waitForOriginalStreams = (originalStreams, spawned, streamInfo) =>125 originalStreams.map((stream, index) => stream === spawned.stdio[index]126 ? undefined127 : waitForStream(stream, index, streamInfo));128129// Some `stdin`/`stdout`/`stderr` options create a stream, e.g. when passing a file path.130// The `.pipe()` method automatically ends that stream when `childProcess` ends.131// This makes sure we wait for the completion of those streams, in order to catch any error.127const waitForCustomStreamsEnd = (stdioStreamsGroups, waitForStream) => stdioStreamsGroups.flat()128 .filter(({type, value}) => type !== 'native' && !isStandardStream(value))129 .map(({value}) => waitForStream(value));132const waitForCustomStreamsEnd = (stdioStreamsGroups, streamInfo) => stdioStreamsGroups.flatMap((stdioStreams, index) => stdioStreams133 .filter(({value}) => isStream(value) && !isStandardStream(value))134 .map(({type, value}) => waitForStream(value, index, streamInfo, {135 isSameDirection: type === 'generator',136 stopOnExit: type === 'native',137 })));138131// Wraps `finished(stream)` to handle the following case:132// - When the child process exits, Node.js automatically calls `childProcess.stdin.destroy()`, which we need to ignore.133// - However, we still need to throw if `childProcess.stdin.destroy()` is called before child process exit.134const onFinishedStream = async ([originalStdin], exitPromise, stream) => {135 await Promise.race([136 ...(stream === originalStdin ? [exitPromise] : []),137 finished(stream, {cleanup: true}),138 ]);139};140139const throwOnProcessError = async (spawned, {signal}) => {140 const [error] = await once(spawned, 'error', {signal});141 throw error;142};
42 unmodified lines
185 originalStreams,186 controller,187}) => {188 const exitPromise = waitForExit(spawned);191 const waitForStream = onFinishedStream.bind(undefined, originalStreams, exitPromise);189 const streamInfo = {originalStreams, stdioStreamsGroups, exitPromise, propagating: new Set([])};190193 const stdioPromises = waitForChildStreams({spawned, stdioStreamsGroups, encoding, buffer, maxBuffer, waitForStream});194 const allPromise = waitForAllStream({spawned, encoding, buffer, maxBuffer, waitForStream});195 const originalPromises = waitForOriginalStreams(originalStreams, spawned, waitForStream);196 const customStreamsEndPromises = waitForCustomStreamsEnd(stdioStreamsGroups, waitForStream);191 const stdioPromises = waitForChildStreams({spawned, encoding, buffer, maxBuffer, streamInfo});192 const allPromise = waitForAllStream({spawned, encoding, buffer, maxBuffer, streamInfo});193 const originalPromises = waitForOriginalStreams(originalStreams, spawned, streamInfo);194 const customStreamsEndPromises = waitForCustomStreamsEnd(stdioStreamsGroups, streamInfo);195196 try {197 return await Promise.race([198 Promise.all([
22 unmodified lines
added:lib/wait.js+61 added
added:test/fixtures/noop-repeat.js+7 added
added:test/fixtures/noop-stdin-fd.js+10 added
modified:test/helpers/stdio.js+2 added
added:test/helpers/stream.js+7 added
modified:test/stdio/generator.js+1 added-1 removed
707 unmodified lines
708const testGeneratorCancel = async (t, error) => {709 const childProcess = execa('noop.js', {stdout: infiniteGenerator});710 await once(childProcess.stdout, 'data');711 childProcess.stdout.destroy(error);712 await t.throwsAsync(childProcess);712 await (error === undefined ? t.notThrowsAsync(childProcess) : t.throwsAsync(childProcess));713};714715test('Running generators are canceled on process abort', testGeneratorCancel, undefined);716test('Running generators are canceled on process error', testGeneratorCancel, new Error('test'));
15 unmodified lines
modified:test/stdio/node-stream.js+97 added-96 removed
1import {once} from 'node:events';2import {createReadStream, createWriteStream} from 'node:fs';3import {readFile, writeFile, rm} from 'node:fs/promises';4import {Readable, Writable, Duplex, PassThrough} from 'node:stream';4import {Writable, PassThrough} from 'node:stream';5import {text} from 'node:stream/consumers';6import {setImmediate} from 'node:timers/promises';7import {callbackify} from 'node:util';8import test from 'ava';
1 unmodified line
10import {execa, execaSync} from '../../index.js';11import {setFixtureDir} from '../helpers/fixtures-dir.js';12import {getStdio} from '../helpers/stdio.js';13import {foobarString} from '../helpers/input.js';14import {noopReadable, noopWritable, noopDuplex, simpleReadable} from '../helpers/stream.js';1516setFixtureDir();1717const noopReadable = () => new Readable({read() {}});18const noopWritable = () => new Writable({write() {}});19const noopDuplex = () => new Duplex({read() {}, write() {}});20const simpleReadable = () => Readable.from([foobarString]);2118const testNoFileStreamSync = async (t, index, stream) => {19 t.throws(() => {20 execaSync('empty.js', getStdio(index, stream));21 }, {code: 'ERR_INVALID_ARG_VALUE'});
33 unmodified lines
55test('stdio[*] cannot be a Node.js Readable without a file descriptor', testNoFileStream, 3, noopReadable());56test('stdio[*] cannot be a Node.js Writable without a file descriptor', testNoFileStream, 3, noopWritable());57test('stdio[*] cannot be a Node.js Duplex without a file descriptor', testNoFileStream, 3, noopDuplex());5863const testFileReadable = async (t, index, execaMethod) => {59const createFileReadStream = async () => {60 const filePath = tempfile();61 await writeFile(filePath, 'foobar');62 const stream = createReadStream(filePath);63 await once(stream, 'open');64 return {stream, filePath};65};6667const createFileWriteStream = async () => {68 const filePath = tempfile();69 const stream = createWriteStream(filePath);70 await once(stream, 'open');71 return {stream, filePath};72};7374const assertFileStreamError = async (t, childProcess, stream, filePath) => {75 await once(childProcess, 'spawn');76 const error = new Error('test');77 stream.destroy(error);7879 t.is(await t.throwsAsync(childProcess), error);80 t.is(error.exitCode, 0);81 t.is(error.signal, undefined);8283 await rm(filePath);84};8586const testFileReadable = async (t, index, execaMethod) => {87 const {stream, filePath} = await createFileReadStream();8889 const indexString = index === 'input' ? '0' : `${index}`;90 const {stdout} = await execaMethod('stdin-fd.js', [indexString], getStdio(index, stream));91 t.is(stdout, 'foobar');92
5 unmodified lines
98test('stdio[*] can be a Node.js Readable with a file descriptor', testFileReadable, 3, execa);99test('stdin can be a Node.js Readable with a file descriptor - sync', testFileReadable, 0, execaSync);100test('stdio[*] can be a Node.js Readable with a file descriptor - sync', testFileReadable, 3, execaSync);101102const testFileReadableError = async (t, index) => {103 const {stream, filePath} = await createFileReadStream();104105 const indexString = index === 'input' ? '0' : `${index}`;106 const childProcess = execa('stdin-fd.js', [indexString], getStdio(index, stream));107108 await assertFileStreamError(t, childProcess, stream, filePath);109};110111test('input handles errors from a Node.js Readable with a file descriptor', testFileReadableError, 'input');112test('stdin handles errors from a Node.js Readable with a file descriptor', testFileReadableError, 0);113test('stdio[*] handles errors from a Node.js Readable with a file descriptor', testFileReadableError, 3);114115const testFileReadableOpen = async (t, index, useSingle, execaMethod) => {116 const {stream, filePath} = await createFileReadStream();117 t.deepEqual(stream.eventNames(), []);118119 const stdioOption = useSingle ? stream : [stream, 'pipe'];120 await execaMethod('empty.js', getStdio(index, stdioOption));121122 t.is(stream.readable, useSingle && index !== 'input');123 t.deepEqual(stream.eventNames(), []);124125 await rm(filePath);126};127128test('input closes a Node.js Readable with a file descriptor', testFileReadableOpen, 'input', true, execa);129test('stdin leaves open a single Node.js Readable with a file descriptor', testFileReadableOpen, 0, true, execa);130test('stdin closes a combined Node.js Readable with a file descriptor', testFileReadableOpen, 0, false, execa);131test('stdio[*] leaves open a single Node.js Readable with a file descriptor', testFileReadableOpen, 3, true, execa);132test('stdin leaves open a single Node.js Readable with a file descriptor - sync', testFileReadableOpen, 0, true, execaSync);133test('stdio[*] leaves open a single Node.js Readable with a file descriptor - sync', testFileReadableOpen, 3, true, execaSync);134135const testFileWritable = async (t, index, execaMethod) => {83 const filePath = tempfile();84 const stream = createWriteStream(filePath);85 await once(stream, 'open');136 const {stream, filePath} = await createFileWriteStream();137138 await execaMethod('noop-fd.js', [`${index}`, 'foobar'], getStdio(index, stream));139 t.is(await readFile(filePath, 'utf8'), 'foobar');140
6 unmodified lines
147test('stdout can be a Node.js Writable with a file descriptor - sync', testFileWritable, 1, execaSync);148test('stderr can be a Node.js Writable with a file descriptor - sync', testFileWritable, 2, execaSync);149test('stdio[*] can be a Node.js Writable with a file descriptor - sync', testFileWritable, 3, execaSync);150151const testFileWritableError = async (t, index) => {152 const {stream, filePath} = await createFileWriteStream();153154 const childProcess = execa('noop-stdin-fd.js', [`${index}`], getStdio(index, stream));155 childProcess.stdin.end(foobarString);156157 await assertFileStreamError(t, childProcess, stream, filePath);158};159160test('stdout handles errors from a Node.js Writable with a file descriptor', testFileWritableError, 1);161test('stderr handles errors from a Node.js Writable with a file descriptor', testFileWritableError, 2);162test('stdio[*] handles errors from a Node.js Writable with a file descriptor', testFileWritableError, 3);163164const testFileWritableOpen = async (t, index, useSingle, execaMethod) => {165 const {stream, filePath} = await createFileWriteStream();166 t.deepEqual(stream.eventNames(), []);167168 const stdioOption = useSingle ? stream : [stream, 'pipe'];169 await execaMethod('empty.js', getStdio(index, stdioOption));170171 t.is(stream.writable, useSingle);172 t.deepEqual(stream.eventNames(), []);173174 await rm(filePath);175};176177test('stdout leaves open a single Node.js Writable with a file descriptor', testFileWritableOpen, 1, true, execa);178test('stdout closes a combined Node.js Writable with a file descriptor', testFileWritableOpen, 1, false, execa);179test('stderr leaves open a single Node.js Writable with a file descriptor', testFileWritableOpen, 2, true, execa);180test('stderr closes a combined Node.js Writable with a file descriptor', testFileWritableOpen, 2, false, execa);181test('stdio[*] leaves open a single Node.js Writable with a file descriptor', testFileWritableOpen, 3, true, execa);182test('stdio[*] closes a combined Node.js Writable with a file descriptor', testFileWritableOpen, 3, false, execa);183test('stdout leaves open a single Node.js Writable with a file descriptor - sync', testFileWritableOpen, 1, true, execaSync);184test('stderr leaves open a single Node.js Writable with a file descriptor - sync', testFileWritableOpen, 2, true, execaSync);185test('stdio[*] leaves open a single Node.js Writable with a file descriptor - sync', testFileWritableOpen, 3, true, execaSync);186187const testLazyFileReadable = async (t, index) => {188 const filePath = tempfile();189 await writeFile(filePath, 'foobar');190 const stream = createReadStream(filePath);
75 unmodified lines
266267test('Can pass Duplex streams to stdout', testOutputDuplexStream, 1);268test('Can pass Duplex streams to stderr', testOutputDuplexStream, 2);269test('Can pass Duplex streams to output stdio[*]', testOutputDuplexStream, 3);183184const assertStreamError = (t, {exitCode, signal, isTerminated, failed}) => {185 t.is(exitCode, 0);186 t.is(signal, undefined);187 t.false(isTerminated);188 t.true(failed);189};190191const getStreamFixtureName = index => index === 0 ? 'stdin.js' : 'noop.js';192193test('Handles output streams ends', async t => {194 const stream = noopWritable();195 const childProcess = execa('stdin.js', {stdout: [stream, 'pipe']});196 childProcess.stdin.end();197 await setImmediate();198 stream.destroy();199200 const error = await t.throwsAsync(childProcess, {code: 'ERR_STREAM_PREMATURE_CLOSE'});201 assertStreamError(t, error);202});203204const testStreamAbort = async (t, stream, index) => {205 const childProcess = execa(getStreamFixtureName(index), getStdio(index, [stream, 'pipe']));206 stream.destroy();207 const error = await t.throwsAsync(childProcess, {code: 'ERR_STREAM_PREMATURE_CLOSE'});208 assertStreamError(t, error);209};210211test('Handles input streams aborts', testStreamAbort, noopReadable(), 0);212test('Handles input Duplex streams aborts', testStreamAbort, noopDuplex(), 0);213test('Handles output streams aborts', testStreamAbort, noopWritable(), 1);214test('Handles output Duplex streams aborts', testStreamAbort, noopDuplex(), 1);215216const testStreamError = async (t, stream, index) => {217 const childProcess = execa(getStreamFixtureName(index), getStdio(index, [stream, 'pipe']));218 const error = new Error('test');219 stream.destroy(error);220 t.is(await t.throwsAsync(childProcess), error);221 assertStreamError(t, error);222};223224test('Handles input streams errors', testStreamError, noopReadable(), 0);225test('Handles input Duplex streams errors', testStreamError, noopDuplex(), 0);226test('Handles output streams errors', testStreamError, noopWritable(), 1);227test('Handles output Duplex streams errors', testStreamError, noopDuplex(), 1);228229const testChildStreamEnd = async (t, stream) => {230 const childProcess = execa('stdin.js', {stdin: [stream, 'pipe']});231 childProcess.stdin.end();232 await setImmediate();233 stream.destroy();234235 const error = await t.throwsAsync(childProcess, {code: 'ERR_STREAM_PREMATURE_CLOSE'});236 assertStreamError(t, error);237 t.true(stream.destroyed);238};239240test('Handles childProcess.stdin end', testChildStreamEnd, noopReadable());241test('Handles childProcess.stdin Duplex end', testChildStreamEnd, noopDuplex());242243const testChildStreamAbort = async (t, stream, index) => {244 const childProcess = execa(getStreamFixtureName(index), getStdio(index, [stream, 'pipe']));245 childProcess.stdio[index].destroy();246 const error = await t.throwsAsync(childProcess, {code: 'ERR_STREAM_PREMATURE_CLOSE'});247 assertStreamError(t, error);248 t.true(stream.destroyed);249};250251test('Handles childProcess.stdin aborts', testChildStreamAbort, noopReadable(), 0);252test('Handles childProcess.stdin Duplex aborts', testChildStreamAbort, noopDuplex(), 0);253test('Handles childProcess.stdout aborts', testChildStreamAbort, noopWritable(), 1);254test('Handles childProcess.stdout Duplex aborts', testChildStreamAbort, noopDuplex(), 1);255256const testChildStreamError = async (t, stream, index) => {257 const childProcess = execa(getStreamFixtureName(index), getStdio(index, [stream, 'pipe']));258 const error = new Error('test');259 childProcess.stdio[index].destroy(error);260 t.is(await t.throwsAsync(childProcess), error);261 assertStreamError(t, error);262 t.true(stream.destroyed);263};264265test('Handles childProcess.stdin errors', testChildStreamError, noopReadable(), 0);266test('Handles childProcess.stdin Duplex errors', testChildStreamError, noopDuplex(), 0);267test('Handles childProcess.stdout errors', testChildStreamError, noopWritable(), 1);268test('Handles childProcess.stdout Duplex errors', testChildStreamError, noopDuplex(), 1);
added:test/stdio/wait.js+228 added
1import {platform} from 'node:process';2import {setImmediate} from 'node:timers/promises';3import test from 'ava';4import {execa} from '../../index.js';5import {setFixtureDir} from '../helpers/fixtures-dir.js';6import {getStdio, prematureClose} from '../helpers/stdio.js';7import {foobarString} from '../helpers/input.js';8import {noopGenerator} from '../helpers/generator.js';9import {noopReadable, noopWritable, noopDuplex} from '../helpers/stream.js';1011setFixtureDir();1213const isWindows = platform === 'win32';1415const noop = () => {};1617const endOptionStream = ({stream}) => {18 stream.end();19};2021const destroyOptionStream = ({stream, error}) => {22 stream.destroy(error);23};2425const destroyChildStream = ({childProcess, index, error}) => {26 childProcess.stdio[index].destroy(error);27};2829const getStreamStdio = (index, stream, useTransform) => getStdio(index, [stream, useTransform ? noopGenerator(false) : 'pipe']);3031// eslint-disable-next-line max-params32const testStreamAbortWait = async (t, streamMethod, stream, index, useTransform) => {33 const childProcess = execa('noop-stdin-fd.js', [`${index}`], getStreamStdio(index, stream, useTransform));34 streamMethod({stream, childProcess, index});35 childProcess.stdin.end();36 await setImmediate();37 stream.destroy();3839 const {stdout} = await childProcess;40 t.is(stdout, '');41 t.true(stream.destroyed);42};4344test('Keeps running when stdin option is used and childProcess.stdin ends', testStreamAbortWait, noop, noopReadable(), 0, false);45test('Keeps running when stdin option is used and childProcess.stdin Duplex ends', testStreamAbortWait, noop, noopDuplex(), 0, false);46test('Keeps running when input stdio[*] option is used and input childProcess.stdio[*] ends', testStreamAbortWait, noop, noopReadable(), 3, false);47test('Keeps running when stdin option is used and childProcess.stdin ends, with a transform', testStreamAbortWait, noop, noopReadable(), 0, true);48test('Keeps running when stdin option is used and childProcess.stdin Duplex ends, with a transform', testStreamAbortWait, noop, noopDuplex(), 0, true);49test('Keeps running when input stdio[*] option is used and input childProcess.stdio[*] ends, with a transform', testStreamAbortWait, noop, noopReadable(), 3, true);5051// eslint-disable-next-line max-params52const testStreamAbortSuccess = async (t, streamMethod, stream, index, useTransform) => {53 const childProcess = execa('noop-stdin-fd.js', [`${index}`], getStreamStdio(index, stream, useTransform));54 streamMethod({stream, childProcess, index});55 childProcess.stdin.end();5657 const {stdout} = await childProcess;58 t.is(stdout, '');59 t.true(stream.destroyed);60};6162test('Passes when stdin option aborts', testStreamAbortSuccess, destroyOptionStream, noopReadable(), 0, false);63test('Passes when stdin option Duplex aborts', testStreamAbortSuccess, destroyOptionStream, noopDuplex(), 0, false);64test('Passes when input stdio[*] option aborts', testStreamAbortSuccess, destroyOptionStream, noopReadable(), 3, false);65test('Passes when stdout option ends with no more writes', testStreamAbortSuccess, endOptionStream, noopWritable(), 1, false);66test('Passes when stderr option ends with no more writes', testStreamAbortSuccess, endOptionStream, noopWritable(), 2, false);67test('Passes when output stdio[*] option ends with no more writes', testStreamAbortSuccess, endOptionStream, noopWritable(), 3, false);68test('Passes when childProcess.stdout aborts with no more writes', testStreamAbortSuccess, destroyChildStream, noopWritable(), 1, false);69test('Passes when childProcess.stdout Duplex aborts with no more writes', testStreamAbortSuccess, destroyChildStream, noopDuplex(), 1, false);70test('Passes when childProcess.stderr aborts with no more writes', testStreamAbortSuccess, destroyChildStream, noopWritable(), 2, false);71test('Passes when childProcess.stderr Duplex aborts with no more writes', testStreamAbortSuccess, destroyChildStream, noopDuplex(), 2, false);72test('Passes when output childProcess.stdio[*] aborts with no more writes', testStreamAbortSuccess, destroyChildStream, noopWritable(), 3, false);73test('Passes when output childProcess.stdio[*] Duplex aborts with no more writes', testStreamAbortSuccess, destroyChildStream, noopDuplex(), 3, false);74test('Passes when stdin option aborts, with a transform', testStreamAbortSuccess, destroyOptionStream, noopReadable(), 0, true);75test('Passes when stdin option Duplex aborts, with a transform', testStreamAbortSuccess, destroyOptionStream, noopDuplex(), 0, true);76test('Passes when input stdio[*] option aborts, with a transform', testStreamAbortSuccess, destroyOptionStream, noopReadable(), 3, true);77test('Passes when stdout option ends with no more writes, with a transform', testStreamAbortSuccess, endOptionStream, noopWritable(), 1, true);78test('Passes when stderr option ends with no more writes, with a transform', testStreamAbortSuccess, endOptionStream, noopWritable(), 2, true);79test('Passes when output stdio[*] option ends with no more writes, with a transform', testStreamAbortSuccess, endOptionStream, noopWritable(), 3, true);80test('Passes when childProcess.stdout aborts with no more writes, with a transform', testStreamAbortSuccess, destroyChildStream, noopWritable(), 1, true);81test('Passes when childProcess.stdout Duplex aborts with no more writes, with a transform', testStreamAbortSuccess, destroyChildStream, noopDuplex(), 1, true);82test('Passes when childProcess.stderr aborts with no more writes, with a transform', testStreamAbortSuccess, destroyChildStream, noopWritable(), 2, true);83test('Passes when childProcess.stderr Duplex aborts with no more writes, with a transform', testStreamAbortSuccess, destroyChildStream, noopDuplex(), 2, true);84test('Passes when output childProcess.stdio[*] aborts with no more writes, with a transform', testStreamAbortSuccess, destroyChildStream, noopWritable(), 3, true);85test('Passes when output childProcess.stdio[*] Duplex aborts with no more writes, with a transform', testStreamAbortSuccess, destroyChildStream, noopDuplex(), 3, true);8687// eslint-disable-next-line max-params88const testStreamAbortFail = async (t, streamMethod, stream, index, useTransform) => {89 const childProcess = execa('noop-stdin-fd.js', [`${index}`], getStreamStdio(index, stream, useTransform));90 streamMethod({stream, childProcess, index});91 if (index !== 0) {92 childProcess.stdin.end();93 }9495 const error = await t.throwsAsync(childProcess);96 t.like(error, {...prematureClose, exitCode: 0});97 t.true(stream.destroyed);98};99100test('Throws abort error when childProcess.stdin aborts', testStreamAbortFail, destroyChildStream, noopReadable(), 0, false);101test('Throws abort error when childProcess.stdin Duplex aborts', testStreamAbortFail, destroyChildStream, noopDuplex(), 0, false);102test('Throws abort error when input childProcess.stdio[*] aborts', testStreamAbortFail, destroyChildStream, noopReadable(), 3, false);103test('Throws abort error when stdout option aborts with no more writes', testStreamAbortFail, destroyOptionStream, noopWritable(), 1, false);104test('Throws abort error when stdout option Duplex aborts with no more writes', testStreamAbortFail, destroyOptionStream, noopDuplex(), 1, false);105test('Throws abort error when stderr option aborts with no more writes', testStreamAbortFail, destroyOptionStream, noopWritable(), 2, false);106test('Throws abort error when stderr option Duplex aborts with no more writes', testStreamAbortFail, destroyOptionStream, noopDuplex(), 2, false);107test('Throws abort error when output stdio[*] option aborts with no more writes', testStreamAbortFail, destroyOptionStream, noopWritable(), 3, false);108test('Throws abort error when output stdio[*] Duplex option aborts with no more writes', testStreamAbortFail, destroyOptionStream, noopDuplex(), 3, false);109test('Throws abort error when childProcess.stdin aborts, with a transform', testStreamAbortFail, destroyChildStream, noopReadable(), 0, true);110test('Throws abort error when childProcess.stdin Duplex aborts, with a transform', testStreamAbortFail, destroyChildStream, noopDuplex(), 0, true);111test('Throws abort error when input childProcess.stdio[*] aborts, with a transform', testStreamAbortFail, destroyChildStream, noopReadable(), 3, true);112test('Throws abort error when stdout option aborts with no more writes, with a transform', testStreamAbortFail, destroyOptionStream, noopWritable(), 1, true);113test('Throws abort error when stdout option Duplex aborts with no more writes, with a transform', testStreamAbortFail, destroyOptionStream, noopDuplex(), 1, true);114test('Throws abort error when stderr option aborts with no more writes, with a transform', testStreamAbortFail, destroyOptionStream, noopWritable(), 2, true);115test('Throws abort error when stderr option Duplex aborts with no more writes, with a transform', testStreamAbortFail, destroyOptionStream, noopDuplex(), 2, true);116test('Throws abort error when output stdio[*] option aborts with no more writes, with a transform', testStreamAbortFail, destroyOptionStream, noopWritable(), 3, true);117test('Throws abort error when output stdio[*] Duplex option aborts with no more writes, with a transform', testStreamAbortFail, destroyOptionStream, noopDuplex(), 3, true);118119// eslint-disable-next-line max-params120const testStreamEpipeSuccess = async (t, streamMethod, stream, index, useTransform) => {121 const childProcess = execa('noop-stdin-fd.js', [`${index}`], getStreamStdio(index, stream, useTransform));122 streamMethod({stream, childProcess, index});123 childProcess.stdin.end(foobarString);124125 const {stdio} = await childProcess;126 t.is(stdio[index], foobarString);127 t.true(stream.destroyed);128};129130test('Passes when stdout option ends with more writes', testStreamEpipeSuccess, endOptionStream, noopWritable(), 1, false);131test('Passes when stderr option ends with more writes', testStreamEpipeSuccess, endOptionStream, noopWritable(), 2, false);132test('Passes when output stdio[*] option ends with more writes', testStreamEpipeSuccess, endOptionStream, noopWritable(), 3, false);133test('Passes when stdout option ends with more writes, with a transform', testStreamEpipeSuccess, endOptionStream, noopWritable(), 1, true);134test('Passes when stderr option ends with more writes, with a transform', testStreamEpipeSuccess, endOptionStream, noopWritable(), 2, true);135test('Passes when output stdio[*] option ends with more writes, with a transform', testStreamEpipeSuccess, endOptionStream, noopWritable(), 3, true);136137// eslint-disable-next-line max-params138const testStreamEpipeFail = async (t, streamMethod, stream, index, useTransform) => {139 const childProcess = execa('noop-stdin-fd.js', [`${index}`], getStreamStdio(index, stream, useTransform));140 streamMethod({stream, childProcess, index});141 await setImmediate();142 childProcess.stdin.end(foobarString);143144 const {exitCode, stdio, stderr} = await t.throwsAsync(childProcess);145 t.is(exitCode, 1);146 t.is(stdio[index], '');147 t.true(stream.destroyed);148 if (index !== 2 && !isWindows) {149 t.true(stderr.includes('EPIPE'));150 }151};152153test('Throws EPIPE when stdout option aborts with more writes', testStreamEpipeFail, destroyOptionStream, noopWritable(), 1, false);154test('Throws EPIPE when stdout option Duplex aborts with more writes', testStreamEpipeFail, destroyOptionStream, noopDuplex(), 1, false);155test('Throws EPIPE when stderr option aborts with more writes', testStreamEpipeFail, destroyOptionStream, noopWritable(), 2, false);156test('Throws EPIPE when stderr option Duplex aborts with more writes', testStreamEpipeFail, destroyOptionStream, noopDuplex(), 2, false);157test('Throws EPIPE when output stdio[*] option aborts with more writes', testStreamEpipeFail, destroyOptionStream, noopWritable(), 3, false);158test('Throws EPIPE when output stdio[*] option Duplex aborts with more writes', testStreamEpipeFail, destroyOptionStream, noopDuplex(), 3, false);159test('Throws EPIPE when childProcess.stdout aborts with more writes', testStreamEpipeFail, destroyChildStream, noopWritable(), 1, false);160test('Throws EPIPE when childProcess.stdout Duplex aborts with more writes', testStreamEpipeFail, destroyChildStream, noopDuplex(), 1, false);161test('Throws EPIPE when childProcess.stderr aborts with more writes', testStreamEpipeFail, destroyChildStream, noopWritable(), 2, false);162test('Throws EPIPE when childProcess.stderr Duplex aborts with more writes', testStreamEpipeFail, destroyChildStream, noopDuplex(), 2, false);163test('Throws EPIPE when output childProcess.stdio[*] aborts with more writes', testStreamEpipeFail, destroyChildStream, noopWritable(), 3, false);164test('Throws EPIPE when output childProcess.stdio[*] Duplex aborts with more writes', testStreamEpipeFail, destroyChildStream, noopDuplex(), 3, false);165test('Throws EPIPE when stdout option aborts with more writes, with a transform', testStreamEpipeFail, destroyOptionStream, noopWritable(), 1, true);166test('Throws EPIPE when stdout option Duplex aborts with more writes, with a transform', testStreamEpipeFail, destroyOptionStream, noopDuplex(), 1, true);167test('Throws EPIPE when stderr option aborts with more writes, with a transform', testStreamEpipeFail, destroyOptionStream, noopWritable(), 2, true);168test('Throws EPIPE when stderr option Duplex aborts with more writes, with a transform', testStreamEpipeFail, destroyOptionStream, noopDuplex(), 2, true);169test('Throws EPIPE when output stdio[*] option aborts with more writes, with a transform', testStreamEpipeFail, destroyOptionStream, noopWritable(), 3, true);170test('Throws EPIPE when output stdio[*] option Duplex aborts with more writes, with a transform', testStreamEpipeFail, destroyOptionStream, noopDuplex(), 3, true);171test('Throws EPIPE when childProcess.stdout aborts with more writes, with a transform', testStreamEpipeFail, destroyChildStream, noopWritable(), 1, true);172test('Throws EPIPE when childProcess.stdout Duplex aborts with more writes, with a transform', testStreamEpipeFail, destroyChildStream, noopDuplex(), 1, true);173test('Throws EPIPE when childProcess.stderr aborts with more writes, with a transform', testStreamEpipeFail, destroyChildStream, noopWritable(), 2, true);174test('Throws EPIPE when childProcess.stderr Duplex aborts with more writes, with a transform', testStreamEpipeFail, destroyChildStream, noopDuplex(), 2, true);175test('Throws EPIPE when output childProcess.stdio[*] aborts with more writes, with a transform', testStreamEpipeFail, destroyChildStream, noopWritable(), 3, true);176test('Throws EPIPE when output childProcess.stdio[*] Duplex aborts with more writes, with a transform', testStreamEpipeFail, destroyChildStream, noopDuplex(), 3, true);177178// eslint-disable-next-line max-params179const testStreamError = async (t, streamMethod, stream, index, useTransform) => {180 const childProcess = execa('empty.js', getStreamStdio(index, stream, useTransform));181 const error = new Error('test');182 streamMethod({stream, childProcess, index, error});183184 t.is(await t.throwsAsync(childProcess), error);185 const {exitCode, signal, isTerminated, failed} = error;186 t.is(exitCode, 0);187 t.is(signal, undefined);188 t.false(isTerminated);189 t.true(failed);190 t.true(stream.destroyed);191};192193test('Throws stream error when stdin option errors', testStreamError, destroyOptionStream, noopReadable(), 0, false);194test('Throws stream error when stdin option Duplex errors', testStreamError, destroyOptionStream, noopDuplex(), 0, false);195test('Throws stream error when stdout option errors', testStreamError, destroyOptionStream, noopWritable(), 1, false);196test('Throws stream error when stdout option Duplex errors', testStreamError, destroyOptionStream, noopDuplex(), 1, false);197test('Throws stream error when stderr option errors', testStreamError, destroyOptionStream, noopWritable(), 2, false);198test('Throws stream error when stderr option Duplex errors', testStreamError, destroyOptionStream, noopDuplex(), 2, false);199test('Throws stream error when output stdio[*] option errors', testStreamError, destroyOptionStream, noopWritable(), 3, false);200test('Throws stream error when output stdio[*] Duplex option errors', testStreamError, destroyOptionStream, noopDuplex(), 3, false);201test('Throws stream error when input stdio[*] option errors', testStreamError, destroyOptionStream, noopReadable(), 3, false);202test('Throws stream error when childProcess.stdin errors', testStreamError, destroyChildStream, noopReadable(), 0, false);203test('Throws stream error when childProcess.stdin Duplex errors', testStreamError, destroyChildStream, noopDuplex(), 0, false);204test('Throws stream error when childProcess.stdout errors', testStreamError, destroyChildStream, noopWritable(), 1, false);205test('Throws stream error when childProcess.stdout Duplex errors', testStreamError, destroyChildStream, noopDuplex(), 1, false);206test('Throws stream error when childProcess.stderr errors', testStreamError, destroyChildStream, noopWritable(), 2, false);207test('Throws stream error when childProcess.stderr Duplex errors', testStreamError, destroyChildStream, noopDuplex(), 2, false);208test('Throws stream error when output childProcess.stdio[*] errors', testStreamError, destroyChildStream, noopWritable(), 3, false);209test('Throws stream error when output childProcess.stdio[*] Duplex errors', testStreamError, destroyChildStream, noopDuplex(), 3, false);210test('Throws stream error when input childProcess.stdio[*] errors', testStreamError, destroyChildStream, noopReadable(), 3, false);211test('Throws stream error when stdin option errors, with a transform', testStreamError, destroyOptionStream, noopReadable(), 0, true);212test('Throws stream error when stdin option Duplex errors, with a transform', testStreamError, destroyOptionStream, noopDuplex(), 0, true);213test('Throws stream error when stdout option errors, with a transform', testStreamError, destroyOptionStream, noopWritable(), 1, true);214test('Throws stream error when stdout option Duplex errors, with a transform', testStreamError, destroyOptionStream, noopDuplex(), 1, true);215test('Throws stream error when stderr option errors, with a transform', testStreamError, destroyOptionStream, noopWritable(), 2, true);216test('Throws stream error when stderr option Duplex errors, with a transform', testStreamError, destroyOptionStream, noopDuplex(), 2, true);217test('Throws stream error when output stdio[*] option errors, with a transform', testStreamError, destroyOptionStream, noopWritable(), 3, true);218test('Throws stream error when output stdio[*] Duplex option errors, with a transform', testStreamError, destroyOptionStream, noopDuplex(), 3, true);219test('Throws stream error when input stdio[*] option errors, with a transform', testStreamError, destroyOptionStream, noopReadable(), 3, true);220test('Throws stream error when childProcess.stdin errors, with a transform', testStreamError, destroyChildStream, noopReadable(), 0, true);221test('Throws stream error when childProcess.stdin Duplex errors, with a transform', testStreamError, destroyChildStream, noopDuplex(), 0, true);222test('Throws stream error when childProcess.stdout errors, with a transform', testStreamError, destroyChildStream, noopWritable(), 1, true);223test('Throws stream error when childProcess.stdout Duplex errors, with a transform', testStreamError, destroyChildStream, noopDuplex(), 1, true);224test('Throws stream error when childProcess.stderr errors, with a transform', testStreamError, destroyChildStream, noopWritable(), 2, true);225test('Throws stream error when childProcess.stderr Duplex errors, with a transform', testStreamError, destroyChildStream, noopDuplex(), 2, true);226test('Throws stream error when output childProcess.stdio[*] errors, with a transform', testStreamError, destroyChildStream, noopWritable(), 3, true);227test('Throws stream error when output childProcess.stdio[*] Duplex errors, with a transform', testStreamError, destroyChildStream, noopDuplex(), 3, true);228test('Throws stream error when input childProcess.stdio[*] errors, with a transform', testStreamError, destroyChildStream, noopReadable(), 3, true);
modified:test/stream.js+6 added-4 removed
5 unmodified lines
6import test from 'ava';7import getStream from 'get-stream';8import {execa, execaSync} from '../index.js';9import {setFixtureDir} from './helpers/fixtures-dir.js';10import {fullStdio, getStdio} from './helpers/stdio.js';10import {fullStdio, getStdio, prematureClose} from './helpers/stdio.js';11import {foobarString} from './helpers/input.js';12import {infiniteGenerator} from './helpers/generator.js';1314setFixtureDir();1516const isWindows = platform === 'win32';1718test.serial('result.all shows both `stdout` and `stderr` intermixed', async t => {19 const {all} = await execa('noop-132.js', {all: true});20 t.is(all, '132');21});
294 unmodified lines
316317const getStreamInputProcess = index => execa('stdin-fd.js', [`${index}`], index === 3318 ? getStdio(3, [new Uint8Array(), infiniteGenerator])319 : {});318const getStreamOutputProcess = index => execa('max-buffer.js', [`${index}`], index === 3 ? fullStdio : {});320const getStreamOutputProcess = index => execa('noop-repeat.js', [`${index}`], index === 3 ? fullStdio : {});321322const assertStreamInputError = (t, {exitCode, signal, isTerminated, failed}) => {323 t.is(exitCode, 0);324 t.is(signal, undefined);
9 unmodified lines
334 t.is(signal, undefined);335 t.false(isTerminated);336 t.true(failed);337336 if (index === 1 && platform !== 'win32') {338 if (index === 1 && !isWindows) {339 t.true(stderr.includes('EPIPE'));340 }341};342343const testStreamInputAbort = async (t, index) => {344 const childProcess = getStreamInputProcess(index);345 childProcess.stdio[index].destroy();344 const error = await t.throwsAsync(childProcess, {code: 'ERR_STREAM_PREMATURE_CLOSE'});346 const error = await t.throwsAsync(childProcess, prematureClose);347 assertStreamInputError(t, error);348};349350test('Aborting stdin should not make the process exit', testStreamInputAbort, 0);
122 unmodified lines