Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support providing a list of allowed packages #21

Merged
merged 3 commits into from Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -37,11 +37,15 @@ linters:
testpackage
```

You can also change regexp that is used to ignore files by the linter. Here is the default value.
You can also change the regexp that is used to ignore files by the linter,
and the list of packages that are allowed by default.

Here are the default values:
```yaml
linters-settings:
testpackage:
skip-regexp: (export|internal)_test\.go
allow-packages: main
```

### Run
Expand Down Expand Up @@ -79,6 +83,8 @@ Usage: testpackage [-flag] [package]
Flags:
-skip-regexp string
regexp pattern to skip file by name. To not skip files use -skip-regexp="^$" (default "(export|internal)_test\\.go")
-allow-packages string
comma separated list of packages that don't end with _test that tests are allowed to be in
-V print version and exit
-c int
display offending line with this many lines of context (default -1)
Expand Down
1 change: 1 addition & 0 deletions pkg/testpackage/testdata/allowed/allowed.go
@@ -0,0 +1 @@
package allowed
1 change: 1 addition & 0 deletions pkg/testpackage/testdata/allowed/allowed_internal_test.go
@@ -0,0 +1 @@
package allowed
1 change: 1 addition & 0 deletions pkg/testpackage/testdata/allowed/allowed_test.go
@@ -0,0 +1 @@
package allowed
1 change: 1 addition & 0 deletions pkg/testpackage/testdata/allowed/export_test.go
@@ -0,0 +1 @@
package allowed
1 change: 1 addition & 0 deletions pkg/testpackage/testdata/allowed/internal_test.go
@@ -0,0 +1 @@
package allowed
21 changes: 21 additions & 0 deletions pkg/testpackage/testpackage.go
Expand Up @@ -14,20 +14,29 @@ const (
SkipRegexpFlagDefault = `(export|internal)_test\.go`
)

const (
AllowPackagesFlagName = "allow-packages"
AllowPackagesFlagUsage = `comma separated list of packages that don't end with _test that tests are allowed to be in`
AllowPackagesFlagDefault = `main`
)

// NewAnalyzer returns Analyzer that makes you use a separate _test package.
func NewAnalyzer() *analysis.Analyzer {
var (
skipFileRegexp = SkipRegexpFlagDefault
allowPackagesStr = AllowPackagesFlagDefault
fs flag.FlagSet
G-Rath marked this conversation as resolved.
Show resolved Hide resolved
)

fs.StringVar(&skipFileRegexp, SkipRegexpFlagName, skipFileRegexp, SkipRegexpFlagUsage)
fs.StringVar(&allowPackagesStr, AllowPackagesFlagName, allowPackagesStr, AllowPackagesFlagUsage)

return &analysis.Analyzer{
Name: "testpackage",
Doc: "linter that makes you use a separate _test package",
Flags: fs,
Run: func(pass *analysis.Pass) (interface{}, error) {
allowedPackages := strings.Split(allowPackagesStr, ",")
skipFile, err := regexp.Compile(skipFileRegexp)
if err != nil {
return nil, err
Expand All @@ -41,6 +50,18 @@ func NewAnalyzer() *analysis.Analyzer {

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)
}
G-Rath marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
16 changes: 16 additions & 0 deletions pkg/testpackage/testpackage_test.go
Expand Up @@ -27,6 +27,22 @@ func TestAnalyzer_Bad(t *testing.T) {
analysistest.Run(t, testdata, testpackage.NewAnalyzer())
}

func TestAnalyzer_Allowed(t *testing.T) {
analyzer := testpackage.NewAnalyzer()
err := analyzer.Flags.Set(testpackage.AllowPackagesFlagName, "allowed")

if err != nil {
t.FailNow()
}

testdata, err := filepath.Abs("testdata/allowed")
if err != nil {
t.FailNow()
}

analysistest.Run(t, testdata, analyzer)
}

func TestAnalyzer_InvalidRegexp(t *testing.T) {
invalid := `\Ca`
analyzer := testpackage.NewAnalyzer()
Expand Down