-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathAT005.go
67 lines (51 loc) · 1.7 KB
/
AT005.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
// Package AT005 defines an Analyzer that checks for
// acceptance tests prefixed with Test but not TestAcc
package AT005
import (
"go/ast"
"strings"
"github.com/bflad/tfproviderlint/helper/terraformtype/helper/resource"
"github.com/bflad/tfproviderlint/passes/commentignore"
"github.com/bflad/tfproviderlint/passes/testfuncdecl"
"golang.org/x/tools/go/analysis"
)
const Doc = `check for acceptance test function names missing TestAcc prefix
The AT005 analyzer reports test function names (Test prefix) that contain
resource.Test() or resource.ParallelTest(), which should be named with
the TestAcc prefix.`
const analyzerName = "AT005"
var Analyzer = &analysis.Analyzer{
Name: analyzerName,
Doc: Doc,
Requires: []*analysis.Analyzer{
commentignore.Analyzer,
testfuncdecl.Analyzer,
},
Run: run,
}
func run(pass *analysis.Pass) (interface{}, error) {
ignorer := pass.ResultOf[commentignore.Analyzer].(*commentignore.Ignorer)
testFuncs := pass.ResultOf[testfuncdecl.Analyzer].([]*ast.FuncDecl)
for _, testFunc := range testFuncs {
if ignorer.ShouldIgnore(analyzerName, testFunc) {
continue
}
if strings.HasPrefix(testFunc.Name.Name, "TestAcc") {
continue
}
ast.Inspect(testFunc.Body, func(n ast.Node) bool {
callExpr, ok := n.(*ast.CallExpr)
if !ok {
return true
}
isResourceTest := resource.IsFunc(callExpr.Fun, pass.TypesInfo, resource.FuncNameTest)
isResourceParallelTest := resource.IsFunc(callExpr.Fun, pass.TypesInfo, resource.FuncNameParallelTest)
if !isResourceTest && !isResourceParallelTest {
return true
}
pass.Reportf(testFunc.Pos(), "%s: acceptance test function name should begin with TestAcc", analyzerName)
return true
})
}
return nil, nil
}