aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/stub/exit_test.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2026-03-17 16:09:14 +0900
committerOphestra <cat@gensokyo.uk>2026-03-17 16:09:14 +0900
commitbac583f89edd1360150143ff51f7264c01aa69ec (patch)
tree47914efe1ae9fae256ef93725097bdb9255c03d5 /internal/stub/exit_test.go
parent722989c68256469963aee963bed9c6ab4bf55b67 (diff)
internal/stub: move from container
This package solves a very specific stubbing use case, in a less than elegant manner. Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/stub/exit_test.go')
-rw-r--r--internal/stub/exit_test.go111
1 files changed, 111 insertions, 0 deletions
diff --git a/internal/stub/exit_test.go b/internal/stub/exit_test.go
new file mode 100644
index 00000000..c977d2ec
--- /dev/null
+++ b/internal/stub/exit_test.go
@@ -0,0 +1,111 @@
+package stub_test
+
+import (
+ "testing"
+ _ "unsafe" // for go:linkname
+
+ "hakurei.app/internal/stub"
+)
+
+// Made available here to check panic recovery behaviour.
+//
+//go:linkname handleExitNew hakurei.app/internal/stub.handleExitNew
+func handleExitNew(t testing.TB)
+
+// 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.Parallel()
+
+ t.Run("exit", func(t *testing.T) {
+ t.Parallel()
+ defer stub.HandleExit(t)
+ panic(stub.PanicExit)
+ })
+
+ t.Run("goexit", func(t *testing.T) {
+ t.Parallel()
+
+ t.Run("FailNow", func(t *testing.T) {
+ t.Parallel()
+
+ ot := &overrideTFailNow{T: t}
+ defer func() {
+ if !ot.failNow {
+ t.Errorf("FailNow was never called")
+ }
+ }()
+ defer stub.HandleExit(ot)
+ panic(0xcafe0)
+ })
+
+ t.Run("Fail", func(t *testing.T) {
+ t.Parallel()
+
+ ot := &overrideTFailNow{T: t}
+ defer func() {
+ if !ot.fail {
+ t.Errorf("Fail was never called")
+ }
+ }()
+ defer handleExitNew(ot)
+ panic(0xcafe0)
+ })
+ })
+
+ t.Run("nil", func(t *testing.T) {
+ t.Parallel()
+ defer stub.HandleExit(t)
+ })
+
+ t.Run("passthrough", func(t *testing.T) {
+ t.Parallel()
+
+ t.Run("toplevel", func(t *testing.T) {
+ t.Parallel()
+
+ defer func() {
+ want := 0xcafe
+ if r := recover(); r != want {
+ t.Errorf("recover: %v, want %v", r, want)
+ }
+
+ }()
+ defer stub.HandleExit(t)
+ panic(0xcafe)
+ })
+
+ t.Run("new", func(t *testing.T) {
+ t.Parallel()
+
+ defer func() {
+ want := 0xcafe
+ if r := recover(); r != want {
+ t.Errorf("recover: %v, want %v", r, want)
+ }
+
+ }()
+ defer handleExitNew(t)
+ panic(0xcafe)
+ })
+ })
+}