Skip to content

Commit

Permalink
test_runner: pass FORCE_COLOR to child process
Browse files Browse the repository at this point in the history
PR-URL: #48057
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
  • Loading branch information
MoLow authored and targos committed May 30, 2023
1 parent 3278542 commit 750d3e8
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/internal/test_runner/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ function setup(root) {
topLevel: 0,
suites: 0,
},
shouldColorizeTestFiles: false,
};
root.startTime = hrtime();
return root;
Expand Down
3 changes: 3 additions & 0 deletions lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,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);
Expand Down
7 changes: 4 additions & 3 deletions lib/internal/test_runner/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const test = require('node:test');
console.log({ foo: 'bar' });
test('passing test', () => {
console.log(1);
});
11 changes: 11 additions & 0 deletions test/fixtures/test-runner/output/arbitrary-output-colored.js
Original file line number Diff line number Diff line change
@@ -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', env: { FORCE_COLOR: 1 } }), 'exit');
await once(spawn(process.execPath, ['--test', '--test-reporter', 'tap', test], { stdio: 'inherit', env: { FORCE_COLOR: 1 } }), 'exit');
})().then(common.mustCall());
28 changes: 28 additions & 0 deletions test/fixtures/test-runner/output/arbitrary-output-colored.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{ foo: [32m'bar'[39m }
[33m1[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 *
12 changes: 11 additions & 1 deletion test/parallel/test-runner-output.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import * as fixtures from '../common/fixtures.mjs';
import * as snapshot from '../common/assertSnapshot.js';
import { describe, it } from 'node:test';

const skipForceColors =
process.config.variables.icu_gyp_path !== 'tools/icu/icu-generic.gyp' ||
process.config.variables.node_shared_openssl;

function replaceTestDuration(str) {
return str
.replaceAll(/duration_ms: 0(\r?\n)/g, 'duration_ms: ZERO$1')
Expand Down Expand Up @@ -46,8 +50,14 @@ const tests = [
{ 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' },
!skipForceColors ? {
name: 'test-runner/output/arbitrary-output-colored.js',
transform: snapshot.transform(specTransform, replaceTestDuration), tty: true
} : false,
{ name: 'test-runner/output/dot_output_custom_columns.js', transform: specTransform, tty: true },
].map(({ name, tty, transform }) => ({
]
.filter(Boolean)
.map(({ name, tty, transform }) => ({
name,
fn: common.mustCall(async () => {
await snapshot.spawnAndAssert(fixtures.path(name), transform ?? defaultTransform, { tty });
Expand Down

0 comments on commit 750d3e8

Please sign in to comment.