-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: filter invalid issues before other processors (#4552)
- Loading branch information
Showing
7 changed files
with
174 additions
and
59 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
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
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,54 @@ | ||
package processors | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/golangci/golangci-lint/pkg/logutils" | ||
"github.com/golangci/golangci-lint/pkg/result" | ||
) | ||
|
||
var _ Processor = InvalidIssue{} | ||
|
||
type InvalidIssue struct { | ||
log logutils.Log | ||
} | ||
|
||
func NewInvalidIssue(log logutils.Log) *InvalidIssue { | ||
return &InvalidIssue{log: log} | ||
} | ||
|
||
func (p InvalidIssue) Process(issues []result.Issue) ([]result.Issue, error) { | ||
return filterIssuesErr(issues, p.shouldPassIssue) | ||
} | ||
|
||
func (p InvalidIssue) Name() string { | ||
return "invalid_issue" | ||
} | ||
|
||
func (p InvalidIssue) Finish() {} | ||
|
||
func (p InvalidIssue) shouldPassIssue(issue *result.Issue) (bool, error) { | ||
if issue.FromLinter == "typecheck" { | ||
return true, nil | ||
} | ||
|
||
if issue.FilePath() == "" { | ||
// contextcheck has a known bug https://github.com/kkHAIKE/contextcheck/issues/21 | ||
if issue.FromLinter != "contextcheck" { | ||
p.log.Warnf("no file path for the issue: probably a bug inside the linter %q: %#v", issue.FromLinter, issue) | ||
} | ||
|
||
return false, nil | ||
} | ||
|
||
if filepath.Base(issue.FilePath()) == "go.mod" { | ||
return true, nil | ||
} | ||
|
||
if !isGoFile(issue.FilePath()) { | ||
p.log.Infof("issue related to file %s is skipped", issue.FilePath()) | ||
return false, nil | ||
} | ||
|
||
return true, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package processors | ||
|
||
import ( | ||
"go/token" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/golangci/golangci-lint/pkg/logutils" | ||
"github.com/golangci/golangci-lint/pkg/result" | ||
) | ||
|
||
func TestInvalidIssue_Process(t *testing.T) { | ||
logger := logutils.NewStderrLog(logutils.DebugKeyInvalidIssue) | ||
logger.SetLevel(logutils.LogLevelDebug) | ||
|
||
p := NewInvalidIssue(logger) | ||
|
||
testCases := []struct { | ||
desc string | ||
issues []result.Issue | ||
expected []result.Issue | ||
}{ | ||
{ | ||
desc: "typecheck", | ||
issues: []result.Issue{ | ||
{FromLinter: "typecheck"}, | ||
}, | ||
expected: []result.Issue{ | ||
{FromLinter: "typecheck"}, | ||
}, | ||
}, | ||
{ | ||
desc: "Go file", | ||
issues: []result.Issue{ | ||
{ | ||
FromLinter: "example", | ||
Pos: token.Position{ | ||
Filename: "test.go", | ||
}, | ||
}, | ||
}, | ||
expected: []result.Issue{ | ||
{ | ||
FromLinter: "example", | ||
Pos: token.Position{ | ||
Filename: "test.go", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
desc: "go.mod", | ||
issues: []result.Issue{ | ||
{ | ||
FromLinter: "example", | ||
Pos: token.Position{ | ||
Filename: "go.mod", | ||
}, | ||
}, | ||
}, | ||
expected: []result.Issue{ | ||
{ | ||
FromLinter: "example", | ||
Pos: token.Position{ | ||
Filename: "go.mod", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
desc: "non Go file", | ||
issues: []result.Issue{ | ||
{ | ||
FromLinter: "example", | ||
Pos: token.Position{ | ||
Filename: "test.txt", | ||
}, | ||
}, | ||
}, | ||
expected: []result.Issue{}, | ||
}, | ||
{ | ||
desc: "no filename", | ||
issues: []result.Issue{ | ||
{ | ||
FromLinter: "example", | ||
Pos: token.Position{ | ||
Filename: "", | ||
}, | ||
}, | ||
}, | ||
expected: []result.Issue{}, | ||
}, | ||
} | ||
|
||
for _, test := range testCases { | ||
test := test | ||
t.Run(test.desc, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
after, err := p.Process(test.issues) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, test.expected, after) | ||
}) | ||
} | ||
} |
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