-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from maratori/skip-pattern
Introduce flag -skip-regexp to skip file name
- Loading branch information
Showing
7 changed files
with
40 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package good |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package good |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package good |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters