|
3 | 3 | const common = require('../common');
|
4 | 4 | const spawn = require('child_process').spawn;
|
5 | 5 |
|
6 |
| -var procStderr = ''; |
7 |
| -var agentStdout = ''; |
8 |
| -var needToSpawnAgent = true; |
9 |
| -var needToExit = true; |
10 |
| - |
11 |
| -const procArgs = [`--debug-brk=${common.PORT}`, '-e', '0']; |
12 |
| -const proc = spawn(process.execPath, procArgs); |
13 |
| -proc.stderr.setEncoding('utf8'); |
14 |
| - |
15 |
| -const exitAll = common.mustCall((processes) => { |
16 |
| - processes.forEach((myProcess) => { myProcess.kill(); }); |
17 |
| -}); |
18 |
| - |
19 |
| -proc.stderr.on('data', (chunk) => { |
20 |
| - procStderr += chunk; |
21 |
| - if (/Debugger listening on/.test(procStderr) && needToSpawnAgent) { |
22 |
| - needToSpawnAgent = false; |
23 |
| - const agentArgs = ['debug', `localhost:${common.PORT}`]; |
24 |
| - const agent = spawn(process.execPath, agentArgs); |
25 |
| - agent.stdout.setEncoding('utf8'); |
26 |
| - |
27 |
| - agent.stdout.on('data', (chunk) => { |
28 |
| - agentStdout += chunk; |
29 |
| - if (/connecting to .+ ok/.test(agentStdout) && needToExit) { |
30 |
| - needToExit = false; |
31 |
| - exitAll([proc, agent]); |
| 6 | +let run = () => {}; |
| 7 | +function test(extraArgs, stdoutPattern) { |
| 8 | + const next = run; |
| 9 | + run = () => { |
| 10 | + var procStdout = ''; |
| 11 | + var procStderr = ''; |
| 12 | + var agentStdout = ''; |
| 13 | + var debuggerListening = false; |
| 14 | + var outputMatched = false; |
| 15 | + var needToSpawnAgent = true; |
| 16 | + var needToExit = true; |
| 17 | + |
| 18 | + const procArgs = [`--debug-brk=${common.PORT}`].concat(extraArgs); |
| 19 | + const proc = spawn(process.execPath, procArgs); |
| 20 | + proc.stderr.setEncoding('utf8'); |
| 21 | + |
| 22 | + const tryStartAgent = () => { |
| 23 | + if (debuggerListening && outputMatched && needToSpawnAgent) { |
| 24 | + needToSpawnAgent = false; |
| 25 | + const agentArgs = ['debug', `localhost:${common.PORT}`]; |
| 26 | + const agent = spawn(process.execPath, agentArgs); |
| 27 | + agent.stdout.setEncoding('utf8'); |
| 28 | + |
| 29 | + agent.stdout.on('data', (chunk) => { |
| 30 | + agentStdout += chunk; |
| 31 | + if (/connecting to .+ ok/.test(agentStdout) && needToExit) { |
| 32 | + needToExit = false; |
| 33 | + exitAll([proc, agent]); |
| 34 | + } |
| 35 | + }); |
32 | 36 | }
|
| 37 | + }; |
| 38 | + |
| 39 | + const exitAll = common.mustCall((processes) => { |
| 40 | + processes.forEach((myProcess) => { myProcess.kill(); }); |
33 | 41 | });
|
34 |
| - } |
35 |
| -}); |
| 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 | + /Debugger listening on/.test(procStderr); |
| 57 | + tryStartAgent(); |
| 58 | + }); |
| 59 | + |
| 60 | + proc.on('exit', () => { |
| 61 | + next(); |
| 62 | + }); |
| 63 | + }; |
| 64 | +} |
| 65 | + |
| 66 | +test(['-e', '0']); |
| 67 | +test(['-e', '0', 'foo']); |
| 68 | +test(['-p', 'process.argv[1]', 'foo'], /^\s*foo\s*$/); |
| 69 | + |
| 70 | +run(); |
0 commit comments