@@ -10,12 +10,25 @@ function runChecks(err, stdio, streamName, expected) {
10
10
assert . deepStrictEqual ( stdio [ streamName ] , expected ) ;
11
11
}
12
12
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
+
13
29
// default value
14
30
{
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 ) => {
19
32
assert ( err instanceof RangeError ) ;
20
33
assert . strictEqual ( err . message , 'stdout maxBuffer length exceeded' ) ;
21
34
assert . strictEqual ( err . code , 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER' ) ;
@@ -24,20 +37,16 @@ function runChecks(err, stdio, streamName, expected) {
24
37
25
38
// default value
26
39
{
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 ) => {
31
41
assert . strictEqual ( stdout . trim ( ) , 'a' . repeat ( 1024 * 1024 - 1 ) ) ;
32
42
assert . strictEqual ( stderr , '' ) ;
33
43
} ) ) ;
34
44
}
35
45
36
46
{
37
- const cmd = `"${ process . execPath } " -e "console.log('hello world');"` ;
38
47
const options = { maxBuffer : Infinity } ;
39
48
40
- cp . exec ( cmd , options , common . mustSucceed ( ( stdout , stderr ) => {
49
+ execNode ( `-e "console.log('hello world');"` , options , common . mustSucceed ( ( stdout , stderr ) => {
41
50
assert . strictEqual ( stdout . trim ( ) , 'hello world' ) ;
42
51
assert . strictEqual ( stderr , '' ) ;
43
52
} ) ) ;
@@ -57,11 +66,8 @@ function runChecks(err, stdio, streamName, expected) {
57
66
58
67
// default value
59
68
{
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))"` ,
65
71
common . mustCall ( ( err , stdout , stderr ) => {
66
72
runChecks (
67
73
err ,
@@ -75,10 +81,7 @@ function runChecks(err, stdio, streamName, expected) {
75
81
76
82
// default value
77
83
{
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 ) => {
82
85
assert . strictEqual ( stdout . trim ( ) , 'a' . repeat ( 1024 * 1024 - 1 ) ) ;
83
86
assert . strictEqual ( stderr , '' ) ;
84
87
} ) ) ;
@@ -87,10 +90,8 @@ function runChecks(err, stdio, streamName, expected) {
87
90
const unicode = '中文测试' ; // length = 4, byte length = 12
88
91
89
92
{
90
- const cmd = `"${ process . execPath } " -e "console.log('${ unicode } ');"` ;
91
-
92
- cp . exec (
93
- cmd ,
93
+ execNode (
94
+ `-e "console.log('${ unicode } ');"` ,
94
95
{ maxBuffer : 10 } ,
95
96
common . mustCall ( ( err , stdout , stderr ) => {
96
97
runChecks ( err , { stdout, stderr } , 'stdout' , '中文测试\n' ) ;
@@ -99,10 +100,8 @@ const unicode = '中文测试'; // length = 4, byte length = 12
99
100
}
100
101
101
102
{
102
- const cmd = `"${ process . execPath } " -e "console.error('${ unicode } ');"` ;
103
-
104
- cp . exec (
105
- cmd ,
103
+ execNode (
104
+ `-e "console.error('${ unicode } ');"` ,
106
105
{ maxBuffer : 3 } ,
107
106
common . mustCall ( ( err , stdout , stderr ) => {
108
107
runChecks ( err , { stdout, stderr } , 'stderr' , '中文测' ) ;
@@ -111,10 +110,8 @@ const unicode = '中文测试'; // length = 4, byte length = 12
111
110
}
112
111
113
112
{
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 } ');"` ,
118
115
{ encoding : null , maxBuffer : 10 } ,
119
116
common . mustCall ( ( err , stdout , stderr ) => {
120
117
runChecks ( err , { stdout, stderr } , 'stdout' , '中文测试\n' ) ;
@@ -125,10 +122,8 @@ const unicode = '中文测试'; // length = 4, byte length = 12
125
122
}
126
123
127
124
{
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 } ');"` ,
132
127
{ encoding : null , maxBuffer : 3 } ,
133
128
common . mustCall ( ( err , stdout , stderr ) => {
134
129
runChecks ( err , { stdout, stderr } , 'stderr' , '中文测' ) ;
@@ -139,10 +134,8 @@ const unicode = '中文测试'; // length = 4, byte length = 12
139
134
}
140
135
141
136
{
142
- const cmd = `"${ process . execPath } " -e "console.error('${ unicode } ');"` ;
143
-
144
- cp . exec (
145
- cmd ,
137
+ execNode (
138
+ `-e "console.error('${ unicode } ');"` ,
146
139
{ encoding : null , maxBuffer : 5 } ,
147
140
common . mustCall ( ( err , stdout , stderr ) => {
148
141
const buf = Buffer . from ( unicode ) . slice ( 0 , 5 ) ;
0 commit comments