Skip to content

Commit

Permalink
Merge pull request #8 from maratori/skip-pattern
Browse files Browse the repository at this point in the history
Introduce flag -skip-regexp to skip file name
  • Loading branch information
maratori authored Nov 4, 2019
2 parents 3084ead + a959fab commit e960977
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 20 deletions.
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))
}

0 comments on commit e960977

Please sign in to comment.