/
go_require.go
327 lines (276 loc) · 8.04 KB
/
go_require.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
package checkers
import (
"fmt"
"go/ast"
"go/types"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/ast/inspector"
"github.com/Antonboom/testifylint/internal/analysisutil"
)
const (
goRequireFnReportFormat = "%s contains assertions that must only be used in the goroutine running the test function"
goRequireCallReportFormat = "%s must only be used in the goroutine running the test function"
)
// GoRequire takes idea from go vet's "testinggoroutine" check
// and detects usage of require package's functions or assert.FailNow in the non-test goroutines
//
// go func() {
// conn, err = lis.Accept()
// require.NoError(t, err)
//
// if assert.Error(err) {
// assert.FailNow(t, msg)
// }
// }()
type GoRequire struct{}
// NewGoRequire constructs GoRequire checker.
func NewGoRequire() GoRequire { return GoRequire{} }
func (GoRequire) Name() string { return "go-require" }
// Check should be consistent with
// https://cs.opensource.google/go/x/tools/+/master:go/analysis/passes/testinggoroutine/testinggoroutine.go
//
// But due to the fact that the Check covers cases missed by go vet,
// the implementation turned out to be terribly complicated.
//
// In simple words, the algorithm is as follows:
// - we walk along the call tree and store the status, whether we are in the test goroutine or not;
// - if we are in a test goroutine, then require is allowed, otherwise not;
// - when we encounter the launch of a subtest or `go` statement, the status changes;
// - in order to correctly handle the return to the correct status when exiting the current function,
// we have to store a stack of statuses (inGoroutineRunningTestFunc).
//
// Other test functions called in the test function are also analyzed to make a verdict about the current function.
// This leads to recursion, which the cache of processed functions (processedFuncs) helps reduce the impact of.
// Also, because of this, we have to pre-collect a list of test function declarations (testsDecls).
func (checker GoRequire) Check(pass *analysis.Pass, inspector *inspector.Inspector) (diagnostics []analysis.Diagnostic) {
testsDecls := make(funcDeclarations)
inspector.Preorder([]ast.Node{(*ast.FuncDecl)(nil)}, func(node ast.Node) {
fd := node.(*ast.FuncDecl)
if isTestingFuncOrMethod(pass, fd) {
if tf, ok := pass.TypesInfo.ObjectOf(fd.Name).(*types.Func); ok {
testsDecls[tf] = fd
}
}
})
var inGoroutineRunningTestFunc boolStack
processedFuncs := make(map[*ast.FuncDecl]goRequireVerdict)
nodesFilter := []ast.Node{
(*ast.FuncDecl)(nil),
(*ast.FuncType)(nil),
(*ast.GoStmt)(nil),
(*ast.CallExpr)(nil),
}
inspector.Nodes(nodesFilter, func(node ast.Node, push bool) bool {
if fd, ok := node.(*ast.FuncDecl); ok {
if !isTestingFuncOrMethod(pass, fd) {
return false
}
if push {
inGoroutineRunningTestFunc.Push(true)
} else {
inGoroutineRunningTestFunc.Pop()
}
return true
}
if ft, ok := node.(*ast.FuncType); ok {
if !isTestingAnonymousFunc(pass, ft) {
return false
}
if push {
inGoroutineRunningTestFunc.Push(true)
} else {
inGoroutineRunningTestFunc.Pop()
}
return true
}
if _, ok := node.(*ast.GoStmt); ok {
if push {
inGoroutineRunningTestFunc.Push(false)
} else {
inGoroutineRunningTestFunc.Pop()
}
return true
}
ce := node.(*ast.CallExpr)
if isSubTestRun(pass, ce) {
if push {
// t.Run spawns the new testing goroutine and declines
// possible warnings from previous "simple" goroutine.
inGoroutineRunningTestFunc.Push(true)
} else {
inGoroutineRunningTestFunc.Pop()
}
return true
}
if !push {
return false
}
if inGoroutineRunningTestFunc.Len() == 0 {
// Insufficient info.
return true
}
if inGoroutineRunningTestFunc.Last() {
// We are in testing goroutine and can skip any assertion checks.
return true
}
testifyCall := NewCallMeta(pass, ce)
if testifyCall != nil {
switch checker.checkCall(testifyCall) {
case goRequireVerdictRequire:
d := newDiagnostic(checker.Name(), testifyCall, fmt.Sprintf(goRequireCallReportFormat, "require"), nil)
diagnostics = append(diagnostics, *d)
case goRequireVerdictAssertFailNow:
d := newDiagnostic(checker.Name(), testifyCall, fmt.Sprintf(goRequireCallReportFormat, testifyCall), nil)
diagnostics = append(diagnostics, *d)
case goRequireVerdictNoExit:
}
return false
}
// Case of nested function call.
{
calledFd := testsDecls.Get(pass, ce)
if calledFd == nil {
return true
}
if v := checker.checkFunc(pass, calledFd, testsDecls, processedFuncs); v != goRequireVerdictNoExit {
caller := analysisutil.NodeString(pass.Fset, ce.Fun)
d := newDiagnostic(checker.Name(), ce, fmt.Sprintf(goRequireFnReportFormat, caller), nil)
diagnostics = append(diagnostics, *d)
}
}
return true
})
return diagnostics
}
func (checker GoRequire) checkFunc(
pass *analysis.Pass,
fd *ast.FuncDecl,
testsDecls funcDeclarations,
processedFuncs map[*ast.FuncDecl]goRequireVerdict,
) (result goRequireVerdict) {
if v, ok := processedFuncs[fd]; ok {
return v
}
ast.Inspect(fd, func(node ast.Node) bool {
if result != goRequireVerdictNoExit {
return false
}
if _, ok := node.(*ast.GoStmt); ok {
return false
}
ce, ok := node.(*ast.CallExpr)
if !ok {
return true
}
testifyCall := NewCallMeta(pass, ce)
if testifyCall != nil {
if v := checker.checkCall(testifyCall); v != goRequireVerdictNoExit {
result, processedFuncs[fd] = v, v
}
return false
}
// Case of nested function call.
{
calledFd := testsDecls.Get(pass, ce)
if calledFd == nil {
return true
}
if calledFd == fd {
// Recursion.
return true
}
if v := checker.checkFunc(pass, calledFd, testsDecls, processedFuncs); v != goRequireVerdictNoExit {
result = v
return false
}
return true
}
})
return result
}
type goRequireVerdict int
const (
goRequireVerdictNoExit goRequireVerdict = iota
goRequireVerdictRequire
goRequireVerdictAssertFailNow
)
func (checker GoRequire) checkCall(call *CallMeta) goRequireVerdict {
if !call.IsAssert {
return goRequireVerdictRequire
}
if call.Fn.NameFTrimmed == "FailNow" {
return goRequireVerdictAssertFailNow
}
return goRequireVerdictNoExit
}
type funcDeclarations map[*types.Func]*ast.FuncDecl
// Get returns the declaration of a called function or method.
// Currently, only static calls within the same package are supported, otherwise returns nil.
func (fd funcDeclarations) Get(pass *analysis.Pass, ce *ast.CallExpr) *ast.FuncDecl {
var obj types.Object
switch fun := ce.Fun.(type) {
case *ast.SelectorExpr:
obj = pass.TypesInfo.ObjectOf(fun.Sel)
case *ast.Ident:
obj = pass.TypesInfo.ObjectOf(fun)
case *ast.IndexExpr:
if id, ok := fun.X.(*ast.Ident); ok {
obj = pass.TypesInfo.ObjectOf(id)
}
case *ast.IndexListExpr:
if id, ok := fun.X.(*ast.Ident); ok {
obj = pass.TypesInfo.ObjectOf(id)
}
}
if tf, ok := obj.(*types.Func); ok {
return fd[tf]
}
return nil
}
type boolStack []bool
func (s boolStack) Len() int {
return len(s)
}
func (s *boolStack) Push(v bool) {
*s = append(*s, v)
}
func (s *boolStack) Pop() bool {
n := len(*s)
if n == 0 {
return false
}
last := (*s)[n-1]
*s = (*s)[:n-1]
return last
}
func (s boolStack) Last() bool {
n := len(s)
if n == 0 {
return false
}
return s[n-1]
}
func isSubTestRun(pass *analysis.Pass, ce *ast.CallExpr) bool {
se, ok := ce.Fun.(*ast.SelectorExpr)
if !ok || se.Sel == nil {
return false
}
return (isTestingTPtr(pass, se.X) || implementsTestifySuiteIface(pass, se.X)) && se.Sel.Name == "Run"
}
func isTestingFuncOrMethod(pass *analysis.Pass, fd *ast.FuncDecl) bool {
return hasTestingTParam(pass, fd.Type) || isTestifySuiteMethod(pass, fd)
}
func isTestingAnonymousFunc(pass *analysis.Pass, ft *ast.FuncType) bool {
return hasTestingTParam(pass, ft)
}
func hasTestingTParam(pass *analysis.Pass, ft *ast.FuncType) bool {
if ft == nil || ft.Params == nil {
return false
}
for _, param := range ft.Params.List {
if isTestingTPtr(pass, param.Type) {
return true
}
}
return false
}