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

Improve Test to use colors to highlight failures and diffs #4170

Merged
merged 1 commit into from
Mar 4, 2021

Conversation

fingolfin
Copy link
Member

@fingolfin fingolfin commented Nov 12, 2020

... and some other test code, to make it easier to see and understand diffs in there.

Consider this test file:

gap> 1+1;
2 
gap> 2+2;
4

This will actually cause a test failure, due to whitespace, which makes it annoying to discover. Currently, Test will produce something like this:

Screenshot 2020-11-12 at 11 48 31

With this PR, you'll get this:
Screenshot 2020-11-12 at 11 55 41

This change is IMHO useful beyond better understanding whitespace diffs. For example when I run the GAP test suite and there are multiple failures, it can be challenging to visually parse what is being displayed. With this PR, I find it much easier to understand what is going on.

Here is a more complex example:

gap> 1+1;
2 
gap> 2+2;
4
# comment in a bad place

gap> 3+3;
6
gap> Print("1\n2\n3\n");
1
2
3
gap> true
true

Which currently produces this, which I find hard to make sense out (basically, I resorted to trying to fix the first one and then hope I can repeat this; or I use the rewriteFile:=true option):
Screenshot 2020-11-12 at 12 16 35

With this PR (the output could still be nicer, but it's a start):
Screenshot 2020-11-12 at 12 17 15

@fingolfin fingolfin added kind: enhancement Label for issues suggesting enhancements; and for pull requests implementing enhancements topic: tests issues or PRs related to tests release notes: to be added PRs introducing changes that should be (but have not yet been) mentioned in the release notes labels Nov 12, 2020
@ChrisJefferson
Copy link
Contributor

Does this make the output hard to read in less, and similar programs, by filling them with colour codes? (does GAPDoc already handle this by dropping them based on where stdout is going?)

@fingolfin
Copy link
Member Author

I've never ever tried to send the output of Test() into a pager. But in general, less can handle color escape codes just fine with the right settings (namely -r or -R).

So I just tried ./gap -c 'Test("simple.tst"); QUIT;' | less -r and it worked fine. But without the -r, less indeed prints the escape sequences. But that's the normal thing for less, isn't it?

Copy link
Member

@wilfwilson wilfwilson left a comment

Choose a reason for hiding this comment

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

I do like the idea of colours in the output. However, I use a terminal with a white background, and the yellow text on a white background is unpleasant to read.

Screen Shot 2020-11-14 at 09 09 51

Could you find a solution for the currently-yellow text that has high contrast with both light and dark terminal backgrounds? Or perhaps only colour the ####### bit (which doesn't actually need to be read), and leave the other yellow text as it was?

@fingolfin
Copy link
Member Author

fingolfin commented Nov 18, 2020

Yeah colors could be tweaked; I also wonder about people who are red-green blind.

@olexandr-konovalov
Copy link
Member

Can you deliberately break some more tests for testing this PR, so that we can see how GitHub actions will render colours.

@fingolfin
Copy link
Member Author

@wilfwilson there are some more colors we could pick from. Also, background color can be used, and of course any combination for foreground and background color could be used. But of course most terminals also allow overriding these colors, so there won't be a single "optimal" choice, ever... julia has a --color=yes/no command line option to be able to disable or enable use of colors. In GAP, we have ColorPrompt and I guess we could also add a test option to force disabling/enabling colors.

Screenshot 2020-11-20 at 01 15 45

@alex-konovalov GitHub Actions renders the colors just fine, see e.g

Copy link
Member

@wilfwilson wilfwilson left a comment

Choose a reason for hiding this comment

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

Actually I'm happy enough with it as it is.

@fingolfin
Copy link
Member Author

@alex-konovalov GH Actions handles the colors just fine. But I introduced a failure so you can look for yourself, e.g. at https://github.com/gap-system/gap/pull/4170/checks?check_run_id=1427795100

@wilfwilson well, I am really open to change the colors, I just don't know what a good compromise might be. Perhaps one could explicitly set foreground and background color to increase the chance of good contrast? Playing with the code to change the colors is relatively easy.

@wilfwilson
Copy link
Member

I had thought about it a bit, and didn't feel that I could suggest an improvement. However I have thought about it a bit more.

I think combining text and bg colours is a good way to make sure that the text looks decent in light and dark terminal windows. And this is my preference (not strongly held opinion though):

  • Text with green background should be 'black'
  • Text with red background should be 'white'
  • The currently yellow text should be given a black background, or this should be inverted to black text on a yellow background.

Here are those styles on white/black terminal backgrounds:

Screen Shot 2020-11-20 at 12 37 29

I think they're all quite clear.

@fingolfin
Copy link
Member Author

That's fine by me; but of course it won't be perfect in all cases: someone might have a terminal theme with a very bright red and a super dark green... There are terminals which allow specifying 24bit RGB values... but I guess that's overkill ;-).

Anyway, I used your color suggestions (with yellow-on-black), and also made these potentially configurable via a global record DefaultReportDiffColors (undocumented for now).

@wilfwilson
Copy link
Member

Yes, but although it might look ugly in some bizarre terminal setups, hopefully it'll all still be readable 🙂

Thanks. I'm happy with it. The record is a good idea.

@frankluebeck
Copy link
Member

A few comments, since I have experimented a bit with colors in terminals:

  • avoid yellow (in standard terminal settings), it is difficult to read on many terminals
  • also avoid using background/foreground combinations, these are unpleasant to read (I think I have removed all background settings in GAPDoc)
  • colors should not be used if the user preference "UseColorsInTerminal" is set to false
  • you could use a user preference to enable users to change the colors (e.g., GAPDoc has the user preference "TextTheme"); any setting without yellow is probably a reasonable default for most users

@fingolfin fingolfin force-pushed the mh/color-tests branch 2 times, most recently from 210e65a to 2199f52 Compare February 17, 2021 00:04
@codecov
Copy link

codecov bot commented Feb 17, 2021

Codecov Report

Merging #4170 (2199f52) into master (d0fb91e) will increase coverage by 1.65%.
The diff coverage is 27.90%.

@@            Coverage Diff             @@
##           master    #4170      +/-   ##
==========================================
+ Coverage   80.17%   81.83%   +1.65%     
==========================================
  Files         637      637              
  Lines      272021   272660     +639     
==========================================
+ Hits       218086   223123    +5037     
+ Misses      53935    49537    -4398     
Impacted Files Coverage Δ
lib/test.gi 63.13% <27.90%> (-1.76%) ⬇️
src/baltree.h 0.00% <0.00%> (-97.53%) ⬇️
src/dynarray.h 0.00% <0.00%> (-94.12%) ⬇️
src/libgap-api.c 4.50% <0.00%> (-59.50%) ⬇️
lib/process.gi 26.42% <0.00%> (-43.58%) ⬇️
src/sysjmp.c 52.17% <0.00%> (-30.44%) ⬇️
src/compiler.c 68.06% <0.00%> (-20.67%) ⬇️
lib/files.gi 58.33% <0.00%> (-17.51%) ⬇️
src/error.c 74.78% <0.00%> (-14.71%) ⬇️
src/vars.c 82.22% <0.00%> (-11.64%) ⬇️
... and 158 more

@fingolfin
Copy link
Member Author

A few comments, since I have experimented a bit with colors in terminals:

  • avoid yellow (in standard terminal settings), it is difficult to read on many terminals

I changed it to blue now, which looks OK for me in various terminal theme choices (but of course you can always find places where any choice is problematic, e.g. if someone really loves a blue background for their terminal.... though at least in my terminal, I would then also change the "blue color" to be something else)

  • also avoid using background/foreground combinations, these are unpleasant to read (I think I have removed all background settings in GAPDoc)

While I am not a fan of them, but for this purpose I think they make sense, because they make it possible to "see" whitespace issues, which often are an annoying source of test diffs.

  • colors should not be used if the user preference "UseColorsInTerminal" is set to false

Good point, and added.

  • you could use a user preference to enable users to change the colors (e.g., GAPDoc has the user preference "TextTheme"); any setting without yellow is probably a reasonable default for most users

I'll need to look into the userpref mechanism to see whether I can do it / whether to put it in this PR or a follow-up.

@fingolfin fingolfin changed the title Use colors in output of Test() and some other tests Use colors in output of Test Feb 17, 2021
@wilfwilson
Copy link
Member

I'm happy with this.

@fingolfin
Copy link
Member Author

Here is a fresh side-by-side comparison screenshot. The output in the left is basically impossible for me to read correctly. The one on the right with colors still is nasty, but I can manage.

Screenshot 2021-02-18 at 00 35 12

... to make it easier to see and understand diffs.
@fingolfin
Copy link
Member Author

I'd rather not delay this for adding UserPreference: the colors already are customizable by tweaking DefaultReportDiffColors in gap.ini, and if a userpref is desired, we can add one later; but I'd like to benefit from the colors now...

@fingolfin fingolfin closed this Mar 4, 2021
@fingolfin fingolfin reopened this Mar 4, 2021
@fingolfin fingolfin merged commit 273886a into gap-system:master Mar 4, 2021
@fingolfin fingolfin deleted the mh/color-tests branch March 4, 2021 11:00
@fingolfin fingolfin changed the title Use colors in output of Test Improve Test to use colors to highlight failures and diffs Aug 17, 2022
@fingolfin fingolfin added release notes: use title For PRs: the title of this PR is suitable for direct use in the release notes and removed release notes: to be added PRs introducing changes that should be (but have not yet been) mentioned in the release notes labels Aug 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: enhancement Label for issues suggesting enhancements; and for pull requests implementing enhancements release notes: use title For PRs: the title of this PR is suitable for direct use in the release notes topic: tests issues or PRs related to tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants