-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
cmd/go: fail tests that invoke os.Exit(0) explicitly #29062
Comments
Here is a smaller repro:
I agree that this can be confusing, since Perhaps |
A perhaps simple fix would be for |
@mvdan: That fix sounds like the right one. |
A test binary can provide an arbitrary |
Are you talking about |
I did some experimenting and it looks like I was just mistaken. Carry on! |
Change https://golang.org/cl/184457 mentions this issue: |
From the CL:
How concerning is this for breaking user tests? If you had to fix up stdlib tests, it's likely that many tests in user code are going to break. @randall77 #31969 mentions a |
Historically, it has been acceptable to break user programs that were doing the opposite of what is documented. For example, see #31859. We want to avoid this breakage whenever possible, but in some cases it's the best option. I think this is one of those cases. The fix will uncover package tests that were succeeding by accident, which was the motivation of this bug. And after all, fixing the broken tests was a two-line change, and I think that's a reasonable thing to ask of the users who weren't following the docs. |
Change https://golang.org/cl/200104 mentions this issue: |
Change https://golang.org/cl/200106 mentions this issue: |
This reverts CL 184457. Reason for revert: introduced failures in the regression test for #18153. Fixes #34791 Updates #29062 Change-Id: I4040965163f809083c023be055e69b1149d6214e Reviewed-on: https://go-review.googlesource.com/c/go/+/200106 Run-TryBot: Bryan C. Mills <bcmills@google.com> Reviewed-by: Alexander Rakoczy <alex@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
Thanks for taking care of this @bcmills. I do fundamentally disagree with the premise of #18153. For example, it makes tests brittle, and requires them to be run with a real terminal, and no arguments:
Moreover, it makes it very hard (or impossible, I suspect) to inspect or modify the output in any way, like we needed to do here. Do you reckon a proposal is necessary to make a proper decision here? |
I realise that the original idea was to color test output, and not necessarily make tests fail or pass depending on stdout being a terminal. Still, I think it's wrong to change the test's behavior depending on whether it happens to be run on a terminal. If a user really wants color on their test outputs, they could use a test flag, or an environment variable like |
This issue is currently labeled as early-in-cycle for Go 1.15. That time is now, so friendly ping. If it no longer needs to be done early in cycle, that label can be removed. |
Change https://golang.org/cl/222243 mentions this issue: |
'go test -json' should report that a test failed if the test binary did not exit normally with status 0. This covers panics, non-zero exits, and abnormal terminations. These tests don't print a final result when run with -test.v (which is used by 'go test -json'). The final result should be "PASS" or "FAIL" on a line by itself. 'go test' prints "FAIL" in this case, but includes error information. test2json was changed in CL 192104 to report that a test passed if it does not report a final status. This caused 'go test -json' to report that a test passed after a panic or non-zero exit. With this change, test2json treats "FAIL" with error information the same as "FAIL" on a line by itself. This is intended to be a minimal fix for backporting, but it will likely be replaced by a complete solution for #29062. Fixes #37555 Updates #29062 Updates #31969 Change-Id: Icb67bcd36bed97e6a8d51f4d14bf71f73c83ac3d Reviewed-on: https://go-review.googlesource.com/c/go/+/222243 Run-TryBot: Jay Conrod <jayconrod@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
Change https://golang.org/cl/222658 mentions this issue: |
…r panicking/exiting tests 'go test -json' should report that a test failed if the test binary did not exit normally with status 0. This covers panics, non-zero exits, and abnormal terminations. These tests don't print a final result when run with -test.v (which is used by 'go test -json'). The final result should be "PASS" or "FAIL" on a line by itself. 'go test' prints "FAIL" in this case, but includes error information. test2json was changed in CL 192104 to report that a test passed if it does not report a final status. This caused 'go test -json' to report that a test passed after a panic or non-zero exit. With this change, test2json treats "FAIL" with error information the same as "FAIL" on a line by itself. This is intended to be a minimal fix for backporting, but it will likely be replaced by a complete solution for #29062. Fixes #37671 Updates #37555 Updates #29062 Updates #31969 Change-Id: Icb67bcd36bed97e6a8d51f4d14bf71f73c83ac3d Reviewed-on: https://go-review.googlesource.com/c/go/+/222243 Run-TryBot: Jay Conrod <jayconrod@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com> (cherry picked from commit 5ea58c6) Reviewed-on: https://go-review.googlesource.com/c/go/+/222658 Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com>
This issue is currently labeled as early-in-cycle for Go 1.16. |
Change https://golang.org/cl/250977 mentions this issue: |
Change https://golang.org/cl/251262 mentions this issue: |
cmd/go.TestScript/test_main_twice demonstrates a program that invokes (*M).Run twice in a row. If we only restore os.Exit(0) in m.afterOnce, we will fail to restore it after the second run and fail the test process despite both runs passing. Updates #29062 Updates #23129 Change-Id: Id22ec68f1708e4583c8dda14a8ba0efae7178b85 Reviewed-on: https://go-review.googlesource.com/c/go/+/251262 Run-TryBot: Bryan C. Mills <bcmills@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
Change https://golang.org/cl/283873 mentions this issue: |
For #29062 Fixes #43263 Change-Id: I160197c94cc4f936967cc22c82cec01663a14fe6 Reviewed-on: https://go-review.googlesource.com/c/go/+/283873 Trust: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com> TryBot-Result: Go Bot <gobot@golang.org>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Here is a code snip to explain this issue:
Please write above code to some Go file and run:
It outputs
0
. So it means that test is ok. However, actually test has stopped at the middle of execution sinceos.Exit(0)
is accidentally called.I'm not sure that this is a bug. It may be intended behavior. But when calling
os.Exit(0)
in tests accidentally (for example, due to lack of understanding of API), I may not notice the tests are wrongly run since it exits successfully. CI also cannot detect it.What did you expect to see?
IMO,
go test
exiting with non-zero exit status when the tests exit at the middle of execution byos.Exit()
would solve this issue.What did you see instead?
echo $?
echoes0
and test exited successfullyThe text was updated successfully, but these errors were encountered: