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
Provenance
- Commit
35c0fb0- Tree
503a8e9- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
4fb8e9a
11 files changed+459 added−138 removed
modified:lib/stdio/pipeline.js+6 added-1 removed
123456743 unmodified lines5152535455565754585960import {finished} from 'node:stream/promises';import mergeStreams from '@sindresorhus/merge-streams';import {isStreamAbort} from '../wait.js';import {isStandardStream} from './utils.js';
// Like `Stream.pipeline(source, destination)`, but does not destroy standard streams.// `sources` might be a single stream, or multiple ones combined with `merge-stream`.43 unmodified lines if (finishedStreams.has(stream)) { return; }
if (isStreamAbort(error)) { stream.destroy(); } else { stream.destroy(error); stream.destroy(error); }};modified:lib/stream.js+34 added-36 removed
1 import {once} from 'node:events';2-import {finished} from 'node:stream/promises';2 import {setImmediate} from 'node:timers/promises';3 import getStream, {getStreamAsArrayBuffer, getStreamAsArray, MaxBufferError} from 'get-stream';4+import {isStream} from 'is-stream';5 import mergeStreams from '@sindresorhus/merge-streams';6 import {waitForSuccessfulExit, throwOnTimeout, errorSignal} from './kill.js';7 import {isStandardStream} from './stdio/utils.js';8 import {generatorToDuplexStream} from './stdio/generator.js';9+import {waitForStream, handleStreamError, isInputFileDescriptor} from './wait.js';10 11 // `all` interleaves `stdout` and `stderr`12 export const makeAllStream = ({stdout, stderr}, {all}) => all && (stdout || stderr)13 ? mergeStreams([stdout, stderr].filter(Boolean))5 unmodified lines19 const 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 };26 27+const handleBufferedData = (error, encoding) => error.bufferedData === undefined || Array.isArray(error.bufferedData)28+ ? error.bufferedData29+ : applyEncoding(error.bufferedData, encoding);30+31 // Read the contents of `childProcess.std*` and|or wait for its completion29-const waitForChildStreams = ({spawned, stdioStreamsGroups, encoding, buffer, maxBuffer, waitForStream}) => spawned.stdio.map((stream, index) => waitForChildStream({32+const 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 }));41 42 // Read the contents of `childProcess.all` and|or wait for its completion40-const waitForAllStream = ({spawned, encoding, buffer, maxBuffer, waitForStream}) => waitForChildStream({43+const 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 });52 53 // 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 chunk11 unmodified lines66 writableObjectMode: true,67 readableObjectMode: true,68 };69 67-const waitForChildStream = async ({stream, spawned, direction, encoding, buffer, maxBuffer, waitForStream}) => {70+const waitForChildStream = async ({stream, spawned, index, encoding, buffer, maxBuffer, streamInfo}) => {71 if (!stream) {72 return;73 }74 72- if (direction === 'input') {73- await waitForStream(stream);75+ if (isInputFileDescriptor(index, streamInfo.stdioStreamsGroups)) {76+ await waitForStream(stream, index, streamInfo);77 return;78 }79 80 if (!buffer) {81 await Promise.all([79- waitForStream(stream),82+ waitForStream(stream, index, streamInfo),83 resumeStream(stream),84 ]);85 return;86 }4 unmodified lines91 if (error instanceof MaxBufferError) {92 spawned.kill();93 }94 92- throw error;95+ handleStreamError(error, index, streamInfo);96+ return handleBufferedData(error, encoding);97 }98 };99 100 // When using `buffer: false`, users need to read `childProcess.stdout|stderr|all` right away19 unmodified lines120 const applyEncoding = (contents, encoding) => encoding === 'buffer' ? new Uint8Array(contents) : contents;121 122 // Transforms replace `childProcess.std*`, which means they are not exposed to users.123 // However, we still want to wait for their completion.120-const waitForOriginalStreams = (originalStreams, spawned, waitForStream) => originalStreams121- .filter((stream, index) => stream !== spawned.stdio[index])122- .map(stream => waitForStream(stream));124+const waitForOriginalStreams = (originalStreams, spawned, streamInfo) =>125+ originalStreams.map((stream, index) => stream === spawned.stdio[index]126+ ? undefined127+ : waitForStream(stream, index, streamInfo));128 129 // 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.127-const waitForCustomStreamsEnd = (stdioStreamsGroups, waitForStream) => stdioStreamsGroups.flat()128- .filter(({type, value}) => type !== 'native' && !isStandardStream(value))129- .map(({value}) => waitForStream(value));132+const 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+ })));138 131-// 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.134-const onFinishedStream = async ([originalStdin], exitPromise, stream) => {135- await Promise.race([136- ...(stream === originalStdin ? [exitPromise] : []),137- finished(stream, {cleanup: true}),138- ]);139-};140-139 const throwOnProcessError = async (spawned, {signal}) => {140 const [error] = await once(spawned, 'error', {signal});141 throw error;142 };42 unmodified lines185 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([])};190 193- 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);195 196 try {197 return await Promise.race([198 Promise.all([22 unmodified linesadded:lib/wait.js+61 added
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061import {finished} from 'node:stream/promises';
// Wraps `finished(stream)` to handle the following case:// - When the child process exits, Node.js automatically calls `childProcess.stdin.destroy()`, which we need to ignore.// - However, we still need to throw if `childProcess.stdin.destroy()` is called before child process exit.export const waitForStream = async (stream, index, streamInfo, {isSameDirection, stopOnExit = false} = {}) => { const {originalStreams: [originalStdin], exitPromise} = streamInfo;
const abortController = new AbortController(); try { await Promise.race([ ...(stopOnExit || stream === originalStdin ? [exitPromise] : []), finished(stream, {cleanup: true, signal: abortController.signal}), ]); } catch (error) { handleStreamError(error, index, streamInfo, isSameDirection); } finally { abortController.abort(); }};
// We ignore EPIPEs on writable streams and aborts on readable streams since those can happen normally.// When one stream errors, the error is propagated to the other streams on the same file descriptor.// Those other streams might have a different direction due to the above.// When this happens, the direction of both the initial stream and the others should then be taken into account.// Therefore, we keep track of which file descriptor is currently propagating stream errors.export const handleStreamError = (error, index, streamInfo, isSameDirection) => { if (!shouldIgnoreStreamError(error, index, streamInfo, isSameDirection)) { throw error; }};
const shouldIgnoreStreamError = (error, index, {stdioStreamsGroups, propagating}, isSameDirection = true) => { if (propagating.has(index)) { return isStreamEpipe(error) || isStreamAbort(error); }
propagating.add(index); return isInputFileDescriptor(index, stdioStreamsGroups) === isSameDirection ? isStreamEpipe(error) : isStreamAbort(error);};
// Unfortunately, we cannot use the stream's class or properties to know whether it is readable or writable.// For example, `childProcess.stdin` is technically a Duplex, but can only be used as a writable.// Therefore, we need to use the file descriptor's direction (`stdin` is input, `stdout` is output, etc.).// However, while `childProcess.std*` and transforms follow that direction, any stream passed the `std*` option has the opposite direction.// For example, `childProcess.stdin` is a writable, but the `stdin` option is a readable.export const isInputFileDescriptor = (index, stdioStreamsGroups) => stdioStreamsGroups[index][0].direction === 'input';
// When `stream.destroy()` is called without an `error` argument, stream is aborted.// This is the only way to abort a readable stream, which can be useful in some instances.// Therefore, we ignore this error on readable streams.export const isStreamAbort = error => error?.code === 'ERR_STREAM_PREMATURE_CLOSE';
// When `stream.write()` is called but the underlying source has been closed, `EPIPE` is emitted.// When piping processes, the source process usually decides when to stop piping.// However, there are some instances when the destination does instead, such as `... | head -n1`.// It notifies the source by using `EPIPE`.// Therefore, we ignore this error on writable streams.const isStreamEpipe = error => error?.code === 'EPIPE';added:test/fixtures/noop-repeat.js+7 added
1234567#!/usr/bin/env nodeimport process from 'node:process';import {writeSync} from 'node:fs';
setInterval(() => { writeSync(Number(process.argv[2]) || 1, '.');}, 10);added:test/fixtures/noop-stdin-fd.js+10 added
12345678910#!/usr/bin/env nodeimport process from 'node:process';import {text} from 'node:stream/consumers';import {writeSync} from 'node:fs';
const stdinString = await text(process.stdin);const writableFileDescriptor = Number(process.argv[2]);if (stdinString !== '') { writeSync(writableFileDescriptor, stdinString);}modified:test/helpers/stdio.js+2 added
13 unmodified lines14151617181913 unmodified lines
export const fullStdio = getStdio(3, 'pipe');
export const STANDARD_STREAMS = [process.stdin, process.stdout, process.stderr];
export const prematureClose = {code: 'ERR_STREAM_PREMATURE_CLOSE'};added:test/helpers/stream.js+7 added
1234567import {Readable, Writable, Duplex} from 'node:stream';import {foobarString} from './input.js';
export const noopReadable = () => new Readable({read() {}});export const noopWritable = () => new Writable({write() {}});export const noopDuplex = () => new Duplex({read() {}, write() {}});export const simpleReadable = () => Readable.from([foobarString]);modified:test/stdio/generator.js+1 added-1 removed
707 unmodified lines708 const 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 };714 715 test('Running generators are canceled on process abort', testGeneratorCancel, undefined);716 test('Running generators are canceled on process error', testGeneratorCancel, new Error('test'));15 unmodified linesmodified:test/stdio/node-stream.js+97 added-96 removed
1 import {once} from 'node:events';2 import {createReadStream, createWriteStream} from 'node:fs';3 import {readFile, writeFile, rm} from 'node:fs/promises';4-import {Readable, Writable, Duplex, PassThrough} from 'node:stream';4+import {Writable, PassThrough} from 'node:stream';5 import {text} from 'node:stream/consumers';6 import {setImmediate} from 'node:timers/promises';7 import {callbackify} from 'node:util';8 import test from 'ava';1 unmodified line10 import {execa, execaSync} from '../../index.js';11 import {setFixtureDir} from '../helpers/fixtures-dir.js';12 import {getStdio} from '../helpers/stdio.js';13 import {foobarString} from '../helpers/input.js';14+import {noopReadable, noopWritable, noopDuplex, simpleReadable} from '../helpers/stream.js';15 16 setFixtureDir();17 17-const noopReadable = () => new Readable({read() {}});18-const noopWritable = () => new Writable({write() {}});19-const noopDuplex = () => new Duplex({read() {}, write() {}});20-const simpleReadable = () => Readable.from([foobarString]);21-18 const testNoFileStreamSync = async (t, index, stream) => {19 t.throws(() => {20 execaSync('empty.js', getStdio(index, stream));21 }, {code: 'ERR_INVALID_ARG_VALUE'});33 unmodified lines55 test('stdio[*] cannot be a Node.js Readable without a file descriptor', testNoFileStream, 3, noopReadable());56 test('stdio[*] cannot be a Node.js Writable without a file descriptor', testNoFileStream, 3, noopWritable());57 test('stdio[*] cannot be a Node.js Duplex without a file descriptor', testNoFileStream, 3, noopDuplex());58 63-const testFileReadable = async (t, index, execaMethod) => {59+const 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+};66 67+const createFileWriteStream = async () => {68+ const filePath = tempfile();69+ const stream = createWriteStream(filePath);70+ await once(stream, 'open');71+ return {stream, filePath};72+};73+74+const assertFileStreamError = async (t, childProcess, stream, filePath) => {75+ await once(childProcess, 'spawn');76+ const error = new Error('test');77+ stream.destroy(error);78+79+ t.is(await t.throwsAsync(childProcess), error);80+ t.is(error.exitCode, 0);81+ t.is(error.signal, undefined);82+83+ await rm(filePath);84+};85+86+const testFileReadable = async (t, index, execaMethod) => {87+ const {stream, filePath} = await createFileReadStream();88+89 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 lines98 test('stdio[*] can be a Node.js Readable with a file descriptor', testFileReadable, 3, execa);99 test('stdin can be a Node.js Readable with a file descriptor - sync', testFileReadable, 0, execaSync);100 test('stdio[*] can be a Node.js Readable with a file descriptor - sync', testFileReadable, 3, execaSync);101 102+const testFileReadableError = async (t, index) => {103+ const {stream, filePath} = await createFileReadStream();104+105+ const indexString = index === 'input' ? '0' : `${index}`;106+ const childProcess = execa('stdin-fd.js', [indexString], getStdio(index, stream));107+108+ await assertFileStreamError(t, childProcess, stream, filePath);109+};110+111+test('input handles errors from a Node.js Readable with a file descriptor', testFileReadableError, 'input');112+test('stdin handles errors from a Node.js Readable with a file descriptor', testFileReadableError, 0);113+test('stdio[*] handles errors from a Node.js Readable with a file descriptor', testFileReadableError, 3);114+115+const testFileReadableOpen = async (t, index, useSingle, execaMethod) => {116+ const {stream, filePath} = await createFileReadStream();117+ t.deepEqual(stream.eventNames(), []);118+119+ const stdioOption = useSingle ? stream : [stream, 'pipe'];120+ await execaMethod('empty.js', getStdio(index, stdioOption));121+122+ t.is(stream.readable, useSingle && index !== 'input');123+ t.deepEqual(stream.eventNames(), []);124+125+ await rm(filePath);126+};127+128+test('input closes a Node.js Readable with a file descriptor', testFileReadableOpen, 'input', true, execa);129+test('stdin leaves open a single Node.js Readable with a file descriptor', testFileReadableOpen, 0, true, execa);130+test('stdin closes a combined Node.js Readable with a file descriptor', testFileReadableOpen, 0, false, execa);131+test('stdio[*] leaves open a single Node.js Readable with a file descriptor', testFileReadableOpen, 3, true, execa);132+test('stdin leaves open a single Node.js Readable with a file descriptor - sync', testFileReadableOpen, 0, true, execaSync);133+test('stdio[*] leaves open a single Node.js Readable with a file descriptor - sync', testFileReadableOpen, 3, true, execaSync);134+135 const 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();137 138 await execaMethod('noop-fd.js', [`${index}`, 'foobar'], getStdio(index, stream));139 t.is(await readFile(filePath, 'utf8'), 'foobar');140 6 unmodified lines147 test('stdout can be a Node.js Writable with a file descriptor - sync', testFileWritable, 1, execaSync);148 test('stderr can be a Node.js Writable with a file descriptor - sync', testFileWritable, 2, execaSync);149 test('stdio[*] can be a Node.js Writable with a file descriptor - sync', testFileWritable, 3, execaSync);150 151+const testFileWritableError = async (t, index) => {152+ const {stream, filePath} = await createFileWriteStream();153+154+ const childProcess = execa('noop-stdin-fd.js', [`${index}`], getStdio(index, stream));155+ childProcess.stdin.end(foobarString);156+157+ await assertFileStreamError(t, childProcess, stream, filePath);158+};159+160+test('stdout handles errors from a Node.js Writable with a file descriptor', testFileWritableError, 1);161+test('stderr handles errors from a Node.js Writable with a file descriptor', testFileWritableError, 2);162+test('stdio[*] handles errors from a Node.js Writable with a file descriptor', testFileWritableError, 3);163+164+const testFileWritableOpen = async (t, index, useSingle, execaMethod) => {165+ const {stream, filePath} = await createFileWriteStream();166+ t.deepEqual(stream.eventNames(), []);167+168+ const stdioOption = useSingle ? stream : [stream, 'pipe'];169+ await execaMethod('empty.js', getStdio(index, stdioOption));170+171+ t.is(stream.writable, useSingle);172+ t.deepEqual(stream.eventNames(), []);173+174+ await rm(filePath);175+};176+177+test('stdout leaves open a single Node.js Writable with a file descriptor', testFileWritableOpen, 1, true, execa);178+test('stdout closes a combined Node.js Writable with a file descriptor', testFileWritableOpen, 1, false, execa);179+test('stderr leaves open a single Node.js Writable with a file descriptor', testFileWritableOpen, 2, true, execa);180+test('stderr closes a combined Node.js Writable with a file descriptor', testFileWritableOpen, 2, false, execa);181+test('stdio[*] leaves open a single Node.js Writable with a file descriptor', testFileWritableOpen, 3, true, execa);182+test('stdio[*] closes a combined Node.js Writable with a file descriptor', testFileWritableOpen, 3, false, execa);183+test('stdout leaves open a single Node.js Writable with a file descriptor - sync', testFileWritableOpen, 1, true, execaSync);184+test('stderr leaves open a single Node.js Writable with a file descriptor - sync', testFileWritableOpen, 2, true, execaSync);185+test('stdio[*] leaves open a single Node.js Writable with a file descriptor - sync', testFileWritableOpen, 3, true, execaSync);186+187 const testLazyFileReadable = async (t, index) => {188 const filePath = tempfile();189 await writeFile(filePath, 'foobar');190 const stream = createReadStream(filePath);75 unmodified lines266 267 test('Can pass Duplex streams to stdout', testOutputDuplexStream, 1);268 test('Can pass Duplex streams to stderr', testOutputDuplexStream, 2);269 test('Can pass Duplex streams to output stdio[*]', testOutputDuplexStream, 3);183-184-const 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-};190-191-const getStreamFixtureName = index => index === 0 ? 'stdin.js' : 'noop.js';192-193-test('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();199-200- const error = await t.throwsAsync(childProcess, {code: 'ERR_STREAM_PREMATURE_CLOSE'});201- assertStreamError(t, error);202-});203-204-const 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-};210-211-test('Handles input streams aborts', testStreamAbort, noopReadable(), 0);212-test('Handles input Duplex streams aborts', testStreamAbort, noopDuplex(), 0);213-test('Handles output streams aborts', testStreamAbort, noopWritable(), 1);214-test('Handles output Duplex streams aborts', testStreamAbort, noopDuplex(), 1);215-216-const 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-};223-224-test('Handles input streams errors', testStreamError, noopReadable(), 0);225-test('Handles input Duplex streams errors', testStreamError, noopDuplex(), 0);226-test('Handles output streams errors', testStreamError, noopWritable(), 1);227-test('Handles output Duplex streams errors', testStreamError, noopDuplex(), 1);228-229-const testChildStreamEnd = async (t, stream) => {230- const childProcess = execa('stdin.js', {stdin: [stream, 'pipe']});231- childProcess.stdin.end();232- await setImmediate();233- stream.destroy();234-235- const error = await t.throwsAsync(childProcess, {code: 'ERR_STREAM_PREMATURE_CLOSE'});236- assertStreamError(t, error);237- t.true(stream.destroyed);238-};239-240-test('Handles childProcess.stdin end', testChildStreamEnd, noopReadable());241-test('Handles childProcess.stdin Duplex end', testChildStreamEnd, noopDuplex());242-243-const 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-};250-251-test('Handles childProcess.stdin aborts', testChildStreamAbort, noopReadable(), 0);252-test('Handles childProcess.stdin Duplex aborts', testChildStreamAbort, noopDuplex(), 0);253-test('Handles childProcess.stdout aborts', testChildStreamAbort, noopWritable(), 1);254-test('Handles childProcess.stdout Duplex aborts', testChildStreamAbort, noopDuplex(), 1);255-256-const 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-};264-265-test('Handles childProcess.stdin errors', testChildStreamError, noopReadable(), 0);266-test('Handles childProcess.stdin Duplex errors', testChildStreamError, noopDuplex(), 0);267-test('Handles childProcess.stdout errors', testChildStreamError, noopWritable(), 1);268-test('Handles childProcess.stdout Duplex errors', testChildStreamError, noopDuplex(), 1);added:test/stdio/wait.js+228 added
1+import {platform} from 'node:process';2+import {setImmediate} from 'node:timers/promises';3+import test from 'ava';4+import {execa} from '../../index.js';5+import {setFixtureDir} from '../helpers/fixtures-dir.js';6+import {getStdio, prematureClose} from '../helpers/stdio.js';7+import {foobarString} from '../helpers/input.js';8+import {noopGenerator} from '../helpers/generator.js';9+import {noopReadable, noopWritable, noopDuplex} from '../helpers/stream.js';10+11+setFixtureDir();12+13+const isWindows = platform === 'win32';14+15+const noop = () => {};16+17+const endOptionStream = ({stream}) => {18+ stream.end();19+};20+21+const destroyOptionStream = ({stream, error}) => {22+ stream.destroy(error);23+};24+25+const destroyChildStream = ({childProcess, index, error}) => {26+ childProcess.stdio[index].destroy(error);27+};28+29+const getStreamStdio = (index, stream, useTransform) => getStdio(index, [stream, useTransform ? noopGenerator(false) : 'pipe']);30+31+// eslint-disable-next-line max-params32+const 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();38+39+ const {stdout} = await childProcess;40+ t.is(stdout, '');41+ t.true(stream.destroyed);42+};43+44+test('Keeps running when stdin option is used and childProcess.stdin ends', testStreamAbortWait, noop, noopReadable(), 0, false);45+test('Keeps running when stdin option is used and childProcess.stdin Duplex ends', testStreamAbortWait, noop, noopDuplex(), 0, false);46+test('Keeps running when input stdio[*] option is used and input childProcess.stdio[*] ends', testStreamAbortWait, noop, noopReadable(), 3, false);47+test('Keeps running when stdin option is used and childProcess.stdin ends, with a transform', testStreamAbortWait, noop, noopReadable(), 0, true);48+test('Keeps running when stdin option is used and childProcess.stdin Duplex ends, with a transform', testStreamAbortWait, noop, noopDuplex(), 0, true);49+test('Keeps running when input stdio[*] option is used and input childProcess.stdio[*] ends, with a transform', testStreamAbortWait, noop, noopReadable(), 3, true);50+51+// eslint-disable-next-line max-params52+const 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();56+57+ const {stdout} = await childProcess;58+ t.is(stdout, '');59+ t.true(stream.destroyed);60+};61+62+test('Passes when stdin option aborts', testStreamAbortSuccess, destroyOptionStream, noopReadable(), 0, false);63+test('Passes when stdin option Duplex aborts', testStreamAbortSuccess, destroyOptionStream, noopDuplex(), 0, false);64+test('Passes when input stdio[*] option aborts', testStreamAbortSuccess, destroyOptionStream, noopReadable(), 3, false);65+test('Passes when stdout option ends with no more writes', testStreamAbortSuccess, endOptionStream, noopWritable(), 1, false);66+test('Passes when stderr option ends with no more writes', testStreamAbortSuccess, endOptionStream, noopWritable(), 2, false);67+test('Passes when output stdio[*] option ends with no more writes', testStreamAbortSuccess, endOptionStream, noopWritable(), 3, false);68+test('Passes when childProcess.stdout aborts with no more writes', testStreamAbortSuccess, destroyChildStream, noopWritable(), 1, false);69+test('Passes when childProcess.stdout Duplex aborts with no more writes', testStreamAbortSuccess, destroyChildStream, noopDuplex(), 1, false);70+test('Passes when childProcess.stderr aborts with no more writes', testStreamAbortSuccess, destroyChildStream, noopWritable(), 2, false);71+test('Passes when childProcess.stderr Duplex aborts with no more writes', testStreamAbortSuccess, destroyChildStream, noopDuplex(), 2, false);72+test('Passes when output childProcess.stdio[*] aborts with no more writes', testStreamAbortSuccess, destroyChildStream, noopWritable(), 3, false);73+test('Passes when output childProcess.stdio[*] Duplex aborts with no more writes', testStreamAbortSuccess, destroyChildStream, noopDuplex(), 3, false);74+test('Passes when stdin option aborts, with a transform', testStreamAbortSuccess, destroyOptionStream, noopReadable(), 0, true);75+test('Passes when stdin option Duplex aborts, with a transform', testStreamAbortSuccess, destroyOptionStream, noopDuplex(), 0, true);76+test('Passes when input stdio[*] option aborts, with a transform', testStreamAbortSuccess, destroyOptionStream, noopReadable(), 3, true);77+test('Passes when stdout option ends with no more writes, with a transform', testStreamAbortSuccess, endOptionStream, noopWritable(), 1, true);78+test('Passes when stderr option ends with no more writes, with a transform', testStreamAbortSuccess, endOptionStream, noopWritable(), 2, true);79+test('Passes when output stdio[*] option ends with no more writes, with a transform', testStreamAbortSuccess, endOptionStream, noopWritable(), 3, true);80+test('Passes when childProcess.stdout aborts with no more writes, with a transform', testStreamAbortSuccess, destroyChildStream, noopWritable(), 1, true);81+test('Passes when childProcess.stdout Duplex aborts with no more writes, with a transform', testStreamAbortSuccess, destroyChildStream, noopDuplex(), 1, true);82+test('Passes when childProcess.stderr aborts with no more writes, with a transform', testStreamAbortSuccess, destroyChildStream, noopWritable(), 2, true);83+test('Passes when childProcess.stderr Duplex aborts with no more writes, with a transform', testStreamAbortSuccess, destroyChildStream, noopDuplex(), 2, true);84+test('Passes when output childProcess.stdio[*] aborts with no more writes, with a transform', testStreamAbortSuccess, destroyChildStream, noopWritable(), 3, true);85+test('Passes when output childProcess.stdio[*] Duplex aborts with no more writes, with a transform', testStreamAbortSuccess, destroyChildStream, noopDuplex(), 3, true);86+87+// eslint-disable-next-line max-params88+const 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+ }94+95+ const error = await t.throwsAsync(childProcess);96+ t.like(error, {...prematureClose, exitCode: 0});97+ t.true(stream.destroyed);98+};99+100+test('Throws abort error when childProcess.stdin aborts', testStreamAbortFail, destroyChildStream, noopReadable(), 0, false);101+test('Throws abort error when childProcess.stdin Duplex aborts', testStreamAbortFail, destroyChildStream, noopDuplex(), 0, false);102+test('Throws abort error when input childProcess.stdio[*] aborts', testStreamAbortFail, destroyChildStream, noopReadable(), 3, false);103+test('Throws abort error when stdout option aborts with no more writes', testStreamAbortFail, destroyOptionStream, noopWritable(), 1, false);104+test('Throws abort error when stdout option Duplex aborts with no more writes', testStreamAbortFail, destroyOptionStream, noopDuplex(), 1, false);105+test('Throws abort error when stderr option aborts with no more writes', testStreamAbortFail, destroyOptionStream, noopWritable(), 2, false);106+test('Throws abort error when stderr option Duplex aborts with no more writes', testStreamAbortFail, destroyOptionStream, noopDuplex(), 2, false);107+test('Throws abort error when output stdio[*] option aborts with no more writes', testStreamAbortFail, destroyOptionStream, noopWritable(), 3, false);108+test('Throws abort error when output stdio[*] Duplex option aborts with no more writes', testStreamAbortFail, destroyOptionStream, noopDuplex(), 3, false);109+test('Throws abort error when childProcess.stdin aborts, with a transform', testStreamAbortFail, destroyChildStream, noopReadable(), 0, true);110+test('Throws abort error when childProcess.stdin Duplex aborts, with a transform', testStreamAbortFail, destroyChildStream, noopDuplex(), 0, true);111+test('Throws abort error when input childProcess.stdio[*] aborts, with a transform', testStreamAbortFail, destroyChildStream, noopReadable(), 3, true);112+test('Throws abort error when stdout option aborts with no more writes, with a transform', testStreamAbortFail, destroyOptionStream, noopWritable(), 1, true);113+test('Throws abort error when stdout option Duplex aborts with no more writes, with a transform', testStreamAbortFail, destroyOptionStream, noopDuplex(), 1, true);114+test('Throws abort error when stderr option aborts with no more writes, with a transform', testStreamAbortFail, destroyOptionStream, noopWritable(), 2, true);115+test('Throws abort error when stderr option Duplex aborts with no more writes, with a transform', testStreamAbortFail, destroyOptionStream, noopDuplex(), 2, true);116+test('Throws abort error when output stdio[*] option aborts with no more writes, with a transform', testStreamAbortFail, destroyOptionStream, noopWritable(), 3, true);117+test('Throws abort error when output stdio[*] Duplex option aborts with no more writes, with a transform', testStreamAbortFail, destroyOptionStream, noopDuplex(), 3, true);118+119+// eslint-disable-next-line max-params120+const 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);124+125+ const {stdio} = await childProcess;126+ t.is(stdio[index], foobarString);127+ t.true(stream.destroyed);128+};129+130+test('Passes when stdout option ends with more writes', testStreamEpipeSuccess, endOptionStream, noopWritable(), 1, false);131+test('Passes when stderr option ends with more writes', testStreamEpipeSuccess, endOptionStream, noopWritable(), 2, false);132+test('Passes when output stdio[*] option ends with more writes', testStreamEpipeSuccess, endOptionStream, noopWritable(), 3, false);133+test('Passes when stdout option ends with more writes, with a transform', testStreamEpipeSuccess, endOptionStream, noopWritable(), 1, true);134+test('Passes when stderr option ends with more writes, with a transform', testStreamEpipeSuccess, endOptionStream, noopWritable(), 2, true);135+test('Passes when output stdio[*] option ends with more writes, with a transform', testStreamEpipeSuccess, endOptionStream, noopWritable(), 3, true);136+137+// eslint-disable-next-line max-params138+const 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);143+144+ 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+};152+153+test('Throws EPIPE when stdout option aborts with more writes', testStreamEpipeFail, destroyOptionStream, noopWritable(), 1, false);154+test('Throws EPIPE when stdout option Duplex aborts with more writes', testStreamEpipeFail, destroyOptionStream, noopDuplex(), 1, false);155+test('Throws EPIPE when stderr option aborts with more writes', testStreamEpipeFail, destroyOptionStream, noopWritable(), 2, false);156+test('Throws EPIPE when stderr option Duplex aborts with more writes', testStreamEpipeFail, destroyOptionStream, noopDuplex(), 2, false);157+test('Throws EPIPE when output stdio[*] option aborts with more writes', testStreamEpipeFail, destroyOptionStream, noopWritable(), 3, false);158+test('Throws EPIPE when output stdio[*] option Duplex aborts with more writes', testStreamEpipeFail, destroyOptionStream, noopDuplex(), 3, false);159+test('Throws EPIPE when childProcess.stdout aborts with more writes', testStreamEpipeFail, destroyChildStream, noopWritable(), 1, false);160+test('Throws EPIPE when childProcess.stdout Duplex aborts with more writes', testStreamEpipeFail, destroyChildStream, noopDuplex(), 1, false);161+test('Throws EPIPE when childProcess.stderr aborts with more writes', testStreamEpipeFail, destroyChildStream, noopWritable(), 2, false);162+test('Throws EPIPE when childProcess.stderr Duplex aborts with more writes', testStreamEpipeFail, destroyChildStream, noopDuplex(), 2, false);163+test('Throws EPIPE when output childProcess.stdio[*] aborts with more writes', testStreamEpipeFail, destroyChildStream, noopWritable(), 3, false);164+test('Throws EPIPE when output childProcess.stdio[*] Duplex aborts with more writes', testStreamEpipeFail, destroyChildStream, noopDuplex(), 3, false);165+test('Throws EPIPE when stdout option aborts with more writes, with a transform', testStreamEpipeFail, destroyOptionStream, noopWritable(), 1, true);166+test('Throws EPIPE when stdout option Duplex aborts with more writes, with a transform', testStreamEpipeFail, destroyOptionStream, noopDuplex(), 1, true);167+test('Throws EPIPE when stderr option aborts with more writes, with a transform', testStreamEpipeFail, destroyOptionStream, noopWritable(), 2, true);168+test('Throws EPIPE when stderr option Duplex aborts with more writes, with a transform', testStreamEpipeFail, destroyOptionStream, noopDuplex(), 2, true);169+test('Throws EPIPE when output stdio[*] option aborts with more writes, with a transform', testStreamEpipeFail, destroyOptionStream, noopWritable(), 3, true);170+test('Throws EPIPE when output stdio[*] option Duplex aborts with more writes, with a transform', testStreamEpipeFail, destroyOptionStream, noopDuplex(), 3, true);171+test('Throws EPIPE when childProcess.stdout aborts with more writes, with a transform', testStreamEpipeFail, destroyChildStream, noopWritable(), 1, true);172+test('Throws EPIPE when childProcess.stdout Duplex aborts with more writes, with a transform', testStreamEpipeFail, destroyChildStream, noopDuplex(), 1, true);173+test('Throws EPIPE when childProcess.stderr aborts with more writes, with a transform', testStreamEpipeFail, destroyChildStream, noopWritable(), 2, true);174+test('Throws EPIPE when childProcess.stderr Duplex aborts with more writes, with a transform', testStreamEpipeFail, destroyChildStream, noopDuplex(), 2, true);175+test('Throws EPIPE when output childProcess.stdio[*] aborts with more writes, with a transform', testStreamEpipeFail, destroyChildStream, noopWritable(), 3, true);176+test('Throws EPIPE when output childProcess.stdio[*] Duplex aborts with more writes, with a transform', testStreamEpipeFail, destroyChildStream, noopDuplex(), 3, true);177+178+// eslint-disable-next-line max-params179+const 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});183+184+ 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+};192+193+test('Throws stream error when stdin option errors', testStreamError, destroyOptionStream, noopReadable(), 0, false);194+test('Throws stream error when stdin option Duplex errors', testStreamError, destroyOptionStream, noopDuplex(), 0, false);195+test('Throws stream error when stdout option errors', testStreamError, destroyOptionStream, noopWritable(), 1, false);196+test('Throws stream error when stdout option Duplex errors', testStreamError, destroyOptionStream, noopDuplex(), 1, false);197+test('Throws stream error when stderr option errors', testStreamError, destroyOptionStream, noopWritable(), 2, false);198+test('Throws stream error when stderr option Duplex errors', testStreamError, destroyOptionStream, noopDuplex(), 2, false);199+test('Throws stream error when output stdio[*] option errors', testStreamError, destroyOptionStream, noopWritable(), 3, false);200+test('Throws stream error when output stdio[*] Duplex option errors', testStreamError, destroyOptionStream, noopDuplex(), 3, false);201+test('Throws stream error when input stdio[*] option errors', testStreamError, destroyOptionStream, noopReadable(), 3, false);202+test('Throws stream error when childProcess.stdin errors', testStreamError, destroyChildStream, noopReadable(), 0, false);203+test('Throws stream error when childProcess.stdin Duplex errors', testStreamError, destroyChildStream, noopDuplex(), 0, false);204+test('Throws stream error when childProcess.stdout errors', testStreamError, destroyChildStream, noopWritable(), 1, false);205+test('Throws stream error when childProcess.stdout Duplex errors', testStreamError, destroyChildStream, noopDuplex(), 1, false);206+test('Throws stream error when childProcess.stderr errors', testStreamError, destroyChildStream, noopWritable(), 2, false);207+test('Throws stream error when childProcess.stderr Duplex errors', testStreamError, destroyChildStream, noopDuplex(), 2, false);208+test('Throws stream error when output childProcess.stdio[*] errors', testStreamError, destroyChildStream, noopWritable(), 3, false);209+test('Throws stream error when output childProcess.stdio[*] Duplex errors', testStreamError, destroyChildStream, noopDuplex(), 3, false);210+test('Throws stream error when input childProcess.stdio[*] errors', testStreamError, destroyChildStream, noopReadable(), 3, false);211+test('Throws stream error when stdin option errors, with a transform', testStreamError, destroyOptionStream, noopReadable(), 0, true);212+test('Throws stream error when stdin option Duplex errors, with a transform', testStreamError, destroyOptionStream, noopDuplex(), 0, true);213+test('Throws stream error when stdout option errors, with a transform', testStreamError, destroyOptionStream, noopWritable(), 1, true);214+test('Throws stream error when stdout option Duplex errors, with a transform', testStreamError, destroyOptionStream, noopDuplex(), 1, true);215+test('Throws stream error when stderr option errors, with a transform', testStreamError, destroyOptionStream, noopWritable(), 2, true);216+test('Throws stream error when stderr option Duplex errors, with a transform', testStreamError, destroyOptionStream, noopDuplex(), 2, true);217+test('Throws stream error when output stdio[*] option errors, with a transform', testStreamError, destroyOptionStream, noopWritable(), 3, true);218+test('Throws stream error when output stdio[*] Duplex option errors, with a transform', testStreamError, destroyOptionStream, noopDuplex(), 3, true);219+test('Throws stream error when input stdio[*] option errors, with a transform', testStreamError, destroyOptionStream, noopReadable(), 3, true);220+test('Throws stream error when childProcess.stdin errors, with a transform', testStreamError, destroyChildStream, noopReadable(), 0, true);221+test('Throws stream error when childProcess.stdin Duplex errors, with a transform', testStreamError, destroyChildStream, noopDuplex(), 0, true);222+test('Throws stream error when childProcess.stdout errors, with a transform', testStreamError, destroyChildStream, noopWritable(), 1, true);223+test('Throws stream error when childProcess.stdout Duplex errors, with a transform', testStreamError, destroyChildStream, noopDuplex(), 1, true);224+test('Throws stream error when childProcess.stderr errors, with a transform', testStreamError, destroyChildStream, noopWritable(), 2, true);225+test('Throws stream error when childProcess.stderr Duplex errors, with a transform', testStreamError, destroyChildStream, noopDuplex(), 2, true);226+test('Throws stream error when output childProcess.stdio[*] errors, with a transform', testStreamError, destroyChildStream, noopWritable(), 3, true);227+test('Throws stream error when output childProcess.stdio[*] Duplex errors, with a transform', testStreamError, destroyChildStream, noopDuplex(), 3, true);228+test('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 lines6 import test from 'ava';7 import getStream from 'get-stream';8 import {execa, execaSync} from '../index.js';9 import {setFixtureDir} from './helpers/fixtures-dir.js';10-import {fullStdio, getStdio} from './helpers/stdio.js';10+import {fullStdio, getStdio, prematureClose} from './helpers/stdio.js';11 import {foobarString} from './helpers/input.js';12 import {infiniteGenerator} from './helpers/generator.js';13 14 setFixtureDir();15 16+const isWindows = platform === 'win32';17+18 test.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 lines316 317 const getStreamInputProcess = index => execa('stdin-fd.js', [`${index}`], index === 3318 ? getStdio(3, [new Uint8Array(), infiniteGenerator])319 : {});318-const getStreamOutputProcess = index => execa('max-buffer.js', [`${index}`], index === 3 ? fullStdio : {});320+const getStreamOutputProcess = index => execa('noop-repeat.js', [`${index}`], index === 3 ? fullStdio : {});321 322 const assertStreamInputError = (t, {exitCode, signal, isTerminated, failed}) => {323 t.is(exitCode, 0);324 t.is(signal, undefined);9 unmodified lines334 t.is(signal, undefined);335 t.false(isTerminated);336 t.true(failed);337 336- if (index === 1 && platform !== 'win32') {338+ if (index === 1 && !isWindows) {339 t.true(stderr.includes('EPIPE'));340 }341 };342 343 const 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 };349 350 test('Aborting stdin should not make the process exit', testStreamInputAbort, 0);122 unmodified lines