Separate promise-related logic into their own files (#325)
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 cba87e1 · Browse files · 4 files·+108 added−95 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+108 added−95 removed
- tests:+52 added−47 removed
- other:+56 added−48 removed
Provenance
- Commit
deb4988- Tree
0ee4f69- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
cba87e1
4 files changed+108 added−95 removed
added:lib/promise.js+54 added
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354'use strict';const mergePromiseProperty = (spawned, getPromise, property) => { Object.defineProperty(spawned, property, { value(...args) { return getPromise()[property](...args); }, writable: true, enumerable: false, configurable: true });};
// The return value is a mixin of `childProcess` and `Promise`const mergePromise = (spawned, getPromise) => { mergePromiseProperty(spawned, getPromise, 'then'); mergePromiseProperty(spawned, getPromise, 'catch');
// TODO: Remove the `if`-guard when targeting Node.js 10 if (Promise.prototype.finally) { mergePromiseProperty(spawned, getPromise, 'finally'); }
return spawned;};
// Use promises instead of `child_process` eventsconst getSpawnedPromise = (spawned, context) => { return new Promise((resolve, reject) => { spawned.on('exit', (code, signal) => { if (context.timedOut) { reject(Object.assign(new Error('Timed out'), {code, signal})); return; }
resolve({code, signal}); });
spawned.on('error', error => { reject(error); });
if (spawned.stdin) { spawned.stdin.on('error', error => { reject(error); }); } });};
module.exports = { mergePromise, getSpawnedPromise};
added:test/promise.js+52 added
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152import path from 'path';import test from 'ava';import execa from '..';
process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;
test('promise methods are not enumerable', t => { const descriptors = Object.getOwnPropertyDescriptors(execa('noop')); // eslint-disable-next-line promise/prefer-await-to-then t.false(descriptors.then.enumerable); t.false(descriptors.catch.enumerable); // TOOD: Remove the `if`-guard when targeting Node.js 10 if (Promise.prototype.finally) { t.false(descriptors.finally.enumerable); }});
// TOOD: Remove the `if`-guard when targeting Node.js 10if (Promise.prototype.finally) { test('finally function is executed on success', async t => { let isCalled = false; const {stdout} = await execa('noop', ['foo']).finally(() => { isCalled = true; }); t.is(isCalled, true); t.is(stdout, 'foo'); });
test('finally function is executed on failure', async t => { let isError = false; const {stdout, stderr} = await t.throwsAsync(execa('exit', ['2']).finally(() => { isError = true; })); t.is(isError, true); t.is(typeof stdout, 'string'); t.is(typeof stderr, 'string'); });
test('throw in finally function bubbles up on success', async t => { const {message} = await t.throwsAsync(execa('noop', ['foo']).finally(() => { throw new Error('called'); })); t.is(message, 'called'); });
test('throw in finally bubbles up on error', async t => { const {message} = await t.throwsAsync(execa('exit', ['2']).finally(() => { throw new Error('called'); })); t.is(message, 'called'); });}modified:test/test.js-47 removed
223 unmodified lines224 225 process.kill(pid, 'SIGKILL');226 });227 228-test('promise methods are not enumerable', t => {229- const descriptors = Object.getOwnPropertyDescriptors(execa('noop'));230- // eslint-disable-next-line promise/prefer-await-to-then231- t.false(descriptors.then.enumerable);232- t.false(descriptors.catch.enumerable);233- // TOOD: Remove the `if`-guard when targeting Node.js 10234- if (Promise.prototype.finally) {235- t.false(descriptors.finally.enumerable);236- }237-});238-239-// TOOD: Remove the `if`-guard when targeting Node.js 10240-if (Promise.prototype.finally) {241- test('finally function is executed on success', async t => {242- let isCalled = false;243- const {stdout} = await execa('noop', ['foo']).finally(() => {244- isCalled = true;245- });246- t.is(isCalled, true);247- t.is(stdout, 'foo');248- });249-250- test('finally function is executed on failure', async t => {251- let isError = false;252- const {stdout, stderr} = await t.throwsAsync(execa('exit', ['2']).finally(() => {253- isError = true;254- }));255- t.is(isError, true);256- t.is(typeof stdout, 'string');257- t.is(typeof stderr, 'string');258- });259-260- test('throw in finally function bubbles up on success', async t => {261- const {message} = await t.throwsAsync(execa('noop', ['foo']).finally(() => {262- throw new Error('called');263- }));264- t.is(message, 'called');265- });266-267- test('throw in finally bubbles up on error', async t => {268- const {message} = await t.throwsAsync(execa('exit', ['2']).finally(() => {269- throw new Error('called');270- }));271- t.is(message, 'called');272- });273-}274-228 test('allow commands with spaces and no array arguments', async t => {229 const {stdout} = await execa('command with space');230 t.is(stdout, '');231 });40 unmodified linesmodified:index.js+2 added-48 removed
7 unmodified lines8 const makeError = require('./lib/error');9 const normalizeStdio = require('./lib/stdio');10 const {spawnedKill, spawnedCancel, setupTimeout, setExitHandler, cleanup} = require('./lib/kill');11 const {handleInput, getSpawnedResult, makeAllStream, validateInputSync} = require('./lib/stream.js');12+const {mergePromise, getSpawnedPromise} = require('./lib/promise.js');13 14 const DEFAULT_MAX_BUFFER = 1000 * 1000 * 100;15 16 const SPACES_REGEXP = / +/g;61 unmodified lines78 79 return [file, ...args].join(' ');80 };81 81-const mergePromiseProperty = (spawned, getPromise, property) => {82- Object.defineProperty(spawned, property, {83- value(...args) {84- return getPromise()[property](...args);85- },86- writable: true,87- enumerable: false,88- configurable: true89- });90-};91-92-// The return value is a mixin of `childProcess` and `Promise`93-const mergePromise = (spawned, getPromise) => {94- mergePromiseProperty(spawned, getPromise, 'then');95- mergePromiseProperty(spawned, getPromise, 'catch');96-97- // TODO: Remove the `if`-guard when targeting Node.js 1098- if (Promise.prototype.finally) {99- mergePromiseProperty(spawned, getPromise, 'finally');100- }101-102- return spawned;103-};104-105-const handleSpawned = (spawned, context) => {106- return new Promise((resolve, reject) => {107- spawned.on('exit', (code, signal) => {108- if (context.timedOut) {109- reject(Object.assign(new Error('Timed out'), {code, signal}));110- return;111- }112-113- resolve({code, signal});114- });115-116- spawned.on('error', error => {117- reject(error);118- });119-120- if (spawned.stdin) {121- spawned.stdin.on('error', error => {122- reject(error);123- });124- }125- });126-};127-82 const execa = (file, args, options) => {83 const parsed = handleArgs(file, args, options);84 const command = joinCommand(file, args);85 24 unmodified lines110 const timeoutId = setupTimeout(spawned, parsed.options, context);111 const removeExitHandler = setExitHandler(spawned, parsed.options);112 113 // TODO: Use native "finally" syntax when targeting Node.js 10160- const processDone = pFinally(handleSpawned(spawned, context), () => {114+ const processDone = pFinally(getSpawnedPromise(spawned, context), () => {115 cleanup(timeoutId, removeExitHandler);116 });117 118 const handlePromise = async () => {163 unmodified lines