command.executableSubcommand.signals.test.js
This file
Path
tests/command.executableSubcommand.signals.test.js
58 lines · 2.0 KiB · JavaScript
Last changed by
Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859const childProcess = require('child_process');const path = require('path');
const pmPath = path.join(__dirname, 'fixtures', 'pm');
// Disabling some tests on Windows as:// "Windows does not support sending signals"// https://nodejs.org/api/process.html#process_signal_eventsconst describeOrSkipOnWindows = process.platform === 'win32' ? describe.skip : describe;
describeOrSkipOnWindows('signals', () => { test.each(['SIGINT', 'SIGHUP', 'SIGTERM', 'SIGUSR1', 'SIGUSR2'])( 'when program sent %s then executableSubcommand sent signal too', (signal, done) => { // Spawn program. The listen subcommand waits for a signal and writes the name of the signal to stdout. const proc = childProcess.spawn(pmPath, ['listen'], {});
let processOutput = ''; proc.stdout.on('data', (data) => { if (processOutput.length === 0) { // Send signal to program. proc.kill(`${signal}`); } processOutput += data.toString(); }); proc.on('close', (code) => { // Check the child subcommand received the signal too. expect(processOutput).toBe(`Listening for signal...${signal}`); done(); }); }, );
test('when executable subcommand sent signal then program exit code is non-zero', () => { const { status } = childProcess.spawnSync(pmPath, ['terminate'], {}); expect(status).toBeGreaterThan(0); });
test('when command has exitOverride and executable subcommand sent signal then exit code is non-zero', () => { const { status } = childProcess.spawnSync( pmPath, ['exit-override', 'terminate'], {}, ); expect(status).toBeGreaterThan(0); });
// Not a signal test, but closely related code so adding here. test('when command has exitOverride and executable subcommand fails then program exit code is subcommand exit code', () => { const { status } = childProcess.spawnSync( pmPath, ['exit-override', 'fail'], {}, ); expect(status).toEqual(42); });});