MichaelMure / git-bug

Distributed, offline-first bug tracker embedded in git, with bridges

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tests fails under interactive shell (color issue)

stacyharper opened this issue · comments

Hey there,

Looks like with my regular shell setup the tests fails. I have the issue with abuild (Alpine Linux) or with regular make test. I guess as an interactive shell, some git commands output are colored (a magenta "John Doe").

=== RUN   TestCommentAdd
Building identity cache... Done.
Building bug cache... Done.
    comment_add_test.go:32: 
        	Error Trace:	/home/willow/sources/git-bug/commands/comment_test.go:151
        	           				/home/willow/sources/git-bug/commands/comment_add_test.go:32
        	Error:      	Not equal: 
        	           	expected: "John Doe"
        	           	actual  : "\x1b[35mJohn Doe\x1b[0m"
        	           	
        	           	Diff:
        	           	--- Expected
        	           	+++ Actual
        	           	@@ -1 +1 @@
        	           	-John Doe
        	           	+John Doe
        	Test:       	TestCommentAdd
--- FAIL: TestCommentAdd (0.02s)
=== RUN   TestCommentEdit
Building identity cache... Done.
Building bug cache... Done.
    comment_edit_test.go:17: 
        	Error Trace:	/home/willow/sources/git-bug/commands/comment_edit_test.go:17
        	Error:      	Received unexpected error:
        	           	comment doesn't exist
        	Test:       	TestCommentEdit
--- FAIL: TestCommentEdit (0.03s)
=== RUN   TestComment
Building identity cache... Done.
Building bug cache... Done.
    comment_test.go:20: 
        	Error Trace:	/home/willow/sources/git-bug/commands/comment_test.go:151
        	           				/home/willow/sources/git-bug/commands/comment_test.go:20
        	Error:      	Not equal: 
        	           	expected: "John Doe"
        	           	actual  : "\x1b[35mJohn Doe\x1b[0m"
        	           	
        	           	Diff:
        	           	--- Expected
        	           	+++ Actual
        	           	@@ -1 +1 @@
        	           	-John Doe
        	           	+John Doe
        	Test:       	TestComment
--- FAIL: TestComment (0.02s)

I successfully built and test with piping "make test | less".

It looks like the tests somehow think that the output buffer is a terminal supporting colors. Quite odd indeed, in particular why that would happen to you and nowhere else.

It could be resolve easily by explicitly disabling color support, but it would be nice to understand what's happening.

This is clearly my fault but it's a bit odd that tests pass without the -bench=. flag and fail if it's present. I'll have to remember to run tests via the Makefile every now and then (before pushing commits?). I'll admit I usually run tests using go test ./... or go test -v ./... just to make them faster.

My initial thought was that the shell used by the Makefile was subtly different but now I've got some investigation into the benchmarking environment. Piping the output to less works because that's disabling ANSI colors (you can see this with grep and many other *nix tools that colorize the output for humans).

This is pretty bizarre - when the tests are run with benchmarking (go test -v -bench=. ./...), os.Stdout contains an FD that is in fact a TTY. When the tests are run without benchmarking, os.Stdout contains an FD that is NOT a TTY. Here's the package/method/function stack I've investigated so far - the problem is in Go cmd runner:

Failing cases:

  • go test -v -bench=. ./...
  • go test -v -fuzz=. ./...
  • cd commands/bug && go test -v ./...

Passing case:

  • go test -v ./...

I've asked for advice in the #testing channel of the Gopher's Slack - my opinion is that buffered or not, the output shouldn't change in this case but I'm waiting for advice on either a work-around or on submitting an issue with a proper description. At this point, I've got a tiny and concise program demonstrates this error so the good news is that it's not a problem with git-bug.

Submitted to the Go issue tracker - golang/go#57671

It turns out this issue was discussed and rejected in golang/go#34877. Fortunately, I already had a fix ready and have opened the associated PR - #984.