Skip to content
Myr

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

6 files+56 added−23 removed

modified:lib/promise.js+1 added-5 removed
added:test/fixtures/no-await.js+9 added
modified:test/promise.js+8 added
modified:test/stream.js+30 added-8 removed
1import {Buffer} from 'node:buffer';2import {exec} from 'node:child_process';3import {once} from 'node:events';4import process from 'node:process';5import {setTimeout} from 'node:timers/promises';6import {promisify} from 'node:util';7import test from 'ava';
123 unmodified lines
131132test('do not buffer stdout when `buffer` set to `false`', testNoMaxBuffer, 'stdout');133test('do not buffer stderr when `buffer` set to `false`', testNoMaxBuffer, 'stderr');134134test('do not buffer when streaming', async t => {135 const {stdout} = execa('max-buffer.js', ['stdout', '21'], {maxBuffer: 10});135test('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});140141test('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});148149test('buffer: false > promise resolves', async t => {150 await t.notThrowsAsync(execa('noop.js', {buffer: false}));151});152
63 unmodified lines
216 const {stdout} = await childProcess;217 t.is(stdout, 'foobar');218});219211// This test is not the desired behavior, but is the current one.212// I.e. this is mostly meant for documentation and regression testing.213test.serial('Processes might successfully exit before their stdout is read', async t => {220test.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});226220test.serial('Processes might fail before their stdout is read', async t => {227test.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});233234test('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});241242test('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 lines
5import {fileURLToPath} from 'node:url';6import crossSpawn from 'cross-spawn';7import stripFinalNewline from 'strip-final-newline';8import {npmRunPathEnv} from 'npm-run-path';9import onetime from 'onetime';9import {makeError} from './lib/error.js';10import {handleInputAsync, pipeOutputAsync} from './lib/stdio/async.js';11import {handleInputSync, pipeOutputSync} from './lib/stdio/sync.js';12import {normalizeStdioNode} from './lib/stdio/normalize.js';
112 unmodified lines
125 mergePromise(dummySpawned, errorPromise);126 return dummySpawned;127 }128130 const spawnedPromise = getSpawnedPromise(spawned);131 const timedPromise = setupTimeout(spawned, options, spawnedPromise);132 const processDone = setExitHandler(spawned, options, timedPromise);133129 const context = {isCanceled: false};130131 spawned.kill = spawnedKill.bind(null, spawned.kill.bind(spawned));132 spawned.cancel = spawnedCancel.bind(null, spawned, context);133139 const handlePromiseOnce = onetime(handlePromise.bind(undefined, {spawned, options, context, stdioStreams, command, escapedCommand, processDone}));140134 pipeOutputAsync(spawned, stdioStreams);135136 spawned.all = makeAllStream(spawned, options);137138 addPipeMethods(spawned);139140 const promise = handlePromise({spawned, options, context, stdioStreams, command, escapedCommand});146 mergePromise(spawned, handlePromiseOnce);141 mergePromise(spawned, promise);142 return spawned;143}144150const handlePromise = async ({spawned, options, context, stdioStreams, command, escapedCommand, processDone}) => {145const 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);149150 const [151 {error, exitCode, signal, timedOut},152 stdoutResult,153 stderrResult,
180 unmodified lines
modified:package.json-1 removed