fix: consider path prefix when matching path patterns (#3571)
This commit is contained in:
parent
e27b129e74
commit
b40a5443b3
@ -27,7 +27,8 @@ run:
|
|||||||
- mytag
|
- mytag
|
||||||
|
|
||||||
# Which dirs to skip: issues from them won't be reported.
|
# Which dirs to skip: issues from them won't be reported.
|
||||||
# Can use regexp here: `generated.*`, regexp is applied on full path.
|
# Can use regexp here: `generated.*`, regexp is applied on full path,
|
||||||
|
# including the path prefix if one is set.
|
||||||
# Default value is empty list,
|
# Default value is empty list,
|
||||||
# but default dirs are skipped independently of this option's value (see skip-dirs-use-default).
|
# but default dirs are skipped independently of this option's value (see skip-dirs-use-default).
|
||||||
# "/" will be replaced by current OS file path separator to properly work on Windows.
|
# "/" will be replaced by current OS file path separator to properly work on Windows.
|
||||||
|
@ -66,6 +66,10 @@ issues:
|
|||||||
|
|
||||||
Exclude issues in path by `run.skip-dirs`, `run.skip-files` or `issues.exclude-rules` config options.
|
Exclude issues in path by `run.skip-dirs`, `run.skip-files` or `issues.exclude-rules` config options.
|
||||||
|
|
||||||
|
Beware that the paths that get matched here are relative to the current working directory.
|
||||||
|
When the configuration contains path patterns that check for specific directories,
|
||||||
|
the `--path-prefix` parameter can be used to extend the paths before matching.
|
||||||
|
|
||||||
In the following example, all the reports from the linters (`linters`) that concerns the path (`path`) are excluded:
|
In the following example, all the reports from the linters (`linters`) that concerns the path (`path`) are excluded:
|
||||||
|
|
||||||
```yml
|
```yml
|
||||||
|
33
pkg/fsutils/files.go
Normal file
33
pkg/fsutils/files.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package fsutils
|
||||||
|
|
||||||
|
import "path/filepath"
|
||||||
|
|
||||||
|
// Files combines different operations related to handling file paths and content.
|
||||||
|
type Files struct {
|
||||||
|
*LineCache
|
||||||
|
pathPrefix string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFiles(lc *LineCache, pathPrefix string) *Files {
|
||||||
|
return &Files{
|
||||||
|
LineCache: lc,
|
||||||
|
pathPrefix: pathPrefix,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithPathPrefix takes a path that is relative to the current directory (as used in issues)
|
||||||
|
// and adds the configured path prefix, if there is one.
|
||||||
|
// The resulting path then can be shown to the user or compared against paths specified in the configuration.
|
||||||
|
func (f *Files) WithPathPrefix(relativePath string) string {
|
||||||
|
return WithPathPrefix(f.pathPrefix, relativePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithPathPrefix takes a path that is relative to the current directory (as used in issues)
|
||||||
|
// and adds the configured path prefix, if there is one.
|
||||||
|
// The resulting path then can be shown to the user or compared against paths specified in the configuration.
|
||||||
|
func WithPathPrefix(pathPrefix, relativePath string) string {
|
||||||
|
if pathPrefix == "" {
|
||||||
|
return relativePath
|
||||||
|
}
|
||||||
|
return filepath.Join(pathPrefix, relativePath)
|
||||||
|
}
|
@ -29,7 +29,12 @@ type Runner struct {
|
|||||||
|
|
||||||
func NewRunner(cfg *config.Config, log logutils.Log, goenv *goutil.Env, es *lintersdb.EnabledSet,
|
func NewRunner(cfg *config.Config, log logutils.Log, goenv *goutil.Env, es *lintersdb.EnabledSet,
|
||||||
lineCache *fsutils.LineCache, dbManager *lintersdb.Manager, pkgs []*gopackages.Package) (*Runner, error) {
|
lineCache *fsutils.LineCache, dbManager *lintersdb.Manager, pkgs []*gopackages.Package) (*Runner, error) {
|
||||||
skipFilesProcessor, err := processors.NewSkipFiles(cfg.Run.SkipFiles)
|
// Beware that some processors need to add the path prefix when working with paths
|
||||||
|
// because they get invoked before the path prefixer (exclude and severity rules)
|
||||||
|
// or process other paths (skip files).
|
||||||
|
files := fsutils.NewFiles(lineCache, cfg.Output.PathPrefix)
|
||||||
|
|
||||||
|
skipFilesProcessor, err := processors.NewSkipFiles(cfg.Run.SkipFiles, cfg.Output.PathPrefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -38,7 +43,7 @@ func NewRunner(cfg *config.Config, log logutils.Log, goenv *goutil.Env, es *lint
|
|||||||
if cfg.Run.UseDefaultSkipDirs {
|
if cfg.Run.UseDefaultSkipDirs {
|
||||||
skipDirs = append(skipDirs, packages.StdExcludeDirRegexps...)
|
skipDirs = append(skipDirs, packages.StdExcludeDirRegexps...)
|
||||||
}
|
}
|
||||||
skipDirsProcessor, err := processors.NewSkipDirs(skipDirs, log.Child(logutils.DebugKeySkipDirs), cfg.Run.Args)
|
skipDirsProcessor, err := processors.NewSkipDirs(skipDirs, log.Child(logutils.DebugKeySkipDirs), cfg.Run.Args, cfg.Output.PathPrefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -82,7 +87,7 @@ func NewRunner(cfg *config.Config, log logutils.Log, goenv *goutil.Env, es *lint
|
|||||||
processors.NewIdentifierMarker(),
|
processors.NewIdentifierMarker(),
|
||||||
|
|
||||||
getExcludeProcessor(&cfg.Issues),
|
getExcludeProcessor(&cfg.Issues),
|
||||||
getExcludeRulesProcessor(&cfg.Issues, log, lineCache),
|
getExcludeRulesProcessor(&cfg.Issues, log, files),
|
||||||
processors.NewNolint(log.Child(logutils.DebugKeyNolint), dbManager, enabledLinters),
|
processors.NewNolint(log.Child(logutils.DebugKeyNolint), dbManager, enabledLinters),
|
||||||
|
|
||||||
processors.NewUniqByLine(cfg),
|
processors.NewUniqByLine(cfg),
|
||||||
@ -92,7 +97,7 @@ func NewRunner(cfg *config.Config, log logutils.Log, goenv *goutil.Env, es *lint
|
|||||||
processors.NewMaxFromLinter(cfg.Issues.MaxIssuesPerLinter, log.Child(logutils.DebugKeyMaxFromLinter), cfg),
|
processors.NewMaxFromLinter(cfg.Issues.MaxIssuesPerLinter, log.Child(logutils.DebugKeyMaxFromLinter), cfg),
|
||||||
processors.NewSourceCode(lineCache, log.Child(logutils.DebugKeySourceCode)),
|
processors.NewSourceCode(lineCache, log.Child(logutils.DebugKeySourceCode)),
|
||||||
processors.NewPathShortener(),
|
processors.NewPathShortener(),
|
||||||
getSeverityRulesProcessor(&cfg.Severity, log, lineCache),
|
getSeverityRulesProcessor(&cfg.Severity, log, files),
|
||||||
processors.NewPathPrefixer(cfg.Output.PathPrefix),
|
processors.NewPathPrefixer(cfg.Output.PathPrefix),
|
||||||
processors.NewSortResults(cfg),
|
processors.NewSortResults(cfg),
|
||||||
},
|
},
|
||||||
@ -259,7 +264,7 @@ func getExcludeProcessor(cfg *config.Issues) processors.Processor {
|
|||||||
return excludeProcessor
|
return excludeProcessor
|
||||||
}
|
}
|
||||||
|
|
||||||
func getExcludeRulesProcessor(cfg *config.Issues, log logutils.Log, lineCache *fsutils.LineCache) processors.Processor {
|
func getExcludeRulesProcessor(cfg *config.Issues, log logutils.Log, files *fsutils.Files) processors.Processor {
|
||||||
var excludeRules []processors.ExcludeRule
|
var excludeRules []processors.ExcludeRule
|
||||||
for _, r := range cfg.ExcludeRules {
|
for _, r := range cfg.ExcludeRules {
|
||||||
excludeRules = append(excludeRules, processors.ExcludeRule{
|
excludeRules = append(excludeRules, processors.ExcludeRule{
|
||||||
@ -287,13 +292,13 @@ func getExcludeRulesProcessor(cfg *config.Issues, log logutils.Log, lineCache *f
|
|||||||
if cfg.ExcludeCaseSensitive {
|
if cfg.ExcludeCaseSensitive {
|
||||||
excludeRulesProcessor = processors.NewExcludeRulesCaseSensitive(
|
excludeRulesProcessor = processors.NewExcludeRulesCaseSensitive(
|
||||||
excludeRules,
|
excludeRules,
|
||||||
lineCache,
|
files,
|
||||||
log.Child(logutils.DebugKeyExcludeRules),
|
log.Child(logutils.DebugKeyExcludeRules),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
excludeRulesProcessor = processors.NewExcludeRules(
|
excludeRulesProcessor = processors.NewExcludeRules(
|
||||||
excludeRules,
|
excludeRules,
|
||||||
lineCache,
|
files,
|
||||||
log.Child(logutils.DebugKeyExcludeRules),
|
log.Child(logutils.DebugKeyExcludeRules),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -301,7 +306,7 @@ func getExcludeRulesProcessor(cfg *config.Issues, log logutils.Log, lineCache *f
|
|||||||
return excludeRulesProcessor
|
return excludeRulesProcessor
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSeverityRulesProcessor(cfg *config.Severity, log logutils.Log, lineCache *fsutils.LineCache) processors.Processor {
|
func getSeverityRulesProcessor(cfg *config.Severity, log logutils.Log, files *fsutils.Files) processors.Processor {
|
||||||
var severityRules []processors.SeverityRule
|
var severityRules []processors.SeverityRule
|
||||||
for _, r := range cfg.Rules {
|
for _, r := range cfg.Rules {
|
||||||
severityRules = append(severityRules, processors.SeverityRule{
|
severityRules = append(severityRules, processors.SeverityRule{
|
||||||
@ -320,14 +325,14 @@ func getSeverityRulesProcessor(cfg *config.Severity, log logutils.Log, lineCache
|
|||||||
severityRulesProcessor = processors.NewSeverityRulesCaseSensitive(
|
severityRulesProcessor = processors.NewSeverityRulesCaseSensitive(
|
||||||
cfg.Default,
|
cfg.Default,
|
||||||
severityRules,
|
severityRules,
|
||||||
lineCache,
|
files,
|
||||||
log.Child(logutils.DebugKeySeverityRules),
|
log.Child(logutils.DebugKeySeverityRules),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
severityRulesProcessor = processors.NewSeverityRules(
|
severityRulesProcessor = processors.NewSeverityRules(
|
||||||
cfg.Default,
|
cfg.Default,
|
||||||
severityRules,
|
severityRules,
|
||||||
lineCache,
|
files,
|
||||||
log.Child(logutils.DebugKeySeverityRules),
|
log.Child(logutils.DebugKeySeverityRules),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -26,14 +26,14 @@ func (r *baseRule) isEmpty() bool {
|
|||||||
return r.text == nil && r.source == nil && r.path == nil && len(r.linters) == 0
|
return r.text == nil && r.source == nil && r.path == nil && len(r.linters) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *baseRule) match(issue *result.Issue, lineCache *fsutils.LineCache, log logutils.Log) bool {
|
func (r *baseRule) match(issue *result.Issue, files *fsutils.Files, log logutils.Log) bool {
|
||||||
if r.isEmpty() {
|
if r.isEmpty() {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if r.text != nil && !r.text.MatchString(issue.Text) {
|
if r.text != nil && !r.text.MatchString(issue.Text) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if r.path != nil && !r.path.MatchString(issue.FilePath()) {
|
if r.path != nil && !r.path.MatchString(files.WithPathPrefix(issue.FilePath())) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if len(r.linters) != 0 && !r.matchLinter(issue) {
|
if len(r.linters) != 0 && !r.matchLinter(issue) {
|
||||||
@ -41,7 +41,7 @@ func (r *baseRule) match(issue *result.Issue, lineCache *fsutils.LineCache, log
|
|||||||
}
|
}
|
||||||
|
|
||||||
// the most heavyweight checking last
|
// the most heavyweight checking last
|
||||||
if r.source != nil && !r.matchSource(issue, lineCache, log) {
|
if r.source != nil && !r.matchSource(issue, files.LineCache, log) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,13 +18,13 @@ type ExcludeRule struct {
|
|||||||
|
|
||||||
type ExcludeRules struct {
|
type ExcludeRules struct {
|
||||||
rules []excludeRule
|
rules []excludeRule
|
||||||
lineCache *fsutils.LineCache
|
files *fsutils.Files
|
||||||
log logutils.Log
|
log logutils.Log
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewExcludeRules(rules []ExcludeRule, lineCache *fsutils.LineCache, log logutils.Log) *ExcludeRules {
|
func NewExcludeRules(rules []ExcludeRule, files *fsutils.Files, log logutils.Log) *ExcludeRules {
|
||||||
r := &ExcludeRules{
|
r := &ExcludeRules{
|
||||||
lineCache: lineCache,
|
files: files,
|
||||||
log: log,
|
log: log,
|
||||||
}
|
}
|
||||||
r.rules = createRules(rules, "(?i)")
|
r.rules = createRules(rules, "(?i)")
|
||||||
@ -59,7 +59,7 @@ func (p ExcludeRules) Process(issues []result.Issue) ([]result.Issue, error) {
|
|||||||
return filterIssues(issues, func(i *result.Issue) bool {
|
return filterIssues(issues, func(i *result.Issue) bool {
|
||||||
for _, rule := range p.rules {
|
for _, rule := range p.rules {
|
||||||
rule := rule
|
rule := rule
|
||||||
if rule.match(i, p.lineCache, p.log) {
|
if rule.match(i, p.files, p.log) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -76,9 +76,9 @@ type ExcludeRulesCaseSensitive struct {
|
|||||||
*ExcludeRules
|
*ExcludeRules
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewExcludeRulesCaseSensitive(rules []ExcludeRule, lineCache *fsutils.LineCache, log logutils.Log) *ExcludeRulesCaseSensitive {
|
func NewExcludeRulesCaseSensitive(rules []ExcludeRule, files *fsutils.Files, log logutils.Log) *ExcludeRulesCaseSensitive {
|
||||||
r := &ExcludeRules{
|
r := &ExcludeRules{
|
||||||
lineCache: lineCache,
|
files: files,
|
||||||
log: log,
|
log: log,
|
||||||
}
|
}
|
||||||
r.rules = createRules(rules, "")
|
r.rules = createRules(rules, "")
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package processors
|
package processors
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -12,6 +13,8 @@ import (
|
|||||||
|
|
||||||
func TestExcludeRulesMultiple(t *testing.T) {
|
func TestExcludeRulesMultiple(t *testing.T) {
|
||||||
lineCache := fsutils.NewLineCache(fsutils.NewFileCache())
|
lineCache := fsutils.NewLineCache(fsutils.NewFileCache())
|
||||||
|
files := fsutils.NewFiles(lineCache, "")
|
||||||
|
|
||||||
p := NewExcludeRules([]ExcludeRule{
|
p := NewExcludeRules([]ExcludeRule{
|
||||||
{
|
{
|
||||||
BaseRule: BaseRule{
|
BaseRule: BaseRule{
|
||||||
@ -37,7 +40,7 @@ func TestExcludeRulesMultiple(t *testing.T) {
|
|||||||
Linters: []string{"lll"},
|
Linters: []string{"lll"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, lineCache, nil)
|
}, files, nil)
|
||||||
|
|
||||||
cases := []issueTestCase{
|
cases := []issueTestCase{
|
||||||
{Path: "e.go", Text: "exclude", Linter: "linter"},
|
{Path: "e.go", Text: "exclude", Linter: "linter"},
|
||||||
@ -70,6 +73,43 @@ func TestExcludeRulesMultiple(t *testing.T) {
|
|||||||
assert.Equal(t, expectedCases, resultingCases)
|
assert.Equal(t, expectedCases, resultingCases)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestExcludeRulesPathPrefix(t *testing.T) {
|
||||||
|
lineCache := fsutils.NewLineCache(fsutils.NewFileCache())
|
||||||
|
pathPrefix := path.Join("some", "dir")
|
||||||
|
files := fsutils.NewFiles(lineCache, pathPrefix)
|
||||||
|
|
||||||
|
p := NewExcludeRules([]ExcludeRule{
|
||||||
|
{
|
||||||
|
BaseRule: BaseRule{
|
||||||
|
Path: `some/dir/e\.go`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, files, nil)
|
||||||
|
|
||||||
|
cases := []issueTestCase{
|
||||||
|
{Path: "e.go"},
|
||||||
|
{Path: "other.go"},
|
||||||
|
}
|
||||||
|
var issues []result.Issue
|
||||||
|
for _, c := range cases {
|
||||||
|
issues = append(issues, newIssueFromIssueTestCase(c))
|
||||||
|
}
|
||||||
|
processedIssues := process(t, p, issues...)
|
||||||
|
var resultingCases []issueTestCase
|
||||||
|
for _, i := range processedIssues {
|
||||||
|
resultingCases = append(resultingCases, issueTestCase{
|
||||||
|
Path: i.FilePath(),
|
||||||
|
Linter: i.FromLinter,
|
||||||
|
Text: i.Text,
|
||||||
|
Line: i.Line(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
expectedCases := []issueTestCase{
|
||||||
|
{Path: "other.go"},
|
||||||
|
}
|
||||||
|
assert.Equal(t, expectedCases, resultingCases)
|
||||||
|
}
|
||||||
|
|
||||||
func TestExcludeRulesText(t *testing.T) {
|
func TestExcludeRulesText(t *testing.T) {
|
||||||
p := NewExcludeRules([]ExcludeRule{
|
p := NewExcludeRules([]ExcludeRule{
|
||||||
{
|
{
|
||||||
@ -104,6 +144,7 @@ func TestExcludeRulesEmpty(t *testing.T) {
|
|||||||
|
|
||||||
func TestExcludeRulesCaseSensitiveMultiple(t *testing.T) {
|
func TestExcludeRulesCaseSensitiveMultiple(t *testing.T) {
|
||||||
lineCache := fsutils.NewLineCache(fsutils.NewFileCache())
|
lineCache := fsutils.NewLineCache(fsutils.NewFileCache())
|
||||||
|
files := fsutils.NewFiles(lineCache, "")
|
||||||
p := NewExcludeRulesCaseSensitive([]ExcludeRule{
|
p := NewExcludeRulesCaseSensitive([]ExcludeRule{
|
||||||
{
|
{
|
||||||
BaseRule: BaseRule{
|
BaseRule: BaseRule{
|
||||||
@ -129,7 +170,7 @@ func TestExcludeRulesCaseSensitiveMultiple(t *testing.T) {
|
|||||||
Linters: []string{"lll"},
|
Linters: []string{"lll"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, lineCache, nil)
|
}, files, nil)
|
||||||
|
|
||||||
cases := []issueTestCase{
|
cases := []issueTestCase{
|
||||||
{Path: "e.go", Text: "exclude", Linter: "linter"},
|
{Path: "e.go", Text: "exclude", Linter: "linter"},
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
package processors
|
package processors
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"path/filepath"
|
"github.com/golangci/golangci-lint/pkg/fsutils"
|
||||||
|
|
||||||
"github.com/golangci/golangci-lint/pkg/result"
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -27,7 +26,7 @@ func (*PathPrefixer) Name() string {
|
|||||||
func (p *PathPrefixer) Process(issues []result.Issue) ([]result.Issue, error) {
|
func (p *PathPrefixer) Process(issues []result.Issue) ([]result.Issue, error) {
|
||||||
if p.prefix != "" {
|
if p.prefix != "" {
|
||||||
for i := range issues {
|
for i := range issues {
|
||||||
issues[i].Pos.Filename = filepath.Join(p.prefix, issues[i].Pos.Filename)
|
issues[i].Pos.Filename = fsutils.WithPathPrefix(p.prefix, issues[i].Pos.Filename)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return issues, nil
|
return issues, nil
|
||||||
|
@ -21,13 +21,13 @@ type SeverityRule struct {
|
|||||||
type SeverityRules struct {
|
type SeverityRules struct {
|
||||||
defaultSeverity string
|
defaultSeverity string
|
||||||
rules []severityRule
|
rules []severityRule
|
||||||
lineCache *fsutils.LineCache
|
files *fsutils.Files
|
||||||
log logutils.Log
|
log logutils.Log
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSeverityRules(defaultSeverity string, rules []SeverityRule, lineCache *fsutils.LineCache, log logutils.Log) *SeverityRules {
|
func NewSeverityRules(defaultSeverity string, rules []SeverityRule, files *fsutils.Files, log logutils.Log) *SeverityRules {
|
||||||
r := &SeverityRules{
|
r := &SeverityRules{
|
||||||
lineCache: lineCache,
|
files: files,
|
||||||
log: log,
|
log: log,
|
||||||
defaultSeverity: defaultSeverity,
|
defaultSeverity: defaultSeverity,
|
||||||
}
|
}
|
||||||
@ -70,7 +70,7 @@ func (p SeverityRules) Process(issues []result.Issue) ([]result.Issue, error) {
|
|||||||
ruleSeverity = rule.severity
|
ruleSeverity = rule.severity
|
||||||
}
|
}
|
||||||
|
|
||||||
if rule.match(i, p.lineCache, p.log) {
|
if rule.match(i, p.files, p.log) {
|
||||||
i.Severity = ruleSeverity
|
i.Severity = ruleSeverity
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
@ -90,9 +90,9 @@ type SeverityRulesCaseSensitive struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewSeverityRulesCaseSensitive(defaultSeverity string, rules []SeverityRule,
|
func NewSeverityRulesCaseSensitive(defaultSeverity string, rules []SeverityRule,
|
||||||
lineCache *fsutils.LineCache, log logutils.Log) *SeverityRulesCaseSensitive {
|
files *fsutils.Files, log logutils.Log) *SeverityRulesCaseSensitive {
|
||||||
r := &SeverityRules{
|
r := &SeverityRules{
|
||||||
lineCache: lineCache,
|
files: files,
|
||||||
log: log,
|
log: log,
|
||||||
defaultSeverity: defaultSeverity,
|
defaultSeverity: defaultSeverity,
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package processors
|
package processors
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -14,6 +15,7 @@ import (
|
|||||||
|
|
||||||
func TestSeverityRulesMultiple(t *testing.T) {
|
func TestSeverityRulesMultiple(t *testing.T) {
|
||||||
lineCache := fsutils.NewLineCache(fsutils.NewFileCache())
|
lineCache := fsutils.NewLineCache(fsutils.NewFileCache())
|
||||||
|
files := fsutils.NewFiles(lineCache, "")
|
||||||
log := report.NewLogWrapper(logutils.NewStderrLog(logutils.DebugKeyEmpty), &report.Data{})
|
log := report.NewLogWrapper(logutils.NewStderrLog(logutils.DebugKeyEmpty), &report.Data{})
|
||||||
p := NewSeverityRules("error", []SeverityRule{
|
p := NewSeverityRules("error", []SeverityRule{
|
||||||
{
|
{
|
||||||
@ -64,7 +66,7 @@ func TestSeverityRulesMultiple(t *testing.T) {
|
|||||||
{
|
{
|
||||||
Severity: "info",
|
Severity: "info",
|
||||||
},
|
},
|
||||||
}, lineCache, log)
|
}, files, log)
|
||||||
|
|
||||||
cases := []issueTestCase{
|
cases := []issueTestCase{
|
||||||
{Path: "ssl.go", Text: "ssl", Linter: "gosec"},
|
{Path: "ssl.go", Text: "ssl", Linter: "gosec"},
|
||||||
@ -104,6 +106,47 @@ func TestSeverityRulesMultiple(t *testing.T) {
|
|||||||
assert.Equal(t, expectedCases, resultingCases)
|
assert.Equal(t, expectedCases, resultingCases)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSeverityRulesPathPrefix(t *testing.T) {
|
||||||
|
lineCache := fsutils.NewLineCache(fsutils.NewFileCache())
|
||||||
|
pathPrefix := path.Join("some", "dir")
|
||||||
|
files := fsutils.NewFiles(lineCache, pathPrefix)
|
||||||
|
log := report.NewLogWrapper(logutils.NewStderrLog(logutils.DebugKeyEmpty), &report.Data{})
|
||||||
|
p := NewSeverityRules("error", []SeverityRule{
|
||||||
|
{
|
||||||
|
Severity: "info",
|
||||||
|
BaseRule: BaseRule{
|
||||||
|
Text: "some",
|
||||||
|
Path: `some/dir/e\.go`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, files, log)
|
||||||
|
|
||||||
|
cases := []issueTestCase{
|
||||||
|
{Path: "e.go", Text: "some", Linter: "linter"},
|
||||||
|
{Path: "other.go", Text: "some", Linter: "linter"},
|
||||||
|
}
|
||||||
|
var issues []result.Issue
|
||||||
|
for _, c := range cases {
|
||||||
|
issues = append(issues, newIssueFromIssueTestCase(c))
|
||||||
|
}
|
||||||
|
processedIssues := process(t, p, issues...)
|
||||||
|
var resultingCases []issueTestCase
|
||||||
|
for _, i := range processedIssues {
|
||||||
|
resultingCases = append(resultingCases, issueTestCase{
|
||||||
|
Path: i.FilePath(),
|
||||||
|
Linter: i.FromLinter,
|
||||||
|
Text: i.Text,
|
||||||
|
Line: i.Line(),
|
||||||
|
Severity: i.Severity,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
expectedCases := []issueTestCase{
|
||||||
|
{Path: "e.go", Text: "some", Linter: "linter", Severity: "info"},
|
||||||
|
{Path: "other.go", Text: "some", Linter: "linter", Severity: "error"},
|
||||||
|
}
|
||||||
|
assert.Equal(t, expectedCases, resultingCases)
|
||||||
|
}
|
||||||
|
|
||||||
func TestSeverityRulesText(t *testing.T) {
|
func TestSeverityRulesText(t *testing.T) {
|
||||||
p := NewSeverityRules("", []SeverityRule{
|
p := NewSeverityRules("", []SeverityRule{
|
||||||
{
|
{
|
||||||
@ -134,8 +177,9 @@ func TestSeverityRulesText(t *testing.T) {
|
|||||||
|
|
||||||
func TestSeverityRulesOnlyDefault(t *testing.T) {
|
func TestSeverityRulesOnlyDefault(t *testing.T) {
|
||||||
lineCache := fsutils.NewLineCache(fsutils.NewFileCache())
|
lineCache := fsutils.NewLineCache(fsutils.NewFileCache())
|
||||||
|
files := fsutils.NewFiles(lineCache, "")
|
||||||
log := report.NewLogWrapper(logutils.NewStderrLog(logutils.DebugKeyEmpty), &report.Data{})
|
log := report.NewLogWrapper(logutils.NewStderrLog(logutils.DebugKeyEmpty), &report.Data{})
|
||||||
p := NewSeverityRules("info", []SeverityRule{}, lineCache, log)
|
p := NewSeverityRules("info", []SeverityRule{}, files, log)
|
||||||
|
|
||||||
cases := []issueTestCase{
|
cases := []issueTestCase{
|
||||||
{Path: "ssl.go", Text: "ssl", Linter: "gosec"},
|
{Path: "ssl.go", Text: "ssl", Linter: "gosec"},
|
||||||
@ -169,6 +213,7 @@ func TestSeverityRulesEmpty(t *testing.T) {
|
|||||||
|
|
||||||
func TestSeverityRulesCaseSensitive(t *testing.T) {
|
func TestSeverityRulesCaseSensitive(t *testing.T) {
|
||||||
lineCache := fsutils.NewLineCache(fsutils.NewFileCache())
|
lineCache := fsutils.NewLineCache(fsutils.NewFileCache())
|
||||||
|
files := fsutils.NewFiles(lineCache, "")
|
||||||
p := NewSeverityRulesCaseSensitive("error", []SeverityRule{
|
p := NewSeverityRulesCaseSensitive("error", []SeverityRule{
|
||||||
{
|
{
|
||||||
Severity: "info",
|
Severity: "info",
|
||||||
@ -177,7 +222,7 @@ func TestSeverityRulesCaseSensitive(t *testing.T) {
|
|||||||
Linters: []string{"gosec", "someotherlinter"},
|
Linters: []string{"gosec", "someotherlinter"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, lineCache, nil)
|
}, files, nil)
|
||||||
|
|
||||||
cases := []issueTestCase{
|
cases := []issueTestCase{
|
||||||
{Path: "e.go", Text: "ssL", Linter: "gosec"},
|
{Path: "e.go", Text: "ssL", Linter: "gosec"},
|
||||||
|
@ -22,13 +22,14 @@ type SkipDirs struct {
|
|||||||
skippedDirs map[string]*skipStat
|
skippedDirs map[string]*skipStat
|
||||||
absArgsDirs []string
|
absArgsDirs []string
|
||||||
skippedDirsCache map[string]bool
|
skippedDirsCache map[string]bool
|
||||||
|
pathPrefix string
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ Processor = (*SkipDirs)(nil)
|
var _ Processor = (*SkipDirs)(nil)
|
||||||
|
|
||||||
const goFileSuffix = ".go"
|
const goFileSuffix = ".go"
|
||||||
|
|
||||||
func NewSkipDirs(patterns []string, log logutils.Log, runArgs []string) (*SkipDirs, error) {
|
func NewSkipDirs(patterns []string, log logutils.Log, runArgs []string, pathPrefix string) (*SkipDirs, error) {
|
||||||
var patternsRe []*regexp.Regexp
|
var patternsRe []*regexp.Regexp
|
||||||
for _, p := range patterns {
|
for _, p := range patterns {
|
||||||
p = fsutils.NormalizePathInRegex(p)
|
p = fsutils.NormalizePathInRegex(p)
|
||||||
@ -62,6 +63,7 @@ func NewSkipDirs(patterns []string, log logutils.Log, runArgs []string) (*SkipDi
|
|||||||
skippedDirs: map[string]*skipStat{},
|
skippedDirs: map[string]*skipStat{},
|
||||||
absArgsDirs: absArgsDirs,
|
absArgsDirs: absArgsDirs,
|
||||||
skippedDirsCache: map[string]bool{},
|
skippedDirsCache: map[string]bool{},
|
||||||
|
pathPrefix: pathPrefix,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,8 +122,9 @@ func (p *SkipDirs) shouldPassIssueDirs(issueRelDir, issueAbsDir string) bool {
|
|||||||
// The alternative solution is to find relative to args path, but it has
|
// The alternative solution is to find relative to args path, but it has
|
||||||
// disadvantages (https://github.com/golangci/golangci-lint/pull/313).
|
// disadvantages (https://github.com/golangci/golangci-lint/pull/313).
|
||||||
|
|
||||||
|
path := fsutils.WithPathPrefix(p.pathPrefix, issueRelDir)
|
||||||
for _, pattern := range p.patterns {
|
for _, pattern := range p.patterns {
|
||||||
if pattern.MatchString(issueRelDir) {
|
if pattern.MatchString(path) {
|
||||||
ps := pattern.String()
|
ps := pattern.String()
|
||||||
if p.skippedDirs[issueRelDir] == nil {
|
if p.skippedDirs[issueRelDir] == nil {
|
||||||
p.skippedDirs[issueRelDir] = &skipStat{
|
p.skippedDirs[issueRelDir] = &skipStat{
|
||||||
|
@ -10,11 +10,12 @@ import (
|
|||||||
|
|
||||||
type SkipFiles struct {
|
type SkipFiles struct {
|
||||||
patterns []*regexp.Regexp
|
patterns []*regexp.Regexp
|
||||||
|
pathPrefix string
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ Processor = (*SkipFiles)(nil)
|
var _ Processor = (*SkipFiles)(nil)
|
||||||
|
|
||||||
func NewSkipFiles(patterns []string) (*SkipFiles, error) {
|
func NewSkipFiles(patterns []string, pathPrefix string) (*SkipFiles, error) {
|
||||||
var patternsRe []*regexp.Regexp
|
var patternsRe []*regexp.Regexp
|
||||||
for _, p := range patterns {
|
for _, p := range patterns {
|
||||||
p = fsutils.NormalizePathInRegex(p)
|
p = fsutils.NormalizePathInRegex(p)
|
||||||
@ -27,6 +28,7 @@ func NewSkipFiles(patterns []string) (*SkipFiles, error) {
|
|||||||
|
|
||||||
return &SkipFiles{
|
return &SkipFiles{
|
||||||
patterns: patternsRe,
|
patterns: patternsRe,
|
||||||
|
pathPrefix: pathPrefix,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,8 +42,9 @@ func (p SkipFiles) Process(issues []result.Issue) ([]result.Issue, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return filterIssues(issues, func(i *result.Issue) bool {
|
return filterIssues(issues, func(i *result.Issue) bool {
|
||||||
for _, p := range p.patterns {
|
path := fsutils.WithPathPrefix(p.pathPrefix, i.FilePath())
|
||||||
if p.MatchString(i.FilePath()) {
|
for _, pattern := range p.patterns {
|
||||||
|
if pattern.MatchString(path) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ func newFileIssue(file string) result.Issue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newTestSkipFiles(t *testing.T, patterns ...string) *SkipFiles {
|
func newTestSkipFiles(t *testing.T, patterns ...string) *SkipFiles {
|
||||||
p, err := NewSkipFiles(patterns)
|
p, err := NewSkipFiles(patterns, "")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
@ -47,7 +47,7 @@ func TestSkipFiles(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSkipFilesInvalidPattern(t *testing.T) {
|
func TestSkipFilesInvalidPattern(t *testing.T) {
|
||||||
p, err := NewSkipFiles([]string{"\\o"})
|
p, err := NewSkipFiles([]string{"\\o"}, "")
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.Nil(t, p)
|
assert.Nil(t, p)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user