github.com/googleapis/api-linter@v1.65.2/lint/lint_test.go (about) 1 // Copyright 2019 Google LLC 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 // https://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 15 package lint 16 17 import ( 18 "fmt" 19 "reflect" 20 "strings" 21 "testing" 22 23 "github.com/jhump/protoreflect/desc" 24 "github.com/jhump/protoreflect/desc/builder" 25 ) 26 27 func TestLinter_run(t *testing.T) { 28 fd, err := builder.NewFile("protofile.proto").Build() 29 if err != nil { 30 t.Fatalf("Failed to build a file descriptor.") 31 } 32 defaultConfigs := Configs{} 33 34 testRuleName := NewRuleName(111, "test-rule") 35 ruleProblems := []Problem{{ 36 Message: "rule1_problem", 37 Descriptor: fd, 38 category: "", 39 RuleID: testRuleName, 40 }} 41 42 tests := []struct { 43 testName string 44 configs Configs 45 problems []Problem 46 }{ 47 {"Empty", Configs{}, []Problem{}}, 48 { 49 "NonMatchingFile", 50 append( 51 defaultConfigs, 52 Config{ 53 IncludedPaths: []string{"nofile"}, 54 }, 55 ), 56 ruleProblems, 57 }, 58 { 59 "NonMatchingRule", 60 append( 61 defaultConfigs, 62 Config{ 63 DisabledRules: []string{"foo::bar"}, 64 }, 65 ), 66 ruleProblems, 67 }, 68 { 69 "DisabledRule", 70 append( 71 defaultConfigs, 72 Config{ 73 DisabledRules: []string{string(testRuleName)}, 74 }, 75 ), 76 []Problem{}, 77 }, 78 } 79 80 for _, test := range tests { 81 t.Run(test.testName, func(t *testing.T) { 82 rules := NewRuleRegistry() 83 err := rules.Register(111, &FileRule{ 84 Name: NewRuleName(111, "test-rule"), 85 LintFile: func(f *desc.FileDescriptor) []Problem { 86 return test.problems 87 }, 88 }) 89 if err != nil { 90 t.Fatal(err) 91 } 92 l := New(rules, test.configs) 93 94 // Actually run the linter. 95 resp, _ := l.lintFileDescriptor(fd) 96 97 // Assert that we got the problems we expected. 98 if !reflect.DeepEqual(resp.Problems, test.problems) { 99 t.Errorf("Expected %v, got %v.", resp.Problems, test.problems) 100 } 101 }) 102 } 103 } 104 105 func TestLinter_LintProtos_RulePanics(t *testing.T) { 106 fd, err := builder.NewFile("test.proto").Build() 107 if err != nil { 108 t.Fatalf("Failed to build the file descriptor.") 109 } 110 111 testAIP := 111 112 113 tests := []struct { 114 testName string 115 rule ProtoRule 116 }{ 117 { 118 testName: "Panic", 119 rule: &FileRule{ 120 Name: NewRuleName(testAIP, "panic"), 121 LintFile: func(_ *desc.FileDescriptor) []Problem { 122 panic("panic") 123 }, 124 }, 125 }, 126 { 127 testName: "PanicError", 128 rule: &FileRule{ 129 Name: NewRuleName(testAIP, "panic-error"), 130 LintFile: func(_ *desc.FileDescriptor) []Problem { 131 panic(fmt.Errorf("panic")) 132 }, 133 }, 134 }, 135 } 136 137 for _, test := range tests { 138 t.Run(test.testName, func(t *testing.T) { 139 rules := NewRuleRegistry() 140 err := rules.Register(testAIP, test.rule) 141 if err != nil { 142 t.Fatalf("Failed to create Rules: %q", err) 143 } 144 145 // Instantiate a linter with the given rule. 146 l := New(rules, nil) 147 148 _, err = l.LintProtos(fd) 149 if err == nil || !strings.Contains(err.Error(), "panic") { 150 t.Fatalf("Expected error with panic, got %q", err) 151 } 152 }) 153 } 154 } 155 156 func TestLinter_debug(t *testing.T) { 157 tests := []struct { 158 name string 159 debug bool 160 }{ 161 { 162 name: "debug", 163 debug: true, 164 }, 165 { 166 name: "do not debug", 167 debug: false, 168 }, 169 } 170 171 for _, test := range tests { 172 t.Run(test.name, func(t *testing.T) { 173 l := New(NewRuleRegistry(), nil, Debug(test.debug)) 174 175 if a, e := l.debug, test.debug; a != e { 176 t.Errorf("got debug %v wanted debug %v", a, e) 177 } 178 }) 179 } 180 } 181 182 func TestLinter_IgnoreCommentDisables(t *testing.T) { 183 tests := []struct { 184 name string 185 ignoreCommentDisables bool 186 }{ 187 { 188 name: "ignoreCommentDisables", 189 ignoreCommentDisables: true, 190 }, 191 { 192 name: "do not ignoreCommentDisables", 193 ignoreCommentDisables: false, 194 }, 195 } 196 197 for _, test := range tests { 198 t.Run(test.name, func(t *testing.T) { 199 l := New(NewRuleRegistry(), nil, IgnoreCommentDisables(test.ignoreCommentDisables)) 200 201 if a, e := l.ignoreCommentDisables, test.ignoreCommentDisables; a != e { 202 t.Errorf("got ignoreCommentDisables %v wanted ignoreCommentDisables %v", a, e) 203 } 204 }) 205 } 206 }