[release-branch.go1.21] runtime: don't let the tests leave core files behind

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

For #65476.
Fixes #65478. 

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>
(cherry picked from commit cffdfe8d2c)
Reviewed-on: https://go-review.googlesource.com/c/go/+/560896
This commit is contained in:
Ian Lance Taylor 2023-09-01 12:52:48 -07:00 committed by Carlos Amedee
parent f38fca30a7
commit 6d31b27150
3 changed files with 16 additions and 1 deletions

View File

@ -24,10 +24,21 @@ import (
var toRemove []string
func TestMain(m *testing.M) {
_, coreErrBefore := os.Stat("core")
status := m.Run()
for _, file := range toRemove {
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)
}

View File

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

View File

@ -92,7 +92,9 @@ func CgoExternalThreadSignal() {
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 {
fmt.Println("C signal did not crash as expected")
fmt.Printf("\n%s\n", out)