diff --git a/.golangci.yml b/.golangci.yml index e14b08e..51c4c13 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -78,7 +78,7 @@ linters: - structcheck - typecheck #- unused # false positives - - varcheck + #- varcheck # false positives ## disabled by default - bodyclose - dupl diff --git a/main.go b/main.go index 04dea18..4c1cf32 100644 --- a/main.go +++ b/main.go @@ -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)) } diff --git a/pkg/testpackage/testdata/good/export_test.go b/pkg/testpackage/testdata/good/export_test.go new file mode 100644 index 0000000..fd2a96f --- /dev/null +++ b/pkg/testpackage/testdata/good/export_test.go @@ -0,0 +1 @@ +package good diff --git a/pkg/testpackage/testdata/good/good_internal_test.go b/pkg/testpackage/testdata/good/good_internal_test.go new file mode 100644 index 0000000..fd2a96f --- /dev/null +++ b/pkg/testpackage/testdata/good/good_internal_test.go @@ -0,0 +1 @@ +package good diff --git a/pkg/testpackage/testdata/good/internal_test.go b/pkg/testpackage/testdata/good/internal_test.go new file mode 100644 index 0000000..fd2a96f --- /dev/null +++ b/pkg/testpackage/testdata/good/internal_test.go @@ -0,0 +1 @@ +package good diff --git a/pkg/testpackage/testpackage.go b/pkg/testpackage/testpackage.go index c62d08c..d5e9b57 100644 --- a/pkg/testpackage/testpackage.go +++ b/pkg/testpackage/testpackage.go @@ -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 + }, + } } diff --git a/pkg/testpackage/testpackage_test.go b/pkg/testpackage/testpackage_test.go index 034670b..4af28fc 100644 --- a/pkg/testpackage/testpackage_test.go +++ b/pkg/testpackage/testpackage_test.go @@ -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) { @@ -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)) }