Skip to content
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

feat: respect NO_COLOR env variable #651

Closed
wants to merge 1 commit into from

Conversation

susliko
Copy link

@susliko susliko commented Apr 25, 2023

Resolves #149

This PR makes use of the NO_COLOR environment variable, disabling all ansi escapes when NO_COLOR=1.

Example:

//> using scala "2.13.10"
//> using lib "org.scalameta::munit::1.0.0-SNAPSHOT" // published locally

class MyTests extends munit.FunSuite {
  test("foo") {
    assert(2 + 2 == 4)
  }
}

@valencik
Copy link
Collaborator

Hey @susliko, thank you for working on this.

However, this PR seems to make a lot more changes than just enabling NO_COLOR support?
It's not clear to me why they changes to JUnitReporter.scala, Lines.scala, or Diff.scala are necessary.

As you can see in the mima run, this is causing some binary incompatibility issues.

Could you perhaps pair this work back to the bare minimum needed to add NO_COLOR support?

@susliko
Copy link
Author

susliko commented Apr 26, 2023

@valencik Thank you for taking a look!

As you can see in the mima run, this is causing some binary incompatibility issues.

Mima issues were solely caused by the removal of unused escapes from AnsiColors. This change is reverted now, CI should be passing

It's not clear to me why they changes to JUnitReporter.scala, Lines.scala, or Diff.scala are necessary.

These files had some string concatenations with direct inclusion of ansi codes. This PR refactors them to use Ansi Colors.c method instead. Please, let me know, if there is another way to ensure escape codes are filtered out while leaving these files untouched

Comment on lines +26 to +28
String no_color = System.getenv("NO_COLOR");
Boolean isNoColorEnvSet = no_color != null && no_color.equals("1");
if (colorSequence == null || isNoColorEnvSet) return s;

Choose a reason for hiding this comment

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

Suggested change
String no_color = System.getenv("NO_COLOR");
Boolean isNoColorEnvSet = no_color != null && no_color.equals("1");
if (colorSequence == null || isNoColorEnvSet) return s;
if (colorSequence == null) return s;
if ("1".equals(System.getenv("NO_COLOR"))) return s;

@@ -229,38 +229,33 @@ final class JUnitReporter(
e.getFileName().contains("file:/")
}
val canHighlight = !PlatformCompat.isNative
new StringBuilder()

Choose a reason for hiding this comment

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

why is this file changed? what does it have to do with NO_COLOR?

Comment on lines -6 to +13
val Reset = "\u001b[0m"
val Reversed = "\u001b[7m"
val Bold = "\u001b[1m"
val Faint = "\u001b[2m"
val RED = "\u001B[31m"
val YELLOW = "\u001B[33m"
// Foreground colors
val BLUE = "\u001B[34m"
val Magenta = "\u001B[35m"
val CYAN = "\u001B[36m"
val GREEN = "\u001B[32m"
val DarkGrey = "\u001B[90m"
val GREEN = "\u001B[32m"
val LightGreen = "\u001b[92m"
val LightRed = "\u001b[91m"
val Magenta = "\u001B[35m"
val RED = "\u001B[31m"
val YELLOW = "\u001B[33m"

Choose a reason for hiding this comment

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

why are these colors modified or reordered? what does it have to do with NO_COLOR?

Comment on lines +22 to +23
val isNoColorEnvSet = System.getenv("NO_COLOR") == "1"
if (colorSequence == null || isNoColorEnvSet) s

Choose a reason for hiding this comment

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

why is this implementation different from Ansi above? please don't read the env var if colorSequence is null, just like above.

Comment on lines -46 to +48
sb.append(
s" (${AnsiColors.LightRed}- obtained${AnsiColors.Reset}, ${AnsiColors.LightGreen}+ expected${AnsiColors.Reset})"
).append("\n")
val obtained = AnsiColors.c("- obtained", AnsiColors.LightRed)
val expected = AnsiColors.c("+ expected", AnsiColors.LightGreen)
sb.append(s" ($obtained, $expected)").append("\n")

Choose a reason for hiding this comment

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

looks like you're changing output to use .c instead of explicit color manipulation. please do that in a separate PR.

non added a commit to non/munit that referenced this pull request Sep 20, 2023
As per https://no-color.org/ this change will disable ANSI colors
if the NO_COLOR environment variable is set to any non-empty string.

(If the environment variable is unset or set to the empty string then
the default colors will still be used.)

This builds on the work of scalameta#651
but attempts to minimize the size of the patch.
@valencik
Copy link
Collaborator

Thank you again for this contribution!
Closing in favour of #703 which was able to minimize the diff nicely.

@valencik valencik closed this Sep 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support NO_COLOR environment variable
3 participants