command.executableSubcommand.signals.test.js
This file
Path
tests/command.executableSubcommand.signals.test.js
62 lines · 2.2 KiB · JavaScript
Last changed by
Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263const childProcess = require('child_process');const path = require('path');const { test, describe } = require('node:test');const assert = require('node:assert/strict');
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('executable subcommand signals', () => { describe('signal forwarding tests', () => { const signals = ['SIGINT', 'SIGHUP', 'SIGTERM', 'SIGUSR1', 'SIGUSR2']; for (const signal of signals) { test(`when program sent ${signal} then executableSubcommand sent signal too`, (t, 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. assert.equal(processOutput, `Listening for signal...${signal}`); done(); }); }); } });
test('when executable subcommand sent signal then program exit code is non-zero', () => { const { status } = childProcess.spawnSync(pmPath, ['terminate'], {}); assert.equal(status > 0, true); });
test('when command has exitOverride and executable subcommand sent signal then exit code is non-zero', () => { const { status } = childProcess.spawnSync( pmPath, ['exit-override', 'terminate'], {}, ); assert.equal(status > 0, true); });
// 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'], {}, ); assert.equal(status, 42); });});