From 52ca7b9c134adadc41d418c9f4f05011be77be3d Mon Sep 17 00:00:00 2001 From: Dave Sirockin Date: Wed, 11 Sep 2024 10:35:43 +0200 Subject: [PATCH 1/3] chore: make Error and Fatal functions compatible with testing --- pkg/f1/testing/t.go | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/pkg/f1/testing/t.go b/pkg/f1/testing/t.go index 5fc287a0..d7ed0868 100644 --- a/pkg/f1/testing/t.go +++ b/pkg/f1/testing/t.go @@ -146,9 +146,18 @@ func (t *T) Errorf(format string, args ...interface{}) { t.Fail() } -// Error is equivalent to Log followed by Fail. -func (t *T) Error(err error) { - t.logger.Error("iteration failed", log.IterationAttr(t.Iteration), log.ErrorAttr(err)) +// Error logs an error then calls Fail() +func (t *T) Error(args ...interface{}) { + // Special case, single error argument + if len(args) == 1 { + err, ok := args[0].(error) + if ok { + t.logger.Error("iteration failed", log.IterationAttr(t.Iteration), log.ErrorAttr(err)) + t.Fail() + return + } + } + t.Log(args...) t.Fail() } @@ -158,9 +167,18 @@ func (t *T) Fatalf(format string, args ...interface{}) { t.FailNow() } -// Fatal is equivalent to Log followed by FailNow. -func (t *T) Fatal(err error) { - t.logger.Error("iteration failed", log.IterationAttr(t.Iteration), log.ErrorAttr(err)) +// Fatal logs an error then calls FailNow. +func (t *T) Fatal(args ...interface{}) { + // Special case, single error argument + if len(args) == 1 { + err, ok := args[0].(error) + if ok { + t.logger.Error("iteration failed", log.IterationAttr(t.Iteration), log.ErrorAttr(err)) + t.FailNow() + return + } + } + t.Log(args...) t.FailNow() } From eee6067358cc64e4351b2a827e8d8e177c2ddd47 Mon Sep 17 00:00:00 2001 From: Dave Sirockin Date: Wed, 11 Sep 2024 11:05:00 +0200 Subject: [PATCH 2/3] test: update Fatal/Error tests to take account of more flexible arguments --- pkg/f1/testing/t_test.go | 68 ++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/pkg/f1/testing/t_test.go b/pkg/f1/testing/t_test.go index fb6e4c7c..8007f9fd 100644 --- a/pkg/f1/testing/t_test.go +++ b/pkg/f1/testing/t_test.go @@ -112,11 +112,29 @@ func TestFailSetsTheFailedState(t *testing.T) { func TestErrorSetsTheFailedState(t *testing.T) { t.Parallel() - newT, teardown := newT() - defer teardown() - - newT.Error(errors.New("boom")) - require.True(t, newT.Failed()) + tests := map[string]struct { + args []any + }{ + "error argument": { + args: []any{errors.New("boom")}, + }, + "no arguments": { + args: []any{}, + }, + "random arguments": { + args: []any{"boom", 1, 2.0}, + }, + } + + for testName, test := range tests { + t.Run(testName, func(t *testing.T) { + newT, teardown := newT() + defer teardown() + + newT.Error(test.args...) + require.True(t, newT.Failed()) + }) + } } func TestErrorfSetsTheFailedState(t *testing.T) { @@ -132,17 +150,35 @@ func TestErrorfSetsTheFailedState(t *testing.T) { func TestFatalSetsTheFailedState(t *testing.T) { t.Parallel() - newT, teardown := newT() - defer teardown() - - done := make(chan struct{}) - go func() { - defer catchPanics(done) - newT.Fatal(errors.New("boom")) - }() - <-done - - require.True(t, newT.Failed()) + tests := map[string]struct { + args []any + }{ + "error argument": { + args: []any{errors.New("boom")}, + }, + "no arguments": { + args: []any{}, + }, + "random arguments": { + args: []any{"boom", 1, 2.0}, + }, + } + + for testName, test := range tests { + t.Run(testName, func(t *testing.T) { + newT, teardown := newT() + defer teardown() + + done := make(chan struct{}) + go func() { + defer catchPanics(done) + newT.Fatal(test.args...) + }() + <-done + + require.True(t, newT.Failed()) + }) + } } func TestFatalfSetsTheFailedState(t *testing.T) { From ac66ef9c3fa6f86c93a30f80541c3581ed4cae27 Mon Sep 17 00:00:00 2001 From: Dave Sirockin Date: Wed, 11 Sep 2024 15:45:02 +0200 Subject: [PATCH 3/3] test: add t.Parallel() to subtests --- pkg/f1/testing/t_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/f1/testing/t_test.go b/pkg/f1/testing/t_test.go index 8007f9fd..3f1a3767 100644 --- a/pkg/f1/testing/t_test.go +++ b/pkg/f1/testing/t_test.go @@ -128,6 +128,7 @@ func TestErrorSetsTheFailedState(t *testing.T) { for testName, test := range tests { t.Run(testName, func(t *testing.T) { + t.Parallel() newT, teardown := newT() defer teardown() @@ -166,6 +167,7 @@ func TestFatalSetsTheFailedState(t *testing.T) { for testName, test := range tests { t.Run(testName, func(t *testing.T) { + t.Parallel() newT, teardown := newT() defer teardown()