Skip to content

Commit 5756924

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

File tree

6 files changed

+87
-23
lines changed

6 files changed

+87
-23
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=

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 error value.
51+
func Recover(t Test, expect error) {
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, "expected 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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package test_test
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"os"
78
"reflect"
@@ -394,6 +395,58 @@ func TestPtr(t *testing.T) {
394395
})
395396
}
396397

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

0 commit comments

Comments
 (0)