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