github.com/googleapis/api-linter@v1.65.2/rules/aip0123/duplicate_resource_test.go (about)

     1  // Copyright 2021 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 aip0123
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  )
    22  
    23  func TestDuplicateResource(t *testing.T) {
    24  	f := testutils.ParseProto3Tmpls(t, map[string]string{
    25  		"dep.proto": `
    26  			import "google/api/resource.proto";
    27  			package xyz;
    28  			message Publisher {
    29  				option (google.api.resource) = { type: "library.googleapis.com/Publisher" };
    30  			}
    31  			`,
    32  		"test.proto": `
    33  			import "dep.proto";
    34  			import "google/api/resource.proto";
    35  			package abc;
    36  			option (google.api.resource_definition) = { type: "library.googleapis.com/Publisher" };
    37  			option (google.api.resource_definition) = { type: "library.googleapis.com/Author" };
    38  			option (google.api.resource_definition) = { type: "library.googleapis.com/Editor" };
    39  			message Book {
    40  				option (google.api.resource) = { type: "library.googleapis.com/Book" };
    41  			}
    42  			message Author {
    43  				option (google.api.resource) = { type: "library.googleapis.com/Author" };
    44  			}
    45  			message Foo {
    46  				message Tome {
    47  					option (google.api.resource) = { type: "library.googleapis.com/Book" };
    48  				}
    49  			}`,
    50  	}, nil)["test.proto"]
    51  	want := testutils.Problems{
    52  		{
    53  			Message:    "resource \"library.googleapis.com/Author\": `google.api.resource_definition` 1 in file `test.proto`, message `abc.Author`.",
    54  			Descriptor: f,
    55  		},
    56  		{
    57  			Message:    "resource \"library.googleapis.com/Author\": `google.api.resource_definition` 1 in file `test.proto`, message `abc.Author`.",
    58  			Descriptor: f.GetMessageTypes()[1],
    59  		},
    60  		{
    61  			Message:    "resource \"library.googleapis.com/Book\": message `abc.Book`, message `abc.Foo.Tome`.",
    62  			Descriptor: f.GetMessageTypes()[0],
    63  		},
    64  		{
    65  			Message:    "resource \"library.googleapis.com/Book\": message `abc.Book`, message `abc.Foo.Tome`.",
    66  			Descriptor: f.GetMessageTypes()[2].GetNestedMessageTypes()[0],
    67  		},
    68  		{
    69  			Message:    "resource \"library.googleapis.com/Publisher\": message `xyz.Publisher`.",
    70  			Descriptor: f,
    71  		},
    72  	}
    73  	if diff := want.Diff(duplicateResource.Lint(f)); diff != "" {
    74  		t.Fatal(diff)
    75  	}
    76  }