Skip to content

Commit 9dd80b6

Browse files
committed
feat: add recover pattern (#130)
Signed-off-by: Tronje Krop <[email protected]>
1 parent 3a81d3b commit 9dd80b6

File tree

12 files changed

+98
-43
lines changed

12 files changed

+98
-43
lines changed

.github/workflows/build.yaml

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,24 @@ jobs:
2525
path-to-profile: ./build/test-all.cover
2626

2727

28-
macos:
29-
runs-on: macos-latest
30-
steps:
31-
- name: Set up Go
32-
uses: actions/setup-go@v5
33-
with:
34-
go-version: 1.25
35-
cache: false
28+
# macos:
29+
# runs-on: macos-latest
30+
# steps:
31+
# - name: Set up Go
32+
# uses: actions/setup-go@v5
33+
# with:
34+
# go-version: 1.25
35+
# cache: false
3636

37-
- name: Checkout code
38-
uses: actions/checkout@v4
37+
# - name: Checkout code
38+
# uses: actions/checkout@v4
3939

40-
- name: Build and tests
41-
env:
42-
BASH_COMPAT: 3.2
43-
CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }}
44-
LANG: en_US.UTF-8
45-
run: make all
40+
# - name: Build and tests
41+
# env:
42+
# BASH_COMPAT: 3.2
43+
# CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }}
44+
# LANG: en_US.UTF-8
45+
# run: make all
4646

4747

