Skip to content

Commit

Permalink
refactor: encapsulate logic in function
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Jun 21, 2022
1 parent 11ed3fe commit 0934ad2
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions pkg/testpackage/testpackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"regexp"
"strings"

"go/ast"
"golang.org/x/tools/go/analysis"
)

Expand All @@ -20,12 +21,26 @@ const (
AllowPackagesFlagDefault = `main`
)

func processTestFile(pass *analysis.Pass, f *ast.File, allowedPackages []string) {
packageName := f.Name.Name

for _, p := range allowedPackages {
if p == packageName {
return
}
}

if !strings.HasSuffix(packageName, "_test") {
pass.Reportf(f.Name.Pos(), "package should be `%s_test` instead of `%s`", packageName, packageName)
}
}

// NewAnalyzer returns Analyzer that makes you use a separate _test package.
func NewAnalyzer() *analysis.Analyzer {
var (
skipFileRegexp = SkipRegexpFlagDefault
skipFileRegexp = SkipRegexpFlagDefault
allowPackagesStr = AllowPackagesFlagDefault
fs flag.FlagSet
fs flag.FlagSet
)

fs.StringVar(&skipFileRegexp, SkipRegexpFlagName, skipFileRegexp, SkipRegexpFlagUsage)
Expand All @@ -44,28 +59,12 @@ func NewAnalyzer() *analysis.Analyzer {

for _, f := range pass.Files {
fileName := pass.Fset.Position(f.Pos()).Filename
if skipFile.MatchString(fileName) {
if !strings.HasSuffix(fileName, "_test.go") || skipFile.MatchString(fileName) {
continue
}

if strings.HasSuffix(fileName, "_test.go") {
packageName := f.Name.Name

allowedPackage := false
for _, p := range allowedPackages {
if p == packageName {
allowedPackage = true
}
}

if allowedPackage {
continue
}

if !strings.HasSuffix(packageName, "_test") {
pass.Reportf(f.Name.Pos(), "package should be `%s_test` instead of `%s`", packageName, packageName)
}
}
processTestFile(pass, f, allowedPackages)
}

return nil, nil
Expand Down

0 comments on commit 0934ad2

Please sign in to comment.