From 2cdbfdfda17f310a55032b6891667c2f2e785dd3 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Fri, 10 May 2019 12:17:33 +0200 Subject: [PATCH 1/2] Improve error.killed --- index.js | 6 +----- test.js | 6 +++--- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index dc0f983c5..aea21445c 100644 --- a/index.js +++ b/index.js @@ -195,6 +195,7 @@ function makeError(result, options) { // `signal` emitted on `spawned.on('exit')` event can be `null`. We normalize // it to `undefined` error.signal = signal || undefined; + error.killed = signal !== null && !timedOut; error.command = joinedCommand; error.timedOut = Boolean(timedOut); error.isCanceled = isCanceled; @@ -367,11 +368,6 @@ const execa = (command, args, options) => { isCanceled }); - // TODO: missing some timeout logic for killed - // https://github.com/nodejs/node/blob/master/lib/child_process.js#L203 - // error.killed = spawned.killed || killed; - error.killed = error.killed || spawned.killed; - if (!parsed.options.reject) { return error; } diff --git a/test.js b/test.js index 5d7a4b41a..3b7429ade 100644 --- a/test.js +++ b/test.js @@ -293,8 +293,7 @@ test('error.killed is true if process was killed directly', async t => { t.true(error.killed); }); -// TODO: Should this really be the case, or should we improve on child_process? -test('error.killed is false if process was killed indirectly', async t => { +test('error.killed is true if process was killed indirectly', async t => { const cp = execa('forever'); process.kill(cp.pid, 'SIGINT'); @@ -302,7 +301,7 @@ test('error.killed is false if process was killed indirectly', async t => { // `process.kill()` is emulated by Node.js on Windows const message = process.platform === 'win32' ? /failed with exit code 1/ : /was killed with SIGINT/; const error = await t.throwsAsync(cp, {message}); - t.false(error.killed); + t.true(error.killed); }); if (process.platform === 'darwin') { @@ -362,6 +361,7 @@ test('error.code is 4', code, 4); test('timeout kills the process if it times out', async t => { const error = await t.throwsAsync(execa('forever', {timeout: 1, message: TIMEOUT_REGEXP})); + t.false(error.killed); t.true(error.timedOut); }); From f4f76194675dd4a6b3702788b01400aa14a437e0 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Fri, 10 May 2019 12:46:45 +0200 Subject: [PATCH 2/2] Fix Windows support --- index.js | 7 ++++--- test.js | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index aea21445c..84d1c484e 100644 --- a/index.js +++ b/index.js @@ -173,7 +173,7 @@ function getStream(process, stream, {encoding, buffer, maxBuffer}) { function makeError(result, options) { const {stdout, stderr, code, signal} = result; let {error} = result; - const {joinedCommand, timedOut, isCanceled, parsed: {options: {timeout}}} = options; + const {joinedCommand, timedOut, isCanceled, killed, parsed: {options: {timeout}}} = options; const [exitCodeName, exitCode] = getCode(result, code); @@ -195,10 +195,10 @@ function makeError(result, options) { // `signal` emitted on `spawned.on('exit')` event can be `null`. We normalize // it to `undefined` error.signal = signal || undefined; - error.killed = signal !== null && !timedOut; error.command = joinedCommand; error.timedOut = Boolean(timedOut); error.isCanceled = isCanceled; + error.killed = killed && !timedOut; if ('all' in result) { error.all = result.all; @@ -365,7 +365,8 @@ const execa = (command, args, options) => { joinedCommand, parsed, timedOut, - isCanceled + isCanceled, + killed: spawned.killed }); if (!parsed.options.reject) { diff --git a/test.js b/test.js index 3b7429ade..9f44b645c 100644 --- a/test.js +++ b/test.js @@ -293,7 +293,7 @@ test('error.killed is true if process was killed directly', async t => { t.true(error.killed); }); -test('error.killed is true if process was killed indirectly', async t => { +test('error.killed is false if process was killed indirectly', async t => { const cp = execa('forever'); process.kill(cp.pid, 'SIGINT'); @@ -301,7 +301,7 @@ test('error.killed is true if process was killed indirectly', async t => { // `process.kill()` is emulated by Node.js on Windows const message = process.platform === 'win32' ? /failed with exit code 1/ : /was killed with SIGINT/; const error = await t.throwsAsync(cp, {message}); - t.true(error.killed); + t.false(error.killed); }); if (process.platform === 'darwin') {