1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
package test
import (
"fmt"
"sort"
"strings"
"testing"
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
"github.com/golangci/golangci-lint/test/testshared"
)
//nolint:funlen
func TestEnabledLinters(t *testing.T) {
// require to display the message "Active x linters: [x,y]"
t.Setenv(lintersdb.EnvTestRun, "1")
cases := []struct {
name string
cfg string
enabledLinters []string
args []string
noImplicitFast bool
}{
{
name: "disable govet in config",
cfg: `
linters:
disable:
- govet
`,
enabledLinters: getEnabledByDefaultFastLintersExcept("govet"),
},
{
name: "enable revive in config",
cfg: `
linters:
enable:
- revive
`,
enabledLinters: getEnabledByDefaultFastLintersWith("revive"),
},
{
name: "disable govet in cmd",
args: []string{"-Dgovet"},
enabledLinters: getEnabledByDefaultFastLintersExcept("govet"),
},
{
name: "enable gofmt in cmd and enable revive in config",
args: []string{"-Egofmt"},
cfg: `
linters:
enable:
- revive
`,
enabledLinters: getEnabledByDefaultFastLintersWith("revive", "gofmt"),
},
{
name: "fast option in config",
cfg: `
linters:
fast: true
`,
enabledLinters: getEnabledByDefaultFastLintersWith(),
noImplicitFast: true,
},
{
name: "explicitly unset fast option in config",
cfg: `
linters:
fast: false
`,
enabledLinters: getEnabledByDefaultLinters(),
noImplicitFast: true,
},
{
name: "set fast option in command-line",
args: []string{"--fast"},
enabledLinters: getEnabledByDefaultFastLintersWith(),
noImplicitFast: true,
},
{
name: "fast option in command-line has higher priority to enable",
cfg: `
linters:
fast: false
`,
args: []string{"--fast"},
enabledLinters: getEnabledByDefaultFastLintersWith(),
noImplicitFast: true,
},
{
name: "fast option in command-line has higher priority to disable",
cfg: `
linters:
fast: true
`,
args: []string{"--fast=false"},
enabledLinters: getEnabledByDefaultLinters(),
noImplicitFast: true,
},
{
name: "fast option combined with enable and enable-all",
args: []string{"--enable-all", "--fast", "--enable=unused"},
enabledLinters: getAllFastLintersWith("unused"),
noImplicitFast: true,
},
}
testshared.InstallGolangciLint(t)
for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
args := []string{"--verbose"}
if !c.noImplicitFast {
args = append(args, "--fast")
}
r := testshared.NewRunnerBuilder(t).
WithCommand("linters").
WithArgs(args...).
WithArgs(c.args...).
WithConfig(c.cfg).
Runner().
Run()
sort.StringSlice(c.enabledLinters).Sort()
r.ExpectOutputContains(fmt.Sprintf("Active %d linters: [%s]",
len(c.enabledLinters), strings.Join(c.enabledLinters, " ")))
})
}
}
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()
var 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()
var 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
}
|