From 98408dca83d91defa464fe08b57f67c4ff3fbff0 Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Thu, 18 May 2023 08:32:44 +0300 Subject: [PATCH] test_runner: pass FORCE_COLOR to child process --- lib/internal/test_runner/harness.js | 1 + lib/internal/test_runner/runner.js | 3 ++ lib/internal/test_runner/utils.js | 7 ++-- .../output/arbitrary-output-colored-1.js | 7 ++++ .../output/arbitrary-output-colored.js | 11 +++++++ .../output/arbitrary-output-colored.snapshot | 32 +++++++++++++++++++ test/parallel/test-runner-output.mjs | 5 ++- 7 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/test-runner/output/arbitrary-output-colored-1.js create mode 100644 test/fixtures/test-runner/output/arbitrary-output-colored.js create mode 100644 test/fixtures/test-runner/output/arbitrary-output-colored.snapshot diff --git a/lib/internal/test_runner/harness.js b/lib/internal/test_runner/harness.js index dd1f386e1017d9..26b85ac0d2d6a5 100644 --- a/lib/internal/test_runner/harness.js +++ b/lib/internal/test_runner/harness.js @@ -179,6 +179,7 @@ function setup(root) { topLevel: 0, suites: 0, }, + shouldColorizeTestFiles: false, }; root.startTime = hrtime(); return root; diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index 056955f04566c3..df520799669432 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -333,6 +333,9 @@ function runTestFile(path, root, inspectPort, filesWatcher, testNamePatterns) { stdio.push('ipc'); env.WATCH_REPORT_DEPENDENCIES = '1'; } + if (root.harness.shouldColorizeTestFiles) { + env.FORCE_COLOR = '1'; + } const child = spawn(process.execPath, args, { signal: t.signal, encoding: 'utf8', env, stdio }); runningProcesses.set(path, child); diff --git a/lib/internal/test_runner/utils.js b/lib/internal/test_runner/utils.js index de9d4e5a1e4345..94a62a44d11d69 100644 --- a/lib/internal/test_runner/utils.js +++ b/lib/internal/test_runner/utils.js @@ -16,7 +16,7 @@ const { createWriteStream } = require('fs'); const { pathToFileURL } = require('internal/url'); const { createDeferredPromise } = require('internal/util'); const { getOptionValue } = require('internal/options'); -const { green, red, white } = require('internal/util/colors'); +const { green, red, white, shouldColorize } = require('internal/util/colors'); const { codes: { @@ -115,9 +115,10 @@ function tryBuiltinReporter(name) { return require(builtinPath); } -async function getReportersMap(reporters, destinations) { +async function getReportersMap(reporters, destinations, rootTest) { return SafePromiseAllReturnArrayLike(reporters, async (name, i) => { const destination = kBuiltinDestinations.get(destinations[i]) ?? createWriteStream(destinations[i]); + rootTest.harness.shouldColorizeTestFiles ||= shouldColorize(destination); // Load the test reporter passed to --test-reporter let reporter = tryBuiltinReporter(name); @@ -154,7 +155,7 @@ async function getReportersMap(reporters, destinations) { async function setupTestReporters(rootTest) { const { reporters, destinations } = parseCommandLine(); - const reportersMap = await getReportersMap(reporters, destinations); + const reportersMap = await getReportersMap(reporters, destinations, rootTest); for (let i = 0; i < reportersMap.length; i++) { const { reporter, destination } = reportersMap[i]; compose(rootTest.reporter, reporter).pipe(destination); diff --git a/test/fixtures/test-runner/output/arbitrary-output-colored-1.js b/test/fixtures/test-runner/output/arbitrary-output-colored-1.js new file mode 100644 index 00000000000000..8df2cadc349ba7 --- /dev/null +++ b/test/fixtures/test-runner/output/arbitrary-output-colored-1.js @@ -0,0 +1,7 @@ +'use strict'; + +const test = require('node:test'); +console.log({ foo: 'bar' }); +test('passing test', () => { + console.log(1); +}); \ No newline at end of file diff --git a/test/fixtures/test-runner/output/arbitrary-output-colored.js b/test/fixtures/test-runner/output/arbitrary-output-colored.js new file mode 100644 index 00000000000000..81f67483691e63 --- /dev/null +++ b/test/fixtures/test-runner/output/arbitrary-output-colored.js @@ -0,0 +1,11 @@ +'use strict'; +const common = require('../../../common'); +const { once } = require('node:events'); +const { spawn } = require('node:child_process'); +const fixtures = require('../../../common/fixtures'); + +(async function run() { + const test = fixtures.path('test-runner/output/arbitrary-output-colored-1.js'); + await once(spawn(process.execPath, ['--test', test], { stdio: 'inherit' }), 'exit'); + await once(spawn(process.execPath, ['--test', '--test-reporter', 'tap', test], { stdio: 'inherit' }), 'exit'); +})().then(common.mustCall()); \ No newline at end of file diff --git a/test/fixtures/test-runner/output/arbitrary-output-colored.snapshot b/test/fixtures/test-runner/output/arbitrary-output-colored.snapshot new file mode 100644 index 00000000000000..a03d918b706ec2 --- /dev/null +++ b/test/fixtures/test-runner/output/arbitrary-output-colored.snapshot @@ -0,0 +1,32 @@ +[34mℹ { foo: [32m'bar'[39m }[39m +[34mℹ [39m +[34mℹ [33m1[39m[39m +[34mℹ [39m +[32m✔ passing test [90m(*ms)[39m[39m +[34mℹ tests 1[39m +[34mℹ suites 0[39m +[34mℹ pass 1[39m +[34mℹ fail 0[39m +[34mℹ cancelled 0[39m +[34mℹ skipped 0[39m +[34mℹ todo 0[39m +[34mℹ duration_ms *[39m +TAP version 13 +# { foo: [32m'bar'[39m } +# +# [33m1[39m +# +# Subtest: passing test +ok 1 - passing test + --- + duration_ms: * + ... +1..1 +# tests 1 +# suites 0 +# pass 1 +# fail 0 +# cancelled 0 +# skipped 0 +# todo 0 +# duration_ms * diff --git a/test/parallel/test-runner-output.mjs b/test/parallel/test-runner-output.mjs index d2fa06b4395095..5b2f07f0358c0c 100644 --- a/test/parallel/test-runner-output.mjs +++ b/test/parallel/test-runner-output.mjs @@ -45,7 +45,10 @@ const tests = [ { name: 'test-runner/output/name_pattern_with_only.js' }, { name: 'test-runner/output/unresolved_promise.js' }, { name: 'test-runner/output/default_output.js', transform: specTransform, tty: true }, - { name: 'test-runner/output/arbitrary-output.js' }, + { + name: 'test-runner/output/arbitrary-output-colored.js', + transform: snapshot.transform(specTransform, replaceTestDuration), tty: true + }, ].map(({ name, tty, transform }) => ({ name, fn: common.mustCall(async () => {