Skip to content

Commit b107877

Browse files
committed
fixup! util: add convertProcessSignalToExitCode utility
1 parent 51cfb9e commit b107877

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

lib/internal/util.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const {
1616
ObjectGetOwnPropertyDescriptor,
1717
ObjectGetOwnPropertyDescriptors,
1818
ObjectGetPrototypeOf,
19+
ObjectKeys,
1920
ObjectPrototypeHasOwnProperty,
2021
ObjectSetPrototypeOf,
2122
ObjectValues,
@@ -106,6 +107,7 @@ function isError(e) {
106107
const codesWarned = new SafeSet();
107108

108109
let validateString;
110+
let validateOneOf;
109111

110112
function getDeprecationWarningEmitter(
111113
code, msg, deprecated, useEmitSync,
@@ -394,15 +396,14 @@ function convertToValidSignal(signal) {
394396
}
395397

396398
function convertProcessSignalToExitCode(signalCode) {
397-
validateString(signalCode, 'signalCode');
399+
// Lazy-load to avoid a circular dependency.
400+
if (validateOneOf === undefined)
401+
({ validateOneOf } = require('internal/validators'));
398402

399-
const signalNumber = signals[signalCode];
400-
if (signalNumber === undefined) {
401-
return null;
402-
}
403+
validateOneOf(signalCode, 'signalCode', ObjectKeys(signals));
403404

404405
// POSIX standard: exit code for signal termination is 128 + signal number.
405-
return 128 + signalNumber;
406+
return 128 + signals[signalCode];
406407
}
407408

408409
function getConstructorOf(obj) {

test/parallel/test-util-convert-signal-to-exit-code.mjs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import assert from 'assert';
33
import { convertProcessSignalToExitCode } from 'util';
44
import { spawn } from 'child_process';
55

6-
// Test valid signal names
76
{
87
// SIGTERM = 15, so 128 + 15 = 143
98
assert.strictEqual(convertProcessSignalToExitCode('SIGTERM'), 143);
@@ -21,16 +20,11 @@ import { spawn } from 'child_process';
2120
assert.strictEqual(convertProcessSignalToExitCode('SIGABRT'), 134);
2221
}
2322

24-
// Test invalid signal names return null
25-
{
26-
assert.strictEqual(convertProcessSignalToExitCode('INVALID'), null);
27-
assert.strictEqual(convertProcessSignalToExitCode(''), null);
28-
assert.strictEqual(convertProcessSignalToExitCode('SIG'), null);
29-
}
30-
31-
// Test invalid argument types
3223
{
3324
[
25+
'INVALID',
26+
'',
27+
'SIG',
3428
undefined,
3529
null,
3630
123,
@@ -44,14 +38,13 @@ import { spawn } from 'child_process';
4438
assert.throws(
4539
() => convertProcessSignalToExitCode(value),
4640
{
47-
code: 'ERR_INVALID_ARG_TYPE',
41+
code: 'ERR_INVALID_ARG_VALUE',
4842
name: 'TypeError',
4943
}
5044
);
5145
});
5246
}
5347

54-
// Test with actual child process termination
5548
{
5649
const cat = spawn(isWindows ? 'cmd' : 'cat');
5750
cat.stdout.on('end', mustCall());
@@ -63,7 +56,6 @@ import { spawn } from 'child_process';
6356
assert.strictEqual(signal, 'SIGTERM');
6457
assert.strictEqual(cat.signalCode, 'SIGTERM');
6558

66-
// Verify the utility function returns correct exit code
6759
const exitCode = convertProcessSignalToExitCode(signal);
6860
assert.strictEqual(exitCode, 143);
6961
}));

0 commit comments

Comments
 (0)