Skip to content
Myr

signal.js

This file

Path

test/terminate/signal.js

91 lines · 5.1 KiB · JavaScript

Last changed by

TweaksSindre Sorhusbefore adoption698a1b8

History of this file

Take it

TweaksSindre Sorhusbefore adoption698a1b8

lines 1–3why, one step back
1import assert from 'node:assert/strict';
2import test from 'node:test';3import {assertThrows} from '../helpers/assert.js';

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

lines 4–16why, one step back
4import {execa, execaSync} from '../../index.js';
5import {setFixtureDirectory} from '../helpers/fixtures-directory.js';67setFixtureDirectory();89const VALIDATION_MESSAGES = {10 string: 'this signal name does not exist',11 integer: 'this signal integer does not exist',12 other: 'it must be a string or an integer',13 rename: 'please rename it to',14 zero: '0 cannot be used',15};16

TweaksSindre Sorhusbefore adoption698a1b8

lines 17–18why, one step back
17const validateMessage = (message, type) => {
18 assert.ok(message.includes(VALIDATION_MESSAGES[type]));

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

line 19why, one step back
19

TweaksSindre Sorhusbefore adoption698a1b8

lines 20–21why, one step back
20 if (type === 'rename' || type === 'zero') {
21 return;

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

line 22why, one step back
22 }

TweaksSindre Sorhusbefore adoption698a1b8

lines 23–25why, one step back
23
24 assert.ok(message.includes('Available signal names: \'SIGABRT\', '));25 assert.ok(message.includes('Available signal numbers: 1, '));

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

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

TweaksSindre Sorhusbefore adoption698a1b8

lines 28–29why, one step back
28const testInvalidKillSignal = (killSignal, type, execaMethod) => {
29 const {message} = assertThrows(() => {

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

lines 30–31why, one step back
30 execaMethod('empty.js', {killSignal});
31 });

TweaksSindre Sorhusbefore adoption698a1b8

lines 32–33why, one step back
32 assert.ok(message.includes('Invalid option `killSignal`'));
33 validateMessage(message, type);

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

lines 34–35why, one step back
34};
35

TweaksSindre Sorhusbefore adoption698a1b8

lines 36–61why, one step back
36test('Cannot use killSignal: "SIGOTHER"', () => testInvalidKillSignal('SIGOTHER', 'string', execa));
37test('Cannot use killSignal: "Sigterm"', () => testInvalidKillSignal('Sigterm', 'rename', execa));38test('Cannot use killSignal: "sigterm"', () => testInvalidKillSignal('sigterm', 'rename', execa));39test('Cannot use killSignal: -1', () => testInvalidKillSignal(-1, 'integer', execa));40test('Cannot use killSignal: 200', () => testInvalidKillSignal(200, 'integer', execa));41test('Cannot use killSignal: 1n', () => testInvalidKillSignal(1n, 'other', execa));42test('Cannot use killSignal: 1.5', () => testInvalidKillSignal(1.5, 'other', execa));43test('Cannot use killSignal: Infinity', () => testInvalidKillSignal(Infinity, 'other', execa));44test('Cannot use killSignal: NaN', () => testInvalidKillSignal(NaN, 'other', execa));45test('Cannot use killSignal: false', () => testInvalidKillSignal(false, 'other', execa));46test('Cannot use killSignal: null', () => testInvalidKillSignal(null, 'other', execa));47test('Cannot use killSignal: symbol', () => testInvalidKillSignal(Symbol('test'), 'other', execa));48test('Cannot use killSignal: {}', () => testInvalidKillSignal({}, 'other', execa));49test('Cannot use killSignal: 0', () => testInvalidKillSignal(0, 'zero', execa));50test('Cannot use killSignal: "SIGOTHER", sync', () => testInvalidKillSignal('SIGOTHER', 'string', execaSync));51test('Cannot use killSignal: "Sigterm", sync', () => testInvalidKillSignal('Sigterm', 'rename', execaSync));52test('Cannot use killSignal: "sigterm", sync', () => testInvalidKillSignal('sigterm', 'rename', execaSync));53test('Cannot use killSignal: -1, sync', () => testInvalidKillSignal(-1, 'integer', execaSync));54test('Cannot use killSignal: 200, sync', () => testInvalidKillSignal(200, 'integer', execaSync));55test('Cannot use killSignal: 1.5, sync', () => testInvalidKillSignal(1.5, 'other', execaSync));56test('Cannot use killSignal: Infinity, sync', () => testInvalidKillSignal(Infinity, 'other', execaSync));57test('Cannot use killSignal: NaN, sync', () => testInvalidKillSignal(NaN, 'other', execaSync));58test('Cannot use killSignal: null, sync', () => testInvalidKillSignal(null, 'other', execaSync));59test('Cannot use killSignal: symbol, sync', () => testInvalidKillSignal(Symbol('test'), 'other', execaSync));60test('Cannot use killSignal: {}, sync', () => testInvalidKillSignal({}, 'other', execaSync));61test('Cannot use killSignal: 0, sync', () => testInvalidKillSignal(0, 'zero', execaSync));

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

line 62why, one step back
62

TweaksSindre Sorhusbefore adoption698a1b8

line 63why, one step back
63const testInvalidSignalArgument = async (signal, type) => {

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

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

TweaksSindre Sorhusbefore adoption698a1b8

line 65why, one step back
65 const {message} = assertThrows(() => {

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

lines 66–69why, one step back
66 subprocess.kill(signal);
67 });6869 if (type === 'other') {

TweaksSindre Sorhusbefore adoption698a1b8

line 70why, one step back
70 assert.ok(message.includes('must be an error instance or a signal name string/integer'));

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

line 71why, one step back
71 } else {

TweaksSindre Sorhusbefore adoption698a1b8

lines 72–73why, one step back
72 assert.ok(message.includes('Invalid `subprocess.kill()`\'s argument'));
73 validateMessage(message, type);

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

lines 74–78why, one step back
74 }
7576 await subprocess;77};78

TweaksSindre Sorhusbefore adoption698a1b8

lines 79–91why, one step back
79test('Cannot use subprocess.kill("SIGOTHER")', () => testInvalidSignalArgument('SIGOTHER', 'string'));
80test('Cannot use subprocess.kill("Sigterm")', () => testInvalidSignalArgument('Sigterm', 'rename'));81test('Cannot use subprocess.kill("sigterm")', () => testInvalidSignalArgument('sigterm', 'rename'));82test('Cannot use subprocess.kill(-1)', () => testInvalidSignalArgument(-1, 'integer'));83test('Cannot use subprocess.kill(200)', () => testInvalidSignalArgument(200, 'integer'));84test('Cannot use subprocess.kill(1n)', () => testInvalidSignalArgument(1n, 'other'));85test('Cannot use subprocess.kill(1.5)', () => testInvalidSignalArgument(1.5, 'other'));86test('Cannot use subprocess.kill(Infinity)', () => testInvalidSignalArgument(Infinity, 'other'));87test('Cannot use subprocess.kill(NaN)', () => testInvalidSignalArgument(NaN, 'other'));88test('Cannot use subprocess.kill(false)', () => testInvalidSignalArgument(false, 'other'));89test('Cannot use subprocess.kill(null)', () => testInvalidSignalArgument(null, 'other'));90test('Cannot use subprocess.kill(symbol)', () => testInvalidSignalArgument(Symbol('test'), 'other'));91test('Cannot use subprocess.kill({})', () => testInvalidSignalArgument({}, 'other'));