@@ -10,12 +10,25 @@ function runChecks(err, stdio, streamName, expected) {
1010 assert . deepStrictEqual ( stdio [ streamName ] , expected ) ;
1111}
1212
13+ // The execPath might contain chars that should be escaped in a shell context.
14+ // On non-Windows, we can pass the path via the env; `"` is not a valid char on
15+ // Windows, so we can simply pass the path.
16+ const execNode = ( args , optionsOrCallback , callback ) => {
17+ let options = optionsOrCallback ;
18+ if ( typeof optionsOrCallback === 'function' ) {
19+ options = undefined ;
20+ callback = optionsOrCallback ;
21+ }
22+ return cp . exec (
23+ `"${ common . isWindows ? process . execPath : '$NODE' } " ${ args } ` ,
24+ common . isWindows ? options : { ...options , env : { ...process . env , NODE : process . execPath } } ,
25+ callback ,
26+ ) ;
27+ } ;
28+
1329// default value
1430{
15- const cmd =
16- `"${ process . execPath } " -e "console.log('a'.repeat(1024 * 1024))"` ;
17-
18- cp . exec ( cmd , common . mustCall ( ( err ) => {
31+ execNode ( `-e "console.log('a'.repeat(1024 * 1024))"` , common . mustCall ( ( err ) => {
1932 assert ( err instanceof RangeError ) ;
2033 assert . strictEqual ( err . message , 'stdout maxBuffer length exceeded' ) ;
2134 assert . strictEqual ( err . code , 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER' ) ;
@@ -24,20 +37,16 @@ function runChecks(err, stdio, streamName, expected) {
2437
2538// default value
2639{
27- const cmd =
28- `${ process . execPath } -e "console.log('a'.repeat(1024 * 1024 - 1))"` ;
29-
30- cp . exec ( cmd , common . mustSucceed ( ( stdout , stderr ) => {
40+ execNode ( `-e "console.log('a'.repeat(1024 * 1024 - 1))"` , common . mustSucceed ( ( stdout , stderr ) => {
3141 assert . strictEqual ( stdout . trim ( ) , 'a' . repeat ( 1024 * 1024 - 1 ) ) ;
3242 assert . strictEqual ( stderr , '' ) ;
3343 } ) ) ;
3444}
3545
3646{
37- const cmd = `"${ process . execPath } " -e "console.log('hello world');"` ;
3847 const options = { maxBuffer : Infinity } ;
3948
40- cp . exec ( cmd , options , common . mustSucceed ( ( stdout , stderr ) => {
49+ execNode ( `-e "console.log('hello world');"` , options , common . mustSucceed ( ( stdout , stderr ) => {
4150 assert . strictEqual ( stdout . trim ( ) , 'hello world' ) ;
4251 assert . strictEqual ( stderr , '' ) ;
4352 } ) ) ;
@@ -57,11 +66,8 @@ function runChecks(err, stdio, streamName, expected) {
5766
5867// default value
5968{
60- const cmd =
61- `"${ process . execPath } " -e "console.log('a'.repeat(1024 * 1024))"` ;
62-
63- cp . exec (
64- cmd ,
69+ execNode (
70+ `-e "console.log('a'.repeat(1024 * 1024))"` ,
6571 common . mustCall ( ( err , stdout , stderr ) => {
6672 runChecks (
6773 err ,
@@ -75,10 +81,7 @@ function runChecks(err, stdio, streamName, expected) {
7581
7682// default value
7783{
78- const cmd =
79- `"${ process . execPath } " -e "console.log('a'.repeat(1024 * 1024 - 1))"` ;
80-
81- cp . exec ( cmd , common . mustSucceed ( ( stdout , stderr ) => {
84+ execNode ( `-e "console.log('a'.repeat(1024 * 1024 - 1))"` , common . mustSucceed ( ( stdout , stderr ) => {
8285 assert . strictEqual ( stdout . trim ( ) , 'a' . repeat ( 1024 * 1024 - 1 ) ) ;
8386 assert . strictEqual ( stderr , '' ) ;
8487 } ) ) ;
@@ -87,10 +90,8 @@ function runChecks(err, stdio, streamName, expected) {
8790const unicode = '中文测试' ; // length = 4, byte length = 12
8891
8992{
90- const cmd = `"${ process . execPath } " -e "console.log('${ unicode } ');"` ;
91-
92- cp . exec (
93- cmd ,
93+ execNode (
94+ `-e "console.log('${ unicode } ');"` ,
9495 { maxBuffer : 10 } ,
9596 common . mustCall ( ( err , stdout , stderr ) => {
9697 runChecks ( err , { stdout, stderr } , 'stdout' , '中文测试\n' ) ;
@@ -99,10 +100,8 @@ const unicode = '中文测试'; // length = 4, byte length = 12
99100}
100101
101102{
102- const cmd = `"${ process . execPath } " -e "console.error('${ unicode } ');"` ;
103-
104- cp . exec (
105- cmd ,
103+ execNode (
104+ `-e "console.error('${ unicode } ');"` ,
106105 { maxBuffer : 3 } ,
107106 common . mustCall ( ( err , stdout , stderr ) => {
108107 runChecks ( err , { stdout, stderr } , 'stderr' , '中文测' ) ;
@@ -111,10 +110,8 @@ const unicode = '中文测试'; // length = 4, byte length = 12
111110}
112111
113112{
114- const cmd = `"${ process . execPath } " -e "console.log('${ unicode } ');"` ;
115-
116- const child = cp . exec (
117- cmd ,
113+ const child = execNode (
114+ `-e "console.log('${ unicode } ');"` ,
118115 { encoding : null , maxBuffer : 10 } ,
119116 common . mustCall ( ( err , stdout , stderr ) => {
120117 runChecks ( err , { stdout, stderr } , 'stdout' , '中文测试\n' ) ;
@@ -125,10 +122,8 @@ const unicode = '中文测试'; // length = 4, byte length = 12
125122}
126123
127124{
128- const cmd = `"${ process . execPath } " -e "console.error('${ unicode } ');"` ;
129-
130- const child = cp . exec (
131- cmd ,
125+ const child = execNode (
126+ `-e "console.error('${ unicode } ');"` ,
132127 { encoding : null , maxBuffer : 3 } ,
133128 common . mustCall ( ( err , stdout , stderr ) => {
134129 runChecks ( err , { stdout, stderr } , 'stderr' , '中文测' ) ;
@@ -139,10 +134,8 @@ const unicode = '中文测试'; // length = 4, byte length = 12
139134}
140135
141136{
142- const cmd = `"${ process . execPath } " -e "console.error('${ unicode } ');"` ;
143-
144- cp . exec (
145- cmd ,
137+ execNode (
138+ `-e "console.error('${ unicode } ');"` ,
146139 { encoding : null , maxBuffer : 5 } ,
147140 common . mustCall ( ( err , stdout , stderr ) => {
148141 const buf = Buffer . from ( unicode ) . slice ( 0 , 5 ) ;
0 commit comments