@@ -65,96 +65,144 @@ func TestManager_GetOptimizedLinters(t *testing.T) {
65
65
}
66
66
67
67
func TestManager_build (t * testing.T ) {
68
- type cs struct {
69
- cfg config.Linters
70
- name string // test case name
71
- def []string // enabled by default linters
72
- exp []string // alphabetically ordered enabled linter names
73
- }
74
-
75
68
allMegacheckLinterNames := []string {"gosimple" , "staticcheck" , "unused" }
76
69
77
- cases := []cs {
70
+ testCases := []struct {
71
+ desc string
72
+ cfg * config.Config
73
+ defaultSet []string // enabled by default linters
74
+ expected []string // alphabetically ordered enabled linter names
75
+ }{
76
+ {
77
+ desc : "disable all linters from megacheck" ,
78
+ cfg : & config.Config {
79
+ Linters : config.Linters {
80
+ Disable : []string {"megacheck" },
81
+ },
82
+ },
83
+ defaultSet : allMegacheckLinterNames ,
84
+ expected : []string {"typecheck" }, // all disabled
85
+ },
78
86
{
79
- cfg : config.Linters {
80
- Disable : []string {"megacheck" },
87
+ desc : "disable only staticcheck" ,
88
+ cfg : & config.Config {
89
+ Linters : config.Linters {
90
+ Disable : []string {"staticcheck" },
91
+ },
81
92
},
82
- name : "disable all linters from megacheck" ,
83
- def : allMegacheckLinterNames ,
84
- exp : []string {"typecheck" }, // all disabled
93
+ defaultSet : allMegacheckLinterNames ,
94
+ expected : []string {"gosimple" , "typecheck" , "unused" },
95
+ },
96
+ {
97
+ desc : "don't merge into megacheck" ,
98
+ defaultSet : allMegacheckLinterNames ,
99
+ expected : []string {"gosimple" , "staticcheck" , "typecheck" , "unused" },
85
100
},
86
101
{
87
- cfg : config.Linters {
88
- Disable : []string {"staticcheck" },
102
+ desc : "expand megacheck" ,
103
+ cfg : & config.Config {
104
+ Linters : config.Linters {
105
+ Enable : []string {"megacheck" },
106
+ },
89
107
},
90
- name : "disable only staticcheck" ,
91
- def : allMegacheckLinterNames ,
92
- exp : []string {"gosimple" , "typecheck" , "unused" },
108
+ defaultSet : nil ,
109
+ expected : []string {"gosimple" , "staticcheck" , "typecheck" , "unused" },
110
+ },
111
+ {
112
+ desc : "don't disable anything" ,
113
+ defaultSet : []string {"gofmt" , "govet" , "typecheck" },
114
+ expected : []string {"gofmt" , "govet" , "typecheck" },
93
115
},
94
116
{
95
- name : "don't merge into megacheck" ,
96
- def : allMegacheckLinterNames ,
97
- exp : []string {"gosimple" , "staticcheck" , "typecheck" , "unused" },
117
+ desc : "enable gosec by gas alias" ,
118
+ cfg : & config.Config {
119
+ Linters : config.Linters {
120
+ Enable : []string {"gas" },
121
+ },
122
+ },
123
+ expected : []string {"gosec" , "typecheck" },
98
124
},
99
125
{
100
- name : "expand megacheck" ,
101
- cfg : config.Linters {
102
- Enable : []string {"megacheck" },
126
+ desc : "enable gosec by primary name" ,
127
+ cfg : & config.Config {
128
+ Linters : config.Linters {
129
+ Enable : []string {"gosec" },
130
+ },
103
131
},
104
- def : nil ,
105
- exp : []string {"gosimple" , "staticcheck" , "typecheck" , "unused" },
132
+ expected : []string {"gosec" , "typecheck" },
106
133
},
107
134
{
108
- name : "don't disable anything" ,
109
- def : []string {"gofmt" , "govet" , "typecheck" },
110
- exp : []string {"gofmt" , "govet" , "typecheck" },
135
+ desc : "enable gosec by both names" ,
136
+ cfg : & config.Config {
137
+ Linters : config.Linters {
138
+ Enable : []string {"gosec" , "gas" },
139
+ },
140
+ },
141
+ expected : []string {"gosec" , "typecheck" },
111
142
},
112
143
{
113
- name : "enable gosec by gas alias" ,
114
- cfg : config.Linters {
115
- Enable : []string {"gas" },
144
+ desc : "disable gosec by gas alias" ,
145
+ cfg : & config.Config {
146
+ Linters : config.Linters {
147
+ Disable : []string {"gas" },
148
+ },
116
149
},
117
- exp : []string {"gosec" , "typecheck" },
150
+ defaultSet : []string {"gosec" },
151
+ expected : []string {"typecheck" },
118
152
},
119
153
{
120
- name : "enable gosec by primary name" ,
121
- cfg : config.Linters {
122
- Enable : []string {"gosec" },
154
+ desc : "disable gosec by primary name" ,
155
+ cfg : & config.Config {
156
+ Linters : config.Linters {
157
+ Disable : []string {"gosec" },
158
+ },
123
159
},
124
- exp : []string {"gosec" , "typecheck" },
160
+ defaultSet : []string {"gosec" },
161
+ expected : []string {"typecheck" },
125
162
},
126
163
{
127
- name : "enable gosec by both names" ,
128
- cfg : config.Linters {
129
- Enable : []string {"gosec" , "gas" },
164
+ desc : "linters and formatters" ,
165
+ cfg : & config.Config {
166
+ Linters : config.Linters {
167
+ Enable : []string {"gosec" },
168
+ },
169
+ Formatters : config.Formatters {
170
+ Enable : []string {"gofmt" },
171
+ },
130
172
},
131
- exp : []string {"gosec" , "typecheck" },
173
+ expected : []string {"gosec" , "gofmt " , "typecheck" },
132
174
},
133
175
{
134
- name : "disable gosec by gas alias" ,
135
- cfg : config.Linters {
136
- Disable : []string {"gas" },
176
+ desc : "linters and formatters but linters configuration disables the formatter" ,
177
+ cfg : & config.Config {
178
+ Linters : config.Linters {
179
+ Enable : []string {"gosec" },
180
+ Disable : []string {"gofmt" },
181
+ },
182
+ Formatters : config.Formatters {
183
+ Enable : []string {"gofmt" },
184
+ },
137
185
},
138
- def : []string {"gosec" },
139
- exp : []string {"typecheck" },
186
+ expected : []string {"gosec" , "typecheck" },
140
187
},
141
188
{
142
- name : "disable gosec by primary name" ,
143
- cfg : config.Linters {
144
- Disable : []string {"gosec" },
189
+ desc : "only formatters" ,
190
+ cfg : & config.Config {
191
+ Formatters : config.Formatters {
192
+ Enable : []string {"gofmt" },
193
+ },
145
194
},
146
- def : []string {"gosec" },
147
- exp : []string {"typecheck" },
195
+ expected : []string {"gofmt" , "typecheck" },
148
196
},
149
197
}
150
198
151
- for _ , c := range cases {
152
- t .Run (c . name , func (t * testing.T ) {
153
- m , err := NewManager (logutils .NewStderrLog ("skip" ), & config. Config { Linters : c . cfg } , NewLinterBuilder ())
199
+ for _ , test := range testCases {
200
+ t .Run (test . desc , func (t * testing.T ) {
201
+ m , err := NewManager (logutils .NewStderrLog ("skip" ), test . cfg , NewLinterBuilder ())
154
202
require .NoError (t , err )
155
203
156
204
var defaultLinters []* linter.Config
157
- for _ , ln := range c . def {
205
+ for _ , ln := range test . defaultSet {
158
206
lcs := m .GetLinterConfigs (ln )
159
207
assert .NotNil (t , lcs , ln )
160
208
defaultLinters = append (defaultLinters , lcs ... )
@@ -167,7 +215,7 @@ func TestManager_build(t *testing.T) {
167
215
enabledLinters = append (enabledLinters , ln )
168
216
}
169
217
170
- assert .ElementsMatch (t , c . exp , enabledLinters )
218
+ assert .ElementsMatch (t , test . expected , enabledLinters )
171
219
})
172
220
}
173
221
}
0 commit comments