Skip to content

Commit 600349a

Browse files
lundibunditargos
authored andcommitted
test: refactor process/worker exitCode tests
PR-URL: #21739 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 961f6e8 commit 600349a

File tree

3 files changed

+133
-234
lines changed

3 files changed

+133
-234
lines changed
+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
5+
const cases = [];
6+
module.exports = cases;
7+
8+
function exitsOnExitCodeSet() {
9+
process.exitCode = 42;
10+
process.on('exit', (code) => {
11+
assert.strictEqual(process.exitCode, 42);
12+
assert.strictEqual(code, 42);
13+
});
14+
}
15+
cases.push({ func: exitsOnExitCodeSet, result: 42 });
16+
17+
function changesCodeViaExit() {
18+
process.exitCode = 99;
19+
process.on('exit', (code) => {
20+
assert.strictEqual(process.exitCode, 42);
21+
assert.strictEqual(code, 42);
22+
});
23+
process.exit(42);
24+
}
25+
cases.push({ func: changesCodeViaExit, result: 42 });
26+
27+
function changesCodeZeroExit() {
28+
process.exitCode = 99;
29+
process.on('exit', (code) => {
30+
assert.strictEqual(process.exitCode, 0);
31+
assert.strictEqual(code, 0);
32+
});
33+
process.exit(0);
34+
}
35+
cases.push({ func: changesCodeZeroExit, result: 0 });
36+
37+
function exitWithOneOnUncaught() {
38+
process.exitCode = 99;
39+
process.on('exit', (code) => {
40+
// cannot use assert because it will be uncaughtException -> 1 exit code
41+
// that will render this test useless
42+
if (code !== 1 || process.exitCode !== 1) {
43+
console.log('wrong code! expected 1 for uncaughtException');
44+
process.exit(99);
45+
}
46+
});
47+
throw new Error('ok');
48+
}
49+
cases.push({
50+
func: exitWithOneOnUncaught,
51+
result: 1,
52+
error: /^Error: ok$/,
53+
});
54+
55+
function changeCodeInsideExit() {
56+
process.exitCode = 95;
57+
process.on('exit', (code) => {
58+
assert.strictEqual(process.exitCode, 95);
59+
assert.strictEqual(code, 95);
60+
process.exitCode = 99;
61+
});
62+
}
63+
cases.push({ func: changeCodeInsideExit, result: 99 });
64+
65+
function zeroExitWithUncaughtHandler() {
66+
process.on('exit', (code) => {
67+
assert.strictEqual(process.exitCode, 0);
68+
assert.strictEqual(code, 0);
69+
});
70+
process.on('uncaughtException', () => {});
71+
throw new Error('ok');
72+
}
73+
cases.push({ func: zeroExitWithUncaughtHandler, result: 0 });
74+
75+
function changeCodeInUncaughtHandler() {
76+
process.on('exit', (code) => {
77+
assert.strictEqual(process.exitCode, 97);
78+
assert.strictEqual(code, 97);
79+
});
80+
process.on('uncaughtException', () => {
81+
process.exitCode = 97;
82+
});
83+
throw new Error('ok');
84+
}
85+
cases.push({ func: changeCodeInUncaughtHandler, result: 97 });
86+
87+
function changeCodeInExitWithUncaught() {
88+
process.on('exit', (code) => {
89+
assert.strictEqual(process.exitCode, 1);
90+
assert.strictEqual(code, 1);
91+
process.exitCode = 98;
92+
});
93+
throw new Error('ok');
94+
}
95+
cases.push({
96+
func: changeCodeInExitWithUncaught,
97+
result: 98,
98+
error: /^Error: ok$/,
99+
});
100+
101+
function exitWithZeroInExitWithUncaught() {
102+
process.on('exit', (code) => {
103+
assert.strictEqual(process.exitCode, 1);
104+
assert.strictEqual(code, 1);
105+
process.exitCode = 0;
106+
});
107+
throw new Error('ok');
108+
}
109+
cases.push({
110+
func: exitWithZeroInExitWithUncaught,
111+
result: 0,
112+
error: /^Error: ok$/,
113+
});

test/parallel/test-process-exit-code.js

+14-117
Original file line numberDiff line numberDiff line change
@@ -23,113 +23,18 @@
2323
require('../common');
2424
const assert = require('assert');
2525

