-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
/
Copy pathprocess-exit-code-cases.js
138 lines (126 loc) Β· 3.63 KB
/
process-exit-code-cases.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
'use strict';
const assert = require('assert');
function getTestCases(isWorker = false) {
const cases = [];
function exitsOnExitCodeSet() {
process.exitCode = 42;
process.on('exit', (code) => {
assert.strictEqual(process.exitCode, 42);
assert.strictEqual(code, 42);
});
}
cases.push({ func: exitsOnExitCodeSet, result: 42 });
function changesCodeViaExit() {
process.exitCode = 99;
process.on('exit', (code) => {
assert.strictEqual(process.exitCode, 42);
assert.strictEqual(code, 42);
});
process.exit(42);
}
cases.push({ func: changesCodeViaExit, result: 42 });
function changesCodeZeroExit() {
process.exitCode = 99;
process.on('exit', (code) => {
assert.strictEqual(process.exitCode, 0);
assert.strictEqual(code, 0);
});
process.exit(0);
}
cases.push({ func: changesCodeZeroExit, result: 0 });
function exitWithOneOnUncaught() {
process.exitCode = 99;
process.on('exit', (code) => {
// Cannot use assert because it will be uncaughtException -> 1 exit code
// that will render this test useless
if (code !== 1 || process.exitCode !== 1) {
console.log('wrong code! expected 1 for uncaughtException');
process.exit(99);
}
});
throw new Error('ok');
}
cases.push({
func: exitWithOneOnUncaught,
result: 1,
error: /^Error: ok$/,
});
function changeCodeInsideExit() {
process.exitCode = 95;
process.on('exit', (code) => {
assert.strictEqual(process.exitCode, 95);
assert.strictEqual(code, 95);
process.exitCode = 99;
});
}
cases.push({ func: changeCodeInsideExit, result: 99 });
function zeroExitWithUncaughtHandler() {
const noop = () => { };
process.on('exit', (code) => {
process.off('uncaughtException', noop);
assert.strictEqual(process.exitCode, undefined);
assert.strictEqual(code, 0);
});
process.on('uncaughtException', noop);
throw new Error('ok');
}
cases.push({ func: zeroExitWithUncaughtHandler, result: 0 });
function changeCodeInUncaughtHandler() {
const modifyExitCode = () => { process.exitCode = 97; };
process.on('exit', (code) => {
process.off('uncaughtException', modifyExitCode);
assert.strictEqual(process.exitCode, 97);
assert.strictEqual(code, 97);
});
process.on('uncaughtException', modifyExitCode);
throw new Error('ok');
}
cases.push({ func: changeCodeInUncaughtHandler, result: 97 });
function changeCodeInExitWithUncaught() {
process.on('exit', (code) => {
assert.strictEqual(process.exitCode, 1);
assert.strictEqual(code, 1);
process.exitCode = 98;
});
throw new Error('ok');
}
cases.push({
func: changeCodeInExitWithUncaught,
result: 98,
error: /^Error: ok$/,
});
function exitWithZeroInExitWithUncaught() {
process.on('exit', (code) => {
assert.strictEqual(process.exitCode, 1);
assert.strictEqual(code, 1);
process.exitCode = 0;
});
throw new Error('ok');
}
cases.push({
func: exitWithZeroInExitWithUncaught,
result: 0,
error: /^Error: ok$/,
});
function exitWithThrowInUncaughtHandler() {
process.on('uncaughtException', () => {
throw new Error('ok');
});
throw new Error('bad');
}
cases.push({
func: exitWithThrowInUncaughtHandler,
result: isWorker ? 1 : 7,
error: /^Error: ok$/,
});
function exitWithUndefinedFatalException() {
process._fatalException = undefined;
throw new Error('ok');
}
cases.push({
func: exitWithUndefinedFatalException,
result: 6,
});
return cases;
}
exports.getTestCases = getTestCases;