From d35105967e5ce5496bf7bd57dec4294db446b9ed Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Mon, 3 Oct 2022 13:12:53 +0300 Subject: [PATCH] test: fix test-runner-inspect PR-URL: https://github.com/nodejs/node/pull/44620 Backport-PR-URL: https://github.com/nodejs/node/pull/44873 Fixes: https://github.com/nodejs/node/issues/44600 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Antoine du Hamel --- lib/internal/test_runner/runner.js | 41 ++++++++++-------------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index 911a700d68d58d..9994ac12ecf3a4 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -4,21 +4,19 @@ const { ArrayPrototypeFilter, ArrayPrototypeIncludes, ArrayPrototypeJoin, - ArrayPrototypePop, ArrayPrototypePush, ArrayPrototypeSlice, ArrayPrototypeSort, ObjectAssign, PromisePrototypeThen, - RegExpPrototypeSymbolSplit, SafePromiseAll, SafeSet, - StringPrototypeEndsWith, } = primordials; -const { Buffer } = require('buffer'); const { spawn } = require('child_process'); const { readdirSync, statSync } = require('fs'); +// TODO(aduh95): switch to internal/readline/interface when backporting to Node.js 16.x is no longer a concern. +const { createInterface } = require('readline'); const console = require('internal/console/global'); const { codes: { @@ -114,28 +112,6 @@ function getRunArgs({ path, inspectPort }) { 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; - if (StringPrototypeEndsWith(lines, '\n')) { - buffer = Buffer.alloc(0); - } else { - lines = RegExpPrototypeSymbolSplit(/\r?\n/, str); - buffer = Buffer.from(ArrayPrototypePop(lines), 'utf8'); - lines = ArrayPrototypeJoin(lines, '\n'); - } - if (isInspectorMessage(lines)) { - process.stderr.write(lines); - } - }; -} function runTestFile(path, root, inspectPort) { const subtest = root.createSubtest(Test, path, async (t) => { @@ -151,9 +127,18 @@ function runTestFile(path, root, inspectPort) { err = error; }); - child.stderr.on('data', makeStderrCallback((data) => { + child.stderr.on('data', (data) => { stderr += data; - })); + }); + + if (isUsingInspector()) { + const rl = createInterface({ input: child.stderr }); + rl.on('line', (line) => { + if (isInspectorMessage(line)) { + process.stderr.write(line + '\n'); + } + }); + } const { 0: { 0: code, 1: signal }, 1: stdout } = await SafePromiseAll([ once(child, 'exit', { signal: t.signal }),