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

Introduce flag -skip-regexp to skip file name #8

Merged
merged 1 commit into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ linters:
- structcheck
- typecheck
#- unused # false positives
- varcheck
#- varcheck # false positives
## disabled by default
- bodyclose
- dupl
Expand Down
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package main

import (
"flag"

"golang.org/x/tools/go/analysis/singlechecker"

"github.com/maratori/testpackage/pkg/testpackage"
)

func main() {
singlechecker.Main(testpackage.Analyzer)
skip := flag.String("skip-regexp", testpackage.DefaultSkipRegexp, `regexp pattern to skip file by name. To not skip files use -skip-regexp="^$"`) // nolint:lll
singlechecker.Main(testpackage.NewAnalyzer(skip))
}
1 change: 1 addition & 0 deletions pkg/testpackage/testdata/good/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package good
1 change: 1 addition & 0 deletions pkg/testpackage/testdata/good/good_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package good
1 change: 1 addition & 0 deletions pkg/testpackage/testdata/good/internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package good
44 changes: 28 additions & 16 deletions pkg/testpackage/testpackage.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
package testpackage

import (
"regexp"
"strings"

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

// Analyzer that make you use a separate _test package
var Analyzer = &analysis.Analyzer{ // nolint:gochecknoglobals,varcheck
Name: "testpackage",
Doc: "linter that make you use a separate _test package",
Run: run,
}
const DefaultSkipRegexp = `(export|internal)_test\.go`

func run(pass *analysis.Pass) (interface{}, error) {
for _, f := range pass.Files {
fileName := pass.Fset.Position(f.Pos()).Filename
if strings.HasSuffix(fileName, "_test.go") {
packageName := f.Name.Name
if !strings.HasSuffix(packageName, "_test") {
pass.Reportf(f.Name.Pos(), `package should be "%s_test" instead of "%s"`, packageName, packageName)
// NewAnalyzer returns Analyzer that make you use a separate _test package
func NewAnalyzer(skipFileRegexp *string) *analysis.Analyzer {
return &analysis.Analyzer{
Name: "testpackage",
Doc: "linter that make you use a separate _test package",
Run: func(pass *analysis.Pass) (interface{}, error) {
skipFile, err := regexp.Compile(*skipFileRegexp)
if err != nil {
return nil, err
}
}
}

return nil, nil
for _, f := range pass.Files {
fileName := pass.Fset.Position(f.Pos()).Filename
if skipFile.MatchString(fileName) {
continue
}

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

return nil, nil
},
}
}
6 changes: 4 additions & 2 deletions pkg/testpackage/testpackage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ func TestAnalyzer_Good(t *testing.T) {
t.FailNow()
}

analysistest.Run(t, testdata, testpackage.Analyzer)
skip := testpackage.DefaultSkipRegexp
analysistest.Run(t, testdata, testpackage.NewAnalyzer(&skip))
}

func TestAnalyzer_Bad(t *testing.T) {
Expand All @@ -24,5 +25,6 @@ func TestAnalyzer_Bad(t *testing.T) {
t.FailNow()
}

analysistest.Run(t, testdata, testpackage.Analyzer)
skip := testpackage.DefaultSkipRegexp
analysistest.Run(t, testdata, testpackage.NewAnalyzer(&skip))
}