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

Conversation

islandryu
Copy link
Contributor

Fixes: #57921

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/test_runner

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem. util Issues and PRs related to the built-in util module. labels Apr 19, 2025
@@ -373,7 +373,7 @@ function runTestFile(path, filesWatcher, opts) {
env.WATCH_REPORT_DEPENDENCIES = '1';
}
if (opts.root.harness.shouldColorizeTestFiles) {
env.FORCE_COLOR = '1';
env.TEST_REPORT_COLORIZE = '1';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

#57921 (comment)
As stated here, I too don't think we should override the existing FORCE_COLOR by test.

Copy link
Contributor

@cjihrig cjihrig Apr 19, 2025

Choose a reason for hiding this comment

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

We definitely shouldn't be creating new, undocumented environment variables like this. There is already NODE_TEST_CONTEXT, which is documented as something that users should not rely on. We would need to leverage that environment variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I addressed it by setting NOE_TEXT_CONTEXT to child-v8-test-colorize. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of adding a new value for NODE_TEST_CONTEXT, would it work to update shouldColorize() such that:

  • process.env.FORCE_COLOR takes the highest priority if it is set.
  • The TTY logic is evaluated after that.
  • process.env.NODE_TEST_CONTEXT is only considered as a last resort.

Copy link
Contributor Author

@islandryu islandryu Apr 21, 2025

Choose a reason for hiding this comment

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

Would it be something like this?

    if (process.env.FORCE_COLOR !== undefined) {
      return lazyInternalTTY().getColorDepth() > 2;
    }
    if (stream?.isTTY) {
      return typeof stream.getColorDepth === 'function' ?
        stream.getColorDepth() > 2 : true;
    }

    if (process.env.NODE_TEST_CONTEXT === 'child-v8') {
      return lazyInternalTTY().getColorDepth() > 2;
    }

That alone would result in behavior differing from the existing implementation in scenarios such as the following.

console.log({foo: "bar"})
node  --test index.js | cat > text.txt
{ foo: 'bar' }

# fixed
./out/Debug/node  --test index.js | cat > text.txt
{ foo: �[32m'bar'�[39m }

Regardless of the order within shouldColorize, I think the test process needs some way to know the value of opts.root.harness.shouldColorizeTestFiles in the parent process.

Copy link
Contributor

Choose a reason for hiding this comment

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

If you want to start passing parent config through to the child, the NODE_TEST_CONTEXT environment variable is probably the best place to do it.

Copy link
Contributor

@cjihrig cjihrig left a comment

Choose a reason for hiding this comment

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

This FORCE_COLOR change came from #48057. Is it possible to achieve the same result without hacking FORCE_COLOR? The test runner child processes already know that they are test runner child processes. Can we leverage that?

@@ -373,7 +373,7 @@ function runTestFile(path, filesWatcher, opts) {
env.WATCH_REPORT_DEPENDENCIES = '1';
}
if (opts.root.harness.shouldColorizeTestFiles) {
env.FORCE_COLOR = '1';
env.TEST_REPORT_COLORIZE = '1';
Copy link
Contributor

@cjihrig cjihrig Apr 19, 2025

Choose a reason for hiding this comment

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

We definitely shouldn't be creating new, undocumented environment variables like this. There is already NODE_TEST_CONTEXT, which is documented as something that users should not rely on. We would need to leverage that environment variable.

Copy link

codecov bot commented Apr 19, 2025

Codecov Report

Attention: Patch coverage is 92.30769% with 1 line in your changes missing coverage. Please review.

Project coverage is 90.27%. Comparing base (52d95f5) to head (90924d3).
Report is 14 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/test_runner/runner.js 0.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main   #57935    +/-   ##
========================================
  Coverage   90.26%   90.27%            
========================================
  Files         630      630            
  Lines      185933   186121   +188     
  Branches    36450    36475    +25     
========================================
+ Hits       167829   168013   +184     
- Misses      10972    10981     +9     
+ Partials     7132     7127     -5     
Files with missing lines Coverage Δ
lib/internal/test_runner/utils.js 60.53% <100.00%> (+0.06%) ⬆️
lib/internal/util/colors.js 100.00% <100.00%> (ø)
lib/util.js 97.78% <100.00%> (+0.02%) ⬆️
lib/internal/test_runner/runner.js 88.87% <0.00%> (ø)

... and 37 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@@ -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.

@cjihrig cjihrig self-requested a review April 23, 2025 14:04
@cjihrig cjihrig dismissed their stale review April 23, 2025 14:07

Dismissing my block because this isn't introducing a new environment variable anymore, but I don't think the current approach is quite correct either. If anything, I'd pass parent settings as a JSON string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

styleText(): isTTY check fails with --test
3 participants