Skip to content
Myr

Fix shape of exceptions rejected by `execa()` (#276)

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

authored by ehmicky · committed by Sindre Sorhus · parent bad2140 · Browse files · 2 files+54 added−16 removed
This commit

State

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

Diff stats

2 files+54 added−16 removed

  • other:+54 added−16 removed

2 files+54 added−16 removed

modified:index.js+34 added-16 removed
226 unmodified lines
227228 return [file, ...args].join(' ');229}230231function mergePromiseProperty(spawned, getPromise, property) {232 Object.defineProperty(spawned, property, {233 value(...args) {234 return getPromise()[property](...args);235 },236 writable: true,237 enumerable: false,238 configurable: true239 });240}241242// The return value is a mixin of `childProcess` and `Promise`243function mergePromise(spawned, getPromise) {244 mergePromiseProperty(spawned, getPromise, 'then');245 mergePromiseProperty(spawned, getPromise, 'catch');246247 // TODO: Remove the `if`-guard when targeting Node.js 10248 if (Promise.prototype.finally) {249 mergePromiseProperty(spawned, getPromise, 'finally');250 }251252 return spawned;253}254255function spawnedKill(kill, signal = 'SIGTERM', options = {}) {256 const killResult = kill(signal);257 setKillTimeout(kill, signal, options, killResult);258 return killResult;
39 unmodified lines
298 let spawned;299 try {300 spawned = childProcess.spawn(parsed.file, parsed.args, parsed.options);301 } catch (error) {302 return mergePromise(new childProcess.ChildProcess(), () =>278 return Promise.reject(makeError({error, stdout: '', stderr: '', all: ''}, {279 command,280 parsed,281 timedOut: false,282 isCanceled: false,283 killed: false284 }));303 Promise.reject(makeError({error, stdout: '', stderr: '', all: ''}, {304 command,305 parsed,306 timedOut: false,307 isCanceled: false,308 killed: false309 }))310 );311 }312313 const kill = spawned.kill.bind(spawned);314 spawned.kill = spawnedKill.bind(null, kill);
113 unmodified lines
428 handleInput(spawned, parsed.options.input);429430 spawned.all = makeAllStream(spawned);431406 // eslint-disable-next-line promise/prefer-await-to-then407 spawned.then = (onFulfilled, onRejected) => handlePromise().then(onFulfilled, onRejected);408 spawned.catch = onRejected => handlePromise().catch(onRejected);432 spawned.cancel = () => {433 if (spawned.kill()) {434 isCanceled = true;435 }436 };437415 // TODO: Remove the `if`-guard when targeting Node.js 10416 if (Promise.prototype.finally) {417 spawned.finally = onFinally => handlePromise().finally(onFinally);418 }419420 return spawned;438 return mergePromise(spawned, handlePromise);439};440441module.exports = execa;442
85 unmodified lines
modified:test.js+20 added
299 unmodified lines
300 t.is(typeof kill, 'function');301 t.is(typeof pid, 'number');302});303304test('child_process.spawn() propagated errors have correct shape', t => {305 const cp = execa('noop', {uid: -1});306 t.notThrows(() => {307 cp.catch(() => {});308 cp.unref();309 cp.on('error', () => {});310 });311});312313test('child_process.spawn() errors are propagated', async t => {314 const {failed} = await t.throwsAsync(execa('noop', {uid: -1}));315 t.true(failed);316});
355 unmodified lines
672 const included = ee.listeners('exit').includes(listener);673 t.false(included);674});675676test('promise methods are not enumerable', t => {677 const descriptors = Object.getOwnPropertyDescriptors(execa('noop'));678 // eslint-disable-next-line promise/prefer-await-to-then679 t.false(descriptors.then.enumerable);680 t.false(descriptors.catch.enumerable);681 // TOOD: Remove the `if`-guard when targeting Node.js 10682 if (Promise.prototype.finally) {683 t.false(descriptors.finally.enumerable);684 }685});686687// TOOD: Remove the `if`-guard when targeting Node.js 10688if (Promise.prototype.finally) {689 test('finally function is executed on success', async t => {690 let isCalled = false;
147 unmodified lines