From 9f095fa23927d4a731d5d18c9d624a794bb96c9b Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Wed, 14 Sep 2022 23:24:20 +0300 Subject: [PATCH] test: fix test-runner-inspect PR-URL: https://github.com/nodejs/node/pull/44620 Fixes: https://github.com/nodejs/node/issues/44600 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Antoine du Hamel (cherry picked from commit 9825a7e01d35b9d49ebb58efed2c316012c19db6) --- lib/internal/test_runner/runner.js | 46 ++++++++++-------------------- 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index a9ad7fb..886adac 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -1,25 +1,23 @@ -// https://github.com/nodejs/node/blob/a165193c5c8e4bcfbd12b2c3f6e55a81a251c258/lib/internal/test_runner/runner.js +// https://github.com/nodejs/node/blob/9825a7e01d35b9d49ebb58efed2c316012c19db6/lib/internal/test_runner/runner.js 'use strict' const { ArrayFrom, ArrayPrototypeFilter, ArrayPrototypeIncludes, ArrayPrototypeJoin, - ArrayPrototypePop, ArrayPrototypePush, ArrayPrototypeSlice, ArrayPrototypeSort, ObjectAssign, PromisePrototypeThen, - RegExpPrototypeSymbolSplit, SafePromiseAll, - SafeSet, - StringPrototypeEndsWith + SafeSet } = require('#internal/per_context/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 { codes: { ERR_TEST_FAILURE @@ -115,29 +113,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) => { const args = getRunArgs({ path, inspectPort }) @@ -152,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 }),