Skip to content

Commit

Permalink
tests: add tests on EnabledSet
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Feb 25, 2024
1 parent 3463a6a commit b9bda16
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pkg/lint/lintersdb/enabled_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,14 @@ func (es EnabledSet) combineGoAnalysisLinters(linters map[string]*linter.Config)
if !ok {
continue
}

if lnt.LoadMode() == goanalysis.LoadModeWholeProgram {
// It's ineffective by CPU and memory to run whole-program and incremental analyzers at once.
continue
}

goanalysisLinters = append(goanalysisLinters, lnt)

for _, p := range lc.InPresets {
goanalysisPresets[p] = true
}
Expand Down Expand Up @@ -185,6 +188,7 @@ func (es EnabledSet) combineGoAnalysisLinters(linters map[string]*linter.Config)
ml := goanalysis.NewMetaLinter(goanalysisLinters)

presets := maps.Keys(goanalysisPresets)
sort.Strings(presets)

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

linters[ml.Name()] = mlConfig

es.debugf("Combined %d go/analysis linters into one metalinter", len(goanalysisLinters))
}

Expand Down
148 changes: 146 additions & 2 deletions pkg/lint/lintersdb/enabled_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,91 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/logutils"
)

func TestGetEnabledLintersSet(t *testing.T) {
type dumbLogger struct{}

func (d dumbLogger) Fatalf(_ string, _ ...any) {}

func (d dumbLogger) Panicf(_ string, _ ...any) {}

func (d dumbLogger) Errorf(_ string, _ ...any) {}

func (d dumbLogger) Warnf(_ string, _ ...any) {}

func (d dumbLogger) Infof(_ string, _ ...any) {}

func (d dumbLogger) Child(_ string) logutils.Log {
return nil
}

func (d dumbLogger) SetLevel(_ logutils.LogLevel) {}

func TestEnabledSet_GetEnabledLintersMap(t *testing.T) {
m := NewManager(nil, nil)

cfg := config.NewDefault()

cfg.Linters.DisableAll = true
cfg.Linters.Enable = []string{"gofmt"}

es := NewEnabledSet(m, NewValidator(m), dumbLogger{}, cfg)

lintersMap, err := es.GetEnabledLintersMap()
require.NoError(t, err)

gofmtConfigs := m.GetLinterConfigs("gofmt")
typecheckConfigs := m.GetLinterConfigs("typecheck")

expected := map[string]*linter.Config{
"gofmt": gofmtConfigs[0],
"typecheck": typecheckConfigs[0],
}

assert.Equal(t, expected, lintersMap)
}

func TestEnabledSet_GetOptimizedLinters(t *testing.T) {
m := NewManager(nil, nil)

cfg := config.NewDefault()

cfg.Linters.DisableAll = true
cfg.Linters.Enable = []string{"gofmt"}

es := NewEnabledSet(m, NewValidator(m), dumbLogger{}, cfg)

optimizedLinters, err := es.GetOptimizedLinters()
require.NoError(t, err)

gofmtConfigs := m.GetLinterConfigs("gofmt")
typecheckConfigs := m.GetLinterConfigs("typecheck")

var gaLinters []*goanalysis.Linter
for _, l := range gofmtConfigs {
gaLinters = append(gaLinters, l.Linter.(*goanalysis.Linter))
}
for _, l := range typecheckConfigs {
gaLinters = append(gaLinters, l.Linter.(*goanalysis.Linter))
}

mlConfig := &linter.Config{
Linter: goanalysis.NewMetaLinter(gaLinters),
InPresets: []string{"bugs", "format"},
}

expected := []*linter.Config{mlConfig.WithLoadForGoAnalysis()}

assert.Equal(t, expected, optimizedLinters)
}

func TestEnabledSet_build(t *testing.T) {
type cs struct {
cfg config.Linters
name string // test case name
Expand Down Expand Up @@ -94,7 +173,7 @@ func TestGetEnabledLintersSet(t *testing.T) {
}

m := NewManager(nil, nil)
es := NewEnabledSet(m, NewValidator(m), nil, nil)
es := NewEnabledSet(m, NewValidator(m), dumbLogger{}, nil)

for _, c := range cases {
c := c
Expand All @@ -117,3 +196,68 @@ func TestGetEnabledLintersSet(t *testing.T) {
})
}
}

func TestEnabledSet_combineGoAnalysisLinters(t *testing.T) {
m := NewManager(nil, nil)

es := NewEnabledSet(m, NewValidator(m), dumbLogger{}, config.NewDefault())

foo := goanalysis.NewLinter("foo", "example foo", nil, nil).WithLoadMode(goanalysis.LoadModeTypesInfo)
bar := goanalysis.NewLinter("bar", "example bar", nil, nil).WithLoadMode(goanalysis.LoadModeTypesInfo)

testCases := []struct {
desc string
linters map[string]*linter.Config
expected map[string]*linter.Config
}{
{
desc: "no combined, one linter",
linters: map[string]*linter.Config{
"foo": {
Linter: foo,
InPresets: []string{"A"},
},
},
expected: map[string]*linter.Config{
"foo": {
Linter: foo,
InPresets: []string{"A"},
},
},
},
{
desc: "combined, several linters",
linters: map[string]*linter.Config{
"foo": {
Linter: foo,
InPresets: []string{"A"},
},
"bar": {
Linter: bar,
InPresets: []string{"B"},
},
},
expected: func() map[string]*linter.Config {
mlConfig := &linter.Config{
Linter: goanalysis.NewMetaLinter([]*goanalysis.Linter{bar, foo}),
InPresets: []string{"A", "B"},
}

return map[string]*linter.Config{
"goanalysis_metalinter": mlConfig.WithLoadForGoAnalysis(),
}
}(),
},
}

for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()

es.combineGoAnalysisLinters(test.linters)

assert.Equal(t, test.expected, test.linters)
})
}
}

0 comments on commit b9bda16

Please sign in to comment.