26-
switch (process.argv[2]) {
27-
case 'child1':
28-
return child1();
29-
case 'child2':
30-
return child2();
31-
case 'child3':
32-
return child3();
33-
case 'child4':
34-
return child4();
35-
case 'child5':
36-
return child5();
37-
case 'child6':
38-
return child6();
39-
case 'child7':
40-
return child7();
41-
case 'child8':
42-
return child8();
43-
case 'child9':
44-
return child9();
45-
case undefined:
46-
return parent();
47-
default:
48-
throw new Error('invalid');
49-
}
50-
51-
function child1() {
52-
process.exitCode = 42;
53-
process.on('exit', function(code) {
54-
assert.strictEqual(process.exitCode, 42);
55-
assert.strictEqual(code, 42);
56-
});
57-
}
58-
59-
function child2() {
60-
process.exitCode = 99;
61-
process.on('exit', function(code) {
62-
assert.strictEqual(process.exitCode, 42);
63-
assert.strictEqual(code, 42);
64-
});
65-
process.exit(42);
66-
}
67-
68-
function child3() {
69-
process.exitCode = 99;
70-
process.on('exit', function(code) {
71-
assert.strictEqual(process.exitCode, 0);
72-
assert.strictEqual(code, 0);
73-
});
74-
process.exit(0);
75-
}
76-
77-
function child4() {
78-
process.exitCode = 99;
79-
process.on('exit', function(code) {
80-
if (code !== 1 || process.exitCode !== 1) {
81-
console.log('wrong code! expected 1 for uncaughtException');
82-
process.exit(99);
83-
}
84-
});
85-
throw new Error('ok');
86-
}
87-
88-
function child5() {
89-
process.exitCode = 95;
90-
process.on('exit', function(code) {
91-
assert.strictEqual(process.exitCode, 95);
92-
assert.strictEqual(code, 95);
93-
process.exitCode = 99;
94-
});
95-
}
96-
97-
function child6() {
98-
process.on('exit', function(code) {
99-
assert.strictEqual(process.exitCode, 0);
100-
assert.strictEqual(code, 0);
101-
});
102-
process.on('uncaughtException', () => {});
103-
throw new Error('ok');
104-
}
105-
106-
function child7() {
107-
process.on('exit', function(code) {
108-
assert.strictEqual(process.exitCode, 97);
109-
assert.strictEqual(code, 97);
110-
});
111-
process.on('uncaughtException', () => {
112-
process.exitCode = 97;
113-
});
114-
throw new Error('ok');
115-
}
116-
117-
function child8() {
118-
process.on('exit', function(code) {
119-
assert.strictEqual(process.exitCode, 1);
120-
assert.strictEqual(code, 1);
121-
process.exitCode = 98;
122-
});
123-
throw new Error('ok');
124-
}
26+
const testCases = require('../fixtures/process-exit-code-cases');
12527

126-
function child9() {
127-
process.on('exit', function(code) {
128-
assert.strictEqual(process.exitCode, 1);
129-
assert.strictEqual(code, 1);
130-
process.exitCode = 0;
131-
});
132-
throw new Error('ok');
28+
if (!process.argv[2]) {
29+
parent();
30+
} else {
31+
const i = parseInt(process.argv[2]);
32+
if (Number.isNaN(i)) {
33+
console.log('Invalid test case index');
34+
process.exit(100);
35+
return;
36+
}
37+
testCases[i].func();
13338
}
13439

13540
function parent() {
@@ -138,22 +43,14 @@ function parent() {
13843
const f = __filename;
13944
const option = { stdio: [ 0, 1, 'ignore' ] };
14045

141-
const test = (arg, exit) => {
46+
const test = (arg, name = 'child', exit) => {
14247
spawn(node, [f, arg], option).on('exit', (code) => {
14348
assert.strictEqual(
14449
code, exit,
145-
`wrong exit for ${arg}\nexpected:${exit} but got:${code}`);
50+
`wrong exit for ${arg}-${name}\nexpected:${exit} but got:${code}`);
14651
console.log(`ok - ${arg} exited with ${exit}`);
14752
});
14853
};
14954

150-
test('child1', 42);
151-
test('child2', 42);
152-
test('child3', 0);
153-
test('child4', 1);
154-
test('child5', 99);
155-
test('child6', 0);
156-
test('child7', 97);
157-
test('child8', 98);
158-
test('child9', 0);
55+
testCases.forEach((tc, i) => test(i, tc.func.name, tc.result));
15956
}

0 commit comments

Comments
 (0)