@@ -2,8 +2,13 @@ package goanalysis
2
2
3
3
import (
4
4
"fmt"
5
+ "go/token"
6
+ "reflect"
5
7
"testing"
6
8
9
+ "github.com/golangci/golangci-lint/pkg/result"
10
+ "golang.org/x/tools/go/analysis"
11
+
7
12
"github.com/stretchr/testify/assert"
8
13
"golang.org/x/tools/go/packages"
9
14
)
@@ -46,3 +51,234 @@ func TestParseError(t *testing.T) {
46
51
assert .Equal (t , "msg" , i .Text )
47
52
}
48
53
}
54
+
55
+ func Test_buildIssues (t * testing.T ) {
56
+ type args struct {
57
+ diags []Diagnostic
58
+ linterNameBuilder func (diag * Diagnostic ) string
59
+ }
60
+ tests := []struct {
61
+ name string
62
+ args args
63
+ want []result.Issue
64
+ }{
65
+ {
66
+ name : "No Diagnostics" ,
67
+ args : args {
68
+ diags : []Diagnostic {},
69
+ linterNameBuilder : func (* Diagnostic ) string {
70
+ return "some-linter"
71
+ },
72
+ },
73
+ want : []result.Issue (nil ),
74
+ },
75
+ {
76
+ name : "Linter Name is Analyzer Name" ,
77
+ args : args {
78
+ diags : []Diagnostic {
79
+ {
80
+ Diagnostic : analysis.Diagnostic {
81
+ Message : "failure message" ,
82
+ },
83
+ Analyzer : & analysis.Analyzer {
84
+ Name : "some-linter" ,
85
+ },
86
+ Position : token.Position {},
87
+ Pkg : nil ,
88
+ },
89
+ },
90
+ linterNameBuilder : func (* Diagnostic ) string {
91
+ return "some-linter"
92
+ },
93
+ },
94
+ want : []result.Issue {
95
+ {
96
+ FromLinter : "some-linter" ,
97
+ Text : "failure message" ,
98
+ },
99
+ },
100
+ },
101
+ }
102
+ for _ , tt := range tests {
103
+ t .Run (tt .name , func (t * testing.T ) {
104
+ if got := buildIssues (tt .args .diags , tt .args .linterNameBuilder ); ! reflect .DeepEqual (got , tt .want ) {
105
+ t .Errorf ("buildIssues() = %v, want %v" , got , tt .want )
106
+ }
107
+ })
108
+ }
109
+ }
110
+
111
+ func Test_buildSingleIssue (t * testing.T ) {
112
+ type args struct {
113
+ diag * Diagnostic
114
+ linterName string
115
+ }
116
+ fakePkg := packages.Package {
117
+ Fset : makeFakeFileSet (),
118
+ }
119
+ tests := []struct {
120
+ name string
121
+ args args
122
+ wantIssue result.Issue
123
+ }{
124
+ {
125
+ name : "Linter Name is Analyzer Name" ,
126
+ args : args {
127
+ diag : & Diagnostic {
128
+ Diagnostic : analysis.Diagnostic {
129
+ Message : "failure message" ,
130
+ },
131
+ Analyzer : & analysis.Analyzer {
132
+ Name : "some-linter" ,
133
+ },
134
+ Position : token.Position {},
135
+ Pkg : nil ,
136
+ },
137
+
138
+ linterName : "some-linter" ,
139
+ },
140
+ wantIssue : result.Issue {
141
+ FromLinter : "some-linter" ,
142
+ Text : "failure message" ,
143
+ },
144
+ },
145
+ {
146
+ name : "Linter Name is NOT Analyzer Name" ,
147
+ args : args {
148
+ diag : & Diagnostic {
149
+ Diagnostic : analysis.Diagnostic {
150
+ Message : "failure message" ,
151
+ },
152
+ Analyzer : & analysis.Analyzer {
153
+ Name : "some-analyzer" ,
154
+ },
155
+ Position : token.Position {},
156
+ Pkg : nil ,
157
+ },
158
+ linterName : "some-linter" ,
159
+ },
160
+ wantIssue : result.Issue {
161
+ FromLinter : "some-linter" ,
162
+ Text : "some-analyzer: failure message" ,
163
+ },
164
+ },
165
+ {
166
+ name : "Shows issue when suggested edits exist but has no TextEdits" ,
167
+ args : args {
168
+ diag : & Diagnostic {
169
+ Diagnostic : analysis.Diagnostic {
170
+ Message : "failure message" ,
171
+ SuggestedFixes : []analysis.SuggestedFix {
172
+ {
173
+ Message : "fix something" ,
174
+ TextEdits : []analysis.TextEdit {},
175
+ },
176
+ },
177
+ },
178
+ Analyzer : & analysis.Analyzer {
179
+ Name : "some-analyzer" ,
180
+ },
181
+ Position : token.Position {},
182
+ Pkg : nil ,
183
+ },
184
+ linterName : "some-linter" ,
185
+ },
186
+ wantIssue : result.Issue {
187
+ FromLinter : "some-linter" ,
188
+ Text : "some-analyzer: failure message" ,
189
+ },
190
+ },
191
+ {
192
+ name : "Replace Whole Line" ,
193
+ args : args {
194
+ diag : & Diagnostic {
195
+ Diagnostic : analysis.Diagnostic {
196
+ Message : "failure message" ,
197
+ SuggestedFixes : []analysis.SuggestedFix {
198
+ {
199
+ Message : "fix something" ,
200
+ TextEdits : []analysis.TextEdit {
201
+ {
202
+ Pos : 101 ,
203
+ End : 201 ,
204
+ NewText : []byte ("// Some comment to fix\n " ),
205
+ },
206
+ },
207
+ },
208
+ },
209
+ },
210
+ Analyzer : & analysis.Analyzer {
211
+ Name : "some-analyzer" ,
212
+ },
213
+ Position : token.Position {},
214
+ Pkg : & fakePkg ,
215
+ },
216
+ linterName : "some-linter" ,
217
+ },
218
+ wantIssue : result.Issue {
219
+ FromLinter : "some-linter" ,
220
+ Text : "some-analyzer: failure message" ,
221
+ LineRange : & result.Range {
222
+ From : 2 ,
223
+ To : 2 ,
224
+ },
225
+ Replacement : & result.Replacement {
226
+ NeedOnlyDelete : false ,
227
+ NewLines : []string {
228
+ "// Some comment to fix" ,
229
+ },
230
+ },
231
+ Pkg : & fakePkg ,
232
+ },
233
+ },
234
+ {
235
+ name : "Excludes Replacement if TextEdit doesn't modify only whole lines" ,
236
+ args : args {
237
+ diag : & Diagnostic {
238
+ Diagnostic : analysis.Diagnostic {
239
+ Message : "failure message" ,
240
+ SuggestedFixes : []analysis.SuggestedFix {
241
+ {
242
+ Message : "fix something" ,
243
+ TextEdits : []analysis.TextEdit {
244
+ {
245
+ Pos : 101 ,
246
+ End : 151 ,
247
+ NewText : []byte ("// Some comment to fix\n " ),
248
+ },
249
+ },
250
+ },
251
+ },
252
+ },
253
+ Analyzer : & analysis.Analyzer {
254
+ Name : "some-analyzer" ,
255
+ },
256
+ Position : token.Position {},
257
+ Pkg : & fakePkg ,
258
+ },
259
+ linterName : "some-linter" ,
260
+ },
261
+ wantIssue : result.Issue {
262
+ FromLinter : "some-linter" ,
263
+ Text : "some-analyzer: failure message" ,
264
+ Pkg : & fakePkg ,
265
+ },
266
+ },
267
+ }
268
+ for _ , tt := range tests {
269
+ t .Run (tt .name , func (t * testing.T ) {
270
+ if gotIssues := buildSingleIssue (tt .args .diag , tt .args .linterName ); ! reflect .DeepEqual (gotIssues , tt .wantIssue ) {
271
+ t .Errorf ("buildSingleIssue() = %v, want %v" , gotIssues , tt .wantIssue )
272
+ }
273
+ })
274
+ }
275
+ }
276
+
277
+ func makeFakeFileSet () * token.FileSet {
278
+ fSet := token .NewFileSet ()
279
+ file := fSet .AddFile ("fake.go" , 1 , 1000 )
280
+ for i := 100 ; i < 1000 ; i += 100 {
281
+ file .AddLine (i )
282
+ }
283
+ return fSet
284
+ }
0 commit comments