| 1 |
// Copyright 2025 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 |
// 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 |
|
| 15 |
package bazel |
| 16 |
|
| 17 |
import ( |
| 18 |
"os" |
| 19 |
"path/filepath" |
| 20 |
"testing" |
| 21 |
) |
| 22 |
|
| 23 |
func TestParse(t *testing.T) { |
| 24 |
content := ` |
| 25 |
go_grpc_library( |
| 26 |
name = "asset_go_proto", |
| 27 |
importpath = "cloud.google.com/go/asset/apiv1/assetpb", |
| 28 |
protos = [":asset_proto"], |
| 29 |
) |
| 30 |
|
| 31 |
go_gapic_library( |
| 32 |
name = "asset_go_gapic", |
| 33 |
srcs = [":asset_proto_with_info"], |
| 34 |
grpc_service_config = "cloudasset_grpc_service_config.json", |
| 35 |
importpath = "cloud.google.com/go/asset/apiv1;asset", |
| 36 |
metadata = True, |
| 37 |
release_level = "ga", |
| 38 |
rest_numeric_enums = True, |
| 39 |
service_yaml = "cloudasset_v1.yaml", |
| 40 |
transport = "grpc+rest", |
| 41 |
diregapic = False, |
| 42 |
) |
| 43 |
` |
| 44 |
tmpDir := t.TempDir() |
| 45 |
buildPath := filepath.Join(tmpDir, "BUILD.bazel") |
| 46 |
if err := os.WriteFile(buildPath, []byte(content), 0644); err != nil { |
| 47 |
t.Fatalf("failed to write test file: %v", err) |
| 48 |
} |
| 49 |
|
| 50 |
got, err := Parse(tmpDir) |
| 51 |
if err != nil { |
| 52 |
t.Fatalf("Parse() failed: %v", err) |
| 53 |
} |
| 54 |
|
| 55 |
if !got.HasGAPIC() { |
| 56 |
t.Error("HasGAPIC() = false; want true") |
| 57 |
} |
| 58 |
if want := "cloud.google.com/go/asset/apiv1;asset"; got.GAPICImportPath() != want { |
| 59 |
t.Errorf("GAPICImportPath() = %q; want %q", got.GAPICImportPath(), want) |
| 60 |
} |
| 61 |
if want := "cloudasset_v1.yaml"; got.ServiceYAML() != want { |
| 62 |
t.Errorf("ServiceYAML() = %q; want %q", got.ServiceYAML(), want) |
| 63 |
} |
| 64 |
if want := "cloudasset_grpc_service_config.json"; got.GRPCServiceConfig() != want { |
| 65 |
t.Errorf("GRPCServiceConfig() = %q; want %q", got.GRPCServiceConfig(), want) |
| 66 |
} |
| 67 |
if want := "grpc+rest"; got.Transport() != want { |
| 68 |
t.Errorf("Transport() = %q; want %q", got.Transport(), want) |
| 69 |
} |
| 70 |
if want := "ga"; got.ReleaseLevel() != want { |
| 71 |
t.Errorf("ReleaseLevel() = %q; want %q", got.ReleaseLevel(), want) |
| 72 |
} |
| 73 |
if !got.HasMetadata() { |
| 74 |
t.Error("HasMetadata() = false; want true") |
| 75 |
} |
| 76 |
if got.HasDiregapic() { |
| 77 |
t.Error("HasDiregapic() = true; want false") |
| 78 |
} |
| 79 |
if !got.HasRESTNumericEnums() { |
| 80 |
t.Error("HasRESTNumericEnums() = false; want true") |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func TestParse_misconfiguration(t *testing.T) { |
| 85 |
content := ` |
| 86 |
go_grpc_library() |
| 87 |
|
| 88 |
go_proto_library() |
| 89 |
` |
| 90 |
tmpDir := t.TempDir() |
| 91 |
buildPath := filepath.Join(tmpDir, "BUILD.bazel") |
| 92 |
if err := os.WriteFile(buildPath, []byte(content), 0644); err != nil { |
| 93 |
t.Fatalf("failed to write test file: %v", err) |
| 94 |
} |
| 95 |
|
| 96 |
if _, err := Parse(tmpDir); err == nil { |
| 97 |
t.Error("Parse() succeeded; want error") |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func TestParse_serviceConfigIsTarget(t *testing.T) { |
| 102 |
content := ` |
| 103 |
go_grpc_library( |
| 104 |
name = "asset_go_proto", |
| 105 |
importpath = "cloud.google.com/go/asset/apiv1/assetpb", |
| 106 |
protos = [":asset_proto"], |
| 107 |
) |
| 108 |
|
| 109 |
go_gapic_library( |
| 110 |
name = "asset_go_gapic", |
| 111 |
srcs = [":asset_proto_with_info"], |
| 112 |
grpc_service_config = "cloudasset_grpc_service_config.json", |
| 113 |
importpath = "cloud.google.com/go/asset/apiv1;asset", |
| 114 |
metadata = True, |
| 115 |
release_level = "ga", |
| 116 |
rest_numeric_enums = True, |
| 117 |
service_yaml = ":cloudasset_v1.yaml", |
| 118 |
transport = "grpc+rest", |
| 119 |
diregapic = False, |
| 120 |
) |
| 121 |
` |
| 122 |
tmpDir := t.TempDir() |
| 123 |
buildPath := filepath.Join(tmpDir, "BUILD.bazel") |
| 124 |
if err := os.WriteFile(buildPath, []byte(content), 0644); err != nil { |
| 125 |
t.Fatalf("failed to write test file: %v", err) |
| 126 |
} |
| 127 |
|
| 128 |
got, err := Parse(tmpDir) |
| 129 |
if err != nil { |
| 130 |
t.Fatalf("Parse() failed: %v", err) |
| 131 |
} |
| 132 |
|
| 133 |
if want := "cloudasset_v1.yaml"; got.ServiceYAML() != want { |
| 134 |
t.Errorf("ServiceYAML() = %q; want %q", got.ServiceYAML(), want) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
func TestConfig_Validate(t *testing.T) { |
| 139 |
tests := []struct { |
| 140 |
name string |
| 141 |
cfg *Config |
| 142 |
wantErr bool |
| 143 |
}{ |
| 144 |
{ |
| 145 |
name: "valid GAPIC", |
| 146 |
cfg: &Config{ |
| 147 |
hasGAPIC: true, |
| 148 |
gapicImportPath: "a", |
| 149 |
serviceYAML: "b", |
| 150 |
grpcServiceConfig: "c", |
| 151 |
transport: "d", |
| 152 |
}, |
| 153 |
wantErr: false, |
| 154 |
}, |
| 155 |
{ |
| 156 |
name: "valid non-GAPIC", |
| 157 |
cfg: &Config{}, |
| 158 |
wantErr: false, |
| 159 |
}, |
| 160 |
{ |
| 161 |
name: "gRPC service config and transport are optional", |
| 162 |
cfg: &Config{hasGAPIC: true, gapicImportPath: "a", serviceYAML: "b"}, |
| 163 |
wantErr: false, |
| 164 |
}, |
| 165 |
{ |
| 166 |
name: "missing gapicImportPath", |
| 167 |
cfg: &Config{hasGAPIC: true, serviceYAML: "b", grpcServiceConfig: "c", transport: "d"}, |
| 168 |
wantErr: true, |
| 169 |
}, |
| 170 |
{ |
| 171 |
name: "missing serviceYAML", |
| 172 |
cfg: &Config{hasGAPIC: true, gapicImportPath: "a", grpcServiceConfig: "c", transport: "d"}, |
| 173 |
wantErr: true, |
| 174 |
}, |
| 175 |
} |
| 176 |
for _, tt := range tests { |
| 177 |
t.Run(tt.name, func(t *testing.T) { |
| 178 |
if err := tt.cfg.Validate(); (err != nil) != tt.wantErr { |
| 179 |
t.Errorf("Config.Validate() error = %v, wantErr %v", err, tt.wantErr) |
| 180 |
} |
| 181 |
}) |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
func TestParse_noGapic(t *testing.T) { |
| 186 |
content := ` |
| 187 |
go_grpc_library( |
| 188 |
name = "asset_go_proto", |
| 189 |
importpath = "cloud.google.com/go/asset/apiv1/assetpb", |
| 190 |
protos = [":asset_proto"], |
| 191 |
) |
| 192 |
` |
| 193 |
tmpDir := t.TempDir() |
| 194 |
buildPath := filepath.Join(tmpDir, "BUILD.bazel") |
| 195 |
if err := os.WriteFile(buildPath, []byte(content), 0644); err != nil { |
| 196 |
t.Fatalf("failed to write test file: %v", err) |
| 197 |
} |
| 198 |
|
| 199 |
got, err := Parse(tmpDir) |
| 200 |
if err != nil { |
| 201 |
t.Fatalf("Parse() failed: %v", err) |
| 202 |
} |
| 203 |
|
| 204 |
if got.HasGAPIC() { |
| 205 |
t.Error("HasGAPIC() = true; want false") |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
func TestParse_legacyProtocPlugin_noGrpc(t *testing.T) { |
| 210 |
content := ` |
| 211 |
go_proto_library( |
| 212 |
name = "asset_go_proto", |
| 213 |
importpath = "cloud.google.com/go/asset/apiv1/assetpb", |
| 214 |
protos = [":asset_proto"], |
| 215 |
) |
| 216 |
|
| 217 |
go_gapic_library( |
| 218 |
name = "asset_go_gapic", |
| 219 |
srcs = [":asset_proto_with_info"], |
| 220 |
grpc_service_config = "cloudasset_grpc_service_config.json", |
| 221 |
importpath = "cloud.google.com/go/asset/apiv1;asset", |
| 222 |
metadata = True, |
| 223 |
release_level = "ga", |
| 224 |
rest_numeric_enums = True, |
| 225 |
service_yaml = "cloudasset_v1.yaml", |
| 226 |
transport = "grpc+rest", |
| 227 |
diregapic = False, |
| 228 |
) |
| 229 |
` |
| 230 |
tmpDir := t.TempDir() |
| 231 |
buildPath := filepath.Join(tmpDir, "BUILD.bazel") |
| 232 |
if err := os.WriteFile(buildPath, []byte(content), 0644); err != nil { |
| 233 |
t.Fatalf("failed to write test file: %v", err) |
| 234 |
} |
| 235 |
|
| 236 |
got, err := Parse(tmpDir) |
| 237 |
if err != nil { |
| 238 |
t.Fatalf("Parse() failed: %v", err) |
| 239 |
} |
| 240 |
|
| 241 |
// Only test the bits related to protoc plugins |
| 242 |
if got.HasGoGRPC() { |
| 243 |
t.Error("HasGoGRPC() = true; want false") |
| 244 |
} |
| 245 |
if got.HasLegacyGRPC() { |
| 246 |
t.Error("HasLegacyGRPC() = true; want false") |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
func TestParse_legacyProtocPlugin_withGrpc(t *testing.T) { |
| 251 |
content := ` |
| 252 |
go_proto_library( |
| 253 |
name = "asset_go_proto", |
| 254 |
compilers = ["@io_bazel_rules_go//proto:go_grpc"], |
| 255 |
importpath = "cloud.google.com/go/asset/apiv1/assetpb", |
| 256 |
protos = [":asset_proto"], |
| 257 |
) |
| 258 |
|
| 259 |
go_gapic_library( |
| 260 |
name = "asset_go_gapic", |
| 261 |
srcs = [":asset_proto_with_info"], |
| 262 |
grpc_service_config = "cloudasset_grpc_service_config.json", |
| 263 |
importpath = "cloud.google.com/go/asset/apiv1;asset", |
| 264 |
metadata = True, |
| 265 |
release_level = "ga", |
| 266 |
rest_numeric_enums = True, |
| 267 |
service_yaml = "cloudasset_v1.yaml", |
| 268 |
transport = "grpc+rest", |
| 269 |
diregapic = False, |
| 270 |
) |
| 271 |
` |
| 272 |
tmpDir := t.TempDir() |
| 273 |
buildPath := filepath.Join(tmpDir, "BUILD.bazel") |
| 274 |
if err := os.WriteFile(buildPath, []byte(content), 0644); err != nil { |
| 275 |
t.Fatalf("failed to write test file: %v", err) |
| 276 |
} |
| 277 |
|
| 278 |
got, err := Parse(tmpDir) |
| 279 |
if err != nil { |
| 280 |
t.Fatalf("Parse() failed: %v", err) |
| 281 |
} |
| 282 |
|
| 283 |
// Only test the bits related to protoc plugins |
| 284 |
if got.HasGoGRPC() { |
| 285 |
t.Error("HasGoGRPC() = true; want false") |
| 286 |
} |
| 287 |
if !got.HasLegacyGRPC() { |
| 288 |
t.Error("HasLegacyGRPC() = false; want true") |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
func TestDisableGAPIC(t *testing.T) { |
| 293 |
cfg := &Config{ |
| 294 |
hasGAPIC: true, |
| 295 |
gapicImportPath: "a", |
| 296 |
serviceYAML: "b", |
| 297 |
grpcServiceConfig: "c", |
| 298 |
transport: "d", |
| 299 |
} |
| 300 |
if !cfg.HasGAPIC() { |
| 301 |
t.Error("HasGAPIC() = false; want true") |
| 302 |
} |
| 303 |
cfg.DisableGAPIC() |
| 304 |
if cfg.HasGAPIC() { |
| 305 |
t.Error("HasLegacyGRPC() = true; want false") |
| 306 |
} |
| 307 |
} |