- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.7k
Color output #1127
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
Color output #1127
Conversation
And do not color when printing to pipes.
Both my attempts at checking whether or not stdout is a terminal failed.
| @boyan-soubachov could you have a look at this? | 
| @alxn you had opinions on the sibling PR which should not be an issue with this one. Do you think this one would work? | 
| Using an env-var to turn it on seems fine. At a later date someone can try to do the  | 
| Any updates? | 
| 
 Not sure who you are asking here @vegerot, but the next step here is getting a review from some project maintainer. Looking at the most recent merges, maintainers include: | 
| While I understand the concerns about portability, file redirection, etc., it feels like all the other test runners I know (GoogleTest, Jest, Deno Test, ScalaTest, XCTest, etc.) all show colors when appropriate (jest does mess up sometimes when piping into other programs). While I know this is a tricky problem to solve, it is definitely not intractable given these other frameworks | 
| // Colorize unconditionally. You probably should call colorize() instead of this | ||
| // function. | ||
| func doColorize(plainText string) string { | ||
| re := regexp.MustCompile(`(.*)\n` + expected_colon + `(.*)\n` + actual_colon + `(.*)`) | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will it be false positive the failureMessage that contains those keywords?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Getting the colors wrong would be possible if:
- failureMessagecontains linefeeds
- one or more lines in failureMessagestarts with eitherexpected:oractual :(note:and spacing)
Just having the words expected or actual in the failureMessage would not trigger any miscoloring.
So yes, getting the colors wrong here is possible.
| // all failed. Patches welcome! | ||
| autoResponse := false | ||
|  | ||
| hint := os.Getenv("TESTIFY_COLOR") | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of defining a new env variable, Could we define this behavior based on the current environment?
Maybe with something like this: https://wiki.archlinux.org/title/Color_output_in_console#Enumerate_supported_colors
This is how people is doing it in js:
https://github.com/chalk/supports-color/blob/ee88ebcfca649233bb35c9b0db226059883a77b8/index.js#L59
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That JS function takes a streamIsTTY parameter:
function _supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
And I don't know how to figure that out :(
Lines 21 to 25 in c602661
| func figureOutShouldColorize() bool { | |
| // FIXME: The auto response should be true on terminals and false on | |
| // not-terminals, but my attempts at checking os.Stdout() terminalness have | |
| // all failed. Patches welcome! | |
| autoResponse := false | 
... so I can't really do it like that unfortunately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And in the long run, somebody will want to override the default behavior, and then the environment variable is likely to be needed anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@GerardRodes is this OK or do you have some suggestion for how to improve it?
| I think leaving this open doesn't provide any value, closing. Feel free to reopen if a reviewer becomes available! | 
Summary
If
TESTIFY_COLOR=trueis in the environment, thenexpectedandactualvalues will be coloured in green and red respectively.Motivation
Stolen from #994:
Unlike #994, this PR is configured once for the whole run by setting an environment variable.
Related issues