golangci-lint/test/enabled_linters_test.go
Tam Mach 18fd36bdd1
build(golang1.15): Upgrade to golang 1.15 for smaller binary (#1303)
Golang 1.15 comes to few improvements, one of them is to have smaller
binary.

This PR is to make golang 1.15 as default version in CI, I also update
Docker based image to golang:1.15* as well.

Two issues faced with golang 1.15:
- Conflict between -v in `golangci-lint` and `go test`. Update to --verbose
to avoid the same. [1]
- `nolintlint_unused.go` testdata is not matching regex. Correct by adding one
space after //

[1]: https://github.com/golang/go/issues/40763

Signed-off-by: Tam Mach <sayboras@yahoo.com>
2020-08-19 09:05:32 +10:00

194 lines
3.9 KiB
Go

package test
import (
"fmt"
"sort"
"strings"
"testing"
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
"github.com/golangci/golangci-lint/test/testshared"
)
func inSlice(s []string, v string) bool {
for _, sv := range s {
if sv == v {
return true
}
}
return false
}
func getEnabledByDefaultFastLintersExcept(except ...string) []string {
m := lintersdb.NewManager(nil, nil)
ebdl := m.GetAllEnabledByDefaultLinters()
ret := []string{}
for _, lc := range ebdl {
if lc.IsSlowLinter() {
continue
}
if !inSlice(except, lc.Name()) {
ret = append(ret, lc.Name())
}
}
return ret
}
func getAllFastLintersWith(with ...string) []string {
linters := lintersdb.NewManager(nil, nil).GetAllSupportedLinterConfigs()
ret := append([]string{}, with...)
for _, lc := range linters {
if lc.IsSlowLinter() {
continue
}
ret = append(ret, lc.Name())
}
return ret
}
func getEnabledByDefaultLinters() []string {
ebdl := lintersdb.NewManager(nil, nil).GetAllEnabledByDefaultLinters()
ret := []string{}
for _, lc := range ebdl {
ret = append(ret, lc.Name())
}
return ret
}
func getEnabledByDefaultFastLintersWith(with ...string) []string {
ebdl := lintersdb.NewManager(nil, nil).GetAllEnabledByDefaultLinters()
ret := append([]string{}, with...)
for _, lc := range ebdl {
if lc.IsSlowLinter() {
continue
}
ret = append(ret, lc.Name())
}
return ret
}
//nolint:funlen
func TestEnabledLinters(t *testing.T) {
type tc struct {
name string
cfg string
el []string
args string
noImplicitFast bool
}
cases := []tc{
{
name: "disable govet in config",
cfg: `
linters:
disable:
- govet
`,
el: getEnabledByDefaultFastLintersExcept("govet"),
},
{
name: "enable golint in config",
cfg: `
linters:
enable:
- golint
`,
el: getEnabledByDefaultFastLintersWith("golint"),
},
{
name: "disable govet in cmd",
args: "-Dgovet",
el: getEnabledByDefaultFastLintersExcept("govet"),
},
{
name: "enable gofmt in cmd and enable golint in config",
args: "-Egofmt",
cfg: `
linters:
enable:
- golint
`,
el: getEnabledByDefaultFastLintersWith("golint", "gofmt"),
},
{
name: "fast option in config",
cfg: `
linters:
fast: true
`,
el: getEnabledByDefaultFastLintersWith(),
noImplicitFast: true,
},
{
name: "explicitly unset fast option in config",
cfg: `
linters:
fast: false
`,
el: getEnabledByDefaultLinters(),
noImplicitFast: true,
},
{
name: "set fast option in command-line",
args: "--fast",
el: getEnabledByDefaultFastLintersWith(),
noImplicitFast: true,
},
{
name: "fast option in command-line has higher priority to enable",
cfg: `
linters:
fast: false
`,
args: "--fast",
el: getEnabledByDefaultFastLintersWith(),
noImplicitFast: true,
},
{
name: "fast option in command-line has higher priority to disable",
cfg: `
linters:
fast: true
`,
args: "--fast=false",
el: getEnabledByDefaultLinters(),
noImplicitFast: true,
},
{
name: "fast option combined with enable and enable-all",
args: "--enable-all --fast --enable=unused",
el: getAllFastLintersWith("unused"),
noImplicitFast: true,
},
}
runner := testshared.NewLintRunner(t)
for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
runArgs := []string{"--verbose"}
if !c.noImplicitFast {
runArgs = append(runArgs, "--fast")
}
if c.args != "" {
runArgs = append(runArgs, strings.Split(c.args, " ")...)
}
r := runner.RunCommandWithYamlConfig(c.cfg, "linters", runArgs...)
sort.StringSlice(c.el).Sort()
expectedLine := fmt.Sprintf("Active %d linters: [%s]", len(c.el), strings.Join(c.el, " "))
r.ExpectOutputContains(expectedLine)
})
}
}