@@ -4,25 +4,23 @@ const common = require('../common');
44const spawn = require ( 'child_process' ) . spawn ;
55
66let run = ( ) => { } ;
7- function test ( extraArgs ) {
7+ function test ( extraArgs , stdoutPattern ) {
88 const next = run ;
99 run = ( ) => {
10+ var procStdout = '' ;
1011 var procStderr = '' ;
1112 var agentStdout = '' ;
13+ var debuggerListening = false ;
14+ var outputMatched = false ;
1215 var needToSpawnAgent = true ;
1316 var needToExit = true ;
1417
1518 const procArgs = [ `--debug-brk=${ common . PORT } ` ] . concat ( extraArgs ) ;
1619 const proc = spawn ( process . execPath , procArgs ) ;
1720 proc . stderr . setEncoding ( 'utf8' ) ;
1821
19- const exitAll = common . mustCall ( ( processes ) => {
20- processes . forEach ( ( myProcess ) => { myProcess . kill ( ) ; } ) ;
21- } ) ;
22-
23- proc . stderr . on ( 'data' , ( chunk ) => {
24- procStderr += chunk ;
25- if ( / D e b u g g e r l i s t e n i n g o n / . test ( procStderr ) && needToSpawnAgent ) {
22+ const tryStartAgent = ( ) => {
23+ if ( debuggerListening && outputMatched && needToSpawnAgent ) {
2624 needToSpawnAgent = false ;
2725 const agentArgs = [ 'debug' , `localhost:${ common . PORT } ` ] ;
2826 const agent = spawn ( process . execPath , agentArgs ) ;
@@ -36,6 +34,27 @@ function test(extraArgs) {
3634 }
3735 } ) ;
3836 }
37+ } ;
38+
39+ const exitAll = common . mustCall ( ( processes ) => {
40+ processes . forEach ( ( myProcess ) => { myProcess . kill ( ) ; } ) ;
41+ } ) ;
42+
43+ if ( stdoutPattern != null ) {
44+ proc . stdout . on ( 'data' , ( chunk ) => {
45+ procStdout += chunk ;
46+ outputMatched = outputMatched || stdoutPattern . test ( procStdout ) ;
47+ tryStartAgent ( ) ;
48+ } ) ;
49+ } else {
50+ outputMatched = true ;
51+ }
52+
53+ proc . stderr . on ( 'data' , ( chunk ) => {
54+ procStderr += chunk ;
55+ debuggerListening = debuggerListening ||
56+ / D e b u g g e r l i s t e n i n g o n / . test ( procStderr ) ;
57+ tryStartAgent ( ) ;
3958 } ) ;
4059
4160 proc . on ( 'exit' , ( ) => {
@@ -46,5 +65,6 @@ function test(extraArgs) {
4665
4766test ( [ '-e' , '0' ] ) ;
4867test ( [ '-e' , '0' , 'foo' ] ) ;
68+ test ( [ '-p' , 'process.argv[1]' , 'foo' ] , / ^ \s * f o o \s * $ / ) ;
4969
5070run ( ) ;
0 commit comments