Skip to content
Myr

Fix `reject` option with early spawning errors (#734)

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

authored by ehmicky · committed by GitHub · parent 66d12bf · Browse files · 3 files+31 added−11 removed
This commit

State

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

Diff stats

3 files+31 added−11 removed

  • tests:+21 added−8 removed
  • other:+10 added−3 removed

3 files+31 added−11 removed

modified:test/error.js+2 added-2 removed
48 unmodified lines
49// `error.code` is OS-specific here50const SPAWN_ERROR_CODES = new Set(['EINVAL', 'ENOTSUP', 'EPERM']);5152test('stdout/stderr/stdio on process spawning errors', async t => {53 const {code, stdout, stderr, stdio} = await t.throwsAsync(execa('noop.js', {uid: -1}));53 const {code, stdout, stderr, stdio} = await t.throwsAsync(execa('empty.js', {uid: -1}));54 t.true(SPAWN_ERROR_CODES.has(code));55 t.is(stdout, undefined);56 t.is(stderr, undefined);57 t.deepEqual(stdio, [undefined, undefined, undefined]);58});5960test('stdout/stderr/all/stdio on process spawning errors - sync', t => {61 const {code, stdout, stderr, stdio} = t.throws(() => {62 execaSync('noop.js', {uid: -1});62 execaSync('empty.js', {uid: -1});63 });64 t.true(SPAWN_ERROR_CODES.has(code));65 t.is(stdout, undefined);66 t.is(stderr, undefined);
228 unmodified lines
modified:test/test.js+19 added-6 removed
151 unmodified lines
152 t.is(typeof subprocess.pid, 'number');153 await subprocess;154});155156test('child_process.spawn() propagated errors have correct shape', t => {157 const subprocess = execa('noop.js', {uid: -1});156const testEarlyErrorShape = async (t, reject) => {157 const subprocess = execa('', {reject});158 t.notThrows(() => {159 subprocess.catch(() => {});160 subprocess.unref();161 subprocess.on('error', () => {});162 });163};164165test('child_process.spawn() early errors have correct shape', testEarlyErrorShape, true);166test('child_process.spawn() early errors have correct shape - reject false', testEarlyErrorShape, false);167168test('child_process.spawn() early errors are propagated', async t => {169 const {failed} = await t.throwsAsync(execa(''));170 t.true(failed);171});172165test('child_process.spawn() errors are propagated', async t => {166 const {failed} = await t.throwsAsync(execa('noop.js', {uid: -1}));173test('child_process.spawn() early errors are returned', async t => {174 const {failed} = await execa('', {reject: false});175 t.true(failed);176});177170test('child_process.spawnSync() errors are propagated with a correct shape', t => {178test('child_process.spawnSync() early errors are propagated with a correct shape', t => {179 const {failed} = t.throws(() => {172 execaSync('noop.js', {uid: -1});180 execaSync('');181 });182 t.true(failed);183});184185test('child_process.spawnSync() early errors are propagated with a correct shape - reject false', t => {186 const {failed} = execaSync('', {reject: false});187 t.true(failed);188});189190test('do not try to consume streams twice', async t => {191 const subprocess = execa('noop.js', ['foo']);192 const {stdout} = await subprocess;193 const {stdout: stdout2} = await subprocess;
130 unmodified lines
modified:index.js+10 added-3 removed
132 unmodified lines
133 spawned = childProcess.spawn(file, args, options);134 } catch (error) {135 // Ensure the returned error is always both a promise and a child process136 const dummySpawned = new childProcess.ChildProcess();137 const errorPromise = Promise.reject(makeError({137 const errorInstance = makeError({138 error,139 stdio: Array.from({length: stdioStreamsGroups.length}),140 command,141 escapedCommand,142 options,143 timedOut: false,144 isCanceled: false,145 }));145 });146 const errorPromise = options.reject ? Promise.reject(errorInstance) : Promise.resolve(errorInstance);147 mergePromise(dummySpawned, errorPromise);148 return dummySpawned;149 }150
68 unmodified lines
219 let result;220 try {221 result = childProcess.spawnSync(file, args, options);222 } catch (error) {222 throw makeError({223 const errorInstance = makeError({224 error,225 stdio: Array.from({length: stdioStreamsGroups.stdioLength}),226 command,227 escapedCommand,228 options,229 timedOut: false,230 isCanceled: false,231 });232233 if (!options.reject) {234 return errorInstance;235 }236237 throw errorInstance;238 }239240 pipeOutputSync(stdioStreamsGroups, result);241
114 unmodified lines