From 7c18f174865772f38d675cf2e86922c13aae766d Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Wed, 8 Jan 2020 13:47:06 -0500 Subject: [PATCH] New Analyzers: testcheckresourceattr, testcheckresourceattrset, testmatchresourceattr --- CHANGELOG.md | 3 ++ .../testcheckresourceattr.go | 41 +++++++++++++++++++ .../testcheckresourceattrset.go | 41 +++++++++++++++++++ .../testmatchresourceattr.go | 41 +++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 passes/testcheckresourceattr/testcheckresourceattr.go create mode 100644 passes/testcheckresourceattrset/testcheckresourceattrset.go create mode 100644 passes/testmatchresourceattr/testmatchresourceattr.go diff --git a/CHANGELOG.md b/CHANGELOG.md index e6a0a20b..584e46d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/passes/testcheckresourceattr/testcheckresourceattr.go b/passes/testcheckresourceattr/testcheckresourceattr.go new file mode 100644 index 00000000..65b9c3fa --- /dev/null +++ b/passes/testcheckresourceattr/testcheckresourceattr.go @@ -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 +} diff --git a/passes/testcheckresourceattrset/testcheckresourceattrset.go b/passes/testcheckresourceattrset/testcheckresourceattrset.go new file mode 100644 index 00000000..79443892 --- /dev/null +++ b/passes/testcheckresourceattrset/testcheckresourceattrset.go @@ -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 +} diff --git a/passes/testmatchresourceattr/testmatchresourceattr.go b/passes/testmatchresourceattr/testmatchresourceattr.go new file mode 100644 index 00000000..30a532cd --- /dev/null +++ b/passes/testmatchresourceattr/testmatchresourceattr.go @@ -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 +}