Skip to content

Commit

Permalink
passes: Support ignore comments in AT004, AT005, AT006, and AT007 (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
bflad authored Mar 4, 2020
1 parent 4fa91e3 commit 768b00a
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 65 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# v0.11.0

ENHANCEMENTS

* passes/AT004: Support ignore comments
* passes/AT005: Support ignore comments
* passes/AT006: Support ignore comments
* passes/AT007: Support ignore comments

# v0.10.0

BREAKING CHANGES
Expand Down
17 changes: 13 additions & 4 deletions passes/AT004/AT004.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"go/token"
"strings"

"github.com/bflad/tfproviderlint/passes/commentignore"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
Expand All @@ -21,13 +22,17 @@ be handled outside individual test configurations (e.g. environment variables).`
const analyzerName = "AT004"

var Analyzer = &analysis.Analyzer{
Name: analyzerName,
Doc: Doc,
Requires: []*analysis.Analyzer{inspect.Analyzer},
Run: run,
Name: analyzerName,
Doc: Doc,
Requires: []*analysis.Analyzer{
commentignore.Analyzer,
inspect.Analyzer,
},
Run: run,
}

func run(pass *analysis.Pass) (interface{}, error) {
ignorer := pass.ResultOf[commentignore.Analyzer].(*commentignore.Ignorer)
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)

nodeFilter := []ast.Node{
Expand All @@ -36,6 +41,10 @@ func run(pass *analysis.Pass) (interface{}, error) {
inspect.Preorder(nodeFilter, func(n ast.Node) {
x := n.(*ast.BasicLit)

if ignorer.ShouldIgnore(analyzerName, x) {
return
}

if x.Kind != token.STRING {
return
}
Expand Down
85 changes: 25 additions & 60 deletions passes/AT004/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,6 @@ func f() {
},
}

/*
Skipping for now since there does not seem to be a way to
correctly add the want comment for multiline string literals
_ = resource.TestCase{
Steps: []resource.TestStep{
{
Config: configWithProvider1,
},
},
}
_ = resource.TestCase{
Steps: []resource.TestStep{
{
Config: configWithProvider2,
},
},
}
_ = resource.TestCase{
Steps: []resource.TestStep{
{
Config: configWithProvider3(),
},
},
}
*/

_ = resource.TestCase{
Steps: []resource.TestStep{
{
Expand All @@ -55,30 +26,6 @@ func f() {
},
}

_ = resource.TestCase{
Steps: []resource.TestStep{
{
Config: configWithoutProvider1,
},
},
}

_ = resource.TestCase{
Steps: []resource.TestStep{
{
Config: configWithoutProvider2,
},
},
}

_ = resource.TestCase{
Steps: []resource.TestStep{
{
Config: configWithoutProvider3(),
},
},
}

resource.Test(t, resource.TestCase{
Steps: []resource.TestStep{
{
Expand All @@ -96,30 +43,48 @@ func f() {
})
}

/*
Skipping for now since there does not seem to be a way to
correctly add the want comment for multiline string literals
const configWithProvider1 = `
const configWithProvider1 = /* want "provider declaration should be omitted" */ `
provider "aws" {
region = "us-east-1"
}
`

var configWithProvider2 = `
var configWithProvider2 = /* want "provider declaration should be omitted" */ `
provider "aws" {
region = "us-east-1"
}
`

func configWithProvider3() string {
return fmt.Sprintf( /* want "provider declaration should be omitted" */ `
provider "aws" {
region = "us-east-1"
}
`)
}

//lintignore:AT004
const configWithProviderIgnored1 = `
provider "aws" {
region = "us-east-1"
}
`

//lintignore:AT004
var configWithProviderIgnored2 = `
provider "aws" {
region = "us-east-1"
}
`

func configWithProviderIgnored3() string {
//lintignore:AT004
return fmt.Sprintf(`
provider "aws" {
region = "us-east-1"
}
`)
}
*/

const configWithoutProvider1 = `
resource "aws_vpc" {
Expand Down
7 changes: 7 additions & 0 deletions passes/AT005/AT005.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"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"
)
Expand All @@ -23,15 +24,21 @@ 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
}
Expand Down
14 changes: 14 additions & 0 deletions passes/AT005/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (

func TestUnitTest(t *testing.T) {}

func TestUnitTest_ResourceUnitTest(t *testing.T) {
resource.UnitTest(t, resource.TestCase{})
}

func TestExampleThing_basic(t *testing.T) { // want "acceptance test function name should begin with TestAcc"
resource.Test(t, resource.TestCase{})
}
Expand All @@ -16,6 +20,16 @@ func TestExampleWidget_basic(t *testing.T) { // want "acceptance test function n
resource.ParallelTest(t, resource.TestCase{})
}

//lintignore:AT005
func TestExampleThing_ignored(t *testing.T) {
resource.Test(t, resource.TestCase{})
}

//lintignore:AT005
func TestExampleWidget_ignored(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{})
}

