1
1
import * as common from '../common/index.mjs' ;
2
2
import { describe , it , beforeEach } from 'node:test' ;
3
- import { once } from 'node:events' ;
4
3
import assert from 'node:assert' ;
5
4
import { spawn } from 'node:child_process' ;
6
5
import { writeFileSync } from 'node:fs' ;
@@ -19,27 +18,10 @@ if (common.isAIX) {
19
18
}
20
19
21
20
const indexContents = `
22
- const { setTimeout } = require("timers/promises");
23
- (async () => {
24
- // Wait a few milliseconds to make sure that the
25
- // parent process has time to attach its listeners
26
- await setTimeout(200);
27
-
28
- process.on('SIGTERM', () => {
29
- console.log('__SIGTERM received__');
30
- process.exit(123);
31
- });
32
-
33
- process.on('SIGINT', () => {
34
- console.log('__SIGINT received__');
35
- process.exit(124);
36
- });
37
-
38
- console.log('ready!');
39
-
40
- // Wait for a long time (just to keep the process alive)
41
- await setTimeout(100_000_000);
42
- })();
21
+ process.on('SIGTERM', () => { console.log('__SIGTERM received__'); process.exit(); });
22
+ process.on('SIGINT', () => { console.log('__SIGINT received__'); process.exit(); });
23
+ process.send('script ready');
24
+ setTimeout(() => {}, 100_000);
43
25
` ;
44
26
45
27
let indexPath = '' ;
@@ -54,60 +36,82 @@ describe('test runner watch mode with --watch-kill-signal', () => {
54
36
beforeEach ( refresh ) ;
55
37
56
38
it ( 'defaults to SIGTERM' , async ( ) => {
57
- let currentRun = Promise . withResolvers ( ) ;
58
- const child = spawn ( process . execPath , [ '--watch' , indexPath ] , {
59
- cwd : tmpdir . path ,
60
- } ) ;
39
+ const child = spawn (
40
+ process . execPath ,
41
+ [ '--watch' , indexPath ] ,
42
+ {
43
+ cwd : tmpdir . path ,
44
+ stdio : [ 'pipe' , 'pipe' , 'pipe' , 'ipc' ] ,
45
+ }
46
+ ) ;
61
47
62
48
let stdout = '' ;
63
49
child . stdout . on ( 'data' , ( data ) => {
64
- stdout += data . toString ( ) ;
65
- currentRun . resolve ( ) ;
50
+ stdout += `${ data } ` ;
51
+ if ( / _ _ ( S I G I N T | S I G T E R M ) r e c e i v e d _ _ / . test ( stdout ) ) {
52
+ child . kill ( ) ;
53
+ }
66
54
} ) ;
67
55
68
- await currentRun . promise ;
56
+ child . on ( 'message' , ( msg ) => {
57
+ if ( msg === 'script ready' ) {
58
+ writeFileSync ( indexPath , indexContents ) ;
59
+ }
60
+ } ) ;
69
61
70
- currentRun = Promise . withResolvers ( ) ;
71
- writeFileSync ( indexPath , indexContents ) ;
62
+ await new Promise ( ( resolve ) =>
63
+ child . on ( 'exit' , ( ) => {
64
+ resolve ( ) ;
65
+ } )
66
+ ) ;
72
67
73
- await currentRun . promise ;
74
- child . kill ( ) ;
75
- const [ exitCode ] = await once ( child , 'exit' ) ;
76
68
assert . match ( stdout , / _ _ S I G T E R M r e c e i v e d _ _ / ) ;
77
- assert . strictEqual ( exitCode , 123 ) ;
69
+ assert . doesNotMatch ( stdout , / _ _ S I G I N T r e c e i v e d _ _ / ) ;
78
70
} ) ;
79
71
80
72
it ( 'can be overridden (to SIGINT)' , async ( ) => {
81
- let currentRun = Promise . withResolvers ( ) ;
82
- const child = spawn ( process . execPath , [ '--watch' , '--watch-kill-signal' , 'SIGINT' , indexPath ] , {
83
- cwd : tmpdir . path ,
84
- } ) ;
85
- let stdout = '' ;
73
+ const child = spawn (
74
+ process . execPath ,
75
+ [ '--watch' , '--watch-kill-signal' , 'SIGINT' , indexPath ] ,
76
+ {
77
+ cwd : tmpdir . path ,
78
+ stdio : [ 'pipe' , 'pipe' , 'pipe' , 'ipc' ] ,
79
+ }
80
+ ) ;
86
81
82
+ let stdout = '' ;
87
83
child . stdout . on ( 'data' , ( data ) => {
88
- stdout += data . toString ( ) ;
89
- if ( stdout . includes ( 'ready!' ) ) {
90
- currentRun . resolve ( ) ;
84
+ stdout += ` ${ data } ` ;
85
+ if ( / _ _ ( S I G I N T | S I G T E R M ) r e c e i v e d _ _ / . test ( stdout ) ) {
86
+ child . kill ( ) ;
91
87
}
92
88
} ) ;
93
89
94
- await currentRun . promise ;
90
+ child . on ( 'message' , ( msg ) => {
91
+ if ( msg === 'script ready' ) {
92
+ writeFileSync ( indexPath , indexContents ) ;
93
+ }
94
+ } ) ;
95
95
96
- currentRun = Promise . withResolvers ( ) ;
97
- writeFileSync ( indexPath , indexContents ) ;
96
+ await new Promise ( ( resolve ) =>
97
+ child . on ( 'exit' , ( ) => {
98
+ resolve ( ) ;
99
+ } )
100
+ ) ;
98
101
99
- await currentRun . promise ;
100
- child . kill ( ) ;
101
- const [ exitCode ] = await once ( child , 'exit' ) ;
102
102
assert . match ( stdout , / _ _ S I G I N T r e c e i v e d _ _ / ) ;
103
- assert . strictEqual ( exitCode , 124 ) ;
103
+ assert . doesNotMatch ( stdout , / _ _ S I G T E R M r e c e i v e d _ _ / ) ;
104
104
} ) ;
105
105
106
106
it ( 'errors if an invalid signal is provided' , async ( ) => {
107
107
const currentRun = Promise . withResolvers ( ) ;
108
- const child = spawn ( process . execPath , [ '--watch' , '--watch-kill-signal' , 'invalid_signal' , indexPath ] , {
109
- cwd : tmpdir . path ,
110
- } ) ;
108
+ const child = spawn (
109
+ process . execPath ,
110
+ [ '--watch' , '--watch-kill-signal' , 'invalid_signal' , indexPath ] ,
111
+ {
112
+ cwd : tmpdir . path ,
113
+ }
114
+ ) ;
111
115
let stdout = '' ;
112
116
113
117
child . stderr . on ( 'data' , ( data ) => {
@@ -117,6 +121,11 @@ describe('test runner watch mode with --watch-kill-signal', () => {
117
121
118
122
await currentRun . promise ;
119
123
120
- assert . match ( stdout , new RegExp ( / T y p e E r r o r \[ E R R _ U N K N O W N _ S I G N A L \] : U n k n o w n s i g n a l : i n v a l i d _ s i g n a l / ) ) ;
124
+ assert . match (
125
+ stdout ,
126
+ new RegExp (
127
+ / T y p e E r r o r \[ E R R _ U N K N O W N _ S I G N A L \] : U n k n o w n s i g n a l : i n v a l i d _ s i g n a l /
128
+ )
129
+ ) ;
121
130
} ) ;
122
131
} ) ;
0 commit comments