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