signal.js
This file
Path
test/terminate/signal.js
91 lines · 5.1 KiB · JavaScript
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192import assert from 'node:assert/strict';import test from 'node:test';import {assertThrows} from '../helpers/assert.js';import {execa, execaSync} from '../../index.js';import {setFixtureDirectory} from '../helpers/fixtures-directory.js';
setFixtureDirectory();
const VALIDATION_MESSAGES = { string: 'this signal name does not exist', integer: 'this signal integer does not exist', other: 'it must be a string or an integer', rename: 'please rename it to', zero: '0 cannot be used',};
const validateMessage = (message, type) => { assert.ok(message.includes(VALIDATION_MESSAGES[type]));
if (type === 'rename' || type === 'zero') { return; }
assert.ok(message.includes('Available signal names: \'SIGABRT\', ')); assert.ok(message.includes('Available signal numbers: 1, '));};
const testInvalidKillSignal = (killSignal, type, execaMethod) => { const {message} = assertThrows(() => { execaMethod('empty.js', {killSignal}); }); assert.ok(message.includes('Invalid option `killSignal`')); validateMessage(message, type);};
test('Cannot use killSignal: "SIGOTHER"', () => testInvalidKillSignal('SIGOTHER', 'string', execa));test('Cannot use killSignal: "Sigterm"', () => testInvalidKillSignal('Sigterm', 'rename', execa));test('Cannot use killSignal: "sigterm"', () => testInvalidKillSignal('sigterm', 'rename', execa));test('Cannot use killSignal: -1', () => testInvalidKillSignal(-1, 'integer', execa));test('Cannot use killSignal: 200', () => testInvalidKillSignal(200, 'integer', execa));test('Cannot use killSignal: 1n', () => testInvalidKillSignal(1n, 'other', execa));test('Cannot use killSignal: 1.5', () => testInvalidKillSignal(1.5, 'other', execa));test('Cannot use killSignal: Infinity', () => testInvalidKillSignal(Infinity, 'other', execa));test('Cannot use killSignal: NaN', () => testInvalidKillSignal(NaN, 'other', execa));test('Cannot use killSignal: false', () => testInvalidKillSignal(false, 'other', execa));test('Cannot use killSignal: null', () => testInvalidKillSignal(null, 'other', execa));test('Cannot use killSignal: symbol', () => testInvalidKillSignal(Symbol('test'), 'other', execa));test('Cannot use killSignal: {}', () => testInvalidKillSignal({}, 'other', execa));test('Cannot use killSignal: 0', () => testInvalidKillSignal(0, 'zero', execa));test('Cannot use killSignal: "SIGOTHER", sync', () => testInvalidKillSignal('SIGOTHER', 'string', execaSync));test('Cannot use killSignal: "Sigterm", sync', () => testInvalidKillSignal('Sigterm', 'rename', execaSync));test('Cannot use killSignal: "sigterm", sync', () => testInvalidKillSignal('sigterm', 'rename', execaSync));test('Cannot use killSignal: -1, sync', () => testInvalidKillSignal(-1, 'integer', execaSync));test('Cannot use killSignal: 200, sync', () => testInvalidKillSignal(200, 'integer', execaSync));test('Cannot use killSignal: 1.5, sync', () => testInvalidKillSignal(1.5, 'other', execaSync));test('Cannot use killSignal: Infinity, sync', () => testInvalidKillSignal(Infinity, 'other', execaSync));test('Cannot use killSignal: NaN, sync', () => testInvalidKillSignal(NaN, 'other', execaSync));test('Cannot use killSignal: null, sync', () => testInvalidKillSignal(null, 'other', execaSync));test('Cannot use killSignal: symbol, sync', () => testInvalidKillSignal(Symbol('test'), 'other', execaSync));test('Cannot use killSignal: {}, sync', () => testInvalidKillSignal({}, 'other', execaSync));test('Cannot use killSignal: 0, sync', () => testInvalidKillSignal(0, 'zero', execaSync));
const testInvalidSignalArgument = async (signal, type) => { const subprocess = execa('empty.js'); const {message} = assertThrows(() => { subprocess.kill(signal); });
if (type === 'other') { assert.ok(message.includes('must be an error instance or a signal name string/integer')); } else { assert.ok(message.includes('Invalid `subprocess.kill()`\'s argument')); validateMessage(message, type); }
await subprocess;};
test('Cannot use subprocess.kill("SIGOTHER")', () => testInvalidSignalArgument('SIGOTHER', 'string'));test('Cannot use subprocess.kill("Sigterm")', () => testInvalidSignalArgument('Sigterm', 'rename'));test('Cannot use subprocess.kill("sigterm")', () => testInvalidSignalArgument('sigterm', 'rename'));test('Cannot use subprocess.kill(-1)', () => testInvalidSignalArgument(-1, 'integer'));test('Cannot use subprocess.kill(200)', () => testInvalidSignalArgument(200, 'integer'));test('Cannot use subprocess.kill(1n)', () => testInvalidSignalArgument(1n, 'other'));test('Cannot use subprocess.kill(1.5)', () => testInvalidSignalArgument(1.5, 'other'));test('Cannot use subprocess.kill(Infinity)', () => testInvalidSignalArgument(Infinity, 'other'));test('Cannot use subprocess.kill(NaN)', () => testInvalidSignalArgument(NaN, 'other'));test('Cannot use subprocess.kill(false)', () => testInvalidSignalArgument(false, 'other'));test('Cannot use subprocess.kill(null)', () => testInvalidSignalArgument(null, 'other'));test('Cannot use subprocess.kill(symbol)', () => testInvalidSignalArgument(Symbol('test'), 'other'));test('Cannot use subprocess.kill({})', () => testInvalidSignalArgument({}, 'other'));