Exit with non-zero code when subprocess terminated by signal (#2023)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by John Gee · committed by GitHub · parent a0ca1bd · Browse files · 5 files·+56 added−36 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+56 added−36 removed
- tests:+48 added−28 removed
- other:+8 added−8 removed
Provenance
- Commit
5aaca0d- Tree
3c666a9- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
a0ca1bd
5 files changed+56 added−36 removed
modified:lib/command.js+8 added-8 removed
1038 unmodified lines1039 });1040 }1041 1042 // By default terminate process when spawned process terminates.1043- // Suppressing the exit if exitCallback defined is a bit messy and of limited use, but does allow process to stay running!1043 const exitCallback = this._exitCallback;1045- if (!exitCallback) {1046- proc.on('close', process.exit.bind(process));1047- } else {1048- proc.on('close', () => {1049- exitCallback(new CommanderError(process.exitCode || 0, 'commander.executeSubCommandAsync', '(close)'));1050- });1051- }1044+ proc.on('close', (code, _signal) => {1045+ code = code ?? 1; // code is null if spawned process terminated due to a signal1046+ if (!exitCallback) {1047+ process.exit(code);1048+ } else {1049+ exitCallback(new CommanderError(code, 'commander.executeSubCommandAsync', '(close)'));1050+ }1051+ });1052 proc.on('error', (err) => {1053 // @ts-ignore1054 if (err.code === 'ENOENT') {1055 const executableDirMessage = executableDir1135 unmodified linesmodified:tests/fixtures/pm+12 added-1 removed
1 #!/usr/bin/env node2 3+const path = require('node:path');3-var { program } = require('../../');4+const { program } = require('../../');5 6 process.env.FORCE_COLOR = 0; // work-around bug in Jest: https://github.com/jestjs/jest/issues/143917 8 program8 unmodified lines17 .command('default', 'default command', { hidden: true, isDefault: true })18 .command('specifyInstall', 'specify install subcommand', { executableFile: 'pm-install' })19 .command('specifyPublish', 'specify publish subcommand', { executableFile: 'pm-publish' })20 .command('silent', 'silently succeed')21+ .command('fail', 'exit with non-zero status code')22+ .command('terminate', 'terminate due to signal');23+24+program25+ .command('exit-override')26+ .exitOverride((err) => { process.exit(err.exitCode); })27+ .command('fail', 'exit with non-zero status code', { executableFile: path.join(__dirname, 'pm-fail.js') })28+ .command('terminate', 'terminate due to signal', { executableFile: path.join(__dirname, 'pm-terminate.js') });29+30+program31 .parse(process.argv);added:tests/fixtures/pm-fail.js+1 added
1process.exit(42);added:tests/fixtures/pm-terminate.js+1 added
1process.kill(process.pid, 'SIGINT');modified:tests/command.executableSubcommand.signals.test.js+34 added-27 removed
123445667891011121314151112131415171819202122161718192021222324252627282930242531323334352728293031323334353637383637383940414243444546const childProcess = require('child_process');const path = require('path');
// Test that a signal sent to the parent process is received by the executable subcommand process (which is listening).const pmPath = path.join(__dirname, 'fixtures', 'pm');
// Disabling tests on Windows as:// 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;
// Note: the previous (sinon) test had custom code for SIGUSR1, revisit if required:// As described at https://nodejs.org/api/process.html#process_signal_events// this signal will start a debugger and thus the process might output an// additional error message:// "Failed to open socket on port 5858, waiting 1000 ms before retrying".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'], {});
describeOrSkipOnWindows.each([['SIGINT'], ['SIGHUP'], ['SIGTERM'], ['SIGUSR1'], ['SIGUSR2']])( 'test signal handling in executableSubcommand', (value) => { // Slightly tricky test, stick with callback and disable lint warning. // eslint-disable-next-line jest/no-done-callback test(`when command killed with ${value} then executableSubcommand receives ${value}`, (done) => { const pmPath = path.join(__dirname, './fixtures/pm'); 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(); }); });
// The child process writes to stdout. const proc = childProcess.spawn(pmPath, ['listen'], {}); test('when executable subcommand sent signal then program exit code is non-zero', () => { const { status } = childProcess.spawnSync(pmPath, ['terminate'], {}); expect(status).toBeGreaterThan(0); });
let processOutput = ''; proc.stdout.on('data', (data) => { if (processOutput.length === 0) { proc.kill(`${value}`); } processOutput += data.toString(); }); proc.on('close', (code) => { expect(processOutput).toBe(`Listening for signal...${value}`); done(); }); }); 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); });});