-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Check: AT009: Check for acctest.RandStringFromCharSet that can be…
… acctest.RandString (#211) Reference: #175 Includes new helper/terraformtype/helper/acctest package and passes/helper/acctest/randstringfromcharsetcallexpr.
- Loading branch information
Showing
104 changed files
with
21,656 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package acctest | ||
|
||
const ( | ||
ConstNameCharSetAlpha = "CharSetAlpha" | ||
ConstNameCharSetAlphaNum = "CharSetAlphaNum" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package acctest | ||
|
||
const ( | ||
FuncNameRandInt = `RandInt` | ||
FuncNameRandIntRange = `RandIntRange` | ||
FuncNameRandIpAddress = `RandIpAddress` | ||
FuncNameRandSSHKeyPair = `RandSSHKeyPair` | ||
FuncNameRandString = `RandString` | ||
FuncNameRandStringFromCharSet = `RandStringFromCharSet` | ||
FuncNameRandTLSCert = `RandTLSCert` | ||
FuncNameRandomWithPrefix = `RandomWithPrefix` | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package acctest | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"go/types" | ||
|
||
"github.com/bflad/tfproviderlint/helper/astutils" | ||
"github.com/bflad/tfproviderlint/helper/terraformtype" | ||
) | ||
|
||
const ( | ||
PackageModule = terraformtype.ModuleTerraformPluginSdk | ||
PackageModulePath = `helper/acctest` | ||
PackageName = `acctest` | ||
PackagePath = PackageModule + `/` + PackageModulePath | ||
) | ||
|
||
// IsConst returns if the expr is a constant in the acctest package | ||
func IsConst(e ast.Expr, info *types.Info, constName string) bool { | ||
// IsModulePackageFunc can handle any SelectorExpr name | ||
return astutils.IsModulePackageFunc(e, info, PackageModule, PackageModulePath, constName) | ||
} | ||
|
||
// IsFunc returns if the function call is in the acctest package | ||
func IsFunc(e ast.Expr, info *types.Info, funcName string) bool { | ||
return astutils.IsModulePackageFunc(e, info, PackageModule, PackageModulePath, funcName) | ||
} | ||
|
||
// IsNamedType returns if the type name matches and is from the helper/acctest package | ||
func IsNamedType(t *types.Named, typeName string) bool { | ||
return astutils.IsModulePackageNamedType(t, PackageModule, PackageModulePath, typeName) | ||
} | ||
|
||
// PackagePathVersion returns the import path for a module version | ||
func PackagePathVersion(moduleVersion int) string { | ||
switch moduleVersion { | ||
case 0, 1: | ||
return PackagePath | ||
default: | ||
return fmt.Sprintf("%s/v%d/%s", PackageModule, moduleVersion, PackageModulePath) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package AT009 | ||
|
||
import ( | ||
"go/ast" | ||
|
||
"github.com/bflad/tfproviderlint/helper/terraformtype/helper/acctest" | ||
"github.com/bflad/tfproviderlint/passes/commentignore" | ||
"github.com/bflad/tfproviderlint/passes/helper/acctest/randstringfromcharsetcallexpr" | ||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
||
const Doc = `check for acctest.RandStringFromCharSet() calls that can be acctest.RandString() | ||
The AT009 analyzer reports where the second parameter of a | ||
RandStringFromCharSet call is acctest.CharSetAlpha, which is equivalent to | ||
calling RandString.` | ||
|
||
const analyzerName = "AT009" | ||
|
||
var Analyzer = &analysis.Analyzer{ | ||
Name: analyzerName, | ||
Doc: Doc, | ||
Requires: []*analysis.Analyzer{ | ||
commentignore.Analyzer, | ||
randstringfromcharsetcallexpr.Analyzer, | ||
}, | ||
Run: run, | ||
} | ||
|
||
func run(pass *analysis.Pass) (interface{}, error) { | ||
ignorer := pass.ResultOf[commentignore.Analyzer].(*commentignore.Ignorer) | ||
callExprs := pass.ResultOf[randstringfromcharsetcallexpr.Analyzer].([]*ast.CallExpr) | ||
for _, callExpr := range callExprs { | ||
if ignorer.ShouldIgnore(analyzerName, callExpr) { | ||
continue | ||
} | ||
|
||
if len(callExpr.Args) < 2 { | ||
continue | ||
} | ||
|
||
if !acctest.IsConst(callExpr.Args[1], pass.TypesInfo, acctest.ConstNameCharSetAlpha) { | ||
continue | ||
} | ||
|
||
pass.Reportf(callExpr.Pos(), "%s: should use RandString call instead", analyzerName) | ||
} | ||
|
||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package AT009 | ||
|
||
import ( | ||
"testing" | ||
|
||
"golang.org/x/tools/go/analysis/analysistest" | ||
) | ||
|
||
func TestAT009(t *testing.T) { | ||
testdata := analysistest.TestData() | ||
analysistest.Run(t, testdata, Analyzer, "a") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# AT009 | ||
|
||
The AT009 analyzer reports where `acctest.RandStringFromCharSet()` calls can be simplified to `acctest.RandString()`. | ||
|
||
## Flagged Code | ||
|
||
```go | ||
rString := acctest.RandStringFromCharSet(8, acctest.CharSetAlphaNum) | ||
``` | ||
|
||
## Passing Code | ||
|
||
```go | ||
rString := acctest.RandString(8) | ||
``` | ||
|
||
## Ignoring Reports | ||
|
||
Singular reports can be ignored by adding the a `//lintignore:AT009` Go code comment at the end of the offending line or on the line immediately proceding, e.g. | ||
|
||
```go | ||
//lintignore:AT009 | ||
rString := acctest.RandStringFromCharSet(8, acctest.CharSetAlphaNum) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package a | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
) | ||
|
||
const ( | ||
ConstKey = "ConstValue" | ||
) | ||
|
||
func f() { | ||
// Comment ignored | ||
|
||
//lintignore:AT009 | ||
_ = acctest.RandStringFromCharSet(1, acctest.CharSetAlpha) | ||
|
||
// Failing | ||
|
||
_ = acctest.RandStringFromCharSet(1, acctest.CharSetAlpha) // want "should use RandString call instead" | ||
|
||
// Passing | ||
|
||
_ = acctest.RandStringFromCharSet(1, acctest.CharSetAlphaNum) | ||
_ = acctest.RandStringFromCharSet(1, "abc123") | ||
_ = acctest.RandString(1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../../vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
passes/helper/acctest/randstringfromcharsetcallexpr/randstringfromcharsetcallexpr.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package randstringfromcharsetcallexpr | ||
|
||
import ( | ||
"github.com/bflad/tfproviderlint/helper/analysisutils" | ||
"github.com/bflad/tfproviderlint/helper/terraformtype/helper/acctest" | ||
) | ||
|
||
var Analyzer = analysisutils.FunctionCallExprAnalyzer( | ||
"randstringfromcharsetcallexpr", | ||
acctest.IsFunc, | ||
acctest.PackagePath, | ||
acctest.FuncNameRandStringFromCharSet, | ||
) |
15 changes: 15 additions & 0 deletions
15
passes/helper/acctest/randstringfromcharsetcallexpr/randstringfromcharsetcallexpr_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package randstringfromcharsetcallexpr | ||
|
||
import ( | ||
"testing" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
||
func TestValidateAnalyzer(t *testing.T) { | ||
err := analysis.Validate([]*analysis.Analyzer{Analyzer}) | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
vendor/github.com/hashicorp/terraform-plugin-sdk/helper/acctest/acctest.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.