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

     1  // Copyright 2020 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 aip0124
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  )
    22  
    23  func TestReferenceSamePackage(t *testing.T) {
    24  	for _, test := range []struct {
    25  		name         string
    26  		Field        string
    27  		Ref          string
    28  		OtherPackage string
    29  		problems     testutils.Problems
    30  	}{
    31  		{"SamePkg", "type", "library.googleapis.com/Book", "same", nil},
    32  		{"SamePkgChild", "child_type", "library.googleapis.com/Book", "same", nil},
    33  		{"NotSamePkg", "type", "library.googleapis.com/Book", "other", testutils.Problems{{Message: "same package"}}},
    34  		{"NotSamePkgChild", "child_type", "library.googleapis.com/Book", "other", testutils.Problems{{Message: "same package"}}},
    35  		{"Ignored", "type", "cloudresourcemanager.googleapis.com/Project", "other", nil},
    36  	} {
    37  		t.Run(test.name, func(t *testing.T) {
    38  			t.Run("DirectDependency", func(t *testing.T) {
    39  				files := testutils.ParseProto3Tmpls(t, map[string]string{
    40  					"dep.proto": `
    41  						package {{.OtherPackage}};
    42  						import "google/api/resource.proto";
    43  						message Book {
    44  							option (google.api.resource) = {
    45  								type: "{{.Ref}}"
    46  							};
    47  						}
    48  					`,
    49  					"leaf.proto": `
    50  						package same;
    51  						import "google/api/resource.proto";
    52  						import "dep.proto";
    53  						message Foo {
    54  							string book = 1 [(google.api.resource_reference).{{.Field}} = "{{.Ref}}"];
    55  						}
    56  					`,
    57  				}, test)
    58  				file := files["leaf.proto"]
    59  				field := file.GetMessageTypes()[0].GetFields()[0]
    60  				if diff := test.problems.SetDescriptor(field).Diff(referenceSamePackage.Lint(file)); diff != "" {
    61  					t.Errorf(diff)
    62  				}
    63  			})
    64  
    65  			t.Run("DirectDependencyResourceDefinition", func(t *testing.T) {
    66  				files := testutils.ParseProto3Tmpls(t, map[string]string{
    67  					"dep.proto": `
    68  						package {{.OtherPackage}};
    69  						import "google/api/resource.proto";
    70  						option (google.api.resource_definition) = {
    71  							type: "{{.Ref}}"
    72  						};
    73  					`,
    74  					"leaf.proto": `
    75  						package same;
    76  						import "google/api/resource.proto";
    77  						import "dep.proto";
    78  						message Foo {
    79  							string book = 1 [(google.api.resource_reference).{{.Field}} = "{{.Ref}}"];
    80  						}
    81  					`,
    82  				}, test)
    83  				file := files["leaf.proto"]
    84  				field := file.GetMessageTypes()[0].GetFields()[0]
    85  				if diff := test.problems.SetDescriptor(field).Diff(referenceSamePackage.Lint(file)); diff != "" {
    86  					t.Errorf(diff)
    87  				}
    88  			})
    89  
    90  			t.Run("RemoteDependency", func(t *testing.T) {
    91  				files := testutils.ParseProto3Tmpls(t, map[string]string{
    92  					"dep.proto": `
    93  						package {{.OtherPackage}};
    94  						import "google/api/resource.proto";
    95  						message Book {
    96  							option (google.api.resource) = {
    97  								type: "library.googleapis.com/Book"
    98  							};
    99  						}
   100  					`,
   101  					"intermediate.proto": `import "dep.proto";`,
   102  					"leaf.proto": `
   103  						package same;
   104  						import "google/api/resource.proto";
   105  						import "intermediate.proto";
   106  						message Foo {
   107  							string book = 1 [(google.api.resource_reference).{{.Field}} = "{{.Ref}}"];
   108  						}
   109  					`,
   110  				}, test)
   111  				file := files["leaf.proto"]
   112  				field := file.GetMessageTypes()[0].GetFields()[0]
   113  				if diff := test.problems.SetDescriptor(field).Diff(referenceSamePackage.Lint(file)); diff != "" {
   114  					t.Errorf(diff)
   115  				}
   116  			})
   117  		})
   118  	}
   119  }