Skip to content
Myr

Add `.kill(signal, error)` (#836)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent f5283d5 · Browse files · 5 files+51 added−18 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+51 added−18 removed

  • tests:+21 added−5 removed
  • other:+30 added−13 removed

5 files+51 added−18 removed

modified:lib/kill.js+19 added-9 removed
modified:test/kill.js+21 added-5 removed
425 unmodified lines
426 t.true(thrownError.isTerminated);427 t.is(thrownError.signal, 'SIGINT');428});429430const testInvalidKillArgument = async (t, killArgument) => {430const testInvalidKillArgument = async (t, killArgument, secondKillArgument) => {431 const subprocess = execa('empty.js');432 const message = secondKillArgument instanceof Error || secondKillArgument === undefined433 ? /error instance or a signal name/434 : /second argument is optional/;435 t.throws(() => {433 subprocess.kill(killArgument);434 }, {message: /error instance or a signal name/});436 subprocess.kill(killArgument, secondKillArgument);437 }, {message});438 await subprocess;439};440441test('Cannot call .kill(null)', testInvalidKillArgument, null);442test('Cannot call .kill(0n)', testInvalidKillArgument, 0n);443test('Cannot call .kill(true)', testInvalidKillArgument, true);444test('Cannot call .kill(errorObject)', testInvalidKillArgument, {name: '', message: '', stack: ''});445test('Cannot call .kill([error])', testInvalidKillArgument, [new Error('test')]);446test('Cannot call .kill(undefined, true)', testInvalidKillArgument, undefined, true);447test('Cannot call .kill("SIGTERM", true)', testInvalidKillArgument, 'SIGTERM', true);448test('Cannot call .kill(true, error)', testInvalidKillArgument, true, new Error('test'));449450test('.kill(error) propagates error', async t => {451 const subprocess = execa('forever.js');452 const originalMessage = 'test';
10 unmodified lines
463});464465test('.kill(error) uses killSignal', async t => {466 const subprocess = execa('forever.js', {killSignal: 'SIGINT'});461 subprocess.kill(new Error('test'));462 t.like(await t.throwsAsync(subprocess), {signal: 'SIGINT'});467 const error = new Error('test');468 subprocess.kill(error);469 t.is(await t.throwsAsync(subprocess), error);470 t.is(error.signal, 'SIGINT');471});472473test('.kill(signal, error) uses signal', async t => {474 const subprocess = execa('forever.js');475 const error = new Error('test');476 subprocess.kill('SIGINT', error);477 t.is(await t.throwsAsync(subprocess), error);478 t.is(error.signal, 'SIGINT');479});480481test('.kill(error) is a noop if process already exited', async t => {482 const subprocess = execa('empty.js');483 await subprocess;484 t.false(isRunning(subprocess.pid));
33 unmodified lines
modified:index.d.ts+2 added-1 removed
849 unmodified lines
850 When an error is passed as argument, its message and stack trace are kept in the child process' error. The child process is then terminated with the default signal. This does not emit the [`error` event](https://nodejs.org/api/child_process.html#event-error).851852 [More info.](https://nodejs.org/api/child_process.html#subprocesskillsignal)853 */854 kill(signalOrError: Parameters<ChildProcess['kill']>[0] | Error): ReturnType<ChildProcess['kill']>;854 kill(signal: Parameters<ChildProcess['kill']>[0], error?: Error): ReturnType<ChildProcess['kill']>;855 kill(error?: Error): ReturnType<ChildProcess['kill']>;856};857858export type ExecaChildProcess<OptionsType extends Options = Options> = ChildProcess &859ExecaChildPromise<OptionsType> &
464 unmodified lines
modified:index.test-d.ts+5 added-1 removed
1298 unmodified lines
1299expectType<boolean>(execa('unicorns').kill());1300execa('unicorns').kill('SIGKILL');1301execa('unicorns').kill(undefined);1302execa('unicorns').kill(new Error('test'));1303expectError(execa('unicorns').kill('SIGKILL', {}));1303execa('unicorns').kill('SIGKILL', new Error('test'));1304execa('unicorns').kill(undefined, new Error('test'));1305expectError(execa('unicorns').kill(null));1306expectError(execa('unicorns').kill(0n));1307expectError(execa('unicorns').kill([new Error('test')]));1308expectError(execa('unicorns').kill({message: 'test'}));1309expectError(execa('unicorns').kill(undefined, {}));1310expectError(execa('unicorns').kill('SIGKILL', {}));1311expectError(execa('unicorns').kill(null, new Error('test')));13121313expectError(execa(['unicorns', 'arg']));1314expectType<ExecaChildProcess>(execa('unicorns'));1315expectType<ExecaChildProcess>(execa(fileUrl));
99 unmodified lines
modified:readme.md+4 added-2 removed
323 unmodified lines
324[`childProcess.stdout`](#stdout) (and/or [`childProcess.stderr`](#stderr) depending on `streamName`) must not be `undefined`. When `streamName` is `"all"`, the [`all` option](#all-2) must be set to `true`.325326Returns `execaChildProcess`, which allows chaining `.pipe()` then `await`ing the [final result](#childprocessresult).327328#### kill(signalOrError?)328#### kill(signal, error?)329#### kill(error?)330330`signalOrError`: `string | number | Error`\331`signal`: `string | number`\332`error`: `Error`\333_Returns_: `boolean`334335Sends a [signal](https://nodejs.org/api/os.html#signal-constants) to the child process. The default signal is the [`killSignal`](#killsignal) option. `killSignal` defaults to `SIGTERM`, which [terminates](#isterminated) the child process.336
623 unmodified lines