@@ -60,6 +60,31 @@ const hasOpenSSL3 = hasCrypto &&
6060
6161const hasQuic = hasCrypto && ! ! process . config . variables . openssl_quic ;
6262
63+ function parseTestFlags ( filename = process . argv [ 1 ] ) {
64+ // The copyright notice is relatively big and the flags could come afterwards.
65+ const bytesToRead = 1500 ;
66+ const buffer = Buffer . allocUnsafe ( bytesToRead ) ;
67+ const fd = fs . openSync ( filename , 'r' ) ;
68+ const bytesRead = fs . readSync ( fd , buffer , 0 , bytesToRead ) ;
69+ fs . closeSync ( fd ) ;
70+ const source = buffer . toString ( 'utf8' , 0 , bytesRead ) ;
71+
72+ const flagStart = source . indexOf ( '// Flags: --' ) + 10 ;
73+
74+ if ( flagStart === 9 ) {
75+ return [ ] ;
76+ }
77+ let flagEnd = source . indexOf ( '\n' , flagStart ) ;
78+ // Normalize different EOL.
79+ if ( source [ flagEnd - 1 ] === '\r' ) {
80+ flagEnd -- ;
81+ }
82+ return source
83+ . substring ( flagStart , flagEnd )
84+ . replace ( / _ / g, '-' )
85+ . split ( ' ' ) ;
86+ }
87+
6388// Check for flags. Skip this for workers (both, the `cluster` module and
6489// `worker_threads`) and child processes.
6590// If the binary was built without-ssl then the crypto flags are
@@ -70,44 +95,25 @@ if (process.argv.length === 2 &&
7095 hasCrypto &&
7196 require ( 'cluster' ) . isPrimary &&
7297 fs . existsSync ( process . argv [ 1 ] ) ) {
73- // The copyright notice is relatively big and the flags could come afterwards.
74- const bytesToRead = 1500 ;
75- const buffer = Buffer . allocUnsafe ( bytesToRead ) ;
76- const fd = fs . openSync ( process . argv [ 1 ] , 'r' ) ;
77- const bytesRead = fs . readSync ( fd , buffer , 0 , bytesToRead ) ;
78- fs . closeSync ( fd ) ;
79- const source = buffer . toString ( 'utf8' , 0 , bytesRead ) ;
80-
81- const flagStart = source . indexOf ( '// Flags: --' ) + 10 ;
82- if ( flagStart !== 9 ) {
83- let flagEnd = source . indexOf ( '\n' , flagStart ) ;
84- // Normalize different EOL.
85- if ( source [ flagEnd - 1 ] === '\r' ) {
86- flagEnd -- ;
87- }
88- const flags = source
89- . substring ( flagStart , flagEnd )
90- . replace ( / _ / g, '-' )
91- . split ( ' ' ) ;
92- const args = process . execArgv . map ( ( arg ) => arg . replace ( / _ / g, '-' ) ) ;
93- for ( const flag of flags ) {
94- if ( ! args . includes ( flag ) &&
95- // If the binary is build without `intl` the inspect option is
96- // invalid. The test itself should handle this case.
97- ( process . features . inspector || ! flag . startsWith ( '--inspect' ) ) ) {
98- console . log (
99- 'NOTE: The test started as a child_process using these flags:' ,
100- inspect ( flags ) ,
101- 'Use NODE_SKIP_FLAG_CHECK to run the test with the original flags.' ,
102- ) ;
103- const args = [ ...flags , ...process . execArgv , ...process . argv . slice ( 1 ) ] ;
104- const options = { encoding : 'utf8' , stdio : 'inherit' } ;
105- const result = spawnSync ( process . execPath , args , options ) ;
106- if ( result . signal ) {
107- process . kill ( 0 , result . signal ) ;
108- } else {
109- process . exit ( result . status ) ;
110- }
98+ const flags = parseTestFlags ( ) ;
99+ const args = process . execArgv . map ( ( arg ) => arg . replace ( / _ / g, '-' ) ) ;
100+ for ( const flag of flags ) {
101+ if ( ! args . includes ( flag ) &&
102+ // If the binary is build without `intl` the inspect option is
103+ // invalid. The test itself should handle this case.
104+ ( process . features . inspector || ! flag . startsWith ( '--inspect' ) ) ) {
105+ console . log (
106+ 'NOTE: The test started as a child_process using these flags:' ,
107+ inspect ( flags ) ,
108+ 'Use NODE_SKIP_FLAG_CHECK to run the test with the original flags.' ,
109+ ) ;
110+ const args = [ ...flags , ...process . execArgv , ...process . argv . slice ( 1 ) ] ;
111+ const options = { encoding : 'utf8' , stdio : 'inherit' } ;
112+ const result = spawnSync ( process . execPath , args , options ) ;
113+ if ( result . signal ) {
114+ process . kill ( 0 , result . signal ) ;
115+ } else {
116+ process . exit ( result . status ) ;
111117 }
112118 }
113119 }
@@ -919,6 +925,7 @@ const common = {
919925 mustSucceed,
920926 nodeProcessAborted,
921927 PIPE ,
928+ parseTestFlags,
922929 platformTimeout,
923930 printSkipMessage,
924931 pwdCommand,
0 commit comments