(args, flags = [], spawnOpts = {}, opts = { randomPort: true })
| 19 | } |
| 20 | |
| 21 | function startCLI(args, flags = [], spawnOpts = {}, opts = { randomPort: true }) { |
| 22 | let stderrOutput = ''; |
| 23 | const child = spawn(process.execPath, [ |
| 24 | ...flags, |
| 25 | 'inspect', |
| 26 | ...(opts.randomPort !== false ? ['--port=0'] : []), |
| 27 | ...args, |
| 28 | ], spawnOpts); |
| 29 | |
| 30 | const outputBuffer = []; |
| 31 | function bufferOutput(chunk) { |
| 32 | if (this === child.stderr) { |
| 33 | stderrOutput += chunk; |
| 34 | } |
| 35 | outputBuffer.push(chunk); |
| 36 | } |
| 37 | |
| 38 | function getOutput() { |
| 39 | return outputBuffer.join('\n').replaceAll('\b', ''); |
| 40 | } |
| 41 | |
| 42 | child.stdout.setEncoding('utf8'); |
| 43 | child.stdout.on('data', bufferOutput); |
| 44 | child.stderr.setEncoding('utf8'); |
| 45 | child.stderr.on('data', bufferOutput); |
| 46 | |
| 47 | if (process.env.VERBOSE === '1') { |
| 48 | child.stdout.pipe(process.stdout); |
| 49 | child.stderr.pipe(process.stderr); |
| 50 | } |
| 51 | |
| 52 | return { |
| 53 | flushOutput() { |
| 54 | const output = this.output; |
| 55 | outputBuffer.length = 0; |
| 56 | return output; |
| 57 | }, |
| 58 | |
| 59 | waitFor(pattern) { |
| 60 | function checkPattern(str) { |
| 61 | if (Array.isArray(pattern)) { |
| 62 | return pattern.every((p) => p.test(str)); |
| 63 | } |
| 64 | return pattern.test(str); |
| 65 | } |
| 66 | |
| 67 | return new Promise((resolve, reject) => { |
| 68 | function checkOutput() { |
| 69 | if (checkPattern(getOutput())) { |
| 70 | tearDown(); |
| 71 | resolve(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | function onChildClose(code, signal) { |
| 76 | tearDown(); |
| 77 | let message = 'Child exited'; |
| 78 | if (code) { |
no test coverage detected
searching dependent graphs…