mirror of
https://github.com/scratchfoundation/golangci-lint.git
synced 2025-08-28 22:28:43 -04:00
parent
af77076207
commit
8a9b3a5143
7 changed files with 263 additions and 44 deletions
|
@ -6,6 +6,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/golangci/golangci-lint/pkg/result"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func newNolintFileIssue(line int, fromLinter string) result.Issue {
|
||||
|
@ -20,6 +21,8 @@ func newNolintFileIssue(line int, fromLinter string) result.Issue {
|
|||
|
||||
func TestNolint(t *testing.T) {
|
||||
p := NewNolint(token.NewFileSet())
|
||||
|
||||
// test inline comments
|
||||
processAssertEmpty(t, p, newNolintFileIssue(3, "gofmt"))
|
||||
processAssertEmpty(t, p, newNolintFileIssue(3, "gofmt")) // check cached is ok
|
||||
processAssertSame(t, p, newNolintFileIssue(3, "gofmtA")) // check different name
|
||||
|
@ -35,6 +38,42 @@ func TestNolint(t *testing.T) {
|
|||
processAssertEmpty(t, p, newNolintFileIssue(7, "any"))
|
||||
|
||||
processAssertSame(t, p, newNolintFileIssue(1, "golint")) // no directive
|
||||
|
||||
// test preceding comments
|
||||
processAssertEmpty(t, p, newNolintFileIssue(10, "any")) // preceding comment for var
|
||||
processAssertEmpty(t, p, newNolintFileIssue(9, "any")) // preceding comment for var itself
|
||||
|
||||
processAssertSame(t, p, newNolintFileIssue(14, "any")) // preceding comment with extra \n
|
||||
processAssertEmpty(t, p, newNolintFileIssue(12, "any")) // preceding comment with extra \n itself
|
||||
|
||||
processAssertSame(t, p, newNolintFileIssue(17, "any")) // preceding comment on different column
|
||||
processAssertEmpty(t, p, newNolintFileIssue(16, "any")) // preceding comment on different column itself
|
||||
|
||||
// preceding comment for func name and comment itself
|
||||
for i := 19; i <= 23; i++ {
|
||||
processAssertEmpty(t, p, newNolintFileIssue(i, "any"))
|
||||
}
|
||||
|
||||
processAssertSame(t, p, newNolintFileIssue(24, "any")) // right after func
|
||||
|
||||
// preceding multiline comment: last line
|
||||
for i := 25; i <= 30; i++ {
|
||||
processAssertEmpty(t, p, newNolintFileIssue(i, "any"))
|
||||
}
|
||||
|
||||
processAssertSame(t, p, newNolintFileIssue(31, "any")) // between funcs
|
||||
|
||||
// preceding multiline comment: first line
|
||||
for i := 32; i <= 37; i++ {
|
||||
processAssertEmpty(t, p, newNolintFileIssue(i, "any"))
|
||||
}
|
||||
|
||||
processAssertSame(t, p, newNolintFileIssue(38, "any")) // between funcs
|
||||
|
||||
// preceding multiline comment: medium line
|
||||
for i := 39; i <= 45; i++ {
|
||||
processAssertEmpty(t, p, newNolintFileIssue(i, "any"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoIssuesInAutogeneratedFiles(t *testing.T) {
|
||||
|
@ -56,3 +95,62 @@ func TestNoIssuesInAutogeneratedFiles(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIgnoredRangeMatches(t *testing.T) {
|
||||
var testcases = []struct {
|
||||
doc string
|
||||
issue result.Issue
|
||||
linters []string
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
doc: "unmatched line",
|
||||
issue: result.Issue{
|
||||
Pos: token.Position{
|
||||
Line: 100,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
doc: "matched line, all linters",
|
||||
issue: result.Issue{
|
||||
Pos: token.Position{
|
||||
Line: 5,
|
||||
},
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
doc: "matched line, unmatched linter",
|
||||
issue: result.Issue{
|
||||
Pos: token.Position{
|
||||
Line: 5,
|
||||
},
|
||||
},
|
||||
linters: []string{"vet"},
|
||||
},
|
||||
{
|
||||
doc: "matched line and linters",
|
||||
issue: result.Issue{
|
||||
Pos: token.Position{
|
||||
Line: 20,
|
||||
},
|
||||
FromLinter: "vet",
|
||||
},
|
||||
linters: []string{"vet"},
|
||||
expected: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testcase := range testcases {
|
||||
ir := ignoredRange{
|
||||
col: 20,
|
||||
Range: result.Range{
|
||||
From: 5,
|
||||
To: 20,
|
||||
},
|
||||
linters: testcase.linters,
|
||||
}
|
||||
assert.Equal(t, testcase.expected, ir.doesMatch(&testcase.issue), testcase.doc)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue