You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pkg/golinters/nolintlint/nolintlint_test.go
+134-20
Original file line number
Diff line number
Diff line change
@@ -7,16 +7,22 @@ import (
7
7
8
8
"github.com/stretchr/testify/assert"
9
9
"github.com/stretchr/testify/require"
10
+
11
+
"github.com/golangci/golangci-lint/pkg/result"
10
12
)
11
13
12
14
//nolint:funlen
13
15
funcTestNoLintLint(t*testing.T) {
16
+
typeissueWithReplacementstruct {
17
+
issuestring
18
+
replacement*result.Replacement
19
+
}
14
20
testCases:= []struct {
15
21
descstring
16
22
needsNeeds
17
23
excludes []string
18
24
contentsstring
19
-
expected []string
25
+
expected []issueWithReplacement
20
26
}{
21
27
{
22
28
desc: "when no explanation is provided",
@@ -33,11 +39,11 @@ func foo() {
33
39
good() //nolint // this is ok
34
40
other() //nolintother
35
41
}`,
36
-
expected: []string{
37
-
"directive `//nolint` should provide explanation such as `//nolint // this is why` at testing.go:5:1",
38
-
"directive `//nolint` should provide explanation such as `//nolint // this is why` at testing.go:7:9",
39
-
"directive `//nolint //` should provide explanation such as `//nolint // this is why` at testing.go:8:9",
40
-
"directive `//nolint // ` should provide explanation such as `//nolint // this is why` at testing.go:9:9",
42
+
expected: []issueWithReplacement{
43
+
{"directive `//nolint` should provide explanation such as `//nolint // this is why` at testing.go:5:1", nil},
44
+
{"directive `//nolint` should provide explanation such as `//nolint // this is why` at testing.go:7:9", nil},
45
+
{"directive `//nolint //` should provide explanation such as `//nolint // this is why` at testing.go:8:9", nil},
46
+
{"directive `//nolint // ` should provide explanation such as `//nolint // this is why` at testing.go:9:9", nil},
41
47
},
42
48
},
43
49
{
@@ -50,8 +56,8 @@ package bar
50
56
//nolint // this is ok
51
57
//nolint:dupl
52
58
func foo() {}`,
53
-
expected: []string{
54
-
"directive `//nolint:dupl` should provide explanation such as `//nolint:dupl // this is why` at testing.go:6:1",
59
+
expected: []issueWithReplacement{
60
+
{"directive `//nolint:dupl` should provide explanation such as `//nolint:dupl // this is why` at testing.go:6:1", nil},
55
61
},
56
62
},
57
63
{
@@ -76,9 +82,9 @@ func foo() {
76
82
bad() //nolint
77
83
bad() // nolint // because
78
84
}`,
79
-
expected: []string{
80
-
"directive `//nolint` should mention specific linter such as `//nolint:my-linter` at testing.go:6:9",
81
-
"directive `// nolint // because` should mention specific linter such as `// nolint:my-linter` at testing.go:7:9",
85
+
expected: []issueWithReplacement{
86
+
{"directive `//nolint` should mention specific linter such as `//nolint:my-linter` at testing.go:6:9", nil},
87
+
{"directive `// nolint // because` should mention specific linter such as `// nolint:my-linter` at testing.go:7:9", nil},
82
88
},
83
89
},
84
90
{
@@ -91,8 +97,17 @@ func foo() {
91
97
bad() // nolint
92
98
good() //nolint
93
99
}`,
94
-
expected: []string{
95
-
"directive `// nolint` should be written without leading space as `//nolint` at testing.go:5:9",
100
+
expected: []issueWithReplacement{
101
+
{
102
+
"directive `// nolint` should be written without leading space as `//nolint` at testing.go:5:9",
103
+
&result.Replacement{
104
+
Inline: &result.InlineFix{
105
+
StartCol: 8,
106
+
Length: 3,
107
+
NewString: "//",
108
+
},
109
+
},
110
+
},
96
111
},
97
112
},
98
113
{
@@ -104,8 +119,17 @@ func foo() {
104
119
bad() // nolint
105
120
good() // nolint
106
121
}`,
107
-
expected: []string{
108
-
"directive `// nolint` should not have more than one leading space at testing.go:5:9",
122
+
expected: []issueWithReplacement{
123
+
{
124
+
"directive `// nolint` should not have more than one leading space at testing.go:5:9",
125
+
&result.Replacement{
126
+
Inline: &result.InlineFix{
127
+
StartCol: 8,
128
+
Length: 4,
129
+
NewString: "//",
130
+
},
131
+
},
132
+
},
109
133
},
110
134
},
111
135
{
@@ -119,8 +143,8 @@ func foo() {
119
143
good() // nolint: linter1,linter2
120
144
good() // nolint: linter1, linter2
121
145
}`,
122
-
expected: []string{
123
-
"directive `// nolint:linter1 linter2` should match `// nolint[:<comma-separated-linters>] [// <explanation>]` at testing.go:6:9", //nolint:lll // this is a string
146
+
expected: []issueWithReplacement{
147
+
{"directive `// nolint:linter1 linter2` should match `// nolint[:<comma-separated-linters>] [// <explanation>]` at testing.go:6:9", nil}, //nolint:lll // this is a string
124
148
},
125
149
},
126
150
{
@@ -133,6 +157,92 @@ func foo() {
133
157
// something else
134
158
}`,
135
159
},
160
+
{
161
+
desc: "needs unused without specific linter generates replacement",
162
+
needs: NeedsUnused,
163
+
contents: `
164
+
package bar
165
+
166
+
func foo() {
167
+
bad() //nolint
168
+
}`,
169
+
expected: []issueWithReplacement{
170
+
{
171
+
"directive `//nolint` is unused at testing.go:5:9",
172
+
&result.Replacement{
173
+
Inline: &result.InlineFix{
174
+
StartCol: 8,
175
+
Length: 8,
176
+
NewString: "",
177
+
},
178
+
},
179
+
},
180
+
},
181
+
},
182
+
{
183
+
desc: "needs unused with one specific linter generates replacement",
184
+
needs: NeedsUnused,
185
+
contents: `
186
+
package bar
187
+
188
+
func foo() {
189
+
bad() //nolint:somelinter
190
+
}`,
191
+
expected: []issueWithReplacement{
192
+
{
193
+
"directive `//nolint:somelinter` is unused for linter \"somelinter\" at testing.go:5:9",
194
+
&result.Replacement{
195
+
Inline: &result.InlineFix{
196
+
StartCol: 8,
197
+
Length: 19,
198
+
NewString: "",
199
+
},
200
+
},
201
+
},
202
+
},
203
+
},
204
+
{
205
+
desc: "needs unused with multiple specific linter generates replacement for each linter",
206
+
needs: NeedsUnused,
207
+
contents: `
208
+
package bar
209
+
210
+
func foo() {
211
+
bad() //nolint:linter1,linter2,linter3
212
+
}`,
213
+
expected: []issueWithReplacement{
214
+
{
215
+
"directive `//nolint:linter1,linter2,linter3` is unused for linter \"linter1\" at testing.go:5:9",
216
+
&result.Replacement{
217
+
Inline: &result.InlineFix{
218
+
StartCol: 17,
219
+
Length: 22,
220
+
NewString: "linter2,linter3",
221
+
},
222
+
},
223
+
},
224
+
{
225
+
"directive `//nolint:linter1,linter2,linter3` is unused for linter \"linter2\" at testing.go:5:9",
226
+
&result.Replacement{
227
+
Inline: &result.InlineFix{
228
+
StartCol: 17,
229
+
Length: 22,
230
+
NewString: "linter1,linter3",
231
+
},
232
+
},
233
+
},
234
+
{
235
+
"directive `//nolint:linter1,linter2,linter3` is unused for linter \"linter3\" at testing.go:5:9",
0 commit comments