container/stub: override goexit methods
All checks were successful
Test / Create distribution (push) Successful in 34s
Test / Sandbox (push) Successful in 1m52s
Test / Hpkg (push) Successful in 3m34s
Test / Sandbox (race detector) (push) Successful in 4m29s
Test / Hakurei (race detector) (push) Successful in 5m25s
Test / Hakurei (push) Successful in 2m25s
Test / Flake checks (push) Successful in 1m36s

FailNow, Fatal, Fatalf, SkipNow, Skip and Skipf must be called from the goroutine created by the test.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2025-09-04 04:51:49 +09:00
parent ddfb865e2d
commit 4051577d6b
6 changed files with 157 additions and 49 deletions

View File

@@ -1,15 +1,36 @@
package stub
import "testing"
// PanicExit is a magic panic value treated as a simulated exit.
const PanicExit = 0xdeadbeef
const (
panicFailNow = 0xcafe0000 + iota
panicFatal
panicFatalf
)
// HandleExit must be deferred before calling with the stub.
func HandleExit() {
r := recover()
if r == PanicExit {
return
}
if r != nil {
func (s *Stub[K]) HandleExit() { handleExit(s.TB, true) }
func handleExit(t testing.TB, root bool) {
switch r := recover(); r {
case PanicExit:
break
case panicFailNow:
if root {
t.FailNow()
} else {
t.Fail()
}
break
case panicFatal, panicFatalf, nil:
break
default:
panic(r)
}
}