Skip to content
Myr

kill-signal.js

This file

Path

test/terminate/kill-signal.js

142 lines · 5.2 KiB · JavaScript

Last changed by

TweaksSindre Sorhusbefore adoption698a1b8

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

TweaksSindre Sorhusbefore adoption698a1b8

line 1why, one step back
1import assert from 'node:assert/strict';

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 2why, one step back
2import {once} from 'node:events';

Improve validation and typing of signals (#1025)ehmickybefore adoption7213287

line 3why, one step back
3import {constants} from 'node:os';

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 4why, one step back
4import {setImmediate} from 'node:timers/promises';

TweaksSindre Sorhusbefore adoption698a1b8

lines 5–6why, one step back
5import test from 'node:test';
6import {assertThrows, assertRejects} from '../helpers/assert.js';

Improve validation and typing of signals (#1025)ehmickybefore adoption7213287

line 7why, one step back
7import {execa, execaSync} from '../../index.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 8–11why, one step back
8import {setFixtureDirectory} from '../helpers/fixtures-directory.js';
910setFixtureDirectory();11

TweaksSindre Sorhusbefore adoption698a1b8

lines 12–15why, one step back
12const testKillSignal = async killSignal => {
13 const {isTerminated, signal} = await assertRejects(execa('forever.js', {killSignal, timeout: 1}));14 assert.equal(isTerminated, true);15 assert.equal(signal, 'SIGINT');

Improve validation and typing of signals (#1025)ehmickybefore adoption7213287

lines 16–17why, one step back
16};
17

TweaksSindre Sorhusbefore adoption698a1b8

lines 18–19why, one step back
18test('Can use killSignal: "SIGINT"', () => testKillSignal('SIGINT'));
19test('Can use killSignal: 2', () => testKillSignal(constants.signals.SIGINT));

Improve validation and typing of signals (#1025)ehmickybefore adoption7213287

line 20why, one step back
20

TweaksSindre Sorhusbefore adoption698a1b8

lines 21–22why, one step back
21const testKillSignalSync = killSignal => {
22 const {isTerminated, signal} = assertThrows(() => {

Improve validation and typing of signals (#1025)ehmickybefore adoption7213287

lines 23–24why, one step back
23 execaSync('forever.js', {killSignal, timeout: 1});
24 });

TweaksSindre Sorhusbefore adoption698a1b8

lines 25–26why, one step back
25 assert.equal(isTerminated, true);
26 assert.equal(signal, 'SIGINT');

Improve validation and typing of signals (#1025)ehmickybefore adoption7213287

lines 27–28why, one step back
27};
28

TweaksSindre Sorhusbefore adoption698a1b8

lines 29–30why, one step back
29test('Can use killSignal: "SIGINT", sync', () => testKillSignalSync('SIGINT'));
30test('Can use killSignal: 2, sync', () => testKillSignalSync(constants.signals.SIGINT));

Improve validation and typing of signals (#1025)ehmickybefore adoption7213287

line 31why, one step back
31

TweaksSindre Sorhusbefore adoption698a1b8

line 32why, one step back
32test('Can call .kill("SIGTERM")', async () => {

Improve validation and typing of signals (#1025)ehmickybefore adoption7213287

lines 33–34why, one step back
33 const subprocess = execa('forever.js');
34 subprocess.kill('SIGTERM');

TweaksSindre Sorhusbefore adoption698a1b8

lines 35–37why, one step back
35 const {isTerminated, signal} = await assertRejects(subprocess);
36 assert.equal(isTerminated, true);37 assert.equal(signal, 'SIGTERM');

Improve validation and typing of signals (#1025)ehmickybefore adoption7213287

lines 38–39why, one step back
38});
39

TweaksSindre Sorhusbefore adoption698a1b8

line 40why, one step back
40test('Can call .kill(15)', async () => {

Improve validation and typing of signals (#1025)ehmickybefore adoption7213287

lines 41–42why, one step back
41 const subprocess = execa('forever.js');
42 subprocess.kill(constants.signals.SIGTERM);

TweaksSindre Sorhusbefore adoption698a1b8

lines 43–45why, one step back
43 const {isTerminated, signal} = await assertRejects(subprocess);
44 assert.equal(isTerminated, true);45 assert.equal(signal, 'SIGTERM');

Improve validation and typing of signals (#1025)ehmickybefore adoption7213287

lines 46–47why, one step back
46});
47

TweaksSindre Sorhusbefore adoption698a1b8

line 48why, one step back
48test('Can call .kill(0)', async () => {

Improve validation and typing of signals (#1025)ehmickybefore adoption7213287

line 49why, one step back
49 const subprocess = execa('forever.js');

TweaksSindre Sorhusbefore adoption698a1b8

line 50why, one step back
50 assert.ok(subprocess.kill(0));

Improve validation and typing of signals (#1025)ehmickybefore adoption7213287

line 51why, one step back
51 subprocess.kill();

TweaksSindre Sorhusbefore adoption698a1b8

lines 52–53why, one step back
52 await assertRejects(subprocess);
53 assert.ok(!subprocess.kill(0));

Improve validation and typing of signals (#1025)ehmickybefore adoption7213287

lines 54–55why, one step back
54});
55

TweaksSindre Sorhusbefore adoption698a1b8

line 56why, one step back
56test('Can call `.kill()` multiple times', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 57–60why, one step back
57 const subprocess = execa('forever.js');
58 subprocess.kill();59 subprocess.kill();60

TweaksSindre Sorhusbefore adoption698a1b8

lines 61–65why, one step back
61 const {exitCode, isTerminated, signal, code} = await assertRejects(subprocess);
62 assert.equal(exitCode, undefined);63 assert.equal(isTerminated, true);64 assert.equal(signal, 'SIGTERM');65 assert.equal(code, undefined);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 66–67why, one step back
66});
67

TweaksSindre Sorhusbefore adoption698a1b8

line 68why, one step back
68test('execa() returns a promise with kill()', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 69why, one step back
69 const subprocess = execa('noop.js', ['foo']);

TweaksSindre Sorhusbefore adoption698a1b8

line 70why, one step back
70 assert.equal(typeof subprocess.kill, 'function');

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 71–73why, one step back
71 await subprocess;
72});73

TweaksSindre Sorhusbefore adoption698a1b8

line 74why, one step back
74const testInvalidKillArgument = async (killArgument, secondKillArgument) => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 75why, one step back
75 const subprocess = execa('empty.js');

TweaksSindre Sorhusbefore adoption698a1b8

line 76why, one step back
76 const message = secondKillArgument === undefined || secondKillArgument instanceof Error

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 77–78why, one step back
77 ? /error instance or a signal name/
78 : /second argument is optional/;

TweaksSindre Sorhusbefore adoption698a1b8

line 79why, one step back
79 assertThrows(() => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 80–84why, one step back
80 subprocess.kill(killArgument, secondKillArgument);
81 }, {message});82 await subprocess;83};84

TweaksSindre Sorhusbefore adoption698a1b8

lines 85–89why, one step back
85test('Cannot call .kill(errorObject)', () => testInvalidKillArgument({name: '', message: '', stack: ''}));
86test('Cannot call .kill(errorArray)', () => testInvalidKillArgument([new Error('test')]));87test('Cannot call .kill(undefined, true)', () => testInvalidKillArgument(undefined, true));88test('Cannot call .kill("SIGTERM", true)', () => testInvalidKillArgument('SIGTERM', true));89test('Cannot call .kill(true, error)', () => testInvalidKillArgument(true, new Error('test')));

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 90why, one step back
90

TweaksSindre Sorhusbefore adoption698a1b8

line 91why, one step back
91test('subprocess errors are handled before spawn', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 92–93why, one step back
92 const subprocess = execa('forever.js');
93 const cause = new Error('test');

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 94why, one step back
94 subprocess.nodeChildProcess.emit('error', cause);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 95why, one step back
95 subprocess.kill();

TweaksSindre Sorhusbefore adoption698a1b8

lines 96–100why, one step back
96 const error = await assertRejects(subprocess);
97 assert.equal(error.cause, cause);98 assert.equal(error.exitCode, undefined);99 assert.equal(error.signal, undefined);100 assert.equal(error.isTerminated, false);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 101–102why, one step back
101});
102

TweaksSindre Sorhusbefore adoption698a1b8

line 103why, one step back
103test('subprocess errors are handled after spawn', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 104why, one step back
104 const subprocess = execa('forever.js');

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 105why, one step back
105 await once(subprocess.nodeChildProcess, 'spawn');

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 106why, one step back
106 const cause = new Error('test');

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 107why, one step back
107 subprocess.nodeChildProcess.emit('error', cause);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 108why, one step back
108 subprocess.kill();

TweaksSindre Sorhusbefore adoption698a1b8

lines 109–113why, one step back
109 const error = await assertRejects(subprocess);
110 assert.equal(error.cause, cause);111 assert.equal(error.exitCode, undefined);112 assert.equal(error.signal, 'SIGTERM');113 assert.equal(error.isTerminated, true);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 114–115why, one step back
114});
115

TweaksSindre Sorhusbefore adoption698a1b8

line 116why, one step back
116test('subprocess double errors are handled after spawn', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 117–118why, one step back
117 const abortController = new AbortController();
118 const subprocess = execa('forever.js', {cancelSignal: abortController.signal});

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 119why, one step back
119 await once(subprocess.nodeChildProcess, 'spawn');

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 120why, one step back
120 const cause = new Error('test');

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 121why, one step back
121 subprocess.nodeChildProcess.emit('error', cause);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 122–123why, one step back
122 await setImmediate();
123 abortController.abort();

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 124why, one step back
124 subprocess.nodeChildProcess.emit('error', cause);

TweaksSindre Sorhusbefore adoption698a1b8

lines 125–129why, one step back
125 const error = await assertRejects(subprocess);
126 assert.equal(error.cause, cause);127 assert.equal(error.exitCode, undefined);128 assert.equal(error.signal, 'SIGTERM');129 assert.equal(error.isTerminated, true);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 130–131why, one step back
130});
131

TweaksSindre Sorhusbefore adoption698a1b8

line 132why, one step back
132test('subprocess errors use killSignal', async () => {

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 133why, one step back
133 const subprocess = execa('forever.js', {killSignal: 'SIGINT'});

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 134why, one step back
134 await once(subprocess.nodeChildProcess, 'spawn');

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 135why, one step back
135 const cause = new Error('test');

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 136why, one step back
136 subprocess.nodeChildProcess.emit('error', cause);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 137why, one step back
137 subprocess.kill();

TweaksSindre Sorhusbefore adoption698a1b8

lines 138–141why, one step back
138 const error = await assertRejects(subprocess);
139 assert.equal(error.cause, cause);140 assert.equal(error.isTerminated, true);141 assert.equal(error.signal, 'SIGINT');

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 142why, one step back
142});