diff options
-rw-r--r-- | internal/test/recorder.go | 14 | ||||
-rw-r--r-- | 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 7d9f4a5f2..086b65191 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 8a40e30ab..6d82069f9 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) }() |