gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/tools/nogo/config/config_test.go (about) 1 // Copyright 2021 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License.package nogo 14 15 package config 16 17 import ( 18 "go/token" 19 "testing" 20 21 "gvisor.dev/gvisor/tools/nogo/check" 22 ) 23 24 // TestShouldReport validates the suppression behavior of Config.ShouldReport. 25 func TestShouldReport(t *testing.T) { 26 config := &Config{ 27 Groups: []Group{ 28 { 29 Name: "default-enabled", 30 Regex: "^default-enabled/", 31 Default: true, 32 }, 33 { 34 Name: "default-disabled", 35 Regex: "^default-disabled/", 36 Default: false, 37 }, 38 { 39 Name: "default-disabled-omitted-from-global", 40 Regex: "^default-disabled-omitted-from-global/", 41 Default: false, 42 }, 43 }, 44 Global: AnalyzerConfig{ 45 "default-enabled": &ItemConfig{ 46 Exclude: []string{"excluded.go"}, 47 Suppress: []string{"suppressed"}, 48 }, 49 "default-disabled": &ItemConfig{ 50 Exclude: []string{"excluded.go"}, 51 Suppress: []string{"suppressed"}, 52 }, 53 // Omitting default-disabled-omitted-from-global here 54 // has no effect on configuration below. 55 }, 56 Analyzers: map[string]AnalyzerConfig{ 57 "analyzer-suppressions": { 58 // Suppress some. 59 "default-enabled": &ItemConfig{ 60 Exclude: []string{"limited-exclude.go"}, 61 Suppress: []string{"limited suppress"}, 62 }, 63 // Enable all. 64 "default-disabled": nil, 65 }, 66 "enabled-for-default-disabled": { 67 "default-disabled": nil, 68 "default-disabled-omitted-from-global": nil, 69 }, 70 }, 71 } 72 73 if err := config.Compile(); err != nil { 74 t.Fatalf("Compile(%+v) = %v, want nil", config, err) 75 } 76 77 cases := []struct { 78 name string 79 finding check.Finding 80 want bool 81 }{ 82 { 83 name: "enabled", 84 finding: check.Finding{ 85 Category: "foo", 86 Position: token.Position{ 87 Filename: "default-enabled/file.go", 88 Offset: 0, 89 Line: 1, 90 Column: 1, 91 }, 92 Message: "message", 93 }, 94 want: true, 95 }, 96 { 97 name: "ungrouped", 98 finding: check.Finding{ 99 Category: "foo", 100 Position: token.Position{ 101 Filename: "ungrouped/file.go", 102 Offset: 0, 103 Line: 1, 104 Column: 1, 105 }, 106 Message: "message", 107 }, 108 want: true, 109 }, 110 { 111 name: "suppressed", 112 finding: check.Finding{ 113 Category: "foo", 114 Position: token.Position{ 115 Filename: "default-enabled/file.go", 116 Offset: 0, 117 Line: 1, 118 Column: 1, 119 }, 120 Message: "message suppressed", 121 }, 122 want: false, 123 }, 124 { 125 name: "excluded", 126 finding: check.Finding{ 127 Category: "foo", 128 Position: token.Position{ 129 Filename: "default-enabled/excluded.go", 130 Offset: 0, 131 Line: 1, 132 Column: 1, 133 }, 134 Message: "message", 135 }, 136 want: false, 137 }, 138 { 139 name: "disabled", 140 finding: check.Finding{ 141 Category: "foo", 142 Position: token.Position{ 143 Filename: "default-disabled/file.go", 144 Offset: 0, 145 Line: 1, 146 Column: 1, 147 }, 148 Message: "message", 149 }, 150 want: false, 151 }, 152 { 153 name: "analyzer suppressed", 154 finding: check.Finding{ 155 Category: "analyzer-suppressions", 156 Position: token.Position{ 157 Filename: "default-enabled/file.go", 158 Offset: 0, 159 Line: 1, 160 Column: 1, 161 }, 162 Message: "message limited suppress", 163 }, 164 want: false, 165 }, 166 { 167 name: "analyzer suppressed not global", 168 finding: check.Finding{ 169 // Doesn't apply outside of analyzer-suppressions. 170 Category: "foo", 171 Position: token.Position{ 172 Filename: "default-enabled/file.go", 173 Offset: 0, 174 Line: 1, 175 Column: 1, 176 }, 177 Message: "message limited suppress", 178 }, 179 want: true, 180 }, 181 { 182 name: "analyzer suppressed grouped", 183 finding: check.Finding{ 184 Category: "analyzer-suppressions", 185 Position: token.Position{ 186 // Doesn't apply outside of default-enabled. 187 Filename: "default-disabled/file.go", 188 Offset: 0, 189 Line: 1, 190 Column: 1, 191 }, 192 Message: "message limited suppress", 193 }, 194 want: true, 195 }, 196 { 197 name: "analyzer excluded", 198 finding: check.Finding{ 199 Category: "analyzer-suppressions", 200 Position: token.Position{ 201 Filename: "default-enabled/limited-exclude.go", 202 Offset: 0, 203 Line: 1, 204 Column: 1, 205 }, 206 Message: "message", 207 }, 208 want: false, 209 }, 210 { 211 name: "analyzer excluded not global", 212 finding: check.Finding{ 213 // Doesn't apply outside of analyzer-suppressions. 214 Category: "foo", 215 Position: token.Position{ 216 Filename: "default-enabled/limited-exclude.go", 217 Offset: 0, 218 Line: 1, 219 Column: 1, 220 }, 221 Message: "message", 222 }, 223 want: true, 224 }, 225 { 226 name: "analyzer excluded grouped", 227 finding: check.Finding{ 228 Category: "analyzer-suppressions", 229 Position: token.Position{ 230 // Doesn't apply outside of default-enabled. 231 Filename: "default-disabled/limited-exclude.go", 232 Offset: 0, 233 Line: 1, 234 Column: 1, 235 }, 236 Message: "message", 237 }, 238 want: true, 239 }, 240 { 241 name: "disabled-omitted", 242 finding: check.Finding{ 243 Category: "foo", 244 Position: token.Position{ 245 Filename: "default-disabled-omitted-from-global/file.go", 246 Offset: 0, 247 Line: 1, 248 Column: 1, 249 }, 250 Message: "message", 251 }, 252 want: false, 253 }, 254 { 255 name: "default enabled applies to customized analyzer", 256 finding: check.Finding{ 257 Category: "enabled-for-default-disabled", 258 Position: token.Position{ 259 Filename: "default-enabled/file.go", 260 Offset: 0, 261 Line: 1, 262 Column: 1, 263 }, 264 Message: "message", 265 }, 266 want: true, 267 }, 268 { 269 name: "default overridden in customized analyzer", 270 finding: check.Finding{ 271 Category: "enabled-for-default-disabled", 272 Position: token.Position{ 273 Filename: "default-disabled/file.go", 274 Offset: 0, 275 Line: 1, 276 Column: 1, 277 }, 278 Message: "message", 279 }, 280 want: true, 281 }, 282 { 283 name: "default overridden in customized analyzer even when omitted from global", 284 finding: check.Finding{ 285 Category: "enabled-for-default-disabled", 286 Position: token.Position{ 287 Filename: "default-disabled-omitted-from-global/file.go", 288 Offset: 0, 289 Line: 1, 290 Column: 1, 291 }, 292 Message: "message", 293 }, 294 want: true, 295 }, 296 } 297 for _, tc := range cases { 298 t.Run(tc.name, func(t *testing.T) { 299 if got := config.ShouldReport(tc.finding); got != tc.want { 300 t.Errorf("ShouldReport(%+v) = %v, want %v", tc.finding, got, tc.want) 301 } 302 }) 303 } 304 }