From 6c4df028210a7b1274777c34fd5d0700edf396b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Mon, 1 Jul 2019 18:30:01 +0200 Subject: [PATCH] cmd/go: fail if a test binary exits with no output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For example, if a test calls os.Exit(0), that could trick a 'go test' run into not running some of the other tests, and thinking that they all succeeded. This can easily go unnoticed and cause developers headaches. Add a simple sanity check as part of 'go test': if the test binary succeeds and doesn't print anything, we should error, as something clearly went very wrong. This is done by inspecting each of the stdout writes from the spawned process, since we don't want to read the entirety of the output into a buffer. We need to introduce a "buffered" bool var, as there's now an io.Writer layer between cmd.Stdout and &buf. A few TestMain funcs in the standard library needed fixing, as they returned without printing anything as a means to skip testing the entire package. For that purpose add testenv.MainMust, which prints a warning and prints SKIP, similar to when -run matches no tests. Finally, add tests for both os.Exit(0) and os.Exit(1), both as part of TestMain and as part of a single test, and test that the various stdout modes still do the right thing. Fixes #29062. Change-Id: Ic6f8ef3387dfc64e4cd3e8f903d7ca5f5f38d397 Reviewed-on: https://go-review.googlesource.com/c/go/+/184457 Run-TryBot: Daniel Martí TryBot-Result: Gobot Gobot Reviewed-by: Bryan C. Mills --- internal/testenv/testenv.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/internal/testenv/testenv.go b/internal/testenv/testenv.go index b036aa6..5cb1327 100644 --- a/internal/testenv/testenv.go +++ b/internal/testenv/testenv.go @@ -13,6 +13,7 @@ package testenv import ( "errors" "flag" + "fmt" "internal/cfg" "os" "os/exec" @@ -32,6 +33,14 @@ func Builder() string { return os.Getenv("GO_BUILDER_NAME") } +func MainMust(cond func() bool) { + if !cond() { + fmt.Println("testenv: warning: can't run any tests") + fmt.Println("SKIP") + os.Exit(0) + } +} + // HasGoBuild reports whether the current system can build programs with ``go build'' // and then run them with os.StartProcess or exec.Command. func HasGoBuild() bool {