Skip to content

Commit 69c7d1e

Browse files
committed
tests: add tests on EnabledSet
1 parent 39e6b9c commit 69c7d1e

File tree

2 files changed

+151
-2
lines changed

2 files changed

+151
-2
lines changed

Diff for: pkg/lint/lintersdb/enabled_set.go

+5
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,14 @@ func (es EnabledSet) combineGoAnalysisLinters(linters map[string]*linter.Config)
148148
if !ok {
149149
continue
150150
}
151+
151152
if lnt.LoadMode() == goanalysis.LoadModeWholeProgram {
152153
// It's ineffective by CPU and memory to run whole-program and incremental analyzers at once.
153154
continue
154155
}
156+
155157
goanalysisLinters = append(goanalysisLinters, lnt)
158+
156159
for _, p := range lc.InPresets {
157160
goanalysisPresets[p] = true
158161
}
@@ -185,6 +188,7 @@ func (es EnabledSet) combineGoAnalysisLinters(linters map[string]*linter.Config)
185188
ml := goanalysis.NewMetaLinter(goanalysisLinters)
186189

187190
presets := maps.Keys(goanalysisPresets)
191+
sort.Strings(presets)
188192

189193
mlConfig := &linter.Config{
190194
Linter: ml,
@@ -197,6 +201,7 @@ func (es EnabledSet) combineGoAnalysisLinters(linters map[string]*linter.Config)
197201
mlConfig = mlConfig.WithLoadForGoAnalysis()
198202

199203
linters[ml.Name()] = mlConfig
204+
200205
es.debugf("Combined %d go/analysis linters into one metalinter", len(goanalysisLinters))
201206
}
202207

Diff for: pkg/lint/lintersdb/enabled_set_test.go

+146-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,91 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
78

89
"github.com/golangci/golangci-lint/pkg/config"
10+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
911
"github.com/golangci/golangci-lint/pkg/lint/linter"
12+
"github.com/golangci/golangci-lint/pkg/logutils"
1013
)
1114

