Skip to content

util: fix test to prevent styleText color from changing #57935

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/internal/test_runner/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ function runTestFile(path, filesWatcher, opts) {
env.WATCH_REPORT_DEPENDENCIES = '1';
}
if (opts.root.harness.shouldColorizeTestFiles) {
env.FORCE_COLOR = '1';
env.NODE_TEST_CONTEXT = 'child-v8-test-colorize';
}

const child = spawn(
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/test_runner/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ function parseCommandLine() {
const watch = getOptionValue('--watch');
const timeout = getOptionValue('--test-timeout') || Infinity;
const isChildProcess = process.env.NODE_TEST_CONTEXT === 'child';
const isChildProcessV8 = process.env.NODE_TEST_CONTEXT === 'child-v8';
const isChildProcessV8 = process.env.NODE_TEST_CONTEXT === 'child-v8' ||
process.env.NODE_TEST_CONTEXT === 'child-v8-test-colorize';
let globalSetupPath;
let concurrency;
let coverageExcludeGlobs;
Expand Down
5 changes: 4 additions & 1 deletion lib/internal/util/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ module.exports = {
clear: '',
reset: '',
hasColors: false,
shouldColorize(stream) {
shouldColorize(stream, ignoreTestContext = false) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this new ignoreTestContext argument necessary? It seems like it could be computed within the function based on the value of stream.

if (!ignoreTestContext && process.env.NODE_TEST_CONTEXT === 'child-v8-test-colorize') {
return lazyInternalTTY().getColorDepth() > 2;
}
if (process.env.FORCE_COLOR !== undefined) {
return lazyInternalTTY().getColorDepth() > 2;
}
Expand Down
7 changes: 6 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
}

// If the stream is falsy or should not be colorized, set skipColorize to true
skipColorize = !lazyUtilColors().shouldColorize(stream);
skipColorize = !lazyUtilColors().shouldColorize(
stream,
// Avoid styleText color changes in tests
// https://github.com/nodejs/node/issues/57921
stream !== process.stdout && stream !== process.stderr,
);
}

// If the format is not an array, convert it to an array
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/test-runner/force-color.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const {WriteStream} = require('tty');
const assert = require('assert');

assert.strictEqual(process.env.FORCE_COLOR, '3');
assert.strictEqual(WriteStream(0).getColorDepth(), 24);
14 changes: 14 additions & 0 deletions test/fixtures/test-runner/output/style-text-output-in-test-tty.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Writable } from 'node:stream';
import { styleText } from 'node:util';

const streamTTY = new class extends Writable {
isTTY = true;
}();
const streamNoTTY = new class extends Writable {
isTTY = false;
}();

console.log(styleText('bgYellow', 'TTY', { stream: streamTTY }));
console.log(styleText('bgYellow', 'No TTY', { stream: streamNoTTY }));
console.log(styleText('bgYellow', 'stdout', { stream: process.stdout }));
console.log(styleText('bgYellow', 'stderr', { stream: process.stderr }));
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
TAP version 13
# [43mTTY[49m
# No TTY
# [43mstdout[49m
# [43mstderr[49m
# Subtest: /test/fixtures/test-runner/output/style-text-output-in-test-tty.mjs
ok 1 - /test/fixtures/test-runner/output/style-text-output-in-test-tty.mjs
---
duration_ms: *
type: 'test'
...
1..1
# tests 1
# suites 0
# pass 1
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms *
14 changes: 14 additions & 0 deletions test/fixtures/test-runner/output/style-text-output-in-test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Writable } from 'node:stream';
import { styleText } from 'node:util';

const streamTTY = new class extends Writable {
isTTY = true;
}();
const streamNoTTY = new class extends Writable {
isTTY = false;
}();

console.log(styleText('bgYellow', 'TTY', { stream: streamTTY }));
console.log(styleText('bgYellow', 'No TTY', { stream: streamNoTTY }));
console.log(styleText('bgYellow', 'stdout', { stream: process.stdout }));
console.log(styleText('bgYellow', 'stderr', { stream: process.stderr }));
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
TAP version 13
# TTY
# No TTY
# stdout
# stderr
# Subtest: /test/fixtures/test-runner/output/style-text-output-in-test.mjs
ok 1 - /test/fixtures/test-runner/output/style-text-output-in-test.mjs
---
duration_ms: *
type: 'test'
...
1..1
# tests 1
# suites 0
# pass 1
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms *
15 changes: 15 additions & 0 deletions test/parallel/test-runner-force-color.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as common from '../common/index.mjs';
import * as fixtures from '../common/fixtures.js';
import assert from 'node:assert';

common.spawnPromisified(process.execPath, [
'--test',
fixtures.path('test-runner', 'force-color.js'),
], {
env: {
...process.env,
FORCE_COLOR: 3,
}
}).then(common.mustCall((result) => {
assert.strictEqual(result.code, 0);
}));
9 changes: 9 additions & 0 deletions test/parallel/test-runner-output.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,15 @@ const tests = [
flags: ['--test-reporter=tap', '--test-coverage-exclude=../output/**'],
cwd: fixtures.path('test-runner/coverage-snap'),
} : false,
canColorize ? {
name: 'test-runner/output/style-text-output-in-test.mjs',
flags: ['--test', '--test-reporter=tap'],
} : false,
canColorize ? {
name: 'test-runner/output/style-text-output-in-test-tty.mjs',
flags: ['--test', '--test-reporter=tap'],
tty: true,
} : false,
]
.filter(Boolean)
.map(({ flags, name, tty, transform, cwd }) => ({
Expand Down
Loading