-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcaller_test.go
145 lines (130 loc) · 4.46 KB
/
caller_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package test_test
// File contains the split out logic to calculate the caller for automated
// testing of test failure validation.
import (
"os"
"path"
"runtime"
"strconv"
"testing"
"github.com/golang/mock/gomock"
"github.com/tkrop/go-testing/mock"
"github.com/tkrop/go-testing/test"
)
// Caller reporter that allows to capture the callers file and line number.
type Caller struct {
path string
}
// Error is the caller reporter function to capture the callers file and line
// number of the `Error` call.
func (c *Caller) Error(_ ...any) {
_, path, line, _ := runtime.Caller(1)
c.path = path + ":" + strconv.Itoa(line)
}
// Errorf is the caller reporter function to capture the callers file and line
// number of the `Errorf` call.
func (c *Caller) Errorf(_ string, _ ...any) {
_, path, line, _ := runtime.Caller(1)
c.path = path + ":" + strconv.Itoa(line)
}
// Fatal is the caller reporter function to capture the callers file and line
// number of the `Fatal` call.
func (c *Caller) Fatal(_ ...any) {
_, path, line, _ := runtime.Caller(1)
c.path = path + ":" + strconv.Itoa(line)
panic("finished") // prevents goexit.
}
// Fatalf is the caller reporter function to capture the callers file and line
// number of the `Fatalf` call.
func (c *Caller) Fatalf(_ string, _ ...any) {
_, path, line, _ := runtime.Caller(1)
c.path = path + ":" + strconv.Itoa(line)
panic("finished") // prevents goexit.
}
// Fail is the caller reporter function to capture the callers file and line
// number of the `Fail` call.
func (c *Caller) Fail() {
_, path, line, _ := runtime.Caller(1)
c.path = path + ":" + strconv.Itoa(line)
panic("finished") // prevents goexit.
}
// FailNow is the caller reporter function to capture the callers file and line
// number of the `FailNow` call.
func (c *Caller) FailNow() {
_, path, line, _ := runtime.Caller(1)
c.path = path + ":" + strconv.Itoa(line)
panic("finished") // prevents goexit.
}
// Panic is the caller reporter function to capture the callers file and line
// number of the `Panic` call.
func (c *Caller) Panic(_ any) {
_, path, line, _ := runtime.Caller(1)
c.path = path + ":" + strconv.Itoa(line)
panic("finished") // prevents goexit.
}
// getCaller implements the capturing logic for the callers file and line
// number for the given call.
func getCaller(call func(t test.Reporter)) string {
t := test.New(&testing.T{}, test.Failure)
mocks := mock.NewMocks(t)
caller := mock.Get(mocks,
func(*gomock.Controller) *Caller {
return &Caller{}
})
t.Reporter(caller)
func() {
defer func() { _ = recover() }()
call(t)
}()
return caller.path
}
var (
// CallerError provides the file with line number of the `Error` call.
CallerError = getCaller(func(t test.Reporter) {
t.Error("fail")
})
// CallerErrorf provides the file with line number of the `Errorf` call.
CallerErrorf = getCaller(func(t test.Reporter) {
t.Errorf("fail")
})
// CallerFatal provides the file with line number of the `Fatal` call.
CallerFatal = getCaller(func(t test.Reporter) {
t.Fatal("fail")
})
// CallerFatalf provides the file with line number of the `Fatalf` call.
CallerFatalf = getCaller(func(t test.Reporter) {
t.Fatalf("fail")
})
// CallerFail provides the file with line number of the `Fail` call.
CallerFail = getCaller(func(t test.Reporter) {
t.Fail()
})
// CallerFailNow provides the file with line number of the `FailNow` call.
CallerFailNow = getCaller(func(t test.Reporter) {
t.FailNow()
})
// CallerPanic provides the file with line number of the `FailNow` call.
CallerPanic = getCaller(func(t test.Reporter) {
t.Panic("fail")
})
// Generic source directory for caller path evaluation.
SourceDir = func() string {
dir, err := os.Getwd()
if err != nil {
panic(err)
}
return dir
}()
// CallerTestError provides the file with the line number of the `Error`
// call in the test context implementation.
CallerTestError = path.Join(SourceDir, "context.go:333")
// CallerReporterErrorf provides the file with the line number of the
// `Errorf` call in the test reporter/validator implementation.
CallerReporterError = path.Join(SourceDir, "reporter.go:83")
// CallerTestErrorf provides the file with the line number of the `Errorf`
// call in the test context implementation.
CallerTestErrorf = path.Join(SourceDir, "context.go:351")
// CallerReporterErrorf provides the file with the line number of the
// `Errorf` call in the test reporter/validator implementation.
CallerReporterErrorf = path.Join(SourceDir, "reporter.go:105")
)