/
require_error.go
338 lines (286 loc) · 8.3 KB
/
require_error.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package checkers
import (
"fmt"
"go/ast"
"go/token"
"regexp"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/ast/inspector"
)
const requireErrorReport = "for error assertions use require"
// RequireError detects error assertions like
//
// assert.Error(t, err) // s.Error(err), s.Assert().Error(err)
// assert.ErrorIs(t, err, io.EOF)
// assert.ErrorAs(t, err, &target)
// assert.EqualError(t, err, "end of file")
// assert.ErrorContains(t, err, "end of file")
// assert.NoError(t, err)
// assert.NotErrorIs(t, err, io.EOF)
//
// and requires
//
// require.Error(t, err) // s.Require().Error(err), s.Require().Error(err)
// require.ErrorIs(t, err, io.EOF)
// require.ErrorAs(t, err, &target)
// ...
//
// RequireError ignores:
// - assertion in the `if` condition;
// - the entire `if-else[-if]` block, if there is an assertion in any `if` condition;
// - the last assertion in the block, if there are no methods/functions calls after it;
// - assertions in an explicit goroutine;
// - assertions in an explicit testing cleanup function or suite teardown methods;
// - sequence of NoError assertions.
type RequireError struct {
fnPattern *regexp.Regexp
}
// NewRequireError constructs RequireError checker.
func NewRequireError() *RequireError { return new(RequireError) }
func (RequireError) Name() string { return "require-error" }
func (checker *RequireError) SetFnPattern(p *regexp.Regexp) *RequireError {
if p != nil {
checker.fnPattern = p
}
return checker
}
func (checker RequireError) Check(pass *analysis.Pass, inspector *inspector.Inspector) []analysis.Diagnostic {
callsByFunc := make(map[funcID][]*callMeta)
// Stage 1. Collect meta information about any calls inside functions.
inspector.WithStack([]ast.Node{(*ast.CallExpr)(nil)}, func(node ast.Node, push bool, stack []ast.Node) bool {
if !push {
return false
}
if len(stack) < 3 {
return true
}
fID := findSurroundingFunc(pass, stack)
if fID == nil {
return true
}
_, prevIsIfStmt := stack[len(stack)-2].(*ast.IfStmt)
_, prevIsAssignStmt := stack[len(stack)-2].(*ast.AssignStmt)
_, prevPrevIsIfStmt := stack[len(stack)-3].(*ast.IfStmt)
inIfCond := prevIsIfStmt || (prevPrevIsIfStmt && prevIsAssignStmt)
callExpr := node.(*ast.CallExpr)
testifyCall := NewCallMeta(pass, callExpr)
call := &callMeta{
call: callExpr,
testifyCall: testifyCall,
rootIf: findRootIf(stack),
parentIf: findNearestNode[*ast.IfStmt](stack),
parentBlock: findNearestNode[*ast.BlockStmt](stack),
inIfCond: inIfCond,
inNoErrorSeq: false, // Will be filled in below.
}
callsByFunc[*fID] = append(callsByFunc[*fID], call)
return testifyCall == nil // Do not support asserts in asserts.
})
// Stage 2. Analyze calls and block context.
var diagnostics []analysis.Diagnostic
callsByBlock := map[*ast.BlockStmt][]*callMeta{}
for _, calls := range callsByFunc {
for _, c := range calls {
if b := c.parentBlock; b != nil {
callsByBlock[b] = append(callsByBlock[b], c)
}
}
}
markCallsInNoErrorSequence(callsByBlock)
for funcInfo, calls := range callsByFunc {
for i, c := range calls {
if funcInfo.isTestCleanup {
continue
}
if funcInfo.isGoroutine {
continue
}
if c.testifyCall == nil {
continue
}
if !c.testifyCall.IsAssert {
continue
}
switch c.testifyCall.Fn.NameFTrimmed {
default:
continue
case "Error", "ErrorIs", "ErrorAs", "EqualError", "ErrorContains", "NoError", "NotErrorIs":
}
if needToSkipBasedOnContext(c, i, calls, callsByBlock) {
continue
}
if p := checker.fnPattern; p != nil && !p.MatchString(c.testifyCall.Fn.Name) {
continue
}
diagnostics = append(diagnostics,
*newDiagnostic(checker.Name(), c.testifyCall, requireErrorReport, nil))
}
}
return diagnostics
}
func needToSkipBasedOnContext(
currCall *callMeta,
currCallIndex int,
otherCalls []*callMeta,
callsByBlock map[*ast.BlockStmt][]*callMeta,
) bool {
if currCall.inNoErrorSeq {
// Skip `assert.NoError` sequence.
return true
}
if currCall.inIfCond {
// Skip assertions in the "if condition".
return true
}
if currCall.rootIf != nil {
for _, rootCall := range otherCalls {
if (rootCall.rootIf == currCall.rootIf) && rootCall.inIfCond {
// Skip assertions in the entire if-else[-if] block, if some of "if condition" contains assertion.
return true
}
}
}
block := currCall.parentBlock
blockCalls := callsByBlock[block]
isLastCallInBlock := blockCalls[len(blockCalls)-1] == currCall
noCallsAfter := true
_, blockEndWithReturn := block.List[len(block.List)-1].(*ast.ReturnStmt)
if !blockEndWithReturn {
for i := currCallIndex + 1; i < len(otherCalls); i++ {
nextCall := otherCalls[i]
nextCallInElseBlock := false
if pIf := currCall.parentIf; pIf != nil && pIf.Else != nil {
ast.Inspect(pIf.Else, func(n ast.Node) bool {
if n == nextCall.call {
nextCallInElseBlock = true
return false
}
return true
})
}
if !nextCallInElseBlock {
noCallsAfter = false
break
}
}
}
// Skip assertion if this is the last operation in the test.
return isLastCallInBlock && noCallsAfter
}
func findSurroundingFunc(pass *analysis.Pass, stack []ast.Node) *funcID {
for i := len(stack) - 2; i >= 0; i-- {
var fType *ast.FuncType
var fName string
var isTestCleanup bool
var isGoroutine bool
switch fd := stack[i].(type) {
case *ast.FuncDecl:
fType, fName = fd.Type, fd.Name.Name
if isTestifySuiteMethod(pass, fd) {
if ident := fd.Name; ident != nil && isAfterTestMethod(ident.Name) {
isTestCleanup = true
}
}
case *ast.FuncLit:
fType, fName = fd.Type, "anonymous"
if i >= 2 { //nolint:nestif
if ce, ok := stack[i-1].(*ast.CallExpr); ok {
if se, ok := ce.Fun.(*ast.SelectorExpr); ok {
isTestCleanup = isTestingTPtr(pass, se.X) && se.Sel != nil && (se.Sel.Name == "Cleanup")
}
if _, ok := stack[i-2].(*ast.GoStmt); ok {
isGoroutine = true
}
}
}
default:
continue
}
return &funcID{
pos: fType.Pos(),
posStr: pass.Fset.Position(fType.Pos()).String(),
name: fName,
isTestCleanup: isTestCleanup,
isGoroutine: isGoroutine,
}
}
return nil
}
func findRootIf(stack []ast.Node) *ast.IfStmt {
nearestIf, i := findNearestNodeWithIdx[*ast.IfStmt](stack)
for ; i > 0; i-- {
parent, ok := stack[i-1].(*ast.IfStmt)
if ok {
nearestIf = parent
} else {
break
}
}
return nearestIf
}
func findNearestNode[T ast.Node](stack []ast.Node) (v T) {
v, _ = findNearestNodeWithIdx[T](stack)
return
}
func findNearestNodeWithIdx[T ast.Node](stack []ast.Node) (v T, index int) {
for i := len(stack) - 2; i >= 0; i-- {
if n, ok := stack[i].(T); ok {
return n, i
}
}
return
}
func markCallsInNoErrorSequence(callsByBlock map[*ast.BlockStmt][]*callMeta) {
for _, calls := range callsByBlock {
for i, c := range calls {
if c.testifyCall == nil {
continue
}
var prevIsNoError bool
if i > 0 {
if prev := calls[i-1].testifyCall; prev != nil {
prevIsNoError = isNoErrorAssertion(prev.Fn.Name)
}
}
var nextIsNoError bool
if i < len(calls)-1 {
if next := calls[i+1].testifyCall; next != nil {
nextIsNoError = isNoErrorAssertion(next.Fn.Name)
}
}
if isNoErrorAssertion(c.testifyCall.Fn.Name) && (prevIsNoError || nextIsNoError) {
calls[i].inNoErrorSeq = true
}
}
}
}
type callMeta struct {
call *ast.CallExpr
testifyCall *CallMeta
rootIf *ast.IfStmt // The root `if` in if-else[-if] chain.
parentIf *ast.IfStmt // The nearest `if`, can be equal with rootIf.
parentBlock *ast.BlockStmt
inIfCond bool // True for code like `if assert.ErrorAs(t, err, &target) {`.
inNoErrorSeq bool // True for sequence of `assert.NoError` assertions.
}
type funcID struct {
pos token.Pos
posStr string
name string
isTestCleanup bool
isGoroutine bool
}
func (id funcID) String() string {
return fmt.Sprintf("%s at %s", id.name, id.posStr)
}
func isAfterTestMethod(name string) bool {
// https://github.com/stretchr/testify/blob/master/suite/interfaces.go
switch name {
case "TearDownSuite", "TearDownTest", "AfterTest", "HandleStats", "TearDownSubTest":
return true
}
return false
}
func isNoErrorAssertion(fnName string) bool {
return (fnName == "NoError") || (fnName == "NoErrorf")
}