template/pkg/result/goexit_test.go
2026-05-05 00:12:54 +00:00

40 lines
1.0 KiB
Go

//go:build result_goexit
// Run with: go test -tags result_goexit -run Test ./pkg/result/...
//
// The -run Test flag is required: Example_catch uses defer result.Catch inside
// a single goroutine, which cannot intercept runtime.Goexit — examples that
// rely on Catch are incompatible with this build tag.
package result_test
import (
"errors"
"testing"
"gitea.djmil.dev/go/template/pkg/result"
)
// TestExpectNotRecoverable verifies the core safety property of the goexit
// build: an Expect failure exits via runtime.Goexit, which is invisible to
// recover() — user code inside a Run/Go closure cannot accidentally swallow it.
func TestExpectNotRecoverable(t *testing.T) {
swallowed := false
err := result.Run(func() {
defer func() {
if recover() != nil {
swallowed = true
}
}()
result.Err[int](errors.New("oops")).Expect("test")
})
if swallowed {
t.Fatal("Expect failure was caught by recover() — goexit build should prevent this")
}
if err == nil {
t.Fatal("expected Run to collect the error, got nil")
}
}