@@ -18,6 +18,7 @@ import (
1818 "testing"
1919
2020 "github.com/googleapis/api-linter/v2/lint"
21+ "github.com/googleapis/api-linter/v2/rules/internal/testutils"
2122 apb "google.golang.org/genproto/googleapis/api/annotations"
2223)
2324
@@ -250,3 +251,57 @@ func TestGetParentIDVariable(t *testing.T) {
250251 })
251252 }
252253}
254+
255+ func TestAIP0123_InvalidPatternsDontPanic (t * testing.T ) {
256+ registry := lint .NewRuleRegistry ()
257+ if err := AddRules (registry ); err != nil {
258+ t .Fatalf ("Failed to add AIP-0123 rules: %v" , err )
259+ }
260+ linter := lint .New (registry , nil )
261+
262+ for _ , test := range []struct {
263+ name string
264+ Pattern string
265+ }{
266+ {
267+ name : "prefixed top level" ,
268+ Pattern : "nonCollectionPrefix/projects/{project}" ,
269+ },
270+ {
271+ name : "prefixed child collection" ,
272+ Pattern : "nonCollectionPrefix/projects/{project}/locations/{location}" ,
273+ },
274+ {
275+ name : "prefixed singleton" ,
276+ Pattern : "nonCollectionPrefix/projects/{project}/config" ,
277+ },
278+ } {
279+ t .Run (test .name , func (t * testing.T ) {
280+ f := testutils .ParseProto3Tmpl (t , `
281+ import "google/api/resource.proto";
282+ message TestResource {
283+ option (google.api.resource) = {
284+ type: "library.googleapis.com/TestResource"
285+ pattern: "{{ .Pattern }}"
286+ singular: "testResource"
287+ plural: "testResources"
288+ };
289+ string name = 1;
290+ }
291+ ` , test )
292+
293+ responses , err := linter .LintProtos (f )
294+ if err != nil {
295+ t .Fatalf ("Linter returned error (possible panic): %v" , err )
296+ }
297+
298+ problemCount := 0
299+ for _ , resp := range responses {
300+ problemCount += len (resp .Problems )
301+ }
302+ if problemCount == 0 {
303+ t .Errorf ("Expected at least one problem for invalid pattern %q, got none" , test .Pattern )
304+ }
305+ })
306+ }
307+ }
0 commit comments