Skip to content
Myr

Child process errors should reject the promise right away (#270)

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 2307d95 · Browse files · 2 files+23 added−31 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+23 added−31 removed

  • other:+23 added−31 removed

2 files+23 added−31 removed

modified:index.js+12 added-24 removed
260 unmodified lines
261 spawned.kill(parsed.options.killSignal);262 }, parsed.options.timeout);263 }264265 const resolvable = (() => {266 let extracted;267 const promise = new Promise(resolve => {268 extracted = resolve;269 });270 promise.resolve = extracted;271 return promise;272 })();273265 // TODO: Use native "finally" syntax when targeting Node.js 10275 const processDone = pFinally(new Promise(resolve => {266 const processDone = pFinally(new Promise((resolve, reject) => {267 spawned.on('exit', (code, signal) => {268 if (timedOut) {278 resolvable.resolve([279 {code, signal}, '', '', ''280 ]);269 return reject(Object.assign(new Error('Timed out'), {code, signal}));270 }271272 resolve({code, signal});273 });274275 spawned.on('error', error => {287 resolve({error});276 reject(error);277 });278279 if (spawned.stdin) {280 spawned.stdin.on('error', error => {292 resolve({error});281 reject(error);282 });283 }284 }), cleanup);285
11 unmodified lines
297 }298 }299300 const handlePromise = () => {312 let processComplete = Promise.all([301 const processComplete = Promise.all([302 processDone,303 getStream(spawned, 'stdout', {encoding, buffer, maxBuffer}),304 getStream(spawned, 'stderr', {encoding, buffer, maxBuffer}),305 getStream(spawned, 'all', {encoding, buffer, maxBuffer: maxBuffer * 2})306 ]);307319 if (timeoutId) {320 processComplete = Promise.race([321 processComplete,322 resolvable323 ]);324 }325308 const finalize = async () => {309 let results;310 try {327 const results = await processComplete;311 results = await processComplete;312 } catch (error) {313 const {stream, code, signal} = error;314 results = [{error, stream, code, signal}, '', '', ''];315 }316317 const [result, stdout, stderr, all] = results;318 result.stdout = handleOutput(parsed.options, stdout);319 result.stderr = handleOutput(parsed.options, stderr);
135 unmodified lines
modified:test.js+11 added-7 removed
63 unmodified lines
64 t.is(stderr, undefined);65 t.is(all, undefined);66});6768const WRONG_COMMAND_STDERR = process.platform === 'win32' ?69 '\'wrong\' is not recognized as an internal or external command,\r\noperable program or batch file.' :70 '';7168test('stdout/stderr/all on process errors', async t => {69 const {stdout, stderr, all} = await t.throwsAsync(execa('wrong command'));70 t.is(stdout, '');75 t.is(stderr, WRONG_COMMAND_STDERR);76 t.is(all, WRONG_COMMAND_STDERR);71 t.is(stderr, '');72 t.is(all, '');73});7475test('stdout/stderr/all on process errors, in sync mode', t => {76 const {stdout, stderr, all} = t.throws(() => {77 execa.sync('wrong command');78 });79 t.is(stdout, '');84 t.is(stderr, WRONG_COMMAND_STDERR);80 t.is(stderr, process.platform === 'win32' ?81 '\'wrong\' is not recognized as an internal or external command,\r\noperable program or batch file.' :82 '');83 t.is(all, undefined);84});8586test('pass `stdout` to a file descriptor', async t => {
127 unmodified lines
214 /The `input` option cannot be a stream in sync mode/215 );216});217218test('child process errors rejects promise right away', async t => {219 const child = execa('forever');220 child.emit('error', new Error('test'));221 await t.throwsAsync(child, /test/);222});223224test('execa() returns a promise with kill() and pid', t => {225 const {kill, pid} = execa('noop', ['foo']);226 t.is(typeof kill, 'function');227 t.is(typeof pid, 'number');
512 unmodified lines