@@ -11,12 +11,8 @@ const childProcess = require('child_process');
1111
1212const nodeBinary = process . argv [ 0 ] ;
1313
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 ] ) ;
2016} ;
2117
2218const fixtureA = fixtures . path ( 'printA.js' ) ;
@@ -34,75 +30,52 @@ assert(!module.isPreloading);
3430
3531// Test that module.isPreloading is set in preloaded module
3632// 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+ ) ) ;
4338
4439// 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+ } ) ) ;
5044
5145// 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 } ) => {
5648 assert . strictEqual ( stdout , 'A\nB\nC\n' ) ;
5749 }
58- ) ;
50+ ) ) ;
5951
6052// 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 ) ;
6957 }
70- ) ;
58+ ) ) ;
7159
7260// 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 } ) => {
7763 assert . strictEqual ( stdout , 'A\nhello\n' ) ;
7864 }
79- ) ;
65+ ) ) ;
8066
8167// 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 } ) => {
10370 assert . strictEqual ( stdout , 'smoosh\n' ) ;
10471 }
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+ ) ) ;
10679
10780// Test that preload can be used with stdin
10881const stdinProc = childProcess . spawn (
@@ -143,61 +116,62 @@ replProc.on('close', function(code) {
143116
144117// Test that preload placement at other points in the cmdline
145118// 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 } ) => {
151125 assert . strictEqual ( stdout , 'A\nB\nhello\n' ) ;
152126 }
153- ) ;
127+ ) ) ;
154128
155129// 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+ }
162141
163142interactive . stdin . write ( 'a\n' ) ;
164143interactive . stdin . write ( 'process.exit()\n' ) ;
165144
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 } ) => {
171148 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+ } ) ) ;
174150
175151// 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 } ) => {
180156 assert . strictEqual ( stdout , 'A\nB\n' ) ;
181157 } )
182158) ;
183159if ( common . isWindows ) {
184160 // 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 } ) => {
189165 assert . strictEqual ( stdout , 'A\nB\n' ) ;
190166 } )
191167 ) ;
192168}
193169
194170// 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 } ) => {
201175 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 / ) ;
202176 }
203- ) ;
177+ ) ) ;
0 commit comments