Skip to content

Commit 600349a

Browse files
lundibunditargos
authored andcommitted
test: refactor process/worker exitCode tests
PR-URL: #21739 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 961f6e8 commit 600349a

3 files changed

Lines changed: 133 additions & 234 deletions

File tree

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
'use strict';
2+
3+
constassert=require('assert');
4+
5+
constcases=[];
6+
module.exports=cases;
7+
8+
functionexitsOnExitCodeSet(){
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+
functionchangesCodeViaExit(){
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+
functionchangesCodeZeroExit(){
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+
functionexitWithOneOnUncaught(){
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+
thrownewError('ok');
48+
}
49+
cases.push({
50+
func: exitWithOneOnUncaught,
51+
result: 1,
52+
error: /^Error:ok$/,
53+
});
54+
55+
functionchangeCodeInsideExit(){
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+
functionzeroExitWithUncaughtHandler(){
66+
process.on('exit',(code)=>{
67+
assert.strictEqual(process.exitCode,0);
68+
assert.strictEqual(code,0);
69+
});
70+
process.on('uncaughtException',()=>{});
71+
thrownewError('ok');
72+
}
73+
cases.push({func: zeroExitWithUncaughtHandler,result: 0});
74+
75+
functionchangeCodeInUncaughtHandler(){
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+
thrownewError('ok');
84+
}
85+
cases.push({func: changeCodeInUncaughtHandler,result: 97});
86+
87+
functionchangeCodeInExitWithUncaught(){
88+
process.on('exit',(code)=>{
89+
assert.strictEqual(process.exitCode,1);
90+
assert.strictEqual(code,1);
91+
process.exitCode=98;
92+
});
93+
thrownewError('ok');
94+
}
95+
cases.push({
96+
func: changeCodeInExitWithUncaught,
97+
result: 98,
98+
error: /^Error:ok$/,
99+
});
100+
101+
functionexitWithZeroInExitWithUncaught(){
102+
process.on('exit',(code)=>{
103+
assert.strictEqual(process.exitCode,1);
104+
assert.strictEqual(code,1);
105+
process.exitCode=0;
106+
});
107+
thrownewError('ok');
108+
}
109+
cases.push({
110+
func: exitWithZeroInExitWithUncaught,
111+
result: 0,
112+
error: /^Error:ok$/,
113+
});

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

Lines changed: 14 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -23,113 +23,18 @@
2323
require('../common');
2424
constassert=require('assert');
2525

26-
switch(process.argv[2]){
27-
case'child1':
28-
returnchild1();
29-
case'child2':
30-
returnchild2();
31-
case'child3':
32-
returnchild3();
33-
case'child4':
34-
returnchild4();
35-
case'child5':
36-
returnchild5();
37-
case'child6':
38-
returnchild6();
39-
case'child7':
40-
returnchild7();
41-
case'child8':
42-
returnchild8();
43-
case'child9':
44-
returnchild9();
45-
caseundefined:
46-
returnparent();
47-
default:
48-
thrownewError('invalid');
49-
}
50-
51-
functionchild1(){
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-
functionchild2(){
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-
functionchild3(){
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-
functionchild4(){
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-
thrownewError('ok');
86-
}
87-
88-
functionchild5(){
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-
functionchild6(){
98-
process.on('exit',function(code){
99-
assert.strictEqual(process.exitCode,0);
100-
assert.strictEqual(code,0);
101-
});
102-
process.on('uncaughtException',()=>{});
103-
thrownewError('ok');
104-
}
105-
106-
functionchild7(){
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-
thrownewError('ok');
115-
}
116-
117-
functionchild8(){
118-
process.on('exit',function(code){
119-
assert.strictEqual(process.exitCode,1);
120-
assert.strictEqual(code,1);
121-
process.exitCode=98;
122-
});
123-
thrownewError('ok');
124-
}
26+
consttestCases=require('../fixtures/process-exit-code-cases');
12527

126-
functionchild9(){
127-
process.on('exit',function(code){
128-
assert.strictEqual(process.exitCode,1);
129-
assert.strictEqual(code,1);
130-
process.exitCode=0;
131-
});
132-
thrownewError('ok');
28+
if(!process.argv[2]){
29+
parent();
30+
}else{
31+
consti=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
functionparent(){
@@ -138,22 +43,14 @@ function parent() {
13843
constf=__filename;
13944
constoption={stdio: [0,1,'ignore']};
14045

141-
consttest=(arg,exit)=>{
46+
consttest=(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)