Skip to content
Snippets Groups Projects
Unverified Commit 1ec304e6 authored by sudoforge's avatar sudoforge Committed by GitHub
Browse files

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
parent 1766f85b
Branches
Tags
No related merge requests found
......@@ -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()
}
......@@ -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)
}()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment