Skip to content

Commit

Permalink
New Analyzers: testcheckresourceattr, testcheckresourceattrset, testm…
Browse files Browse the repository at this point in the history
…atchresourceattr
  • Loading branch information
bflad committed Jan 8, 2020
1 parent f79b1af commit 7c18f17
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ NOTES
FEATURES

* **New Analyzer:** `accteststep`: returns `resource.TestStep` literals for later passes
* **New Analyzer:** `testcheckresourceattr`: returns `resource.TestCheckResourceAttr()` calls for later passes
* **New Analyzer:** `testcheckresourceattrset`: returns `resource.TestCheckResourceAttrSet()` calls for later passes
* **New Analyzer:** `testmatchresourceattr`: returns `resource.TestMatchResourceAttr()` calls for later passes
* **New Check:** `AT005`: check for acceptance test names missing `TestAcc` prefix
* **New Check:** `AT006`: check for acceptance tests containing multiple `resource.Test()` invocations
* **New Check:** `AT007`: check for acceptance tests containing multiple `resource.ParallelTest()` invocations
Expand Down
41 changes: 41 additions & 0 deletions passes/testcheckresourceattr/testcheckresourceattr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package testcheckresourceattr

import (
"go/ast"
"reflect"

"github.com/bflad/tfproviderlint/helper/terraformtype"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)

var Analyzer = &analysis.Analyzer{
Name: "testcheckresourceattr",
Doc: "find github.com/hashicorp/terraform-plugin-sdk/helper/resource.TestCheckResourceAttr calls for later passes",
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
Run: run,
ResultType: reflect.TypeOf([]*ast.CallExpr{}),
}

func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{
(*ast.CallExpr)(nil),
}
var result []*ast.CallExpr

inspect.Preorder(nodeFilter, func(n ast.Node) {
callExpr := n.(*ast.CallExpr)

if !terraformtype.IsHelperResourceFunc(callExpr.Fun, pass.TypesInfo, terraformtype.FuncNameTestCheckResourceAttr) {
return
}

result = append(result, callExpr)
})

return result, nil
}
41 changes: 41 additions & 0 deletions passes/testcheckresourceattrset/testcheckresourceattrset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package testcheckresourceattrset

import (
"go/ast"
"reflect"

"github.com/bflad/tfproviderlint/helper/terraformtype"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)

var Analyzer = &analysis.Analyzer{
Name: "testcheckresourceattrset",
Doc: "find github.com/hashicorp/terraform-plugin-sdk/helper/resource.TestCheckResourceAttrSet calls for later passes",
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
Run: run,
ResultType: reflect.TypeOf([]*ast.CallExpr{}),
}

func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{
(*ast.CallExpr)(nil),
}
var result []*ast.CallExpr

inspect.Preorder(nodeFilter, func(n ast.Node) {
callExpr := n.(*ast.CallExpr)

if !terraformtype.IsHelperResourceFunc(callExpr.Fun, pass.TypesInfo, terraformtype.FuncNameTestCheckResourceAttrSet) {
return
}

result = append(result, callExpr)
})

return result, nil
}
41 changes: 41 additions & 0 deletions passes/testmatchresourceattr/testmatchresourceattr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package testmatchresourceattr

import (
"go/ast"
"reflect"

"github.com/bflad/tfproviderlint/helper/terraformtype"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)

var Analyzer = &analysis.Analyzer{
Name: "testmatchresourceattr",
Doc: "find github.com/hashicorp/terraform-plugin-sdk/helper/resource.TestMatchResourceAttr calls for later passes",
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
Run: run,
ResultType: reflect.TypeOf([]*ast.CallExpr{}),
}

func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{
(*ast.CallExpr)(nil),
}
var result []*ast.CallExpr

inspect.Preorder(nodeFilter, func(n ast.Node) {
callExpr := n.(*ast.CallExpr)

if !terraformtype.IsHelperResourceFunc(callExpr.Fun, pass.TypesInfo, terraformtype.FuncNameTestMatchResourceAttr) {
return
}

result = append(result, callExpr)
})

return result, nil
}

0 comments on commit 7c18f17

Please sign in to comment.