Fix unhandled rejections and missing data (#658)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 6cf1c5e · Browse files · 6 files·+56 added−23 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+56 added−23 removed
- tests:+47 added−8 removed
- other:+9 added−15 removed
Provenance
- Commit
20b2ee6- Tree
079abba- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
6cf1c5e
6 files changed+56 added−23 removed
modified:lib/promise.js+1 added-5 removed
9 unmodified lines10111213141516171418151617185 unmodified lines9 unmodified lines
// The return value is a mixin of `childProcess` and `Promise`export const mergePromise = (spawned, promise) => { for (const [property, descriptor] of descriptors) { // Starting the main `promise` is deferred to avoid consuming streams const value = typeof promise === 'function' ? (...args) => Reflect.apply(descriptor.value, promise(), args) : descriptor.value.bind(promise); const value = descriptor.value.bind(promise);
Reflect.defineProperty(spawned, property, {...descriptor, value}); }};
5 unmodified linesadded:test/fixtures/no-await.js+9 added
123456789#!/usr/bin/env nodeimport process from 'node:process';import {once} from 'node:events';import {execa} from '../../index.js';
const [options, file, ...args] = process.argv.slice(2);execa(file, args, JSON.parse(options));const [error] = await once(process, 'unhandledRejection');console.log(error.shortMessage);modified:test/promise.js+8 added
41 unmodified lines42434445464748495051525341 unmodified lines throw new Error('called'); })); t.is(message, 'called');});
const testNoAwait = async (t, fixtureName, options, message) => { const {stdout} = await execa('no-await.js', [JSON.stringify(options), fixtureName]); t.true(stdout.includes(message));};
test('Throws if promise is not awaited and process fails', testNoAwait, 'fail.js', {}, 'exit code 2');test('Throws if promise is not awaited and process times out', testNoAwait, 'forever.js', {timeout: 1}, 'timed out');modified:test/stream.js+30 added-8 removed
1 import {Buffer} from 'node:buffer';2 import {exec} from 'node:child_process';3+import {once} from 'node:events';4 import process from 'node:process';5 import {setTimeout} from 'node:timers/promises';6 import {promisify} from 'node:util';7 import test from 'ava';123 unmodified lines131 132 test('do not buffer stdout when `buffer` set to `false`', testNoMaxBuffer, 'stdout');133 test('do not buffer stderr when `buffer` set to `false`', testNoMaxBuffer, 'stderr');134 134-test('do not buffer when streaming', async t => {135- const {stdout} = execa('max-buffer.js', ['stdout', '21'], {maxBuffer: 10});135+test('do not buffer when streaming and `buffer` is `false`', async t => {136+ const {stdout} = execa('max-buffer.js', ['stdout', '21'], {maxBuffer: 10, buffer: false});137 const result = await getStream(stdout);138 t.is(result, '....................\n');139 });140 141+test('buffers when streaming and `buffer` is `true`', async t => {142+ const childProcess = execa('max-buffer.js', ['stdout', '21'], {maxBuffer: 10});143+ await Promise.all([144+ t.throwsAsync(childProcess, {message: /maxBuffer exceeded/}),145+ t.throwsAsync(getStream(childProcess.stdout), {code: 'ABORT_ERR'}),146+ ]);147+});148+149 test('buffer: false > promise resolves', async t => {150 await t.notThrowsAsync(execa('noop.js', {buffer: false}));151 });152 63 unmodified lines216 const {stdout} = await childProcess;217 t.is(stdout, 'foobar');218 });219 211-// This test is not the desired behavior, but is the current one.212-// I.e. this is mostly meant for documentation and regression testing.213-test.serial('Processes might successfully exit before their stdout is read', async t => {220+test.serial('Processes buffers stdout right away, on successfully exit', async t => {221 const childProcess = execa('noop.js', ['foobar']);222 await setTimeout(1e3);223 const {stdout} = await childProcess;217- t.is(stdout, '');224+ t.is(stdout, 'foobar');225 });226 220-test.serial('Processes might fail before their stdout is read', async t => {227+test.serial('Processes buffers stdout right away, on failure', async t => {228 const childProcess = execa('noop-fail.js', ['foobar'], {reject: false});229 await setTimeout(1e3);230 const {stdout} = await childProcess;224- t.is(stdout, '');231+ t.is(stdout, 'foobar');232 });233+234+test('Processes buffers stdout right away, even if directly read', async t => {235+ const childProcess = execa('noop.js', ['foobar']);236+ const data = await once(childProcess.stdout, 'data');237+ t.is(data.toString().trim(), 'foobar');238+ const {stdout} = await childProcess;239+ t.is(stdout, 'foobar');240+});241+242+test('childProcess.stdout|stderr must be read right away', async t => {243+ const childProcess = execa('noop.js', ['foobar']);244+ const {stdout} = await childProcess;245+ t.is(stdout, 'foobar');246+ t.true(childProcess.stdout.destroyed);247+});modified:index.js+8 added-9 removed
4 unmodified lines5 import {fileURLToPath} from 'node:url';6 import crossSpawn from 'cross-spawn';7 import stripFinalNewline from 'strip-final-newline';8 import {npmRunPathEnv} from 'npm-run-path';9-import onetime from 'onetime';9 import {makeError} from './lib/error.js';10 import {handleInputAsync, pipeOutputAsync} from './lib/stdio/async.js';11 import {handleInputSync, pipeOutputSync} from './lib/stdio/sync.js';12 import {normalizeStdioNode} from './lib/stdio/normalize.js';112 unmodified lines125 mergePromise(dummySpawned, errorPromise);126 return dummySpawned;127 }128 130- const spawnedPromise = getSpawnedPromise(spawned);131- const timedPromise = setupTimeout(spawned, options, spawnedPromise);132- const processDone = setExitHandler(spawned, options, timedPromise);133-129 const context = {isCanceled: false};130 131 spawned.kill = spawnedKill.bind(null, spawned.kill.bind(spawned));132 spawned.cancel = spawnedCancel.bind(null, spawned, context);133 139- const handlePromiseOnce = onetime(handlePromise.bind(undefined, {spawned, options, context, stdioStreams, command, escapedCommand, processDone}));140-134 pipeOutputAsync(spawned, stdioStreams);135 136 spawned.all = makeAllStream(spawned, options);137 138 addPipeMethods(spawned);139+140+ const promise = handlePromise({spawned, options, context, stdioStreams, command, escapedCommand});146- mergePromise(spawned, handlePromiseOnce);141+ mergePromise(spawned, promise);142 return spawned;143 }144 150-const handlePromise = async ({spawned, options, context, stdioStreams, command, escapedCommand, processDone}) => {145+const handlePromise = async ({spawned, options, context, stdioStreams, command, escapedCommand}) => {146+ const spawnedPromise = getSpawnedPromise(spawned);147+ const timedPromise = setupTimeout(spawned, options, spawnedPromise);148+ const processDone = setExitHandler(spawned, options, timedPromise);149+150 const [151 {error, exitCode, signal, timedOut},152 stdoutResult,153 stderrResult,180 unmodified linesmodified:package.json-1 removed
49 unmodified lines50515253545455565733 unmodified lines49 unmodified lines "get-stream": "^8.0.1", "human-signals": "^6.0.0", "is-stream": "^3.0.0", "npm-run-path": "^5.2.0", "onetime": "^7.0.0", "signal-exit": "^4.1.0", "strip-final-newline": "^4.0.0" }, "devDependencies": {33 unmodified lines