1// Copyright 2023 The Bazel Authors. All rights reserved. 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. 14 15package main 16 17import ( 18 "reflect" 19 "testing" 20) 21 22func TestParseNolint(t *testing.T) { 23 tests := []struct { 24 Name string 25 Comment string 26 Valid bool 27 Linters []string 28 }{ 29 { 30 Name: "Invalid", 31 Comment: "not a comment", 32 }, 33 { 34 Name: "No match", 35 Comment: "// comment", 36 }, 37 { 38 Name: "All linters", 39 Comment: "//nolint", 40 Valid: true, 41 }, 42 { 43 Name: "All linters (explicit)", 44 Comment: "//nolint:all", 45 Valid: true, 46 }, 47 { 48 Name: "Single linter", 49 Comment: "// nolint:foo", 50 Valid: true, 51 Linters: []string{"foo"}, 52 }, 53 { 54 Name: "Multiple linters", 55 Comment: "// nolint:a,b,c", 56 Valid: true, 57 Linters: []string{"a", "b", "c"}, 58 }, 59 } 60 61 for _, tc := range tests { 62 t.Run(tc.Name, func(t *testing.T) { 63 result, ok := parseNolint(tc.Comment) 64 if tc.Valid != ok { 65 t.Fatalf("parseNolint expect %t got %t", tc.Valid, ok) 66 } 67 var linters map[string]bool 68 if len(tc.Linters) != 0 { 69 linters = make(map[string]bool) 70 for _, l := range tc.Linters { 71 linters[l] = true 72 } 73 } 74 if !reflect.DeepEqual(result, linters) { 75 t.Fatalf("parseNolint expect %v got %v", linters, result) 76 } 77 }) 78 } 79} 80