@@ -11,12 +11,8 @@ const childProcess = require('child_process');
11
11
12
12
const nodeBinary = process . argv [ 0 ] ;
13
13
14
- const preloadOption = ( preloads ) => {
15
- let option = '' ;
16
- preloads . forEach ( function ( preload , index ) {
17
- option += `-r "${ preload } " ` ;
18
- } ) ;
19
- return option ;
14
+ const preloadOption = ( ...preloads ) => {
15
+ return preloads . flatMap ( ( preload ) => [ '-r' , preload ] ) ;
20
16
} ;
21
17
22
18
const fixtureA = fixtures . path ( 'printA.js' ) ;
@@ -34,75 +30,52 @@ assert(!module.isPreloading);
34
30
35
31
// Test that module.isPreloading is set in preloaded module
36
32
// Test preloading a single module works
37
- childProcess . exec (
38
- `"${ nodeBinary } " ${ preloadOption ( [ fixtureIsPreloading ] ) } "${ fixtureB } "` ,
39
- function ( err , stdout , stderr ) {
40
- assert . ifError ( err ) ;
41
- assert . strictEqual ( stdout , 'B\n' ) ;
42
- } ) ;
33
+ common . spawnPromisified ( nodeBinary , [ ...preloadOption ( fixtureIsPreloading ) , fixtureB ] ) . then ( common . mustCall (
34
+ ( { stdout } ) => {
35
+ assert . strictEqual ( stdout . trim ( ) , 'B' ) ;
36
+ }
37
+ ) ) ;
43
38
44
39
// Test preloading a single module works
45
- childProcess . exec ( `"${ nodeBinary } " ${ preloadOption ( [ fixtureA ] ) } "${ fixtureB } "` ,
46
- function ( err , stdout , stderr ) {
47
- assert . ifError ( err ) ;
48
- assert . strictEqual ( stdout , 'A\nB\n' ) ;
49
- } ) ;
40
+ common . spawnPromisified ( nodeBinary , [ ...preloadOption ( fixtureA ) , fixtureB ] ) . then ( common . mustCall (
41
+ ( { stdout } ) => {
42
+ assert . strictEqual ( stdout , 'A\nB\n' ) ;
43
+ } ) ) ;
50
44
51
45
// Test preloading multiple modules works
52
- childProcess . exec (
53
- `"${ nodeBinary } " ${ preloadOption ( [ fixtureA , fixtureB ] ) } "${ fixtureC } "` ,
54
- function ( err , stdout , stderr ) {
55
- assert . ifError ( err ) ;
46
+ common . spawnPromisified ( nodeBinary , [ ...preloadOption ( fixtureA , fixtureB ) , fixtureC ] ) . then ( common . mustCall (
47
+ ( { stdout } ) => {
56
48
assert . strictEqual ( stdout , 'A\nB\nC\n' ) ;
57
49
}
58
- ) ;
50
+ ) ) ;
59
51
60
52
// Test that preloading a throwing module aborts
61
- childProcess . exec (
62
- `"${ nodeBinary } " ${ preloadOption ( [ fixtureA , fixtureThrows ] ) } "${ fixtureB } "` ,
63
- function ( err , stdout , stderr ) {
64
- if ( err ) {
65
- assert . strictEqual ( stdout , 'A\n' ) ;
66
- } else {
67
- throw new Error ( 'Preload should have failed' ) ;
68
- }
53
+ common . spawnPromisified ( nodeBinary , [ ...preloadOption ( fixtureA , fixtureThrows ) , fixtureB ] ) . then ( common . mustCall (
54
+ ( { code, stdout } ) => {
55
+ assert . strictEqual ( stdout , 'A\n' ) ;
56
+ assert . strictEqual ( code , 1 ) ;
69
57
}
70
- ) ;
58
+ ) ) ;
71
59
72
60
// Test that preload can be used with --eval
73
- childProcess . exec (
74
- `"${ nodeBinary } " ${ preloadOption ( [ fixtureA ] ) } -e "console.log('hello');"` ,
75
- function ( err , stdout , stderr ) {
76
- assert . ifError ( err ) ;
61
+ common . spawnPromisified ( nodeBinary , [ ...preloadOption ( fixtureA ) , '-e' , 'console.log("hello");' ] ) . then ( common . mustCall (
62
+ ( { stdout } ) => {
77
63
assert . strictEqual ( stdout , 'A\nhello\n' ) ;
78
64
}
79
- ) ;
65
+ ) ) ;
80
66
81
67
// Test that preload can be used with --frozen-intrinsics
82
- childProcess . exec (
83
- `"${ nodeBinary } " --frozen-intrinsics ${
84
- preloadOption ( [ fixtureE ] )
85
- } ${
86
- fixtureF
87
- } `,
88
- function ( err , stdout ) {
89
- assert . ifError ( err ) ;
90
- assert . strictEqual ( stdout , 'smoosh\n' ) ;
91
- }
92
- ) ;
93
- childProcess . exec (
94
- `"${
95
- nodeBinary
96
- } " --frozen-intrinsics ${
97
- preloadOption ( [ fixtureE ] )
98
- } ${
99
- fixtureG
100
- } ${ fixtureF } `,
101
- function ( err , stdout ) {
102
- assert . ifError ( err ) ;
68
+ common . spawnPromisified ( nodeBinary , [ '--frozen-intrinsics' , ...preloadOption ( fixtureE ) , fixtureF ] ) . then ( common . mustCall (
69
+ ( { stdout } ) => {
103
70
assert . strictEqual ( stdout , 'smoosh\n' ) ;
104
71
}
105
- ) ;
72
+ ) ) ;
73
+ common . spawnPromisified ( nodeBinary , [ '--frozen-intrinsics' , ...preloadOption ( fixtureE ) , fixtureG , fixtureF ] )
74
+ . then ( common . mustCall (
75
+ ( { stdout } ) => {
76
+ assert . strictEqual ( stdout , 'smoosh\n' ) ;
77
+ }
78
+ ) ) ;
106
79
107
80
// Test that preload can be used with stdin
108
81
const stdinProc = childProcess . spawn (
@@ -143,61 +116,62 @@ replProc.on('close', function(code) {
143
116
144
117
// Test that preload placement at other points in the cmdline
145
118
// also test that duplicated preload only gets loaded once
146
- childProcess . exec (
147
- `"${ nodeBinary } " ${ preloadOption ( [ fixtureA ] ) } -e "console.log('hello');" ${
148
- preloadOption ( [ fixtureA , fixtureB ] ) } `,
149
- function ( err , stdout , stderr ) {
150
- assert . ifError ( err ) ;
119
+ common . spawnPromisified ( nodeBinary , [
120
+ ...preloadOption ( fixtureA ) ,
121
+ '-e' , 'console.log("hello");' ,
122
+ ...preloadOption ( fixtureA , fixtureB ) ,
123
+ ] ) . then ( common . mustCall (
124
+ ( { stdout } ) => {
151
125
assert . strictEqual ( stdout , 'A\nB\nhello\n' ) ;
152
126
}
153
- ) ;
127
+ ) ) ;
154
128
155
129
// Test that preload works with -i
156
- const interactive = childProcess . exec (
157
- `"${ nodeBinary } " ${ preloadOption ( [ fixtureD ] ) } -i` ,
158
- common . mustSucceed ( ( stdout , stderr ) => {
159
- assert . ok ( stdout . endsWith ( "> 'test'\n> " ) ) ;
160
- } )
161
- ) ;
130
+ const interactive = childProcess . spawn ( nodeBinary , [ ...preloadOption ( fixtureD ) , '-i' ] ) ;
131
+
132
+ {
133
+ const stdout = [ ] ;
134
+ interactive . stdout . on ( 'data' , ( chunk ) => {
135
+ stdout . push ( chunk ) ;
136
+ } ) ;
137
+ interactive . on ( 'close' , common . mustCall ( ( ) => {
138
+ assert . match ( Buffer . concat ( stdout ) . toString ( 'utf8' ) , / > ' t e s t ' \r ? \n > $ / ) ;
139
+ } ) ) ;
140
+ }
162
141
163
142
interactive . stdin . write ( 'a\n' ) ;
164
143
interactive . stdin . write ( 'process.exit()\n' ) ;
165
144
166
- childProcess . exec (
167
- `"${ nodeBinary } " --require "${ fixtures . path ( 'cluster-preload.js' ) } " "${
168
- fixtures . path ( 'cluster-preload-test.js' ) } "`,
169
- function ( err , stdout , stderr ) {
170
- assert . ifError ( err ) ;
145
+ common . spawnPromisified ( nodeBinary ,
146
+ [ '--require' , fixtures . path ( 'cluster-preload.js' ) , fixtures . path ( 'cluster-preload-test.js' ) ] )
147
+ . then ( common . mustCall ( ( { stdout } ) => {
171
148
assert . match ( stdout , / w o r k e r t e r m i n a t e d w i t h c o d e 4 3 / ) ;
172
- }
173
- ) ;
149
+ } ) ) ;
174
150
175
151
// Test that preloading with a relative path works
176
- childProcess . exec (
177
- `" ${ nodeBinary } " ${ preloadOption ( [ './printA.js' ] ) } " ${ fixtureB } "` ,
178
- { cwd : fixtures . fixturesDir } ,
179
- common . mustSucceed ( ( stdout , stderr ) => {
152
+ common . spawnPromisified ( nodeBinary ,
153
+ [ ... preloadOption ( './printA.js' ) , fixtureB ] ,
154
+ { cwd : fixtures . fixturesDir } ) . then ( common . mustCall (
155
+ ( { stdout } ) => {
180
156
assert . strictEqual ( stdout , 'A\nB\n' ) ;
181
157
} )
182
158
) ;
183
159
if ( common . isWindows ) {
184
160
// https://github.com/nodejs/node/issues/21918
185
- childProcess . exec (
186
- `" ${ nodeBinary } " ${ preloadOption ( [ '.\\printA.js' ] ) } " ${ fixtureB } "` ,
187
- { cwd : fixtures . fixturesDir } ,
188
- common . mustSucceed ( ( stdout , stderr ) => {
161
+ common . spawnPromisified ( nodeBinary ,
162
+ [ ... preloadOption ( '.\\printA.js' ) , fixtureB ] ,
163
+ { cwd : fixtures . fixturesDir } ) . then ( common . mustCall (
164
+ ( { stdout } ) => {
189
165
assert . strictEqual ( stdout , 'A\nB\n' ) ;
190
166
} )
191
167
) ;
192
168
}
193
169
194
170
// https://github.com/nodejs/node/issues/1691
195
- childProcess . exec (
196
- `"${ nodeBinary } " --require ` +
197
- `"${ fixtures . path ( 'cluster-preload.js' ) } " cluster-preload-test.js` ,
198
- { cwd : fixtures . fixturesDir } ,
199
- function ( err , stdout , stderr ) {
200
- assert . ifError ( err ) ;
171
+ common . spawnPromisified ( nodeBinary ,
172
+ [ '--require' , fixtures . path ( 'cluster-preload.js' ) , 'cluster-preload-test.js' ] ,
173
+ { cwd : fixtures . fixturesDir } ) . then ( common . mustCall (
174
+ ( { stdout } ) => {
201
175
assert . match ( stdout , / w o r k e r t e r m i n a t e d w i t h c o d e 4 3 / ) ;
202
176
}
203
- ) ;
177
+ ) ) ;
0 commit comments