-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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
testing: report non-zero exit status in -json output #61839
Comments
The exit code in the output is not “as expected“. We normally only log the exit status if there is no other output to explain the failure: What you are seeing here is a subtle (and unfortunate) combination of factors.
|
That said: I think it would be reasonable to explicitly include the exit status in the JSON output when known. However, it should not be logged as an |
Thank you for clarifying that!
Does this mean that for the exit status we will need a new action type? Or maybe adding another field to the It might be helpful to panic in case of an exit code to highlight the termination stack trace. What do you think about that? |
Either of those seems reasonable to me.
Unfortunately that would be a backward-incompatible change: some existing tests already use It might be feasible for the specific case when |
Indeed. For example, in GoLand, the flag |
By the way, is the flag func Test(t *testing.T) {
println(1)
os.Exit(0)
} $ go test .
1
--- FAIL: Test (0.00s)
panic: unexpected call to os.Exit(0) during test [recovered]
panic: unexpected call to os.Exit(0) during test
goroutine 6 [running]:
testing.tRunner.func1.2({0x100d715c0, 0x100d93f88})
/opt/homebrew/opt/go/libexec/src/testing/testing.go:1526 +0x1c8
testing.tRunner.func1()
/opt/homebrew/opt/go/libexec/src/testing/testing.go:1529 +0x384
panic({0x100d715c0, 0x100d93f88})
/opt/homebrew/opt/go/libexec/src/runtime/panic.go:884 +0x204
os.Exit(0x0)
/opt/homebrew/opt/go/libexec/src/os/proc.go:67 +0x58
awesomeProject3.Test(0x0?)
/Users/Andrei.Efanov/Projects/chore/awesomeProject3/sample_test.go:10 +0x34
testing.tRunner(0x14000003a00, 0x100d939d8)
/opt/homebrew/opt/go/libexec/src/testing/testing.go:1576 +0x10c
created by testing.(*T).Run
/opt/homebrew/opt/go/libexec/src/testing/testing.go:1629 +0x368
FAIL awesomeProject3 0.139s
FAIL |
Yes; see #29062. |
I believe, since we agreed on panicing in case |
Change https://go.dev/cl/517355 mentions this issue: |
Given the linked CL 517355, should this proposal be retitled? |
Sure! Thank you for outlining that. |
This proposal has been added to the active column of the proposals project |
There are a bunch of tests that do things like implement a -updategolden flag that takes over and may exit non-zero at that point (if it fails). We should figure out how many tests would start breaking if we panic on non-zero exit status. I have definitely run into this myself and had to work around it. |
Can I contribute to this investigation anyhow? |
Thinking more about this, any code that can call log.Fatal but might be run during a test would now cause a test panic on top of the log.Fatal. This doesn't seem right to me. The panic on exit(0) is important because exit(0) stops the test and makes it look like it passed. Any given test has no idea whether there are more test functions to run, so exit(0) making the test look like it passed is definitely wrong. A non-zero exit (call it exit(1)) will not make a test look like it passed, so the same concerns do not apply. We retitled this issue because a CL was written focusing on exit(1) but the original complaint was about exit status not appearing in the test2json output. Maybe we should fix that instead. Maybe if test2json does not see the usual PASS/FAIL at the end, it should write its own {"Action": "fail", "Output: test output ended unexpectedly"} or something like that. |
Are there any objections to shifting this proposal over to showing the test exit status in the test2json output? |
@rsc , |
Hmm. I had a couple of recent encounters with tests that ended up invoking |
Going back to the top comment, both the 'go test' and 'go tool test2json' versions print a final Action:fail, though they differ about the package. Deleting the timestamps, go test prints:
and go tool test2json prints:
The "FAIL" print is coming from cmd/go, so it makes sense that test2json would not print it (it's not there to be printed). But we can make test2json synthesize a "exit status 2"-style print. |
Have all remaining concerns about this proposal been addressed? When using
This would match the output printed by |
Based on the discussion above, this proposal seems like a likely accept. When using
This would match the output printed by |
No change in consensus, so accepted. 🎉 When using
This would match the output printed by |
I have the following test file
sample_test.go
:If I use go test on that file, I get the exit code in the output as expected:
If I use go test2json instead, I will get no exit code in the output at all.
Try the same commands with the zero exit code and you will see a detailed panic message in both cases.
The text was updated successfully, but these errors were encountered: