-
Notifications
You must be signed in to change notification settings - Fork 17.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/go, testing, os: fail test that calls os.Exit(0)
This catches cases where a test calls code that calls os.Exit(0), thereby skipping all subsequent tests. Fixes #29062 Change-Id: If9478972f40189e27623557e7141469ca4234d89 Reviewed-on: https://go-review.googlesource.com/c/go/+/250977 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
- Loading branch information
1 parent
cdc6355
commit 4f76fe8
Showing
9 changed files
with
195 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# Builds and runs test binaries, so skip in short mode. | ||
[short] skip | ||
|
||
env GO111MODULE=on | ||
|
||
# If a test invoked by 'go test' exits with a zero status code, | ||
# it will panic. | ||
! go test ./zero | ||
! stdout ^ok | ||
! stdout 'exit status' | ||
stdout 'panic' | ||
stdout ^FAIL | ||
|
||
# If a test exits with a non-zero status code, 'go test' fails normally. | ||
! go test ./one | ||
! stdout ^ok | ||
stdout 'exit status' | ||
! stdout 'panic' | ||
stdout ^FAIL | ||
|
||
# Ensure that other flags still do the right thing. | ||
go test -list=. ./zero | ||
stdout ExitZero | ||
|
||
! go test -bench=. ./zero | ||
stdout 'panic' | ||
|
||
# 'go test' with no args streams output without buffering. Ensure that it still | ||
# catches a zero exit with missing output. | ||
cd zero | ||
! go test | ||
stdout 'panic' | ||
cd ../normal | ||
go test | ||
stdout ^ok | ||
cd .. | ||
|
||
# If a TestMain exits with a zero status code, 'go test' shouldn't | ||
# complain about that. It's a common way to skip testing a package | ||
# entirely. | ||
go test ./main_zero | ||
! stdout 'skipping all tests' | ||
stdout ^ok | ||
|
||
# With -v, we'll see the warning from TestMain. | ||
go test -v ./main_zero | ||
stdout 'skipping all tests' | ||
stdout ^ok | ||
|
||
# Listing all tests won't actually give a result if TestMain exits. That's okay, | ||
# because this is how TestMain works. If we decide to support -list even when | ||
# TestMain is used to skip entire packages, we can change this test case. | ||
go test -list=. ./main_zero | ||
stdout 'skipping all tests' | ||
! stdout TestNotListed | ||
|
||
-- go.mod -- | ||
module m | ||
|
||
-- ./normal/normal.go -- | ||
package normal | ||
-- ./normal/normal_test.go -- | ||
package normal | ||
|
||
import "testing" | ||
|
||
func TestExitZero(t *testing.T) { | ||
} | ||
|
||
-- ./zero/zero.go -- | ||
package zero | ||
-- ./zero/zero_test.go -- | ||
package zero | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestExitZero(t *testing.T) { | ||
os.Exit(0) | ||
} | ||
|
||
-- ./one/one.go -- | ||
package one | ||
-- ./one/one_test.go -- | ||
package one | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestExitOne(t *testing.T) { | ||
os.Exit(1) | ||
} | ||
|
||
-- ./main_zero/zero.go -- | ||
package zero | ||
-- ./main_zero/zero_test.go -- | ||
package zero | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
fmt.Println("skipping all tests") | ||
os.Exit(0) | ||
} | ||
|
||
func TestNotListed(t *testing.T) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2020 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package testlog | ||
|
||
import "sync" | ||
|
||
// PanicOnExit0 reports whether to panic on a call to os.Exit(0). | ||
// This is in the testlog package because, like other definitions in | ||
// package testlog, it is a hook between the testing package and the | ||
// os package. This is used to ensure that an early call to os.Exit(0) | ||
// does not cause a test to pass. | ||
func PanicOnExit0() bool { | ||
panicOnExit0.mu.Lock() | ||
defer panicOnExit0.mu.Unlock() | ||
return panicOnExit0.val | ||
} | ||
|
||
// panicOnExit0 is the flag used for PanicOnExit0. This uses a lock | ||
// because the value can be cleared via a timer call that may race | ||
// with calls to os.Exit | ||
var panicOnExit0 struct { | ||
mu sync.Mutex | ||
val bool | ||
} | ||
|
||
// SetPanicOnExit0 sets panicOnExit0 to v. | ||
func SetPanicOnExit0(v bool) { | ||
panicOnExit0.mu.Lock() | ||
defer panicOnExit0.mu.Unlock() | ||
panicOnExit0.val = v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters