Skip to content

Conversation

@walles
Copy link

@walles walles commented Nov 23, 2021

Summary

If TESTIFY_COLOR=true is in the environment, then expected and actual values will be coloured in green and red respectively.

Motivation

Stolen from #994:

  1. @boyan-soubachov says "It's an interesting idea."
  2. @mvdkleijn says "I like this idea."

Unlike #994, this PR is configured once for the whole run by setting an environment variable.

testicolor

Related issues

And do not color when printing to pipes.
Both my attempts at checking whether or not stdout is a terminal failed.
@walles walles marked this pull request as ready for review November 23, 2021 14:00
@walles
Copy link
Author

walles commented Dec 3, 2021

@boyan-soubachov could you have a look at this?

@walles
Copy link
Author

walles commented Dec 13, 2021

@alxn you had opinions on the sibling PR which should not be an issue with this one.

Do you think this one would work?

@alxn
Copy link
Contributor

alxn commented Dec 19, 2021

Using an env-var to turn it on seems fine. At a later date someone can try to do the isatty check.

@vegerot
Copy link

vegerot commented Mar 2, 2022

Any updates?

@walles
Copy link
Author

walles commented Mar 2, 2022

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:

@vegerot
Copy link

vegerot commented Mar 2, 2022

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 + `(.*)`)
Copy link

@ysmood ysmood Mar 25, 2022

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?

Copy link
Author

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:

  • failureMessage contains linefeeds
  • one or more lines in failureMessage starts with either expected: or actual : (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")
Copy link

@GerardRodes GerardRodes Apr 1, 2022

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

Copy link
Author

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 :(

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.

Copy link
Author

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.

Copy link
Author

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?

@walles
Copy link
Author

walles commented Sep 9, 2022

I think leaving this open doesn't provide any value, closing.

Feel free to reopen if a reviewer becomes available!

@walles walles closed this Sep 9, 2022
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.

Colorize "expected" and "actual"

5 participants