runtime: don't let the tests leave core files behind

Also add a check that we didn't leave any core files behind.

Change-Id: I30444ef43ad1a8cc1cacd3b75280f2128e104939
Reviewed-on: https://go-review.googlesource.com/c/go/+/525175
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
Ian Lance Taylor 2023-09-01 12:52:48 -07:00 committed by Gopher Robot
parent a819178915
commit cffdfe8d2c
3 changed files with 16 additions and 1 deletions

View File

@ -24,10 +24,21 @@ import (
var toRemove []string var toRemove []string
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
_, coreErrBefore := os.Stat("core")
status := m.Run() status := m.Run()
for _, file := range toRemove { for _, file := range toRemove {
os.RemoveAll(file) os.RemoveAll(file)
} }
_, coreErrAfter := os.Stat("core")
if coreErrBefore != nil && coreErrAfter == nil {
fmt.Fprintln(os.Stderr, "runtime.test: some test left a core file behind")
if status == 0 {
status = 1
}
}
os.Exit(status) os.Exit(status)
} }

View File

@ -91,6 +91,7 @@ func TestCrashDumpsAllThreads(t *testing.T) {
cmd := testenv.Command(t, exe, "CrashDumpsAllThreads") cmd := testenv.Command(t, exe, "CrashDumpsAllThreads")
cmd = testenv.CleanCmdEnv(cmd) cmd = testenv.CleanCmdEnv(cmd)
cmd.Dir = t.TempDir() // put any core file in tempdir
cmd.Env = append(cmd.Env, cmd.Env = append(cmd.Env,
"GOTRACEBACK=crash", "GOTRACEBACK=crash",
// Set GOGC=off. Because of golang.org/issue/10958, the tight // Set GOGC=off. Because of golang.org/issue/10958, the tight
@ -164,6 +165,7 @@ func TestPanicSystemstack(t *testing.T) {
t.Parallel() t.Parallel()
cmd := exec.Command(os.Args[0], "testPanicSystemstackInternal") cmd := exec.Command(os.Args[0], "testPanicSystemstackInternal")
cmd = testenv.CleanCmdEnv(cmd) cmd = testenv.CleanCmdEnv(cmd)
cmd.Dir = t.TempDir() // put any core file in tempdir
cmd.Env = append(cmd.Env, "GOTRACEBACK=crash") cmd.Env = append(cmd.Env, "GOTRACEBACK=crash")
pr, pw, err := os.Pipe() pr, pw, err := os.Pipe()
if err != nil { if err != nil {

View File

@ -92,7 +92,9 @@ func CgoExternalThreadSignal() {
return return
} }
out, err := exec.Command(os.Args[0], "CgoExternalThreadSignal", "crash").CombinedOutput() cmd := exec.Command(os.Args[0], "CgoExternalThreadSignal", "crash")
cmd.Dir = os.TempDir() // put any core file in tempdir
out, err := cmd.CombinedOutput()
if err == nil { if err == nil {
fmt.Println("C signal did not crash as expected") fmt.Println("C signal did not crash as expected")
fmt.Printf("\n%s\n", out) fmt.Printf("\n%s\n", out)