-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy patherrtrace_line_test.go
286 lines (256 loc) · 6.39 KB
/
errtrace_line_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package errtrace_test
import (
_ "embed"
"errors"
"fmt"
"go/scanner"
"go/token"
"strconv"
"strings"
"testing"
"braces.dev/errtrace"
)
//go:embed errtrace_line_test.go
var errtraceLineTestFile string
// Note: The following tests verify the line, and assume that the
// test names are unique, and that they are the only tests in this file.
func TestWrap_Line(t *testing.T) {
failed := errors.New("failed")
tests := []struct {
name string
f func() error
}{
{
name: "return Wrap", // @group
f: func() error {
return errtrace.Wrap(failed) // @trace
},
},
{
name: "Wrap to intermediate and return", // @group
f: func() (retErr error) {
wrapped := errtrace.Wrap(failed) // @trace
return wrapped
},
},
{
name: "Decorate error after Wrap", // @group
f: func() (retErr error) {
wrapped := errtrace.Wrap(failed) // @trace
return fmt.Errorf("got err: %w", wrapped)
},
},
{
name: "defer updates errTrace", // @group
f: func() (retErr error) {
defer func() {
retErr = errtrace.Wrap(retErr) // @trace
}()
return failed
},
},
// Test error creation helpers.
{
name: "New", // @group
f: func() (retErr error) {
return errtrace.New("test") // @trace
},
},
{
name: "Errorf with no error args", // @group
f: func() (retErr error) {
return errtrace.Errorf("test %v", 1) // @trace
},
},
{
name: "Errorf with wrapped error arg", // @group
f: func() (retErr error) {
err := errtrace.New("test1") // @trace
return errtrace.Errorf("test2: %w", err) // @trace
},
},
// Sanity testing for WrapN functions.
{
name: "Test Wrap2", // @group
f: func() (retErr error) {
nested := func() (int, error) {
return errtrace.Wrap2(returnErr2()) // @trace
}
_, err := nested()
return err
},
},
{
name: "Test Wrap3", // @group
f: func() (retErr error) {
nested := func() (int, int, error) {
return errtrace.Wrap3(returnErr3()) // @trace
}
_, _, err := nested()
return err
},
},
{
name: "Test Wrap4", // @group
f: func() (retErr error) {
nested := func() (int, int, int, error) {
return errtrace.Wrap4(returnErr4()) // @trace
}
_, _, _, err := nested()
return err
},
},
{
name: "Test Wrap5", // @group
f: func() (retErr error) {
nested := func() (int, int, int, int, error) {
return errtrace.Wrap5(returnErr5()) // @trace
}
_, _, _, _, err := nested()
return err
},
},
{
name: "Test Wrap6", // @group
f: func() (retErr error) {
nested := func() (int, int, int, int, int, error) {
return errtrace.Wrap6(returnErr6()) // @trace
}
_, _, _, _, _, err := nested()
return err
},
},
// Multi-errors.
{
name: "MutiError", // @group
f: func() error {
return errors.Join(
errtrace.New("foo"), // @trace
errtrace.New("bar"), // @trace
)
},
},
{
name: "MultiError/Wrapped", // @group
f: func() error {
err := errors.Join(
errtrace.New("foo"), // @trace
errtrace.New("bar"), // @trace
)
return errtrace.Wrap(err) // @trace
},
},
}
testMarkers, err := parseMarkers(errtraceLineTestFile)
if err != nil {
t.Fatal(err)
}
t.Logf("parsed markers: %v", testMarkers)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
markers := testMarkers[tt.name]
if len(markers) == 0 {
t.Fatalf("didn't find any markers for test")
}
gotErr := tt.f()
got := errtrace.FormatString(gotErr)
for _, wantLine := range markers {
wantFileLine := fmt.Sprintf("errtrace_line_test.go:%v", wantLine)
if !strings.Contains(got, wantFileLine) {
t.Errorf("formatted output is missing file:line %q in:\n%s", wantFileLine, got)
}
}
})
}
}
func returnErr2() (int, error) { return 1, errors.New("test") }
func returnErr3() (int, int, error) { return 1, 2, errors.New("test") }
func returnErr4() (int, int, int, error) { return 1, 2, 3, errors.New("test") }
func returnErr5() (int, int, int, int, error) { return 1, 2, 3, 4, errors.New("test") }
func returnErr6() (int, int, int, int, int, error) { return 1, 2, 3, 4, 5, errors.New("test") }
// parseMarkers parses the source file and returns a map
// from marker group name to line numbers in that group.
//
// Marker groups are identified by a '@group' comment
// immediately following a string literal -- ignoring operators.
// For example:
//
// {
// name: "foo", // @group
// // Note that the "," is ignored as it's an operator.
// }
//
// Markers in the group are identified by a '@trace' comment.
// For example:
//
// {
// name: "foo", // @group
// f: func() error {
// return errtrace.Wrap(failed) // @trace
// },
// }
//
// A group ends when a new group starts or the end of the file is reached.
func parseMarkers(src string) (map[string][]int, error) {
// We don't need to parse the Go AST.
// Just lexical analysis is enough.
fset := token.NewFileSet()
file := fset.AddFile("errtrace_line_test.go", fset.Base(), len(src))
var (
errs []error
scan scanner.Scanner
)
scan.Init(
file,
[]byte(src),
func(pos token.Position, msg string) {
// This function is called for each error encountered
// while scanning.
errs = append(errs, fmt.Errorf("%v:%v", pos, msg))
},
scanner.ScanComments,
)
errf := func(pos token.Pos, format string, args ...any) {
msg := fmt.Sprintf(format, args...)
errs = append(errs, fmt.Errorf("%v:%v", file.Position(pos), msg))
}
markers := make(map[string][]int)
var (
currentMarker string
lastStringLiteral string
)
for {
pos, tok, lit := scan.Scan()
switch tok {
case token.EOF:
return markers, errors.Join(errs...)
case token.STRING:
s, err := strconv.Unquote(lit)
if err != nil {
errf(pos, "bad string literal: %v", err)
continue
}
lastStringLiteral = s
case token.COMMENT:
switch lit {
case "// @group":
if lastStringLiteral == "" {
errf(pos, "expected string literal before @group")
continue
}
currentMarker = lastStringLiteral
case "// @trace":
if currentMarker == "" {
errf(pos, "expected @group before @trace")
continue
}
markers[currentMarker] = append(markers[currentMarker], file.Line(pos))
}
default:
// For all other non-operator tokens, reset the last string literal.
if !tok.IsOperator() {
lastStringLiteral = ""
}
}
}
}