golangci-lint/pkg/lint/lintersdb/enabled_set_test.go
Denis Isaev 7705f82591 Update megacheck to the latest version
Also do following improvements:
  - show proper sublinter name for megacheck sublinters
  - refactor and make more simple and robust megacheck
  merging/optimizing
  - improve handling of unknown linter names in //nolint directives
  - minimize diff of our megacheck version from the upstream,
  https://github.com/golang/go/issues/29612 blocks usage of the upstream
  version
  - support the new `stylecheck` linter
  - improve tests coverage for megacheck and nolint related cases
  - update and use upstream versions of unparam and interfacer instead of forked
  ones
  - don't use golangci/tools repo anymore
  - fix newly found issues after updating linters

Also should be noted that megacheck works much faster and consumes less
memory in the newest release, therefore golangci-lint works noticeably
faster and consumes less memory for large repos.

Relates: #314
2019-01-08 21:16:15 +03:00

120 lines
2.8 KiB
Go

package lintersdb
import (
"sort"
"testing"
"github.com/golangci/golangci-lint/pkg/golinters"
"github.com/stretchr/testify/assert"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/lint/linter"
)
func TestGetEnabledLintersSet(t *testing.T) {
type cs struct {
cfg config.Linters
name string // test case name
def []string // enabled by default linters
exp []string // alphabetically ordered enabled linter names
}
cases := []cs{
{
cfg: config.Linters{
Disable: []string{golinters.MegacheckMetalinter{}.Name()},
},
name: "disable all linters from megacheck",
def: golinters.MegacheckMetalinter{}.DefaultChildLinterNames(),
exp: nil, // all disabled
},
{
cfg: config.Linters{
Disable: []string{golinters.MegacheckStaticcheckName},
},
name: "disable only staticcheck",
def: golinters.MegacheckMetalinter{}.DefaultChildLinterNames(),
exp: []string{golinters.MegacheckGosimpleName, golinters.MegacheckUnusedName},
},
{
name: "don't merge into megacheck",
def: golinters.MegacheckMetalinter{}.DefaultChildLinterNames(),
exp: golinters.MegacheckMetalinter{}.DefaultChildLinterNames(),
},
{
name: "expand megacheck",
cfg: config.Linters{
Enable: []string{golinters.MegacheckMetalinter{}.Name()},
},
def: nil,
exp: golinters.MegacheckMetalinter{}.DefaultChildLinterNames(),
},
{
name: "don't disable anything",
def: []string{"gofmt", "govet"},
exp: []string{"gofmt", "govet"},
},
{
name: "enable gosec by gas alias",
cfg: config.Linters{
Enable: []string{"gas"},
},
exp: []string{"gosec"},
},
{
name: "enable gosec by primary name",
cfg: config.Linters{
Enable: []string{"gosec"},
},
exp: []string{"gosec"},
},
{
name: "enable gosec by both names",
cfg: config.Linters{
Enable: []string{"gosec", "gas"},
},
exp: []string{"gosec"},
},
{
name: "disable gosec by gas alias",
cfg: config.Linters{
Disable: []string{"gas"},
},
def: []string{"gosec"},
},
{
name: "disable gosec by primary name",
cfg: config.Linters{
Disable: []string{"gosec"},
},
def: []string{"gosec"},
},
}
m := NewManager()
es := NewEnabledSet(m, NewValidator(m), nil, nil)
for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
var defaultLinters []*linter.Config
for _, ln := range c.def {
lc := m.GetLinterConfig(ln)
assert.NotNil(t, lc, ln)
defaultLinters = append(defaultLinters, lc)
}
els := es.build(&c.cfg, defaultLinters)
var enabledLinters []string
for ln, lc := range els {
assert.Equal(t, ln, lc.Name())
enabledLinters = append(enabledLinters, ln)
}
sort.Strings(enabledLinters)
sort.Strings(c.exp)
assert.Equal(t, c.exp, enabledLinters)
})
}
}