4848
release:

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.43
1+
0.0.44

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ require (
1515

1616
require (
1717
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
18-
golang.org/x/mod v0.29.0 // indirect
19-
golang.org/x/sync v0.17.0 // indirect
18+
golang.org/x/mod v0.30.0 // indirect
19+
golang.org/x/sync v0.18.0 // indirect
2020
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
2121
gopkg.in/yaml.v3 v3.0.1 // indirect
2222
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu
2828
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
2929
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
3030
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
31-
golang.org/x/mod v0.29.0 h1:HV8lRxZC4l2cr3Zq1LvtOsi/ThTgWnUk/y64QSs8GwA=
32-
golang.org/x/mod v0.29.0/go.mod h1:NyhrlYXJ2H4eJiRy/WDBO6HMqZQ6q9nk4JzS3NuCK+w=
33-
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
34-
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
31+
golang.org/x/mod v0.30.0 h1:fDEXFVZ/fmCKProc/yAXXUijritrDzahmwwefnjoPFk=
32+
golang.org/x/mod v0.30.0/go.mod h1:lAsf5O2EvJeSFMiBxXDki7sCgAxEUcZHXoXMKT4GJKc=
33+
golang.org/x/sync v0.18.0 h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I=
34+
golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
3535
golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k=
3636
golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM=
3737
golang.org/x/tools v0.38.0 h1:Hx2Xv8hISq8Lm16jvBZ2VQf+RLmbd7wVUsALibYI/IQ=

gock/controller_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,7 @@ func TestController(t *testing.T) {
7575

7676
func TestPanic(t *testing.T) {
7777
// Given
78-
defer func() {
79-
if err := recover(); err == nil {
80-
assert.Fail(t, "did not panic")
81-
}
82-
}()
78+
defer test.Recover(t, "gock not supported by test setup")
8379

8480
// When
8581
gock.NewGock(gomock.NewController(struct{ gomock.TestReporter }{}))

internal/sync/waitgroup_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import (
77
"github.com/stretchr/testify/assert"
88

99
"github.com/tkrop/go-testing/internal/sync"
10+
"github.com/tkrop/go-testing/test"
1011
)
1112

1213
func TestWaitGroup(t *testing.T) {
1314
t.Parallel()
1415

1516
// Given
16-
defer func() { _ = recover() }()
17+
defer test.Recover(t, "sync: negative WaitGroup counter")
1718
wg := sync.NewWaitGroup()
1819

1920
// When
@@ -23,7 +24,7 @@ func TestWaitGroup(t *testing.T) {
2324
wg.Done()
2425

2526
// Then
26-
assert.Fail(t, "not recovered from panic")
27+
assert.Fail(t, "did not panic")
2728
}
2829

2930
func TestLenientWaitGroup(t *testing.T) {

mock/mocks_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -557,10 +557,7 @@ var panicTestCases = map[string]PanicParams{
557557
func TestPanic(t *testing.T) {
558558
test.Map(t, panicTestCases).Run(func(t test.Test, param PanicParams) {
559559
// Given
560-
defer func() {
561-
err := recover()
562-
assert.Equal(t, param.expectError, err)
563-
}()
560+
defer test.Recover(t, param.expectError)
564561

565562
// When
566563
MockSetup(t, param.setup)

test/caller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ var (
128128
CallerTestError = path.Join(SourceDir, "context.go:352")
129129
// CallerReporterErrorf provides the file with the line number of the
130130
// `Errorf` call in the test reporter/validator implementation.
131-
CallerReporterError = path.Join(SourceDir, "reporter.go:83")
131+
CallerReporterError = path.Join(SourceDir, "reporter.go:87")
132132

133133
// CallerTestErrorf provides the file with the line number of the `Errorf`
134134
// call in the test context implementation.
135135
CallerTestErrorf = path.Join(SourceDir, "context.go:370")
136136
// CallerReporterErrorf provides the file with the line number of the
137137
// `Errorf` call in the test reporter/validator implementation.
138-
CallerReporterErrorf = path.Join(SourceDir, "reporter.go:105")
138+
CallerReporterErrorf = path.Join(SourceDir, "reporter.go:109")
139139
)

test/pattern.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ func Ptr[T any](v T) *T {
4646
// others arguments. The method allows to write concise test setup code.
4747
func First[T any](arg T, _ ...any) T { return arg }
4848

49+
// Recover is a convenience method to be used in deferred calls to verify that
50+
// a panic occurred with the expected panic response.
51+
func Recover(t Test, expect any) {
52+
// revive:disable-next-line:defer // caller is expected to use defer.
53+
if actual := recover(); actual != nil {
54+
assert.Equal(t, expect, actual)
55+
} else {
56+
assert.Fail(t, "did not panic: %#v", expect)
57+
}
58+
}
59+
4960
// TODO: consider following convenience methods:
5061
//
5162
// // Check is a convenience method that returns the second argument and swallows

test/pattern_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,56 @@ func TestPtr(t *testing.T) {
394394
})
395395
}
396396

397+
type RecoverParams struct {
398+
setup any
399+
expect test.Expect
400+
panic any
401+
}
402+
403+
var recoverTestCases = map[string]RecoverParams{
404+
// Failure to panic.
405+
"no panic with nil": {},
406+
"no panic with string": {
407+
setup: "no panic",
408+
},
409+
"no panic with error": {
410+
setup: assert.AnError,
411+
},
412+
"no panic with pointer error": {
413+
setup: &assert.AnError,
414+
},
415+
416+
// Successful recovery.
417+
"panic with string": {
418+
panic: "a panic occurred",
419+
setup: "a panic occurred",
420+
expect: test.Success,
421+
},
422+
"panic with error": {
423+
panic: assert.AnError,
424+
setup: assert.AnError,
425+
expect: test.Success,
426+
},
427+
"panic with same error pointer": {
428+
panic: &assert.AnError,
429+
setup: &assert.AnError,
430+
expect: test.Success,
431+
},
432+
}
433+
434+
func TestRecover(t *testing.T) {
435+
test.Map(t, recoverTestCases).
436+
Run(func(t test.Test, param RecoverParams) {
437+
// Given
438+
defer test.Recover(t, param.setup)
439+
440+
// When
441+
if param.panic != nil {
442+
panic(param.panic)
443+
}
444+
})
445+
}
446+
397447
// ctx returns the current time formatted as RFC3339Nano truncated to 26
398448
// characters to avoid excessive precision in test output.
399449
func ctx() string {

0 commit comments

Comments
 (0)