Skip to content

Commit

Permalink
fixup! fixup! fixup! fixup! fixup! test,debugger: migrate node-inspec…
Browse files Browse the repository at this point in the history
…t tests to core
  • Loading branch information
Trott committed Apr 10, 2021
1 parent c9e09fc commit d1647d4
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions test/common/inspector-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ function isPreBreak(output) {
}

function startCLI(args, flags = [], spawnOpts = {}) {
let stderrOutput = '';
const child =
spawn(process.execPath, [...flags, 'inspect', ...args], spawnOpts);

const outputBuffer = [];
function bufferOutput(chunk) {
if (this === child.stderr) {
stderrOutput += chunk;
}
outputBuffer.push(chunk);
}

Expand All @@ -32,7 +36,7 @@ function startCLI(args, flags = [], spawnOpts = {}) {
child.stderr.on('data', bufferOutput);

if (process.env.VERBOSE === '1') {
child.stdout.pipe(process.stderr);
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
}

Expand All @@ -59,10 +63,20 @@ function startCLI(args, flags = [], spawnOpts = {}) {
}
}

function onChildExit() {
function onChildClose(code, signal) {
tearDown();
reject(new Error(
`Child quit while waiting for ${pattern}; found: ${this.output}`));
let message = 'Child exited';
if (code) {
message += `, code ${code}`;
}
if (signal) {
message += `, signal ${signal}`;
}
message += ` while waiting for ${pattern}; found: ${this.output}`;
if (stderrOutput) {
message += `\n STDERR: ${stderrOutput}`;
}
reject(new Error(message));
}

const timer = setTimeout(() => {
Expand All @@ -76,10 +90,10 @@ function startCLI(args, flags = [], spawnOpts = {}) {
function tearDown() {
clearTimeout(timer);
child.stdout.removeListener('data', checkOutput);
child.removeListener('exit', onChildExit);
child.removeListener('close', onChildClose);
}

child.on('exit', onChildExit);
child.on('close', onChildClose);
child.stdout.on('data', checkOutput);
checkOutput();
});
Expand Down Expand Up @@ -156,7 +170,7 @@ function startCLI(args, flags = [], spawnOpts = {}) {
quit() {
return new Promise((resolve) => {
child.stdin.end();
child.on('exit', resolve);
child.on('close', resolve);
});
},
};
Expand Down

0 comments on commit d1647d4

Please sign in to comment.