Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201105173854-bc9fc8d8c4bc / go / analysis / passes / testinggoroutine / testinggoroutine.go
1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package testinggoroutine
6
7 import (
8         "go/ast"
9
10         "golang.org/x/tools/go/analysis"
11         "golang.org/x/tools/go/analysis/passes/inspect"
12         "golang.org/x/tools/go/analysis/passes/internal/analysisutil"
13         "golang.org/x/tools/go/ast/inspector"
14 )
15
16 const Doc = `report calls to (*testing.T).Fatal from goroutines started by a test.
17
18 Functions that abruptly terminate a test, such as the Fatal, Fatalf, FailNow, and
19 Skip{,f,Now} methods of *testing.T, must be called from the test goroutine itself.
20 This checker detects calls to these functions that occur within a goroutine
21 started by the test. For example:
22
23 func TestFoo(t *testing.T) {
24     go func() {
25         t.Fatal("oops") // error: (*T).Fatal called from non-test goroutine
26     }()
27 }
28 `
29
30 var Analyzer = &analysis.Analyzer{
31         Name:     "testinggoroutine",
32         Doc:      Doc,
33         Requires: []*analysis.Analyzer{inspect.Analyzer},
34         Run:      run,
35 }
36
37 var forbidden = map[string]bool{
38         "FailNow": true,
39         "Fatal":   true,
40         "Fatalf":  true,
41         "Skip":    true,
42         "Skipf":   true,
43         "SkipNow": true,
44 }
45
46 func run(pass *analysis.Pass) (interface{}, error) {
47         inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
48
49         if !analysisutil.Imports(pass.Pkg, "testing") {
50                 return nil, nil
51         }
52
53         // Filter out anything that isn't a function declaration.
54         onlyFuncs := []ast.Node{
55                 (*ast.FuncDecl)(nil),
56         }
57
58         inspect.Nodes(onlyFuncs, func(node ast.Node, push bool) bool {
59                 fnDecl, ok := node.(*ast.FuncDecl)
60                 if !ok {
61                         return false
62                 }
63
64                 if !hasBenchmarkOrTestParams(fnDecl) {
65                         return false
66                 }
67
68                 // Now traverse the benchmark/test's body and check that none of the
69                 // forbidden methods are invoked in the goroutines within the body.
70                 ast.Inspect(fnDecl, func(n ast.Node) bool {
71                         goStmt, ok := n.(*ast.GoStmt)
72                         if !ok {
73                                 return true
74                         }
75
76                         checkGoStmt(pass, goStmt)
77
78                         // No need to further traverse the GoStmt since right
79                         // above we manually traversed it in the ast.Inspect(goStmt, ...)
80                         return false
81                 })
82
83                 return false
84         })
85
86         return nil, nil
87 }
88
89 func hasBenchmarkOrTestParams(fnDecl *ast.FuncDecl) bool {
90         // Check that the function's arguments include "*testing.T" or "*testing.B".
91         params := fnDecl.Type.Params.List
92
93         for _, param := range params {
94                 if _, ok := typeIsTestingDotTOrB(param.Type); ok {
95                         return true
96                 }
97         }
98
99         return false
100 }
101
102 func typeIsTestingDotTOrB(expr ast.Expr) (string, bool) {
103         starExpr, ok := expr.(*ast.StarExpr)
104         if !ok {
105                 return "", false
106         }
107         selExpr, ok := starExpr.X.(*ast.SelectorExpr)
108         if !ok {
109                 return "", false
110         }
111
112         varPkg := selExpr.X.(*ast.Ident)
113         if varPkg.Name != "testing" {
114                 return "", false
115         }
116
117         varTypeName := selExpr.Sel.Name
118         ok = varTypeName == "B" || varTypeName == "T"
119         return varTypeName, ok
120 }
121
122 // checkGoStmt traverses the goroutine and checks for the
123 // use of the forbidden *testing.(B, T) methods.
124 func checkGoStmt(pass *analysis.Pass, goStmt *ast.GoStmt) {
125         // Otherwise examine the goroutine to check for the forbidden methods.
126         ast.Inspect(goStmt, func(n ast.Node) bool {
127                 selExpr, ok := n.(*ast.SelectorExpr)
128                 if !ok {
129                         return true
130                 }
131
132                 _, bad := forbidden[selExpr.Sel.Name]
133                 if !bad {
134                         return true
135                 }
136
137                 // Now filter out false positives by the import-path/type.
138                 ident, ok := selExpr.X.(*ast.Ident)
139                 if !ok {
140                         return true
141                 }
142                 if ident.Obj == nil || ident.Obj.Decl == nil {
143                         return true
144                 }
145                 field, ok := ident.Obj.Decl.(*ast.Field)
146                 if !ok {
147                         return true
148                 }
149                 if typeName, ok := typeIsTestingDotTOrB(field.Type); ok {
150                         pass.ReportRangef(selExpr, "call to (*%s).%s from a non-test goroutine", typeName, selExpr.Sel)
151                 }
152                 return true
153         })
154 }