github.com/ldez/golangci-lint@v1.10.1/pkg/result/processors/nolint_test.go (about) 1 package processors 2 3 import ( 4 "go/token" 5 "path/filepath" 6 "testing" 7 8 "github.com/golangci/golangci-lint/pkg/lint/astcache" 9 "github.com/golangci/golangci-lint/pkg/logutils" 10 "github.com/golangci/golangci-lint/pkg/result" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func newNolintFileIssue(line int, fromLinter string) result.Issue { 15 return result.Issue{ 16 Pos: token.Position{ 17 Filename: filepath.Join("testdata", "nolint.go"), 18 Line: line, 19 }, 20 FromLinter: fromLinter, 21 } 22 } 23 24 func newNolint2FileIssue(line int) result.Issue { 25 i := newNolintFileIssue(line, "errcheck") 26 i.Pos.Filename = filepath.Join("testdata", "nolint2.go") 27 return i 28 } 29 30 func TestNolint(t *testing.T) { 31 p := NewNolint(astcache.NewCache(logutils.NewStderrLog(""))) 32 33 // test inline comments 34 processAssertEmpty(t, p, newNolintFileIssue(3, "gofmt")) 35 processAssertEmpty(t, p, newNolintFileIssue(3, "gofmt")) // check cached is ok 36 processAssertSame(t, p, newNolintFileIssue(3, "gofmtA")) // check different name 37 38 processAssertEmpty(t, p, newNolintFileIssue(4, "gofmt")) 39 processAssertSame(t, p, newNolintFileIssue(4, "gofmtA")) // check different name 40 41 processAssertEmpty(t, p, newNolintFileIssue(5, "gofmt")) 42 processAssertEmpty(t, p, newNolintFileIssue(5, "govet")) 43 processAssertSame(t, p, newNolintFileIssue(5, "gofmtA")) // check different name 44 45 processAssertEmpty(t, p, newNolintFileIssue(6, "any")) 46 processAssertEmpty(t, p, newNolintFileIssue(7, "any")) 47 48 processAssertSame(t, p, newNolintFileIssue(1, "golint")) // no directive 49 50 // test preceding comments 51 processAssertEmpty(t, p, newNolintFileIssue(10, "any")) // preceding comment for var 52 processAssertEmpty(t, p, newNolintFileIssue(9, "any")) // preceding comment for var itself 53 54 processAssertSame(t, p, newNolintFileIssue(14, "any")) // preceding comment with extra \n 55 processAssertEmpty(t, p, newNolintFileIssue(12, "any")) // preceding comment with extra \n itself 56 57 processAssertSame(t, p, newNolintFileIssue(17, "any")) // preceding comment on different column 58 processAssertEmpty(t, p, newNolintFileIssue(16, "any")) // preceding comment on different column itself 59 60 // preceding comment for func name and comment itself 61 for i := 19; i <= 23; i++ { 62 processAssertEmpty(t, p, newNolintFileIssue(i, "any")) 63 } 64 65 processAssertSame(t, p, newNolintFileIssue(24, "any")) // right after func 66 67 // preceding multiline comment: last line 68 for i := 25; i <= 30; i++ { 69 processAssertEmpty(t, p, newNolintFileIssue(i, "any")) 70 } 71 72 processAssertSame(t, p, newNolintFileIssue(31, "any")) // between funcs 73 74 // preceding multiline comment: first line 75 for i := 32; i <= 37; i++ { 76 processAssertEmpty(t, p, newNolintFileIssue(i, "any")) 77 } 78 79 processAssertSame(t, p, newNolintFileIssue(38, "any")) // between funcs 80 81 // preceding multiline comment: medium line 82 for i := 39; i <= 45; i++ { 83 processAssertEmpty(t, p, newNolintFileIssue(i, "any")) 84 } 85 86 // check bug with transitive expanding for next and next line 87 for i := 1; i <= 8; i++ { 88 processAssertSame(t, p, newNolint2FileIssue(i)) 89 } 90 for i := 9; i <= 10; i++ { 91 processAssertEmpty(t, p, newNolint2FileIssue(i)) 92 } 93 94 // check inline comment for function 95 for i := 11; i <= 13; i++ { 96 processAssertSame(t, p, newNolint2FileIssue(i)) 97 } 98 processAssertEmpty(t, p, newNolint2FileIssue(14)) 99 for i := 15; i <= 18; i++ { 100 processAssertSame(t, p, newNolint2FileIssue(i)) 101 } 102 103 } 104 105 func TestIgnoredRangeMatches(t *testing.T) { 106 var testcases = []struct { 107 doc string 108 issue result.Issue 109 linters []string 110 expected bool 111 }{ 112 { 113 doc: "unmatched line", 114 issue: result.Issue{ 115 Pos: token.Position{ 116 Line: 100, 117 }, 118 }, 119 }, 120 { 121 doc: "matched line, all linters", 122 issue: result.Issue{ 123 Pos: token.Position{ 124 Line: 5, 125 }, 126 }, 127 expected: true, 128 }, 129 { 130 doc: "matched line, unmatched linter", 131 issue: result.Issue{ 132 Pos: token.Position{ 133 Line: 5, 134 }, 135 }, 136 linters: []string{"vet"}, 137 }, 138 { 139 doc: "matched line and linters", 140 issue: result.Issue{ 141 Pos: token.Position{ 142 Line: 20, 143 }, 144 FromLinter: "vet", 145 }, 146 linters: []string{"vet"}, 147 expected: true, 148 }, 149 } 150 151 for _, testcase := range testcases { 152 ir := ignoredRange{ 153 col: 20, 154 Range: result.Range{ 155 From: 5, 156 To: 20, 157 }, 158 linters: testcase.linters, 159 } 160 assert.Equal(t, testcase.expected, ir.doesMatch(&testcase.issue), testcase.doc) 161 } 162 }