Skip to content
Myr

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

8 files+67 added−68 removed

modified:docs/scripts.md+2 added-2 removed
498 unmodified lines
499 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 lines
520// stdout: '',521// stderr: '',522// failed: true,523// isCanceled: false,524// killed: false524// isTerminated: false525// }526```527528### Shared options
148 unmodified lines
modified:lib/error.js+1 added-2 removed
modified:test/error.js+30 added-36 removed
1import process from 'node:process';2import childProcess from 'node:child_process';3import {promisify} from 'node:util';2import test from 'ava';3import {execa, execaSync} from '../index.js';4import {FIXTURES_DIR, setFixtureDir} from './helpers/fixtures-dir.js';58const pExec = promisify(childProcess.exec);6const isWindows = process.platform === 'win32';78setFixtureDir();910const TIMEOUT_REGEXP = /timed out after/;
6 unmodified lines
17 t.is(typeof stderr, 'string');18 t.is(typeof all, 'string');19});2023const WRONG_COMMAND = process.platform === 'win32'21const WRONG_COMMAND = isWindows22 ? '\'wrong\' is not recognized as an internal or external command,\r\noperable program or batch file.'23 : '';2425test('stdout/stderr/all on process errors', async t => {
62 unmodified lines
88 const {failed} = await t.throwsAsync(execa('exit.js', ['2']));89 t.true(failed);90});9194test('error.killed is true if process was killed directly', async t => {92test('error.isTerminated is true if process was killed directly', async t => {93 const subprocess = execa('forever.js');9495 subprocess.kill();9699 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});100103test('error.killed is false if process was killed indirectly', async t => {101test('error.isTerminated is true if process was killed indirectly', async t => {102 const subprocess = execa('forever.js');103104 process.kill(subprocess.pid, 'SIGINT');105106 // `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});111114test('result.killed is false if not killed', async t => {115 const {killed} = await execa('noop.js');116 t.false(killed);112test('result.isTerminated is false if not killed', async t => {113 const {isTerminated} = await execa('noop.js');114 t.false(isTerminated);115});116119test('result.killed is false if not killed, in sync mode', t => {120 const {killed} = execaSync('noop.js');121 t.false(killed);117test('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});124124test('result.killed is false on process error', async t => {125 const {killed} = await t.throwsAsync(execa('wrong command'));126 t.false(killed);125test('result.isTerminated is false if not killed, in sync mode', t => {126 const {isTerminated} = execaSync('noop.js');127 t.false(isTerminated);128});129130test('result.isTerminated is false on process error', async t => {131 const {isTerminated} = await t.throwsAsync(execa('wrong command'));132 t.false(isTerminated);133});134129test('result.killed is false on process error, in sync mode', t => {130 const {killed} = t.throws(() => {135test('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});141136if (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');139140 process.kill(promise.child.pid, 'SIGINT');141142 const error = await t.throwsAsync(promise);143 t.truthy(error);144 t.false(error.killed);145 });146}147148if (process.platform !== 'win32') {142if (!isWindows) {143 test('error.signal is SIGINT', async t => {144 const subprocess = execa('forever.js');145146 process.kill(subprocess.pid, 'SIGINT');
73 unmodified lines
modified:test/kill.js+7 added-5 removed
80 unmodified lines
81 t.is(typeof kill, 'function');82});8384test('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});8990test('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});9798test('timeout does not kill the process if it does not time out', async t => {
98 unmodified lines
197 const included = emitter.listeners.exit.includes(listener);198 t.false(included);199});200201test('cancel method kills the subprocess', t => {201test('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});208209test('result.isCanceled is false when spawned.cancel() isn\'t called (success)', async t => {210 const {isCanceled} = await execa('noop.js');
109 unmodified lines
modified:index.d.ts+11 added-7 removed
263 unmodified lines
264265 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 lines
387 */388 timedOut: boolean;389390 /**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;396397 /**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).401402 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 lines
423Result of a child process execution. On success this is a plain object. On failure this is also an `Error` instance.424425The 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 lines
645 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 lines
725 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 lines
modified:index.js+2 added-6 removed
98 unmodified lines
99 escapedCommand,100 parsed,101 timedOut: false,102 isCanceled: false,103 killed: false,103 }));104 mergePromise(dummySpawned, errorPromise);105 return dummySpawned;106 }
25 unmodified lines
132 escapedCommand,133 parsed,134 timedOut,135 isCanceled: context.isCanceled || (parsed.options.signal ? parsed.options.signal.aborted : false),137 killed: spawned.killed,136 });137138 if (!parsed.options.reject) {139 return returnedError;
11 unmodified lines
151 all,152 failed: false,153 timedOut: false,154 isCanceled: false,157 killed: false,155 isTerminated: false,156 };157 };158159 const handlePromiseOnce = onetime(handlePromise);
28 unmodified lines
188 escapedCommand,189 parsed,190 timedOut: false,191 isCanceled: false,194 killed: false,192 });193 }194195 pipeOutputSync(stdioStreams, result);
11 unmodified lines
207 escapedCommand,208 parsed,209 timedOut: result.error && result.error.code === 'ETIMEDOUT',210 isCanceled: false,214 killed: result.signal !== null,211 });212213 if (!parsed.options.reject) {214 return error;
10 unmodified lines
225 stderr,226 failed: false,227 timedOut: false,228 isCanceled: false,233 killed: false,229 isTerminated: false,230 };231}232233const normalizeScriptStdin = ({input, inputFile, stdio}) => input === undefined && inputFile === undefined && stdio === undefined
76 unmodified lines
modified:index.test-d.ts+4 added-4 removed
66 unmodified lines
67 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 lines
82 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 lines
104 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 lines
119 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 lines
modified:readme.md+10 added-6 removed
210 unmodified lines
211 stderr: '',212 failed: true,213 timedOut: false,214 isCanceled: false,215 killed: false215 isTerminated: false216 }217 */218}219```
149 unmodified lines
369Result of a child process execution. On success this is a plain object. On failure this is also an `Error` instance.370371The 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 lines
439Whether the process was canceled.440441You can cancel the spawned process using the [`signal`](#signal-1) option.442443#### killed443#### isTerminated444445Type: `boolean`446447Whether the process was killed.447Whether 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).450451#### signal452453Type: `string | undefined`454453The name of the signal that was used to terminate the process. For example, `SIGFPE`.455The 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).458459If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`.460461#### signalDescription
330 unmodified lines
792793try {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