From 1ec304e638c10d9e068f34fff287266c032a6130 Mon Sep 17 00:00:00 2001
From: sudoforge <no-reply@sudoforge.com>
Date: Thu, 8 May 2025 03:08:28 -0700
Subject: [PATCH] test: add support for Helper() (#1400)

This change adds support to //internal/test%recorder for Helper(), and
improves the ergonomics around error reporting.

Change-Id: Ia1762587b16dee9ba6ca3c428c1f935eb333a63b
---
 internal/test/recorder.go | 14 ++++++++++++++
 internal/test/test.go     | 11 ++++++-----
 2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/internal/test/recorder.go b/internal/test/recorder.go
index 7d9f4a5f..086b6519 100644
--- a/internal/test/recorder.go
+++ b/internal/test/recorder.go
@@ -7,6 +7,8 @@ import (
 
 const (
 	RecorderFailNow int = iota
+	RecorderFatal
+	RecorderFatalf
 )
 
 type recorder struct {
@@ -17,20 +19,24 @@ type recorder struct {
 }
 
 func (r *recorder) Error(args ...any) {
+	r.Helper()
 	r.failed = true
 	r.fail(fmt.Sprint(args...))
 }
 
 func (r *recorder) Errorf(format string, args ...any) {
+	r.Helper()
 	r.failed = true
 	r.fail(fmt.Sprintf(format, args...))
 }
 
 func (r *recorder) Fail() {
+	r.Helper()
 	r.failed = true
 }
 
 func (r *recorder) FailNow() {
+	r.Helper()
 	r.failed = true
 	panic(RecorderFailNow)
 }
@@ -40,11 +46,19 @@ func (r *recorder) Failed() bool {
 }
 
 func (r *recorder) Fatal(args ...any) {
+	r.Helper()
 	r.failed = true
 	r.fatal(fmt.Sprint(args...))
+	panic(RecorderFatal)
 }
 
 func (r *recorder) Fatalf(format string, args ...any) {
+	r.Helper()
 	r.failed = true
 	r.fatal(fmt.Sprintf(format, args...))
+	panic(RecorderFatalf)
+}
+
+func (r *recorder) Helper() {
+	r.TB.Helper()
 }
diff --git a/internal/test/test.go b/internal/test/test.go
index 8a40e30a..6d82069f 100644
--- a/internal/test/test.go
+++ b/internal/test/test.go
@@ -3,6 +3,7 @@ package test
 import (
 	"errors"
 	"math/rand"
+	"slices"
 	"testing"
 	"time"
 )
@@ -38,8 +39,6 @@ func (f *flaky) Run(fn func(t testing.TB)) {
 	var last error
 
 	for attempt := 1; attempt <= f.o.MaxAttempts; attempt++ {
-		f.t.Logf("attempt %d of %d", attempt, f.o.MaxAttempts)
-
 		r := &recorder{
 			TB:    f.t,
 			fail:  func(s string) { last = errors.New(s) },
@@ -47,13 +46,15 @@ func (f *flaky) Run(fn func(t testing.TB)) {
 		}
 
 		func() {
+			failCodes := []int{RecorderFailNow, RecorderFatalf, RecorderFatal}
 			defer func() {
-				if v := recover(); v != nil {
-					if code, ok := v.(int); ok && code != RecorderFailNow {
-						panic(v)
+				if rec := recover(); rec != nil {
+					if code, ok := rec.(int); ok && !slices.Contains(failCodes, code) {
+						panic(rec)
 					}
 				}
 			}()
+
 			fn(r)
 		}()
 
-- 
GitLab