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