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
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:
@@ -2,18 +2,67 @@ package stub_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
_ "unsafe"
|
||||
|
||||
"hakurei.app/container/stub"
|
||||
)
|
||||
|
||||
//go:linkname handleExit hakurei.app/container/stub.handleExit
|
||||
func handleExit(_ testing.TB, _ bool)
|
||||
|
||||
// overrideTFailNow overrides the Fail and FailNow method.
|
||||
type overrideTFailNow struct {
|
||||
*testing.T
|
||||
failNow bool
|
||||
fail bool
|
||||
}
|
||||
|
||||
func (o *overrideTFailNow) FailNow() {
|
||||
if o.failNow {
|
||||
o.Errorf("attempted to FailNow twice")
|
||||
}
|
||||
o.failNow = true
|
||||
}
|
||||
|
||||
func (o *overrideTFailNow) Fail() {
|
||||
if o.fail {
|
||||
o.Errorf("attempted to Fail twice")
|
||||
}
|
||||
o.fail = true
|
||||
}
|
||||
|
||||
func TestHandleExit(t *testing.T) {
|
||||
t.Run("exit", func(t *testing.T) {
|
||||
defer stub.HandleExit()
|
||||
defer handleExit(t, true)
|
||||
panic(stub.PanicExit)
|
||||
})
|
||||
|
||||
t.Run("goexit", func(t *testing.T) {
|
||||
t.Run("FailNow", func(t *testing.T) {
|
||||
ot := &overrideTFailNow{T: t}
|
||||
defer func() {
|
||||
if !ot.failNow {
|
||||
t.Errorf("FailNow was never called")
|
||||
}
|
||||
}()
|
||||
defer handleExit(ot, true)
|
||||
panic(0xcafe0000)
|
||||
})
|
||||
|
||||
t.Run("Fail", func(t *testing.T) {
|
||||
ot := &overrideTFailNow{T: t}
|
||||
defer func() {
|
||||
if !ot.fail {
|
||||
t.Errorf("Fail was never called")
|
||||
}
|
||||
}()
|
||||
defer handleExit(ot, false)
|
||||
panic(0xcafe0000)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("nil", func(t *testing.T) {
|
||||
defer stub.HandleExit()
|
||||
defer handleExit(t, true)
|
||||
})
|
||||
|
||||
t.Run("passthrough", func(t *testing.T) {
|
||||
@@ -24,7 +73,7 @@ func TestHandleExit(t *testing.T) {
|
||||
}
|
||||
|
||||
}()
|
||||
defer stub.HandleExit()
|
||||
defer handleExit(t, true)
|
||||
panic(0xcafebabe)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user