Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: support using --inspect with --test #44520

Merged
merged 15 commits into from
Sep 10, 2022
Prev Previous commit
Next Next commit
fix
  • Loading branch information
MoLow committed Sep 9, 2022
commit 92ea9bbc9b382f0d1f6c3763accac969248db965
32 changes: 24 additions & 8 deletions lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
SafeSet,
} = primordials;

const { Buffer } = require('buffer');
const { spawn } = require('child_process');
const { readdirSync, statSync } = require('fs');
const console = require('internal/console/global');
Expand Down Expand Up @@ -103,15 +104,34 @@ function filterExecArgv(arg) {

function getRunArgs({ path, inspectPort }) {
const argv = ArrayPrototypeFilter(process.execArgv, filterExecArgv);
console.log(`Running test ${path}...`, { inspectPort, isUsingInspector: isUsingInspector() });
if (isUsingInspector()) {
ArrayPrototypePush(argv, `--inspect-port=${getInspectPort(inspectPort)}`);
}
console.log(`Running test ${path}...`, { inspectPort, argv });
ArrayPrototypePush(argv, path);
return argv;
}

function makeStderrCallback(callback) {
if (!isUsingInspector()) {
return callback;
}
let buffer = Buffer.alloc(0);
return (data) => {
callback(data);
const newData = Buffer.concat([buffer, data]);
const str = newData.toString('utf8');
let lines = str.split(/\r?\n/);
MoLow marked this conversation as resolved.
Show resolved Hide resolved
if (str.endsWith('\n'))
MoLow marked this conversation as resolved.
Show resolved Hide resolved
buffer = Buffer.alloc(0);
else
buffer = Buffer.from(lines.pop(), 'utf8');
MoLow marked this conversation as resolved.
Show resolved Hide resolved
lines = lines.join('\n');
MoLow marked this conversation as resolved.
Show resolved Hide resolved
if (isInspectorMessage(lines)) {
process.stderr.write(lines);
}
};
}

function runTestFile(path, root, inspectPort) {
const subtest = root.createSubtest(Test, path, async (t) => {
const args = getRunArgs({ path, inspectPort });
Expand All @@ -126,13 +146,9 @@ function runTestFile(path, root, inspectPort) {
err = error;
});

child.stderr.on('data', (data) => {
console.log('[stderr]', { data: data.toString() });
if (isInspectorMessage(data.toString())) {
process.stderr.write(data);
}
child.stderr.on('data', makeStderrCallback((data) => {
stderr += data;
});
}));

const { 0: { 0: code, 1: signal }, 1: stdout } = await SafePromiseAll([
once(child, 'exit', { signal: t.signal }),
Expand Down