Fix `error.killed` and rename to `error.isTerminated` (#625)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 05541b4 · Browse files · 8 files·+67 added−68 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+67 added−68 removed
- tests:+37 added−41 removed
- other:+30 added−27 removed
Provenance
- Commit
248349c- Tree
7baa3e2- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
05541b4
8 files changed+67 added−68 removed
modified:docs/scripts.md+2 added-2 removed
498 unmodified lines499 escapedCommand,500 failed,501 timedOut,502 isCanceled,503- killed,503+ isTerminated,504 // and other error-related properties: code, etc.505 } = await $({timeout: 1})`sleep 2`;506 // file:///home/me/code/execa/lib/kill.js:60507 // reject(Object.assign(new Error('Timed out'), {timedOut: true, signal}));12 unmodified lines520 // stdout: '',521 // stderr: '',522 // failed: true,523 // isCanceled: false,524-// killed: false524+// isTerminated: false525 // }526 ```527 528 ### Shared options148 unmodified linesmodified:lib/error.js+1 added-2 removed
34 unmodified lines35363738393940414236 unmodified lines79808182848384858634 unmodified lines command, escapedCommand, timedOut, isCanceled, killed, parsed: {options: {timeout, cwd = process.cwd()}},}) => { // `signal` and `exitCode` emitted on `spawned.on('exit')` event can be `null`. // We normalize them to `undefined`36 unmodified lines
error.failed = true; error.timedOut = Boolean(timedOut); error.isCanceled = isCanceled; error.killed = killed && !timedOut; error.isTerminated = signal !== undefined;
return error;};modified:test/error.js+30 added-36 removed
1 import process from 'node:process';2-import childProcess from 'node:child_process';3-import {promisify} from 'node:util';2 import test from 'ava';3 import {execa, execaSync} from '../index.js';4 import {FIXTURES_DIR, setFixtureDir} from './helpers/fixtures-dir.js';5 8-const pExec = promisify(childProcess.exec);6+const isWindows = process.platform === 'win32';7 8 setFixtureDir();9 10 const TIMEOUT_REGEXP = /timed out after/;6 unmodified lines17 t.is(typeof stderr, 'string');18 t.is(typeof all, 'string');19 });20 23-const WRONG_COMMAND = process.platform === 'win32'21+const WRONG_COMMAND = isWindows22 ? '\'wrong\' is not recognized as an internal or external command,\r\noperable program or batch file.'23 : '';24 25 test('stdout/stderr/all on process errors', async t => {62 unmodified lines88 const {failed} = await t.throwsAsync(execa('exit.js', ['2']));89 t.true(failed);90 });91 94-test('error.killed is true if process was killed directly', async t => {92+test('error.isTerminated is true if process was killed directly', async t => {93 const subprocess = execa('forever.js');94 95 subprocess.kill();96 99- const {killed} = await t.throwsAsync(subprocess, {message: /was killed with SIGTERM/});100- t.true(killed);97+ const {isTerminated} = await t.throwsAsync(subprocess, {message: /was killed with SIGTERM/});98+ t.true(isTerminated);99 });100 103-test('error.killed is false if process was killed indirectly', async t => {101+test('error.isTerminated is true if process was killed indirectly', async t => {102 const subprocess = execa('forever.js');103 104 process.kill(subprocess.pid, 'SIGINT');105 106 // `process.kill()` is emulated by Node.js on Windows109- const message = process.platform === 'win32' ? /failed with exit code 1/ : /was killed with SIGINT/;110- const {killed} = await t.throwsAsync(subprocess, {message});111- t.false(killed);107+ const message = isWindows ? /failed with exit code 1/ : /was killed with SIGINT/;108+ const {isTerminated} = await t.throwsAsync(subprocess, {message});109+ t.not(isTerminated, isWindows);110 });111 114-test('result.killed is false if not killed', async t => {115- const {killed} = await execa('noop.js');116- t.false(killed);112+test('result.isTerminated is false if not killed', async t => {113+ const {isTerminated} = await execa('noop.js');114+ t.false(isTerminated);115 });116 119-test('result.killed is false if not killed, in sync mode', t => {120- const {killed} = execaSync('noop.js');121- t.false(killed);117+test('result.isTerminated is false if not killed and childProcess.kill() was called', async t => {118+ const subprocess = execa('noop.js');119+ subprocess.kill(0);120+ t.true(subprocess.killed);121+ const {isTerminated} = await subprocess;122+ t.false(isTerminated);123 });124 124-test('result.killed is false on process error', async t => {125- const {killed} = await t.throwsAsync(execa('wrong command'));126- t.false(killed);125+test('result.isTerminated is false if not killed, in sync mode', t => {126+ const {isTerminated} = execaSync('noop.js');127+ t.false(isTerminated);128 });129 130+test('result.isTerminated is false on process error', async t => {131+ const {isTerminated} = await t.throwsAsync(execa('wrong command'));132+ t.false(isTerminated);133+});134+129-test('result.killed is false on process error, in sync mode', t => {130- const {killed} = t.throws(() => {135+test('result.isTerminated is false on process error, in sync mode', t => {136+ const {isTerminated} = t.throws(() => {137 execaSync('wrong command');138 });133- t.false(killed);139+ t.false(isTerminated);140 });141 136-if (process.platform === 'darwin') {137- test('sanity check: child_process.exec also has killed.false if killed indirectly', async t => {138- const promise = pExec('forever.js');139-140- process.kill(promise.child.pid, 'SIGINT');141-142- const error = await t.throwsAsync(promise);143- t.truthy(error);144- t.false(error.killed);145- });146-}147-148-if (process.platform !== 'win32') {142+if (!isWindows) {143 test('error.signal is SIGINT', async t => {144 const subprocess = execa('forever.js');145 146 process.kill(subprocess.pid, 'SIGINT');73 unmodified linesmodified:test/kill.js+7 added-5 removed
80 unmodified lines81 t.is(typeof kill, 'function');82 });83 84 test('timeout kills the process if it times out', async t => {85- const {killed, timedOut} = await t.throwsAsync(execa('noop.js', {timeout: 1}), {message: TIMEOUT_REGEXP});86- t.false(killed);85+ const {isTerminated, timedOut} = await t.throwsAsync(execa('noop.js', {timeout: 1}), {message: TIMEOUT_REGEXP});86+ t.true(isTerminated);87 t.true(timedOut);88 });89 90 test('timeout kills the process if it times out, in sync mode', async t => {91- const {killed, timedOut} = await t.throws(() => {91+ const {isTerminated, timedOut} = await t.throws(() => {92 execaSync('noop.js', {timeout: 1, message: TIMEOUT_REGEXP});93 });94- t.false(killed);94+ t.true(isTerminated);95 t.true(timedOut);96 });97 98 test('timeout does not kill the process if it does not time out', async t => {98 unmodified lines197 const included = emitter.listeners.exit.includes(listener);198 t.false(included);199 });200 201-test('cancel method kills the subprocess', t => {201+test('cancel method kills the subprocess', async t => {202 const subprocess = execa('node');203 subprocess.cancel();204 t.true(subprocess.killed);205+ const {isTerminated} = await t.throwsAsync(subprocess);206+ t.true(isTerminated);207 });208 209 test('result.isCanceled is false when spawned.cancel() isn\'t called (success)', async t => {210 const {isCanceled} = await execa('noop.js');109 unmodified linesmodified:index.d.ts+11 added-7 removed
263 unmodified lines264 265 try {266 await subprocess;267 } catch (error) {268- console.log(subprocess.killed); // true268+ console.log(error.isTerminated); // true269 console.log(error.isCanceled); // true270 }271 ```272 */114 unmodified lines387 */388 timedOut: boolean;389 390 /**391- Whether the process was killed.391+ Whether the process was terminated using either:392+ - `childProcess.kill()`.393+ - A signal sent by another process. This case is [not supported on Windows](https://nodejs.org/api/process.html#signal-events).394 */393- killed: boolean;395+ isTerminated: boolean;396 397 /**396- The name of the signal that was used to terminate the process. For example, `SIGFPE`.398+ The name of the signal (like `SIGFPE`) that terminated the process using either:399+ - `childProcess.kill()`.400+ - A signal sent by another process. This case is [not supported on Windows](https://nodejs.org/api/process.html#signal-events).401 402 If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`.403 */404 signal?: string;18 unmodified lines423 Result of a child process execution. On success this is a plain object. On failure this is also an `Error` instance.424 425 The child process fails when:426 - its exit code is not `0`423-- it was killed with a signal427+- it was terminated with a signal428 - timing out429 - being canceled430 - there's not enough memory or there are already too many child processes431 */213 unmodified lines645 stderr: '',646 failed: true,647 timedOut: false,648 isCanceled: false,645- killed: false,649+ isTerminated: false,650 cwd: '/path/to/cwd'651 }652 \*\/653 }71 unmodified lines725 stderr: '',726 failed: true,727 timedOut: false,728 isCanceled: false,725- killed: false,729+ isTerminated: false,730 cwd: '/path/to/cwd'731 }732 \*\/733 }286 unmodified linesmodified:index.js+2 added-6 removed
98 unmodified lines99 escapedCommand,100 parsed,101 timedOut: false,102 isCanceled: false,103- killed: false,103 }));104 mergePromise(dummySpawned, errorPromise);105 return dummySpawned;106 }25 unmodified lines132 escapedCommand,133 parsed,134 timedOut,135 isCanceled: context.isCanceled || (parsed.options.signal ? parsed.options.signal.aborted : false),137- killed: spawned.killed,136 });137 138 if (!parsed.options.reject) {139 return returnedError;11 unmodified lines151 all,152 failed: false,153 timedOut: false,154 isCanceled: false,157- killed: false,155+ isTerminated: false,156 };157 };158 159 const handlePromiseOnce = onetime(handlePromise);28 unmodified lines188 escapedCommand,189 parsed,190 timedOut: false,191 isCanceled: false,194- killed: false,192 });193 }194 195 pipeOutputSync(stdioStreams, result);11 unmodified lines207 escapedCommand,208 parsed,209 timedOut: result.error && result.error.code === 'ETIMEDOUT',210 isCanceled: false,214- killed: result.signal !== null,211 });212 213 if (!parsed.options.reject) {214 return error;10 unmodified lines225 stderr,226 failed: false,227 timedOut: false,228 isCanceled: false,233- killed: false,229+ isTerminated: false,230 };231 }232 233 const normalizeScriptStdin = ({input, inputFile, stdio}) => input === undefined && inputFile === undefined && stdio === undefined76 unmodified linesmodified:index.test-d.ts+4 added-4 removed
66 unmodified lines67 expectType<string | undefined>(unicornsResult.all);68 expectType<boolean>(unicornsResult.failed);69 expectType<boolean>(unicornsResult.timedOut);70 expectType<boolean>(unicornsResult.isCanceled);71- expectType<boolean>(unicornsResult.killed);71+ expectType<boolean>(unicornsResult.isTerminated);72 expectType<string | undefined>(unicornsResult.signal);73 expectType<string | undefined>(unicornsResult.signalDescription);74 expectType<string>(unicornsResult.cwd);75 } catch (error: unknown) {6 unmodified lines82 expectType<string | undefined>(execaError.all);83 expectType<boolean>(execaError.failed);84 expectType<boolean>(execaError.timedOut);85 expectType<boolean>(execaError.isCanceled);86- expectType<boolean>(execaError.killed);86+ expectType<boolean>(execaError.isTerminated);87 expectType<string | undefined>(execaError.signal);88 expectType<string | undefined>(execaError.signalDescription);89 expectType<string>(execaError.cwd);90 expectType<string>(execaError.shortMessage);13 unmodified lines104 expectError(unicornsResult.pipeAll);105 expectType<boolean>(unicornsResult.failed);106 expectType<boolean>(unicornsResult.timedOut);107 expectError(unicornsResult.isCanceled);108- expectType<boolean>(unicornsResult.killed);108+ expectType<boolean>(unicornsResult.isTerminated);109 expectType<string | undefined>(unicornsResult.signal);110 expectType<string | undefined>(unicornsResult.signalDescription);111 expectType<string>(unicornsResult.cwd);112 } catch (error: unknown) {6 unmodified lines119 expectError(execaError.all);120 expectType<boolean>(execaError.failed);121 expectType<boolean>(execaError.timedOut);122 expectError(execaError.isCanceled);123- expectType<boolean>(execaError.killed);123+ expectType<boolean>(execaError.isTerminated);124 expectType<string | undefined>(execaError.signal);125 expectType<string | undefined>(execaError.signalDescription);126 expectType<string>(execaError.cwd);127 expectType<string>(execaError.shortMessage);247 unmodified linesmodified:readme.md+10 added-6 removed
210 unmodified lines211 stderr: '',212 failed: true,213 timedOut: false,214 isCanceled: false,215- killed: false215+ isTerminated: false216 }217 */218 }219 ```149 unmodified lines369 Result of a child process execution. On success this is a plain object. On failure this is also an `Error` instance.370 371 The child process [fails](#failed) when:372 - its [exit code](#exitcode) is not `0`373-- it was [killed](#killed) with a [signal](#signal)373+- it was [terminated](#isterminated) with a [signal](#signal)374 - [timing out](#timedout)375 - [being canceled](#iscanceled)376 - there's not enough memory or there are already too many child processes377 61 unmodified lines439 Whether the process was canceled.440 441 You can cancel the spawned process using the [`signal`](#signal-1) option.442 443-#### killed443+#### isTerminated444 445 Type: `boolean`446 447-Whether the process was killed.447+Whether the process was terminated using either:448+ - [`childProcess.kill()`](#killsignal-options).449+ - A signal sent by another process. This case is [not supported on Windows](https://nodejs.org/api/process.html#signal-events).450 451 #### signal452 453 Type: `string | undefined`454 453-The name of the signal that was used to terminate the process. For example, `SIGFPE`.455+The name of the signal (like `SIGFPE`) that terminated the process using either:456+ - [`childProcess.kill()`](#killsignal-options).457+ - A signal sent by another process. This case is [not supported on Windows](https://nodejs.org/api/process.html#signal-events).458 459 If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`.460 461 #### signalDescription330 unmodified lines792 793 try {794 await subprocess;795 } catch (error) {792- console.log(subprocess.killed); // true796+ console.log(error.isTerminated); // true797 console.log(error.isCanceled); // true798 }799 ```800 21 unmodified lines