Skip to content
Myr

Do not prevent process normal exit (#809)

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

authored by ehmicky · committed by GitHub · parent 8ff263c · Browse files · 11 files+201 added−143 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+201 added−143 removed

  • tests:+172 added−126 removed
  • other:+29 added−17 removed

11 files+201 added−143 removed

modified:lib/error.js+1 added-1 removed
modified:lib/kill.js+4 added-3 removed
61 unmodified lines
6263const isProcessErrorExit = (exitCode, signal) => exitCode === undefined && signal === undefined;64export const isFailedExit = (exitCode, signal) => exitCode !== 0 || signal !== null;6566const killAfterTimeout = async (timeout, context, {signal}) => {66const killAfterTimeout = async (spawned, timeout, context, {signal}) => {67 await setTimeout(timeout, undefined, {signal});68 context.timedOut = true;69 spawned.kill();70 const message = getErrorPrefix({timedOut: true, timeout});71 throw setDiscardedError(new Error(message));72};7374// `timeout` option handling74export const throwOnTimeout = (timeout, context, controller) => timeout === 0 || timeout === undefined75export const throwOnTimeout = (spawned, timeout, context, controller) => timeout === 0 || timeout === undefined76 ? []76 : [killAfterTimeout(timeout, context, controller)];77 : [killAfterTimeout(spawned, timeout, context, controller)];7879export const validateTimeout = ({timeout}) => {80 if (timeout !== undefined && (!Number.isFinite(timeout) || timeout < 0)) {81 throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`);
19 unmodified lines
modified:lib/stream.js+24 added-12 removed
1import {once} from 'node:events';2import {finished} from 'node:stream/promises';3import {setImmediate} from 'node:timers/promises';4import getStream, {getStreamAsArrayBuffer, getStreamAsArray} from 'get-stream';4import getStream, {getStreamAsArrayBuffer, getStreamAsArray, MaxBufferError} from 'get-stream';5import mergeStreams from '@sindresorhus/merge-streams';6import {waitForSuccessfulExit, throwOnTimeout} from './kill.js';7import {isStandardStream} from './stdio/utils.js';8import {generatorToDuplexStream} from './stdio/generator.js';
4 unmodified lines
13 : undefined;1415// On failure, `result.stdout|stderr|all` should contain the currently buffered stream16// They are automatically closed and flushed by Node.js when the child process exits17// We guarantee this by calling `childProcess.kill()`17// When `buffer` is `false`, `streamPromise` is `undefined` and there is no buffered data to retrieve18const getBufferedData = async (streamPromise, encoding) => {19 try {20 return await streamPromise;
6 unmodified lines
2728// 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({30 stream,31 spawned,32 direction: stdioStreamsGroups[index][0].direction,33 encoding,34 buffer,35 maxBuffer,
2 unmodified lines
3839// Read the contents of `childProcess.all` and|or wait for its completion40const waitForAllStream = ({spawned, encoding, buffer, maxBuffer, waitForStream}) => waitForChildStream({41 stream: getAllStream(spawned, encoding),42 spawned,43 direction: 'output',44 encoding,45 buffer,46 maxBuffer: maxBuffer * 2,
16 unmodified lines
63 writableObjectMode: true,64 readableObjectMode: true,65};6666const waitForChildStream = async ({stream, direction, encoding, buffer, maxBuffer, waitForStream}) => {67const waitForChildStream = async ({stream, spawned, direction, encoding, buffer, maxBuffer, waitForStream}) => {68 if (!stream) {69 return;70 }71
9 unmodified lines
81 ]);82 return;83 }8484 if (stream.readableObjectMode) {85 return getStreamAsArray(stream, {maxBuffer});86 }85 try {86 return await getAnyStream(stream, encoding, maxBuffer);87 } catch (error) {88 if (error instanceof MaxBufferError) {89 spawned.kill();90 }9188 const contents = encoding === 'buffer'89 ? await getStreamAsArrayBuffer(stream, {maxBuffer})90 : await getStream(stream, {maxBuffer});91 return applyEncoding(contents, encoding);92 throw error;93 }94};9596// When using `buffer: false`, users need to read `childProcess.stdout|stderr|all` right away97// See https://github.com/sindresorhus/execa/issues/730 and https://github.com/sindresorhus/execa/pull/729#discussion_r1465496310
3 unmodified lines
101 stream.resume();102 }103};104105const getAnyStream = async (stream, encoding, maxBuffer) => {106 if (stream.readableObjectMode) {107 return getStreamAsArray(stream, {maxBuffer});108 }109110 const contents = encoding === 'buffer'111 ? await getStreamAsArrayBuffer(stream, {maxBuffer})112 : await getStream(stream, {maxBuffer});113 return applyEncoding(contents, encoding);114};115116const applyEncoding = (contents, encoding) => encoding === 'buffer' ? new Uint8Array(contents) : contents;117118// Transforms replace `childProcess.std*`, which means they are not exposed to users.119// However, we still want to wait for their completion.
80 unmodified lines
200 ...originalPromises,201 ...customStreamsEndPromises,202 ]),203 throwOnProcessError(spawned, controller),191 ...throwOnTimeout(timeout, context, controller),204 ...throwOnTimeout(spawned, timeout, context, controller),205 ]);206 } catch (error) {194 spawned.kill();207 return Promise.all([208 {error},209 exitPromise,210 Promise.all(stdioPromises.map(stdioPromise => getBufferedData(stdioPromise, encoding))),
6 unmodified lines
modified:test/stdio/array.js+1 added
262 unmodified lines
263const testDestroyStandardStream = async (t, index) => {264 const childProcess = execa('forever.js', getStdio(index, [STANDARD_STREAMS[index], 'pipe']));265 const error = new Error('test');266 childProcess.stdio[index].destroy(error);267 childProcess.kill();268 const thrownError = await t.throwsAsync(childProcess);269 t.is(thrownError, error);270 t.false(STANDARD_STREAMS[index].destroyed);271};
4 unmodified lines
modified:test/stdio/file-path.js+12 added-12 removed
137 unmodified lines
138test('stdout be an object when it is a file path string - sync', testFilePathObject, 1, execaSync);139test('stderr be an object when it is a file path string - sync', testFilePathObject, 2, execaSync);140test('stdio[*] must be an object when it is a file path string - sync', testFilePathObject, 3, execaSync);141142const testFileError = async (t, mapFile, index) => {142const testFileError = async (t, fixtureName, mapFile, index) => {143 await t.throwsAsync(144 execa('forever.js', getStdio(index, mapFile('./unknown/file'))),144 execa(fixtureName, [`${index}`], getStdio(index, mapFile('./unknown/file'))),145 {code: 'ENOENT'},146 );147};148149test('inputFile file URL errors should be handled', testFileError, pathToFileURL, 'inputFile');150test('stdin file URL errors should be handled', testFileError, pathToFileURL, 0);151test('stdout file URL errors should be handled', testFileError, pathToFileURL, 1);152test('stderr file URL errors should be handled', testFileError, pathToFileURL, 2);153test('stdio[*] file URL errors should be handled', testFileError, pathToFileURL, 3);154test('inputFile file path errors should be handled', testFileError, identity, 'inputFile');155test('stdin file path errors should be handled', testFileError, getAbsolutePath, 0);156test('stdout file path errors should be handled', testFileError, getAbsolutePath, 1);157test('stderr file path errors should be handled', testFileError, getAbsolutePath, 2);158test('stdio[*] file path errors should be handled', testFileError, getAbsolutePath, 3);149test.serial('inputFile file URL errors should be handled', testFileError, 'stdin-fd.js', pathToFileURL, 'inputFile');150test.serial('stdin file URL errors should be handled', testFileError, 'stdin-fd.js', pathToFileURL, 0);151test.serial('stdout file URL errors should be handled', testFileError, 'noop-fd.js', pathToFileURL, 1);152test.serial('stderr file URL errors should be handled', testFileError, 'noop-fd.js', pathToFileURL, 2);153test.serial('stdio[*] file URL errors should be handled', testFileError, 'noop-fd.js', pathToFileURL, 3);154test.serial('inputFile file path errors should be handled', testFileError, 'stdin-fd.js', identity, 'inputFile');155test.serial('stdin file path errors should be handled', testFileError, 'stdin-fd.js', getAbsolutePath, 0);156test.serial('stdout file path errors should be handled', testFileError, 'noop-fd.js', getAbsolutePath, 1);157test.serial('stderr file path errors should be handled', testFileError, 'noop-fd.js', getAbsolutePath, 2);158test.serial('stdio[*] file path errors should be handled', testFileError, 'noop-fd.js', getAbsolutePath, 3);159160const testFileErrorSync = (t, mapFile, index) => {161 t.throws(() => {162 execaSync('empty.js', getStdio(index, mapFile('./unknown/file')));
51 unmodified lines
modified:test/stdio/generator.js+1 added
717 unmodified lines
718const testGeneratorDestroy = async (t, transform) => {719 const childProcess = execa('forever.js', {stdout: transform});720 const error = new Error('test');721 childProcess.stdout.destroy(error);722 childProcess.kill();723 t.is(await t.throwsAsync(childProcess), error);724};725726test('Generators are destroyed on process error, sync', testGeneratorDestroy, noopGenerator(false));
5 unmodified lines
modified:test/stdio/iterable.js+8 added-8 removed
32 unmodified lines
33 const {stdout} = await execa('stdin-fd.js', [`${index}`], getStdio(index, stdioOption));34 t.is(stdout, 'foobar');35};3637test('stdin option can be an array of strings', testIterable, [stringArray], 0);38test('stdio[*] option can be an array of strings', testIterable, [stringArray], 3);39test('stdin option can be an array of Uint8Arrays', testIterable, [binaryArray], 0);40test('stdio[*] option can be an array of Uint8Arrays', testIterable, [binaryArray], 3);41test('stdin option can be an iterable of strings', testIterable, stringGenerator(), 0);42test('stdio[*] option can be an iterable of strings', testIterable, stringGenerator(), 3);43test('stdin option can be an iterable of Uint8Arrays', testIterable, binaryGenerator(), 0);44test('stdio[*] option can be an iterable of Uint8Arrays', testIterable, binaryGenerator(), 3);37test.serial('stdin option can be an array of strings', testIterable, [stringArray], 0);38test.serial('stdio[*] option can be an array of strings', testIterable, [stringArray], 3);39test.serial('stdin option can be an array of Uint8Arrays', testIterable, [binaryArray], 0);40test.serial('stdio[*] option can be an array of Uint8Arrays', testIterable, [binaryArray], 3);41test.serial('stdin option can be an iterable of strings', testIterable, stringGenerator(), 0);42test.serial('stdio[*] option can be an iterable of strings', testIterable, stringGenerator(), 3);43test.serial('stdin option can be an iterable of Uint8Arrays', testIterable, binaryGenerator(), 0);44test.serial('stdio[*] option can be an iterable of Uint8Arrays', testIterable, binaryGenerator(), 3);45test.serial('stdin option can be an async iterable', testIterable, asyncGenerator(), 0);46test.serial('stdio[*] option can be an async iterable', testIterable, asyncGenerator(), 3);4748const foobarObjectGenerator = function * () {
84 unmodified lines
modified:test/stdio/node-stream.js+49 added-39 removed
179 unmodified lines
180test('Can pass Duplex streams to stdout', testOutputDuplexStream, 1);181test('Can pass Duplex streams to stderr', testOutputDuplexStream, 2);182test('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 stream.end();187 await t.throwsAsync(188 execa('forever.js', {stdout: [stream, 'pipe']}),189 {code: 'ERR_STREAM_PREMATURE_CLOSE'},190 );197 childProcess.stdin.end();198 const error = await t.throwsAsync(childProcess, {code: 'ERR_STREAM_PREMATURE_CLOSE'});199 assertStreamError(t, error);200});201193const testStreamAbort = async (t, stream, streamName) => {202const testStreamAbort = async (t, stream, index) => {203 const childProcess = execa(getStreamFixtureName(index), getStdio(index, [stream, 'pipe']));204 stream.destroy();195 await t.throwsAsync(196 execa('forever.js', {[streamName]: [stream, 'pipe']}),197 {code: 'ERR_STREAM_PREMATURE_CLOSE'},198 );205 const error = await t.throwsAsync(childProcess, {code: 'ERR_STREAM_PREMATURE_CLOSE'});206 assertStreamError(t, error);207};208201test('Handles input streams aborts', testStreamAbort, noopReadable(), 'stdin');202test('Handles input Duplex streams aborts', testStreamAbort, noopDuplex(), 'stdin');203test('Handles output streams aborts', testStreamAbort, noopWritable(), 'stdout');204test('Handles output Duplex streams aborts', testStreamAbort, noopDuplex(), 'stdout');209test('Handles input streams aborts', testStreamAbort, noopReadable(), 0);210test('Handles input Duplex streams aborts', testStreamAbort, noopDuplex(), 0);211test('Handles output streams aborts', testStreamAbort, noopWritable(), 1);212test('Handles output Duplex streams aborts', testStreamAbort, noopDuplex(), 1);213206const testStreamError = async (t, stream, streamName) => {214const testStreamError = async (t, stream, index) => {215 const childProcess = execa(getStreamFixtureName(index), getStdio(index, [stream, 'pipe']));216 const error = new Error('test');217 stream.destroy(error);209 t.is(210 await t.throwsAsync(execa('forever.js', {[streamName]: [stream, 'pipe']})),211 error,212 );218 t.is(await t.throwsAsync(childProcess), error);219 assertStreamError(t, error);220};221215test('Handles input streams errors', testStreamError, noopReadable(), 'stdin');216test('Handles input Duplex streams errors', testStreamError, noopDuplex(), 'stdin');217test('Handles output streams errors', testStreamError, noopWritable(), 'stdout');218test('Handles output Duplex streams errors', testStreamError, noopDuplex(), 'stdout');222test('Handles input streams errors', testStreamError, noopReadable(), 0);223test('Handles input Duplex streams errors', testStreamError, noopDuplex(), 0);224test('Handles output streams errors', testStreamError, noopWritable(), 1);225test('Handles output Duplex streams errors', testStreamError, noopDuplex(), 1);226227const testChildStreamEnd = async (t, stream) => {221 const childProcess = execa('forever.js', {stdin: [stream, 'pipe']});228 const childProcess = execa('stdin.js', {stdin: [stream, 'pipe']});229 childProcess.stdin.end();223 await t.throwsAsync(childProcess, {code: 'ERR_STREAM_PREMATURE_CLOSE'});230 const error = await t.throwsAsync(childProcess, {code: 'ERR_STREAM_PREMATURE_CLOSE'});231 assertStreamError(t, error);232 t.true(stream.destroyed);233};234235test('Handles childProcess.stdin end', testChildStreamEnd, noopReadable());236test('Handles childProcess.stdin Duplex end', testChildStreamEnd, noopDuplex());237230const testChildStreamAbort = async (t, stream, streamName) => {231 const childProcess = execa('forever.js', {[streamName]: [stream, 'pipe']});232 childProcess[streamName].destroy();233 await t.throwsAsync(childProcess, {code: 'ERR_STREAM_PREMATURE_CLOSE'});238const testChildStreamAbort = async (t, stream, index) => {239 const childProcess = execa(getStreamFixtureName(index), getStdio(index, [stream, 'pipe']));240 childProcess.stdio[index].destroy();241 const error = await t.throwsAsync(childProcess, {code: 'ERR_STREAM_PREMATURE_CLOSE'});242 assertStreamError(t, error);243 t.true(stream.destroyed);244};245237test('Handles childProcess.stdin aborts', testChildStreamAbort, noopReadable(), 'stdin');238test('Handles childProcess.stdin Duplex aborts', testChildStreamAbort, noopDuplex(), 'stdin');239test('Handles childProcess.stdout aborts', testChildStreamAbort, noopWritable(), 'stdout');240test('Handles childProcess.stdout Duplex aborts', testChildStreamAbort, noopDuplex(), 'stdout');246test('Handles childProcess.stdin aborts', testChildStreamAbort, noopReadable(), 0);247test('Handles childProcess.stdin Duplex aborts', testChildStreamAbort, noopDuplex(), 0);248test('Handles childProcess.stdout aborts', testChildStreamAbort, noopWritable(), 1);249test('Handles childProcess.stdout Duplex aborts', testChildStreamAbort, noopDuplex(), 1);250242const testChildStreamError = async (t, stream, streamName) => {243 const childProcess = execa('forever.js', {[streamName]: [stream, 'pipe']});251const testChildStreamError = async (t, stream, index) => {252 const childProcess = execa(getStreamFixtureName(index), getStdio(index, [stream, 'pipe']));253 const error = new Error('test');245 childProcess[streamName].destroy(error);254 childProcess.stdio[index].destroy(error);255 t.is(await t.throwsAsync(childProcess), error);256 assertStreamError(t, error);257 t.true(stream.destroyed);258};259250test('Handles childProcess.stdin errors', testChildStreamError, noopReadable(), 'stdin');251test('Handles childProcess.stdin Duplex errors', testChildStreamError, noopDuplex(), 'stdin');252test('Handles childProcess.stdout errors', testChildStreamError, noopWritable(), 'stdout');253test('Handles childProcess.stdout Duplex errors', testChildStreamError, noopDuplex(), 'stdout');260test('Handles childProcess.stdin errors', testChildStreamError, noopReadable(), 0);261test('Handles childProcess.stdin Duplex errors', testChildStreamError, noopDuplex(), 0);262test('Handles childProcess.stdout errors', testChildStreamError, noopWritable(), 1);263test('Handles childProcess.stdout Duplex errors', testChildStreamError, noopDuplex(), 1);
modified:test/kill.js+11 added-18 removed
117 unmodified lines
118 t.true(isTerminated);119 t.is(signal, 'SIGKILL');120 });121122 test('`forceKillAfterDelay` works with "error" events on childProcess', async t => {123 const subprocess = spawnNoKillableSimple();124 await once(subprocess, 'spawn');125 subprocess.emit('error', new Error('test'));126 const {isTerminated, signal} = await t.throwsAsync(subprocess);127 t.true(isTerminated);128 t.is(signal, 'SIGKILL');129 });130131 test('`forceKillAfterDelay` works with "error" events on childProcess.stdout', async t => {132 const subprocess = spawnNoKillableSimple();133 await once(subprocess, 'spawn');134 subprocess.stdout.destroy(new Error('test'));135 const {isTerminated, signal} = await t.throwsAsync(subprocess);136 t.true(isTerminated);137 t.is(signal, 'SIGKILL');138 });139122 test.serial('Can call `.kill()` with `forceKillAfterDelay` many times without triggering the maxListeners warning', async t => {123 let warning;124 const captureWarning = warningArgument => {125 warning = warningArgument;
102 unmodified lines
228 const {timedOut} = execaSync('noop.js');229 t.false(timedOut);230});231232test('timedOut is true if the timeout happened after a different error occurred', async t => {233 const childProcess = execa('forever.js', {timeout: 1e3});234 const error = new Error('test');235 childProcess.emit('error', error);236 t.is(await t.throwsAsync(childProcess), error);237 t.true(error.timedOut);238});239240// When child process exits before parent process241const spawnAndExit = async (t, cleanup, detached) => {242 await t.notThrowsAsync(execa('nested.js', [JSON.stringify({cleanup, detached}), 'noop.js']));243};
137 unmodified lines
381test('child process errors are handled before spawn', async t => {382 const subprocess = execa('forever.js');383 const error = new Error('test');384 subprocess.emit('error', error);385 subprocess.kill();386 const thrownError = await t.throwsAsync(subprocess);387 t.is(thrownError, error);388 t.is(thrownError.exitCode, undefined);389 t.is(thrownError.signal, undefined);
4 unmodified lines
394 const subprocess = execa('forever.js');395 await once(subprocess, 'spawn');396 const error = new Error('test');397 subprocess.emit('error', error);398 subprocess.kill();399 const thrownError = await t.throwsAsync(subprocess);400 t.is(thrownError, error);401 t.is(thrownError.exitCode, undefined);402 t.is(thrownError.signal, 'SIGTERM');
19 unmodified lines
422 const subprocess = execa('forever.js', {killSignal: 'SIGINT'});423 await once(subprocess, 'spawn');424 const error = new Error('test');425 subprocess.emit('error', error);426 subprocess.kill();427 const thrownError = await t.throwsAsync(subprocess);428 t.is(thrownError, error);429 t.true(thrownError.isTerminated);430 t.is(thrownError.signal, 'SIGINT');
1 unmodified line
modified:test/stream.js+90 added-49 removed
1import {Buffer} from 'node:buffer';2import {once} from 'node:events';3import {platform} from 'node:process';4import {getDefaultHighWaterMark} from 'node:stream';5import {setTimeout} from 'node:timers/promises';6import test from 'ava';7import getStream from 'get-stream';
92 unmodified lines
100 t.is(allOutput, '');101 }102};103103test.serial('Lacks some data when stdout is read too late `buffer` set to `false`', testLateStream, 1, false);104test.serial('Lacks some data when stderr is read too late `buffer` set to `false`', testLateStream, 2, false);105test.serial('Lacks some data when stdio[*] is read too late `buffer` set to `false`', testLateStream, 3, false);106test.serial('Lacks some data when all is read too late `buffer` set to `false`', testLateStream, 1, true);104test('Lacks some data when stdout is read too late `buffer` set to `false`', testLateStream, 1, false);105test('Lacks some data when stderr is read too late `buffer` set to `false`', testLateStream, 2, false);106test('Lacks some data when stdio[*] is read too late `buffer` set to `false`', testLateStream, 3, false);107test('Lacks some data when all is read too late `buffer` set to `false`', testLateStream, 1, true);108109// eslint-disable-next-line max-params110const testIterationBuffer = async (t, index, buffer, useDataEvents, all) => {111 const subprocess = execa('noop-fd.js', [`${index}`, foobarString], {...fullStdio, buffer, all});
199 unmodified lines
311test('Process buffers stderr, which does not prevent exit if read and buffer is false', testBufferRead, 2, false);312test('Process buffers stdio[*], which does not prevent exit if read and buffer is false', testBufferRead, 3, false);313test('Process buffers all, which does not prevent exit if read and buffer is false', testBufferRead, 1, true);314314const getStreamDestroyOptions = (index, isInput) => {315const getStreamInputProcess = index => execa('stdin-fd.js', [`${index}`], index === 3316 ? getStdio(3, [new Uint8Array(), infiniteGenerator])317 : {});318const getStreamOutputProcess = index => execa('max-buffer.js', [`${index}`], index === 3 ? fullStdio : {});319320const assertStreamInputError = (t, {exitCode, signal, isTerminated, failed}) => {321 t.is(exitCode, 0);322 t.is(signal, undefined);323 t.false(isTerminated);324 t.true(failed);325};326327const assertStreamOutputError = (t, index, {exitCode, signal, isTerminated, failed, stderr}) => {328 if (index !== 3) {316 return {};329 t.is(exitCode, 1);330 }331319 return getStdio(3, isInput ? [new Uint8Array(), infiniteGenerator] : 'pipe');332 t.is(signal, undefined);333 t.false(isTerminated);334 t.true(failed);335336 if (index === 1 && platform !== 'win32') {337 t.true(stderr.includes('EPIPE'));338 }339};340322const testStreamAbort = async (t, index, isInput) => {323 const childProcess = execa('forever.js', getStreamDestroyOptions(index, isInput));341const testStreamInputAbort = async (t, index) => {342 const childProcess = getStreamInputProcess(index);343 childProcess.stdio[index].destroy();325 await t.throwsAsync(childProcess, {code: 'ERR_STREAM_PREMATURE_CLOSE'});344 const error = await t.throwsAsync(childProcess, {code: 'ERR_STREAM_PREMATURE_CLOSE'});345 assertStreamInputError(t, error);346};347328test('Aborting stdin should make the process exit', testStreamAbort, 0, true);329test('Aborting stdout should make the process exit', testStreamAbort, 1, false);330test('Aborting stderr should make the process exit', testStreamAbort, 2, false);331test('Aborting output stdio[*] should make the process exit', testStreamAbort, 3, false);332test('Aborting input stdio[*] should make the process exit', testStreamAbort, 3, true);348test('Aborting stdin should not make the process exit', testStreamInputAbort, 0);349test('Aborting input stdio[*] should not make the process exit', testStreamInputAbort, 3);350334const testStreamDestroy = async (t, index, isInput) => {335 const childProcess = execa('forever.js', getStreamDestroyOptions(index, isInput));351const testStreamOutputAbort = async (t, index) => {352 const childProcess = getStreamOutputProcess(index);353 childProcess.stdio[index].destroy();354 const error = await t.throwsAsync(childProcess);355 assertStreamOutputError(t, index, error);356};357358test('Aborting stdout should not make the process exit', testStreamOutputAbort, 1);359test('Aborting stderr should not make the process exit', testStreamOutputAbort, 2);360test('Aborting output stdio[*] should not make the process exit', testStreamOutputAbort, 3);361362const testStreamInputDestroy = async (t, index) => {363 const childProcess = getStreamInputProcess(index);364 const error = new Error('test');365 childProcess.stdio[index].destroy(error);366 t.is(await t.throwsAsync(childProcess), error);367 assertStreamInputError(t, error);368};369341test('Destroying stdin should make the process exit', testStreamDestroy, 0, true);342test('Destroying stdout should make the process exit', testStreamDestroy, 1, false);343test('Destroying stderr should make the process exit', testStreamDestroy, 2, false);344test('Destroying output stdio[*] should make the process exit', testStreamDestroy, 3, false);345test('Destroying input stdio[*] should make the process exit', testStreamDestroy, 3, true);370test('Destroying stdin should not make the process exit', testStreamInputDestroy, 0);371test('Destroying input stdio[*] should not make the process exit', testStreamInputDestroy, 3);372347const testStreamError = async (t, index, isInput) => {348 const childProcess = execa('forever.js', getStreamDestroyOptions(index, isInput));373const testStreamOutputDestroy = async (t, index) => {374 const childProcess = getStreamOutputProcess(index);375 const error = new Error('test');350 childProcess.stdio[index].emit('error', error);376 childProcess.stdio[index].destroy(error);377 t.is(await t.throwsAsync(childProcess), error);378 assertStreamOutputError(t, index, error);379};380354test('Errors on stdin should make the process exit', testStreamError, 0, true);355test('Errors on stdout should make the process exit', testStreamError, 1, false);356test('Errors on stderr should make the process exit', testStreamError, 2, false);357test('Errors on output stdio[*] should make the process exit', testStreamError, 3, false);358test('Errors on input stdio[*] should make the process exit', testStreamError, 3, true);381test('Destroying stdout should not make the process exit', testStreamOutputDestroy, 1);382test('Destroying stderr should not make the process exit', testStreamOutputDestroy, 2);383test('Destroying output stdio[*] should not make the process exit', testStreamOutputDestroy, 3);384360test('Errors on streams use killSignal', async t => {361 const childProcess = execa('forever.js', {killSignal: 'SIGINT'});385const testStreamInputError = async (t, index) => {386 const childProcess = getStreamInputProcess(index);387 const error = new Error('test');363 childProcess.stdout.destroy(error);364 const thrownError = await t.throwsAsync(childProcess);365 t.is(thrownError, error);366 t.true(error.isTerminated);367 t.is(error.signal, 'SIGINT');368});388 const stream = childProcess.stdio[index];389 stream.emit('error', error);390 stream.end();391 t.is(await t.throwsAsync(childProcess), error);392 assertStreamInputError(t, error);393};394395test('Errors on stdin should not make the process exit', testStreamInputError, 0);396test('Errors on input stdio[*] should not make the process exit', testStreamInputError, 3);397398const testStreamOutputError = async (t, index) => {399 const childProcess = getStreamOutputProcess(index);400 const error = new Error('test');401 const stream = childProcess.stdio[index];402 stream.emit('error', error);403 t.is(await t.throwsAsync(childProcess), error);404 assertStreamOutputError(t, index, error);405};406407test('Errors on stdout should not make the process exit', testStreamOutputError, 1);408test('Errors on stderr should not make the process exit', testStreamOutputError, 2);409test('Errors on output stdio[*] should not make the process exit', testStreamOutputError, 3);410411const testWaitOnStreamEnd = async (t, index) => {412 const childProcess = execa('stdin-fd.js', [`${index}`], fullStdio);413 await setTimeout(100);414 childProcess.stdio[index].end('foobar');415 const {stdout} = await childProcess;416 t.is(stdout, 'foobar');417};418378test.serial('Process waits on stdin before exiting', testWaitOnStreamEnd, 0);379test.serial('Process waits on stdio[*] before exiting', testWaitOnStreamEnd, 3);419test('Process waits on stdin before exiting', testWaitOnStreamEnd, 0);420test('Process waits on stdio[*] before exiting', testWaitOnStreamEnd, 3);421422const testBufferExit = async (t, index, fixtureName, reject) => {423 const childProcess = execa(fixtureName, [`${index}`], {...fullStdio, reject});424 await setTimeout(100);425 const {stdio} = await childProcess;426 t.is(stdio[index], 'foobar');427};428388test.serial('Process buffers stdout before it is read', testBufferExit, 1, 'noop-delay.js', true);389test.serial('Process buffers stderr before it is read', testBufferExit, 2, 'noop-delay.js', true);390test.serial('Process buffers stdio[*] before it is read', testBufferExit, 3, 'noop-delay.js', true);391test.serial('Process buffers stdout right away, on successfully exit', testBufferExit, 1, 'noop-fd.js', true);392test.serial('Process buffers stderr right away, on successfully exit', testBufferExit, 2, 'noop-fd.js', true);393test.serial('Process buffers stdio[*] right away, on successfully exit', testBufferExit, 3, 'noop-fd.js', true);394test.serial('Process buffers stdout right away, on failure', testBufferExit, 1, 'noop-fail.js', false);395test.serial('Process buffers stderr right away, on failure', testBufferExit, 2, 'noop-fail.js', false);396test.serial('Process buffers stdio[*] right away, on failure', testBufferExit, 3, 'noop-fail.js', false);429test('Process buffers stdout before it is read', testBufferExit, 1, 'noop-delay.js', true);430test('Process buffers stderr before it is read', testBufferExit, 2, 'noop-delay.js', true);431test('Process buffers stdio[*] before it is read', testBufferExit, 3, 'noop-delay.js', true);432test('Process buffers stdout right away, on successfully exit', testBufferExit, 1, 'noop-fd.js', true);433test('Process buffers stderr right away, on successfully exit', testBufferExit, 2, 'noop-fd.js', true);434test('Process buffers stdio[*] right away, on successfully exit', testBufferExit, 3, 'noop-fd.js', true);435test('Process buffers stdout right away, on failure', testBufferExit, 1, 'noop-fail.js', false);436test('Process buffers stderr right away, on failure', testBufferExit, 2, 'noop-fail.js', false);437test('Process buffers stdio[*] right away, on failure', testBufferExit, 3, 'noop-fail.js', false);438439const testBufferDirect = async (t, index) => {440 const childProcess = execa('noop-fd.js', [`${index}`], fullStdio);441 const data = await once(childProcess.stdio[index], 'data');
29 unmodified lines
modified:package.json-1 removed
78 unmodified lines
79 ]80 },81 "ava": {82 "workerThreads": false,83 "concurrency": 1,83 "timeout": "60s"84 },85 "xo": {86 "rules": {
5 unmodified lines