|
| 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() |
| 25 | + ebdl := m.GetAllEnabledByDefaultLinters() |
| 26 | + ret := []string{} |
| 27 | + for _, lc := range ebdl { |
| 28 | + if lc.NeedsSSARepr { |
| 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().GetAllSupportedLinterConfigs() |
| 42 | + ret := append([]string{}, with...) |
| 43 | + for _, lc := range linters { |
| 44 | + if lc.NeedsSSARepr { |
| 45 | + continue |
| 46 | + } |
| 47 | + ret = append(ret, lc.Name()) |
| 48 | + } |
| 49 | + |
| 50 | + return ret |
| 51 | +} |
| 52 | + |
| 53 | +func getEnabledByDefaultLinters() []string { |
| 54 | + ebdl := lintersdb.NewManager().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().GetAllEnabledByDefaultLinters() |
| 65 | + ret := append([]string{}, with...) |
| 66 | + for _, lc := range ebdl { |
| 67 | + if lc.NeedsSSARepr { |
| 68 | + continue |
| 69 | + } |
| 70 | + |
| 71 | + ret = append(ret, lc.Name()) |
| 72 | + } |
| 73 | + |
| 74 | + return ret |
| 75 | +} |
| 76 | + |
| 77 | +func mergeMegacheck(linters []string) []string { |
| 78 | + if inSlice(linters, "staticcheck") && |
| 79 | + inSlice(linters, "gosimple") && |
| 80 | + inSlice(linters, "unused") { |
| 81 | + ret := []string{"megacheck"} |
| 82 | + for _, linter := range linters { |
| 83 | + if !inSlice([]string{"staticcheck", "gosimple", "unused"}, linter) { |
| 84 | + ret = append(ret, linter) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return ret |
| 89 | + } |
| 90 | + |
| 91 | + return linters |
| 92 | +} |
| 93 | + |
| 94 | +func TestEnabledLinters(t *testing.T) { |
| 95 | + type tc struct { |
| 96 | + name string |
| 97 | + cfg string |
| 98 | + el []string |
| 99 | + args string |
| 100 | + noImplicitFast bool |
| 101 | + } |
| 102 | + |
| 103 | + cases := []tc{ |
| 104 | + { |
| 105 | + name: "disable govet in config", |
| 106 | + cfg: ` |
| 107 | + linters: |
| 108 | + disable: |
| 109 | + - govet |
| 110 | + `, |
| 111 | + el: getEnabledByDefaultFastLintersExcept("govet"), |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "enable golint in config", |
| 115 | + cfg: ` |
| 116 | + linters: |
| 117 | + enable: |
| 118 | + - golint |
| 119 | + `, |
| 120 | + el: getEnabledByDefaultFastLintersWith("golint"), |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "disable govet in cmd", |
| 124 | + args: "-Dgovet", |
| 125 | + el: getEnabledByDefaultFastLintersExcept("govet"), |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "enable gofmt in cmd and enable golint in config", |
| 129 | + args: "-Egofmt", |
| 130 | + cfg: ` |
| 131 | + linters: |
| 132 | + enable: |
| 133 | + - golint |
| 134 | + `, |
| 135 | + el: getEnabledByDefaultFastLintersWith("golint", "gofmt"), |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "fast option in config", |
| 139 | + cfg: ` |
| 140 | + linters: |
| 141 | + fast: true |
| 142 | + `, |
| 143 | + el: getEnabledByDefaultFastLintersWith(), |
| 144 | + noImplicitFast: true, |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "explicitly unset fast option in config", |
| 148 | + cfg: ` |
| 149 | + linters: |
| 150 | + fast: false |
| 151 | + `, |
| 152 | + el: getEnabledByDefaultLinters(), |
| 153 | + noImplicitFast: true, |
| 154 | + }, |
| 155 | + { |
| 156 | + name: "set fast option in command-line", |
| 157 | + args: "--fast", |
| 158 | + el: getEnabledByDefaultFastLintersWith(), |
| 159 | + noImplicitFast: true, |
| 160 | + }, |
| 161 | + { |
| 162 | + name: "fast option in command-line has higher priority to enable", |
| 163 | + cfg: ` |
| 164 | + linters: |
| 165 | + fast: false |
| 166 | + `, |
| 167 | + args: "--fast", |
| 168 | + el: getEnabledByDefaultFastLintersWith(), |
| 169 | + noImplicitFast: true, |
| 170 | + }, |
| 171 | + { |
| 172 | + name: "fast option in command-line has higher priority to disable", |
| 173 | + cfg: ` |
| 174 | + linters: |
| 175 | + fast: true |
| 176 | + `, |
| 177 | + args: "--fast=false", |
| 178 | + el: getEnabledByDefaultLinters(), |
| 179 | + noImplicitFast: true, |
| 180 | + }, |
| 181 | + { |
| 182 | + name: "fast option combined with enable and enable-all", |
| 183 | + args: "--enable-all --fast --enable=megacheck", |
| 184 | + el: getAllFastLintersWith("megacheck"), |
| 185 | + noImplicitFast: true, |
| 186 | + }, |
| 187 | + } |
| 188 | + |
| 189 | + for _, c := range cases { |
| 190 | + c := c |
| 191 | + t.Run(c.name, func(t *testing.T) { |
| 192 | + runArgs := []string{"-v"} |
| 193 | + if !c.noImplicitFast { |
| 194 | + runArgs = append(runArgs, "--fast") |
| 195 | + } |
| 196 | + if c.args != "" { |
| 197 | + runArgs = append(runArgs, strings.Split(c.args, " ")...) |
| 198 | + } |
| 199 | + r := testshared.NewLintRunner(t).RunWithYamlConfig(c.cfg, runArgs...) |
| 200 | + el := mergeMegacheck(c.el) |
| 201 | + sort.StringSlice(el).Sort() |
| 202 | + |
| 203 | + expectedLine := fmt.Sprintf("Active %d linters: [%s]", len(el), strings.Join(el, " ")) |
| 204 | + r.ExpectOutputContains(expectedLine) |
| 205 | + }) |
| 206 | + } |
| 207 | +} |
0 commit comments