12-
func TestGetEnabledLintersSet(t *testing.T) {
15+
type dumbLogger struct{}
16+
17+
func (d dumbLogger) Fatalf(_ string, _ ...any) {}
18+
19+
func (d dumbLogger) Panicf(_ string, _ ...any) {}
20+
21+
func (d dumbLogger) Errorf(_ string, _ ...any) {}
22+
23+
func (d dumbLogger) Warnf(_ string, _ ...any) {}
24+
25+
func (d dumbLogger) Infof(_ string, _ ...any) {}
26+
27+
func (d dumbLogger) Child(_ string) logutils.Log {
28+
return nil
29+
}
30+
31+
func (d dumbLogger) SetLevel(_ logutils.LogLevel) {}
32+
33+
func TestEnabledSet_GetEnabledLintersMap(t *testing.T) {
34+
m := NewManager(nil, nil)
35+
36+
cfg := config.NewDefault()
37+
38+
cfg.Linters.DisableAll = true
39+
cfg.Linters.Enable = []string{"gofmt"}
40+
41+
es := NewEnabledSet(m, NewValidator(m), dumbLogger{}, cfg)
42+
43+
lintersMap, err := es.GetEnabledLintersMap()
44+
require.NoError(t, err)
45+
46+
gofmtConfigs := m.GetLinterConfigs("gofmt")
47+
typecheckConfigs := m.GetLinterConfigs("typecheck")
48+
49+
expected := map[string]*linter.Config{
50+
"gofmt": gofmtConfigs[0],
51+
"typecheck": typecheckConfigs[0],
52+
}
53+
54+
assert.Equal(t, expected, lintersMap)
55+
}
56+
57+
func TestEnabledSet_GetOptimizedLinters(t *testing.T) {
58+
m := NewManager(nil, nil)
59+
60+
cfg := config.NewDefault()
61+
62+
cfg.Linters.DisableAll = true
63+
cfg.Linters.Enable = []string{"gofmt"}
64+
65+
es := NewEnabledSet(m, NewValidator(m), dumbLogger{}, cfg)
66+
67+
optimizedLinters, err := es.GetOptimizedLinters()
68+
require.NoError(t, err)
69+
70+
gofmtConfigs := m.GetLinterConfigs("gofmt")
71+
typecheckConfigs := m.GetLinterConfigs("typecheck")
72+
73+
var gaLinters []*goanalysis.Linter
74+
for _, l := range gofmtConfigs {
75+
gaLinters = append(gaLinters, l.Linter.(*goanalysis.Linter))
76+
}
77+
for _, l := range typecheckConfigs {
78+
gaLinters = append(gaLinters, l.Linter.(*goanalysis.Linter))
79+
}
80+
81+
mlConfig := &linter.Config{
82+
Linter: goanalysis.NewMetaLinter(gaLinters),
83+
InPresets: []string{"bugs", "format"},
84+
}
85+
86+
expected := []*linter.Config{mlConfig.WithLoadForGoAnalysis()}
87+
88+
assert.Equal(t, expected, optimizedLinters)
89+
}
90+
91+
func TestEnabledSet_build(t *testing.T) {
1392
type cs struct {
1493
cfg config.Linters
1594
name string // test case name
@@ -94,7 +173,7 @@ func TestGetEnabledLintersSet(t *testing.T) {
94173
}
95174

96175
m := NewManager(nil, nil)
97-
es := NewEnabledSet(m, NewValidator(m), nil, nil)
176+
es := NewEnabledSet(m, NewValidator(m), dumbLogger{}, nil)
98177

99178
for _, c := range cases {
100179
c := c
@@ -117,3 +196,68 @@ func TestGetEnabledLintersSet(t *testing.T) {
117196
})
118197
}
119198
}
199+
200+
func TestEnabledSet_combineGoAnalysisLinters(t *testing.T) {
201+
m := NewManager(nil, nil)
202+
203+
es := NewEnabledSet(m, NewValidator(m), dumbLogger{}, config.NewDefault())
204+
205+
foo := goanalysis.NewLinter("foo", "example foo", nil, nil).WithLoadMode(goanalysis.LoadModeTypesInfo)
206+
bar := goanalysis.NewLinter("bar", "example bar", nil, nil).WithLoadMode(goanalysis.LoadModeTypesInfo)
207+
208+
testCases := []struct {
209+
desc string
210+
linters map[string]*linter.Config
211+
expected map[string]*linter.Config
212+
}{
213+
{
214+
desc: "no combined, one linter",
215+
linters: map[string]*linter.Config{
216+
"foo": {
217+
Linter: foo,
218+
InPresets: []string{"A"},
219+
},
220+
},
221+
expected: map[string]*linter.Config{
222+
"foo": {
223+
Linter: foo,
224+
InPresets: []string{"A"},
225+
},
226+
},
227+
},
228+
{
229+
desc: "combined, several linters",
230+
linters: map[string]*linter.Config{
231+
"foo": {
232+
Linter: foo,
233+
InPresets: []string{"A"},
234+
},
235+
"bar": {
236+
Linter: bar,
237+
InPresets: []string{"B"},
238+
},
239+
},
240+
expected: func() map[string]*linter.Config {
241+
mlConfig := &linter.Config{
242+
Linter: goanalysis.NewMetaLinter([]*goanalysis.Linter{bar, foo}),
243+
InPresets: []string{"A", "B"},
244+
}
245+
246+
return map[string]*linter.Config{
247+
"goanalysis_metalinter": mlConfig.WithLoadForGoAnalysis(),
248+
}
249+
}(),
250+
},
251+
}
252+
253+
for _, test := range testCases {
254+
test := test
255+
t.Run(test.desc, func(t *testing.T) {
256+
t.Parallel()
257+
258+
es.combineGoAnalysisLinters(test.linters)
259+
260+
assert.Equal(t, test.expected, test.linters)
261+
})
262+
}
263+
}

0 commit comments

Comments
 (0)