-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
nolint_test.go
347 lines (294 loc) · 9.56 KB
/
nolint_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package processors
import (
"fmt"
"go/token"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters"
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/result"
)
func newNolintFileIssue(line int, fromLinter string) result.Issue {
return result.Issue{
Pos: token.Position{
Filename: filepath.Join("testdata", "nolint.go"),
Line: line,
},
FromLinter: fromLinter,
}
}
func newNolint2FileIssue(line int) result.Issue {
i := newNolintFileIssue(line, "errcheck")
i.Pos.Filename = filepath.Join("testdata", "nolint2.go")
return i
}
func newTestNolintProcessor(log logutils.Log) *Nolint {
return NewNolint(log, lintersdb.NewManager(nil, nil), nil)
}
func getMockLog() *logutils.MockLog {
log := logutils.NewMockLog()
log.On("Infof", mock.Anything, mock.Anything).Maybe()
return log
}
func TestNolint(t *testing.T) {
p := newTestNolintProcessor(getMockLog())
defer p.Finish()
// 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
processAssertEmpty(t, p, newNolintFileIssue(4, "gofmt"))
processAssertSame(t, p, newNolintFileIssue(4, "gofmtA")) // check different name
processAssertEmpty(t, p, newNolintFileIssue(5, "gofmt"))
processAssertEmpty(t, p, newNolintFileIssue(5, "govet"))
processAssertSame(t, p, newNolintFileIssue(5, "gofmtA")) // check different name
processAssertEmpty(t, p, newNolintFileIssue(6, "any"))
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"))
}
// check bug with transitive expanding for next and next line
for i := 1; i <= 8; i++ {
processAssertSame(t, p, newNolint2FileIssue(i))
}
for i := 9; i <= 10; i++ {
processAssertEmpty(t, p, newNolint2FileIssue(i))
}
// check inline comment for function
for i := 11; i <= 13; i++ {
processAssertSame(t, p, newNolint2FileIssue(i))
}
processAssertEmpty(t, p, newNolint2FileIssue(14))
for i := 15; i <= 18; i++ {
processAssertSame(t, p, newNolint2FileIssue(i))
}
// variables block exclude
for i := 55; i <= 56; i++ {
processAssertSame(t, p, newNolint2FileIssue(i))
}
}
func TestNolintInvalidLinterName(t *testing.T) {
fileName := filepath.Join("testdata", "nolint_bad_names.go")
issues := []result.Issue{
{
Pos: token.Position{
Filename: fileName,
Line: 3,
},
FromLinter: "varcheck",
},
{
Pos: token.Position{
Filename: fileName,
Line: 6,
},
FromLinter: "deadcode",
},
}
log := getMockLog()
log.On("Warnf", "Found unknown linters in //nolint directives: %s", "bad1, bad2")
p := newTestNolintProcessor(log)
processAssertEmpty(t, p, issues...)
p.Finish()
}
func TestNolintInvalidLinterNameWithViolationOnTheSameLine(t *testing.T) {
log := getMockLog()
log.On("Warnf", "Found unknown linters in //nolint directives: %s", "foobar")
issues := []result.Issue{
{
Pos: token.Position{
Filename: filepath.Join("testdata", "nolint_apply_to_unknown.go"),
Line: 4,
},
FromLinter: "gofmt",
},
}
p := newTestNolintProcessor(log)
processedIssues, err := p.Process(issues)
p.Finish()
assert.Len(t, processedIssues, 1)
assert.Equal(t, issues, processedIssues)
assert.NoError(t, err)
}
func TestNolintAliases(t *testing.T) {
p := newTestNolintProcessor(getMockLog())
for _, line := range []int{47, 49, 51} {
line := line
t.Run(fmt.Sprintf("line-%d", line), func(t *testing.T) {
processAssertEmpty(t, p, newNolintFileIssue(line, "gosec"))
})
}
p.Finish()
}
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)
}
}
func TestNolintWholeFile(t *testing.T) {
fileName := filepath.Join("testdata", "nolint_whole_file.go")
p := newTestNolintProcessor(nil)
defer p.Finish()
processAssertEmpty(t, p, result.Issue{
Pos: token.Position{
Filename: fileName,
Line: 4,
},
FromLinter: "varcheck",
})
processAssertSame(t, p, result.Issue{
Pos: token.Position{
Filename: fileName,
Line: 4,
},
FromLinter: "deadcode",
})
}
func TestNolintUnused(t *testing.T) {
fileName := filepath.Join("testdata", "nolint_unused.go")
log := getMockLog()
log.On("Warnf", "Found unknown linters in //nolint directives: %s", "blah")
createProcessor := func(t *testing.T, log *logutils.MockLog, enabledLinters []string) *Nolint {
enabledSetLog := logutils.NewMockLog()
enabledSetLog.On("Infof", "Active %d linters: %s", len(enabledLinters), enabledLinters)
cfg := &config.Config{Linters: config.Linters{DisableAll: true, Enable: enabledLinters}}
dbManager := lintersdb.NewManager(cfg, nil)
enabledLintersSet := lintersdb.NewEnabledSet(dbManager, lintersdb.NewValidator(dbManager), enabledSetLog, cfg)
enabledLintersMap, err := enabledLintersSet.GetEnabledLintersMap()
assert.NoError(t, err)
return NewNolint(log, dbManager, enabledLintersMap)
}
// the issue below is the nolintlint issue that would be generated for the test file
nolintlintIssueVarcheck := result.Issue{
Pos: token.Position{
Filename: fileName,
Line: 3,
},
FromLinter: golinters.NolintlintName,
ExpectNoLint: true,
ExpectedNoLintLinter: "varcheck",
}
// the issue below is another nolintlint issue that would be generated for the test file
nolintlintIssueVarcheckUnusedOK := result.Issue{
Pos: token.Position{
Filename: fileName,
Line: 5,
},
FromLinter: golinters.NolintlintName,
ExpectNoLint: true,
ExpectedNoLintLinter: "varcheck",
}
t.Run("when an issue does not occur, it is not removed from the nolintlint issues", func(t *testing.T) {
p := createProcessor(t, log, []string{"nolintlint", "varcheck"})
defer p.Finish()
processAssertSame(t, p, nolintlintIssueVarcheck)
})
t.Run("when an issue does not occur but nolintlint is nolinted, it is removed from the nolintlint issues", func(t *testing.T) {
p := createProcessor(t, log, []string{"nolintlint", "varcheck"})
defer p.Finish()
processAssertEmpty(t, p, nolintlintIssueVarcheckUnusedOK)
})
t.Run("when an issue occurs, it is removed from the nolintlint issues", func(t *testing.T) {
p := createProcessor(t, log, []string{"nolintlint", "varcheck"})
defer p.Finish()
processAssertEmpty(t, p, []result.Issue{{
Pos: token.Position{
Filename: fileName,
Line: 3,
},
FromLinter: "varcheck",
}, nolintlintIssueVarcheck}...)
})
t.Run("when a linter is not enabled, it is removed from the nolintlint unused issues", func(t *testing.T) {
enabledSetLog := logutils.NewMockLog()
enabledSetLog.On("Infof", "Active %d linters: %s", 1, []string{"nolintlint"})
cfg := &config.Config{Linters: config.Linters{DisableAll: true, Enable: []string{"nolintlint"}}}
dbManager := lintersdb.NewManager(cfg, nil)
enabledLintersSet := lintersdb.NewEnabledSet(dbManager, lintersdb.NewValidator(dbManager), enabledSetLog, cfg)
enabledLintersMap, err := enabledLintersSet.GetEnabledLintersMap()
assert.NoError(t, err)
p := NewNolint(log, dbManager, enabledLintersMap)
defer p.Finish()
processAssertEmpty(t, p, nolintlintIssueVarcheck)
})
}