1
1
package processors
2
2
3
3
import (
4
+ "go/token"
4
5
"path/filepath"
5
6
"testing"
6
7
7
8
"github.com/stretchr/testify/assert"
8
9
"github.com/stretchr/testify/require"
10
+
11
+ "github.com/golangci/golangci-lint/pkg/result"
9
12
)
10
13
11
14
func TestAutogeneratedExclude_isGeneratedFileLax_generated (t * testing.T ) {
@@ -79,12 +82,12 @@ func TestAutogeneratedExclude_isGeneratedFileStrict(t *testing.T) {
79
82
}{
80
83
{
81
84
desc : "" ,
82
- filepath : filepath .FromSlash ("./ testdata/autogen_go_strict.go" ),
85
+ filepath : filepath .FromSlash ("testdata/autogen_go_strict.go" ),
83
86
assert : assert .True ,
84
87
},
85
88
{
86
89
desc : "" ,
87
- filepath : filepath .FromSlash ("./ testdata/autogen_go_strict_invalid.go" ),
90
+ filepath : filepath .FromSlash ("testdata/autogen_go_strict_invalid.go" ),
88
91
assert : assert .False ,
89
92
},
90
93
}
@@ -108,19 +111,19 @@ func Test_getComments(t *testing.T) {
108
111
doc string
109
112
}{
110
113
{
111
- fpath : filepath .Join ("testdata" , " autogen_exclude.go" ),
114
+ fpath : filepath .FromSlash ("testdata/ autogen_exclude.go" ),
112
115
doc : `first line
113
116
second line
114
117
third line
115
118
this text also
116
119
and this text also` ,
117
120
},
118
121
{
119
- fpath : filepath .Join ("testdata" , " autogen_exclude_doc.go" ),
122
+ fpath : filepath .FromSlash ("testdata/ autogen_exclude_doc.go" ),
120
123
doc : `DO NOT EDIT` ,
121
124
},
122
125
{
123
- fpath : filepath .Join ("testdata" , " autogen_exclude_block_comment.go" ),
126
+ fpath : filepath .FromSlash ("testdata/ autogen_exclude_block_comment.go" ),
124
127
doc : `* first line
125
128
*
126
129
* second line
@@ -141,7 +144,140 @@ this one line comment also`,
141
144
// Issue 954: Some lines can be very long, e.g. auto-generated
142
145
// embedded resources. Reported on file of 86.2KB.
143
146
func Test_getComments_fileWithLongLine (t * testing.T ) {
144
- fpath := filepath .Join ("testdata" , " autogen_exclude_long_line.go" )
147
+ fpath := filepath .FromSlash ("testdata/ autogen_exclude_long_line.go" )
145
148
_ , err := getComments (fpath )
146
149
assert .NoError (t , err )
147
150
}
151
+
152
+ func Test_shouldPassIssue (t * testing.T ) {
153
+ testCases := []struct {
154
+ desc string
155
+ strict bool
156
+ issue * result.Issue
157
+ assert assert.BoolAssertionFunc
158
+ }{
159
+ {
160
+ desc : "typecheck issue" ,
161
+ strict : false ,
162
+ issue : & result.Issue {
163
+ FromLinter : "typecheck" ,
164
+ },
165
+ assert : assert .True ,
166
+ },
167
+ {
168
+ desc : "go.mod" ,
169
+ strict : false ,
170
+ issue : & result.Issue {
171
+ FromLinter : "example" ,
172
+ Pos : token.Position {
173
+ Filename : filepath .FromSlash ("/a/b/c/go.mod" ),
174
+ },
175
+ },
176
+ assert : assert .True ,
177
+ },
178
+ {
179
+ desc : "non Go file" ,
180
+ strict : false ,
181
+ issue : & result.Issue {
182
+ FromLinter : "example" ,
183
+ Pos : token.Position {
184
+ Filename : filepath .FromSlash ("/a/b/c/test.txt" ),
185
+ },
186
+ },
187
+ assert : assert .False ,
188
+ },
189
+ {
190
+ desc : "lax " ,
191
+ strict : false ,
192
+ issue : & result.Issue {
193
+ FromLinter : "example" ,
194
+ Pos : token.Position {
195
+ Filename : filepath .FromSlash ("testdata/autogen_go_strict_invalid.go" ),
196
+ },
197
+ },
198
+ assert : assert .False ,
199
+ },
200
+ {
201
+ desc : "strict " ,
202
+ strict : true ,
203
+ issue : & result.Issue {
204
+ FromLinter : "example" ,
205
+ Pos : token.Position {
206
+ Filename : filepath .FromSlash ("testdata/autogen_go_strict_invalid.go" ),
207
+ },
208
+ },
209
+ assert : assert .True ,
210
+ },
211
+ }
212
+
213
+ for _ , test := range testCases {
214
+ test := test
215
+ t .Run (test .desc , func (t * testing.T ) {
216
+ t .Parallel ()
217
+
218
+ p := NewAutogeneratedExclude (test .strict )
219
+
220
+ pass , err := p .shouldPassIssue (test .issue )
221
+ require .NoError (t , err )
222
+
223
+ test .assert (t , pass )
224
+ })
225
+ }
226
+ }
227
+
228
+ func Test_shouldPassIssue_error (t * testing.T ) {
229
+ testCases := []struct {
230
+ desc string
231
+ strict bool
232
+ issue * result.Issue
233
+ expected string
234
+ }{
235
+ {
236
+ desc : "missing Filename" ,
237
+ strict : false ,
238
+ issue : & result.Issue {
239
+ FromLinter : "example" ,
240
+ Pos : token.Position {
241
+ Filename : "" ,
242
+ },
243
+ },
244
+ expected : "no file path for issue" ,
245
+ },
246
+ {
247
+ desc : "non-existing file (lax)" ,
248
+ strict : false ,
249
+ issue : & result.Issue {
250
+ FromLinter : "example" ,
251
+ Pos : token.Position {
252
+ Filename : filepath .FromSlash ("./no-existing.go" ),
253
+ },
254
+ },
255
+ expected : "failed to get doc (lax) of file ./no-existing.go: failed to parse file: open ./no-existing.go: no such file or directory" ,
256
+ },
257
+ {
258
+ desc : "non-existing file (strict)" ,
259
+ strict : true ,
260
+ issue : & result.Issue {
261
+ FromLinter : "example" ,
262
+ Pos : token.Position {
263
+ Filename : filepath .FromSlash ("./no-existing.go" ),
264
+ },
265
+ },
266
+ expected : "failed to get doc (strict) of file ./no-existing.go: failed to parse file: open ./no-existing.go: no such file or directory" ,
267
+ },
268
+ }
269
+
270
+ for _ , test := range testCases {
271
+ test := test
272
+ t .Run (test .desc , func (t * testing.T ) {
273
+ t .Parallel ()
274
+
275
+ p := NewAutogeneratedExclude (test .strict )
276
+
277
+ pass , err := p .shouldPassIssue (test .issue )
278
+
279
+ assert .EqualError (t , err , test .expected )
280
+ assert .False (t , pass )
281
+ })
282
+ }
283
+ }
0 commit comments