-
Notifications
You must be signed in to change notification settings - Fork 7
/
tenv.go
223 lines (200 loc) · 5.38 KB
/
tenv.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
package tenv
import (
"go/ast"
"go/token"
"go/types"
"strings"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
const doc = "tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17"
// Analyzer is tenv analyzer
var Analyzer = &analysis.Analyzer{
Name: "tenv",
Doc: doc,
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
}
var (
A = "all"
aflag bool
)
func init() {
Analyzer.Flags.BoolVar(&aflag, A, false, "the all option will run against all method in test file")
}
func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{
(*ast.FuncDecl)(nil),
(*ast.FuncLit)(nil),
}
inspect.Preorder(nodeFilter, func(n ast.Node) {
switch n := n.(type) {
case *ast.FuncDecl:
checkFuncDecl(pass, n, pass.Fset.File(n.Pos()).Name())
case *ast.FuncLit:
checkFuncLit(pass, n, pass.Fset.File(n.Pos()).Name())
}
})
return nil, nil
}
func checkFuncDecl(pass *analysis.Pass, f *ast.FuncDecl, fileName string) {
argName, ok := targetRunner(pass, f.Type.Params.List, fileName)
if !ok {
return
}
checkStmts(pass, f.Body.List, f.Name.Name, argName)
}
func checkFuncLit(pass *analysis.Pass, f *ast.FuncLit, fileName string) {
argName, ok := targetRunner(pass, f.Type.Params.List, fileName)
if !ok {
return
}
checkStmts(pass, f.Body.List, "anonymous function", argName)
}
func checkStmts(pass *analysis.Pass, stmts []ast.Stmt, funcName, argName string) {
for _, stmt := range stmts {
switch stmt := stmt.(type) {
case *ast.ExprStmt:
checkExprStmt(pass, stmt, funcName, argName)
case *ast.IfStmt:
checkIfStmt(pass, stmt, funcName, argName)
case *ast.AssignStmt:
checkAssignStmt(pass, stmt, funcName, argName)
case *ast.ForStmt:
checkForStmt(pass, stmt, funcName, argName)
}
}
}
func checkExprStmt(pass *analysis.Pass, stmt *ast.ExprStmt, funcName, argName string) {
callExpr, ok := stmt.X.(*ast.CallExpr)
if !ok {
return
}
checkArgs(pass, callExpr.Args, funcName, argName)
ident, ok := callExpr.Fun.(*ast.Ident)
if ok {
obj := pass.TypesInfo.ObjectOf(ident)
checkObj(pass, obj, stmt.Pos(), funcName, argName)
}
fun, ok := callExpr.Fun.(*ast.SelectorExpr)
if ok {
obj := pass.TypesInfo.ObjectOf(fun.Sel)
checkObj(pass, obj, stmt.Pos(), funcName, argName)
}
}
func checkArgs(pass *analysis.Pass, args []ast.Expr, funcName, argName string) {
for _, arg := range args {
callExpr, ok := arg.(*ast.CallExpr)
if !ok {
continue
}
ident, ok := callExpr.Fun.(*ast.Ident)
if ok {
obj := pass.TypesInfo.ObjectOf(ident)
checkObj(pass, obj, arg.Pos(), funcName, argName)
}
fun, ok := callExpr.Fun.(*ast.SelectorExpr)
if ok {
obj := pass.TypesInfo.ObjectOf(fun.Sel)
checkObj(pass, obj, arg.Pos(), funcName, argName)
}
}
}
func checkIfStmt(pass *analysis.Pass, stmt *ast.IfStmt, funcName, argName string) {
assignStmt, ok := stmt.Init.(*ast.AssignStmt)
if !ok {
return
}
rhs, ok := assignStmt.Rhs[0].(*ast.CallExpr)
if !ok {
return
}
ident, ok := rhs.Fun.(*ast.Ident)
if ok {
obj := pass.TypesInfo.ObjectOf(ident)
checkObj(pass, obj, stmt.Pos(), funcName, argName)
}
fun, ok := rhs.Fun.(*ast.SelectorExpr)
if ok {
obj := pass.TypesInfo.ObjectOf(fun.Sel)
checkObj(pass, obj, stmt.Pos(), funcName, argName)
}
}
func checkAssignStmt(pass *analysis.Pass, stmt *ast.AssignStmt, funcName, argName string) {
rhs, ok := stmt.Rhs[0].(*ast.CallExpr)
if !ok {
return
}
ident, ok := rhs.Fun.(*ast.Ident)
if ok {
obj := pass.TypesInfo.ObjectOf(ident)
checkObj(pass, obj, stmt.Pos(), funcName, argName)
}
fun, ok := rhs.Fun.(*ast.SelectorExpr)
if ok {
obj := pass.TypesInfo.ObjectOf(fun.Sel)
checkObj(pass, obj, stmt.Pos(), funcName, argName)
}
}
func checkObj(pass *analysis.Pass, obj types.Object, pos token.Pos, funcName, argName string) {
// For built-in objects, obj.Pkg() returns nil.
var pkgPrefix string
if pkg := obj.Pkg(); pkg != nil {
pkgPrefix = pkg.Name() + "."
}
targetName := pkgPrefix + obj.Name()
if targetName == "os.Setenv" {
if argName == "" {
argName = "testing"
}
pass.Reportf(pos, "os.Setenv() can be replaced by `%s.Setenv()` in %s", argName, funcName)
}
}
func checkForStmt(pass *analysis.Pass, stmt *ast.ForStmt, funcName, argName string) {
checkStmts(pass, stmt.Body.List, funcName, argName)
}
func targetRunner(pass *analysis.Pass, params []*ast.Field, fileName string) (string, bool) {
for _, p := range params {
switch typ := p.Type.(type) {
case *ast.StarExpr:
if checkStarExprTarget(pass, typ) {
if len(p.Names) == 0 {
return "", false
}
argName := p.Names[0].Name
return argName, true
}
case *ast.SelectorExpr:
if checkSelectorExprTarget(pass, typ) {
if len(p.Names) == 0 {
return "", false
}
argName := p.Names[0].Name
return argName, true
}
}
}
if aflag && strings.HasSuffix(fileName, "_test.go") {
return "", true
}
return "", false
}
func checkStarExprTarget(pass *analysis.Pass, typ *ast.StarExpr) bool {
selector, ok := typ.X.(*ast.SelectorExpr)
if !ok {
return false
}
switch pass.TypesInfo.TypeOf(selector).String() {
case "testing.T", "testing.B":
return true
default:
return false
}
}
func checkSelectorExprTarget(pass *analysis.Pass, typ *ast.SelectorExpr) bool {
return pass.TypesInfo.TypeOf(typ).String() == "testing.TB"
}