dev: group skip-dir related elements (#4631)
This commit is contained in:
parent
3f3741229e
commit
ba1eb3556f
@ -12,7 +12,7 @@ import (
|
||||
"github.com/golangci/golangci-lint/pkg/config"
|
||||
"github.com/golangci/golangci-lint/pkg/exitcodes"
|
||||
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
|
||||
"github.com/golangci/golangci-lint/pkg/packages"
|
||||
"github.com/golangci/golangci-lint/pkg/result/processors"
|
||||
)
|
||||
|
||||
func setupLintersFlagSet(v *viper.Viper, fs *pflag.FlagSet) {
|
||||
@ -129,7 +129,7 @@ func getDefaultIssueExcludeHelp() string {
|
||||
|
||||
func getDefaultDirectoryExcludeHelp() string {
|
||||
parts := []string{color.GreenString("Use or not use default excluded directories:")}
|
||||
for _, dir := range packages.StdExcludeDirRegexps {
|
||||
for _, dir := range processors.StdExcludeDirRegexps {
|
||||
parts = append(parts, fmt.Sprintf(" - %s", color.YellowString(dir)))
|
||||
}
|
||||
parts = append(parts, "")
|
||||
|
@ -14,7 +14,6 @@ import (
|
||||
"github.com/golangci/golangci-lint/pkg/lint/linter"
|
||||
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
|
||||
"github.com/golangci/golangci-lint/pkg/logutils"
|
||||
"github.com/golangci/golangci-lint/pkg/packages"
|
||||
"github.com/golangci/golangci-lint/pkg/result"
|
||||
"github.com/golangci/golangci-lint/pkg/result/processors"
|
||||
"github.com/golangci/golangci-lint/pkg/timeutils"
|
||||
@ -48,10 +47,10 @@ func NewRunner(log logutils.Log, cfg *config.Config, args []string, goenv *gouti
|
||||
|
||||
skipDirs := cfg.Issues.ExcludeDirs
|
||||
if cfg.Issues.UseDefaultExcludeDirs {
|
||||
skipDirs = append(skipDirs, packages.StdExcludeDirRegexps...)
|
||||
skipDirs = append(skipDirs, processors.StdExcludeDirRegexps...)
|
||||
}
|
||||
|
||||
skipDirsProcessor, err := processors.NewSkipDirs(skipDirs, log.Child(logutils.DebugKeySkipDirs), args, cfg.Output.PathPrefix)
|
||||
skipDirsProcessor, err := processors.NewSkipDirs(log.Child(logutils.DebugKeySkipDirs), skipDirs, args, cfg.Output.PathPrefix)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -1,25 +0,0 @@
|
||||
package packages
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func pathElemReImpl(e string, sep rune) string {
|
||||
escapedSep := regexp.QuoteMeta(string(sep)) // needed for windows sep '\\'
|
||||
return fmt.Sprintf(`(^|%s)%s($|%s)`, escapedSep, e, escapedSep)
|
||||
}
|
||||
|
||||
func pathElemRe(e string) string {
|
||||
return pathElemReImpl(e, filepath.Separator)
|
||||
}
|
||||
|
||||
var StdExcludeDirRegexps = []string{
|
||||
pathElemRe("vendor"),
|
||||
pathElemRe("third_party"),
|
||||
pathElemRe("testdata"),
|
||||
pathElemRe("examples"),
|
||||
pathElemRe("Godeps"),
|
||||
pathElemRe("builtin"),
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
package packages
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPathElemRe(t *testing.T) {
|
||||
matches := [][]string{
|
||||
{"dir"},
|
||||
{"root", "dir"},
|
||||
{"root", "dir", "subdir"},
|
||||
{"dir", "subdir"},
|
||||
}
|
||||
noMatches := [][]string{
|
||||
{"nodir"},
|
||||
{"dirno"},
|
||||
{"root", "dirno"},
|
||||
{"root", "nodir"},
|
||||
{"root", "dirno", "subdir"},
|
||||
{"root", "nodir", "subdir"},
|
||||
{"dirno", "subdir"},
|
||||
{"nodir", "subdir"},
|
||||
}
|
||||
for _, sep := range []rune{'/', '\\'} {
|
||||
reStr := pathElemReImpl("dir", sep)
|
||||
re := regexp.MustCompile(reStr)
|
||||
for _, m := range matches {
|
||||
assert.Regexp(t, re, strings.Join(m, string(sep)))
|
||||
}
|
||||
for _, m := range noMatches {
|
||||
assert.NotRegexp(t, re, strings.Join(m, string(sep)))
|
||||
}
|
||||
}
|
||||
}
|
@ -12,6 +12,15 @@ import (
|
||||
|
||||
var _ Processor = (*SkipDirs)(nil)
|
||||
|
||||
var StdExcludeDirRegexps = []string{
|
||||
normalizePathRegex("vendor"),
|
||||
normalizePathRegex("third_party"),
|
||||
normalizePathRegex("testdata"),
|
||||
normalizePathRegex("examples"),
|
||||
normalizePathRegex("Godeps"),
|
||||
normalizePathRegex("builtin"),
|
||||
}
|
||||
|
||||
type skipStat struct {
|
||||
pattern string
|
||||
count int
|
||||
@ -26,7 +35,7 @@ type SkipDirs struct {
|
||||
pathPrefix string
|
||||
}
|
||||
|
||||
func NewSkipDirs(patterns []string, log logutils.Log, args []string, pathPrefix string) (*SkipDirs, error) {
|
||||
func NewSkipDirs(log logutils.Log, patterns, args []string, pathPrefix string) (*SkipDirs, error) {
|
||||
var patternsRe []*regexp.Regexp
|
||||
for _, p := range patterns {
|
||||
p = fsutils.NormalizePathInRegex(p)
|
||||
@ -152,3 +161,12 @@ func absDirs(args []string) ([]string, error) {
|
||||
|
||||
return absArgsDirs, nil
|
||||
}
|
||||
|
||||
func normalizePathRegex(e string) string {
|
||||
return createPathRegex(e, filepath.Separator)
|
||||
}
|
||||
|
||||
func createPathRegex(e string, sep rune) string {
|
||||
escapedSep := regexp.QuoteMeta(string(sep)) // needed for windows sep '\\'
|
||||
return fmt.Sprintf(`(^|%[1]s)%[2]s($|%[1]s)`, escapedSep, e)
|
||||
}
|
||||
|
@ -2,6 +2,8 @@ package processors
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@ -58,6 +60,38 @@ func Test_absDirs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_createPathRegex(t *testing.T) {
|
||||
matches := [][]string{
|
||||
{"dir"},
|
||||
{"root", "dir"},
|
||||
{"root", "dir", "subdir"},
|
||||
{"dir", "subdir"},
|
||||
}
|
||||
|
||||
noMatches := [][]string{
|
||||
{"nodir"},
|
||||
{"dirno"},
|
||||
{"root", "dirno"},
|
||||
{"root", "nodir"},
|
||||
{"root", "dirno", "subdir"},
|
||||
{"root", "nodir", "subdir"},
|
||||
{"dirno", "subdir"},
|
||||
{"nodir", "subdir"},
|
||||
}
|
||||
|
||||
for _, sep := range []rune{'/', '\\'} {
|
||||
exp := regexp.MustCompile(createPathRegex("dir", sep))
|
||||
|
||||
for _, m := range matches {
|
||||
assert.Regexp(t, exp, strings.Join(m, string(sep)))
|
||||
}
|
||||
|
||||
for _, m := range noMatches {
|
||||
assert.NotRegexp(t, exp, strings.Join(m, string(sep)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func mustAbs(t *testing.T, p string) string {
|
||||
t.Helper()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user