func TestAccExampleThing_basic(t *testing.T) {
resource.Test(t, resource.TestCase{})
}
Expand Down
7 changes: 7 additions & 0 deletions passes/AT006/AT006.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"go/ast"

"github.com/bflad/tfproviderlint/helper/terraformtype/helper/resource"
"github.com/bflad/tfproviderlint/passes/commentignore"
"github.com/bflad/tfproviderlint/passes/testaccfuncdecl"
"golang.org/x/tools/go/analysis"
)
Expand All @@ -21,15 +22,21 @@ var Analyzer = &analysis.Analyzer{
Name: analyzerName,
Doc: Doc,
Requires: []*analysis.Analyzer{
commentignore.Analyzer,
testaccfuncdecl.Analyzer,
},
Run: run,
}

func run(pass *analysis.Pass) (interface{}, error) {
ignorer := pass.ResultOf[commentignore.Analyzer].(*commentignore.Ignorer)
testFuncs := pass.ResultOf[testaccfuncdecl.Analyzer].([]*ast.FuncDecl)

for _, testFunc := range testFuncs {
if ignorer.ShouldIgnore(analyzerName, testFunc) {
continue
}

var resourceTestInvocations int

ast.Inspect(testFunc.Body, func(n ast.Node) bool {
Expand Down
6 changes: 6 additions & 0 deletions passes/AT006/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ func TestAccExampleThing_single(t *testing.T) {
resource.Test(t, resource.TestCase{})
}

//lintignore:AT006
func TestAccExampleThing_ignored(t *testing.T) {
resource.Test(t, resource.TestCase{})
resource.Test(t, resource.TestCase{})
}

func TestAccExampleThing_multiple(t *testing.T) { // want "acceptance test function should contain only one Test invocation"
resource.Test(t, resource.TestCase{})
resource.Test(t, resource.TestCase{})
Expand Down
7 changes: 7 additions & 0 deletions passes/AT007/AT007.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"go/ast"

"github.com/bflad/tfproviderlint/helper/terraformtype/helper/resource"
"github.com/bflad/tfproviderlint/passes/commentignore"
"github.com/bflad/tfproviderlint/passes/testaccfuncdecl"
"golang.org/x/tools/go/analysis"
)
Expand All @@ -22,15 +23,21 @@ var Analyzer = &analysis.Analyzer{
Name: analyzerName,
Doc: Doc,
Requires: []*analysis.Analyzer{
commentignore.Analyzer,
testaccfuncdecl.Analyzer,
},
Run: run,
}

func run(pass *analysis.Pass) (interface{}, error) {
ignorer := pass.ResultOf[commentignore.Analyzer].(*commentignore.Ignorer)
testFuncs := pass.ResultOf[testaccfuncdecl.Analyzer].([]*ast.FuncDecl)

for _, testFunc := range testFuncs {
if ignorer.ShouldIgnore(analyzerName, testFunc) {
continue
}

var resourceParallelTestInvocations int

ast.Inspect(testFunc.Body, func(n ast.Node) bool {
Expand Down
6 changes: 6 additions & 0 deletions passes/AT007/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ func TestAccExampleThing_single(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{})
}

//lintignore:AT007
func TestAccExampleThing_ignored(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{})
resource.ParallelTest(t, resource.TestCase{})
}

func TestAccExampleThing_multiple(t *testing.T) { // want "acceptance test function should contain only one ParallelTest invocation"
resource.ParallelTest(t, resource.TestCase{})
resource.ParallelTest(t, resource.TestCase{})
Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var (
GitCommit string

// The main version number that is being run at the moment.
Version = "0.10.0"
Version = "0.11.0"

// A pre-release marker for the version. If this is "" (empty string)
// then it means that it is a final release. Otherwise, this is a pre-release
Expand Down

0 comments on commit 768b00a

Please sign in to comment.