Skip to content

Commit

Permalink
New Analyzer: accteststep: returns resource.TestStep literals for lat…
Browse files Browse the repository at this point in the history
…er passes
  • Loading branch information
bflad committed Jan 8, 2020
1 parent f9c6355 commit f79b1af
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ NOTES

FEATURES

* **New Analyzer:** `accteststep`: returns `resource.TestStep` literals 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
10 changes: 10 additions & 0 deletions helper/terraformtype/helper_resource_teststep.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,13 @@ func NewHelperResourceTestStepInfo(cl *ast.CompositeLit, info *types.Info) *Help
func (info *HelperResourceTestStepInfo) DeclaresField(fieldName string) bool {
return info.Fields[fieldName] != nil
}

// IsHelperResourceTypeTestStep returns if the type is TestStep from the helper/schema package
func IsHelperResourceTypeTestStep(t types.Type) bool {
switch t := t.(type) {
case *types.Named:
return IsHelperResourceNamedType(t, TypeNameTestStep)
default:
return false
}
}
50 changes: 50 additions & 0 deletions passes/accteststep/accteststep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package accteststep

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: "accteststep",
Doc: "find github.com/hashicorp/terraform-plugin-sdk/helper/resource.TestStep literals for later passes",
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
Run: run,
ResultType: reflect.TypeOf([]*terraformtype.HelperResourceTestStepInfo{}),
}

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

inspect.Preorder(nodeFilter, func(n ast.Node) {
x := n.(*ast.CompositeLit)

if !isResourceTestStep(pass, x) {
return
}

result = append(result, terraformtype.NewHelperResourceTestStepInfo(x, pass.TypesInfo))
})

return result, nil
}

func isResourceTestStep(pass *analysis.Pass, cl *ast.CompositeLit) bool {
switch v := cl.Type.(type) {
default:
return false
case *ast.SelectorExpr:
return terraformtype.IsHelperResourceTypeTestStep(pass.TypesInfo.TypeOf(v))
}
}

0 comments on commit f79b1af

Please sign in to comment.