| 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 generate |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"errors" |
| 20 |
"os" |
| 21 |
"path/filepath" |
| 22 |
"testing" |
| 23 |
|
| 24 |
"cloud.google.com/go/internal/postprocessor/librarian/librariangen/config" |
| 25 |
"cloud.google.com/go/internal/postprocessor/librarian/librariangen/request" |
| 26 |
) |
| 27 |
|
| 28 |
// testEnv encapsulates a temporary test environment. |
| 29 |
type testEnv struct { |
| 30 |
tmpDir string |
| 31 |
librarianDir string |
| 32 |
sourceDir string |
| 33 |
outputDir string |
| 34 |
} |
| 35 |
|
| 36 |
// newTestEnv creates a new test environment. |
| 37 |
func newTestEnv(t *testing.T) *testEnv { |
| 38 |
t.Helper() |
| 39 |
tmpDir, err := os.MkdirTemp("", "generator-test") |
| 40 |
if err != nil { |
| 41 |
t.Fatalf("failed to create temp dir: %v", err) |
| 42 |
} |
| 43 |
e := &testEnv{tmpDir: tmpDir} |
| 44 |
e.librarianDir = filepath.Join(tmpDir, "librarian") |
| 45 |
e.sourceDir = filepath.Join(tmpDir, "source") |
| 46 |
e.outputDir = filepath.Join(tmpDir, "output") |
| 47 |
generatorInputDir := filepath.Join(e.librarianDir, config.GeneratorInputDir) |
| 48 |
for _, dir := range []string{e.librarianDir, e.sourceDir, e.outputDir, generatorInputDir} { |
| 49 |
if err := os.Mkdir(dir, 0755); err != nil { |
| 50 |
t.Fatalf("failed to create dir %s: %v", dir, err) |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
// An empty config file is valid, and any modules that are requested will |
| 55 |
// just get default values. |
| 56 |
configFile := filepath.Join(generatorInputDir, config.RepoConfigFile) |
| 57 |
err = os.WriteFile(configFile, make([]byte, 0), 0644) |
| 58 |
if err != nil { |
| 59 |
t.Fatalf("failed to create file %s: %v", configFile, err) |
| 60 |
} |
| 61 |
|
| 62 |
return e |
| 63 |
} |
| 64 |
|
| 65 |
// cleanup removes the temporary directory. |
| 66 |
func (e *testEnv) cleanup(t *testing.T) { |
| 67 |
t.Helper() |
| 68 |
if err := os.RemoveAll(e.tmpDir); err != nil { |
| 69 |
t.Fatalf("failed to remove temp dir: %v", err) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// writeRequestFile writes a generate-request.json file. |
| 74 |
func (e *testEnv) writeRequestFile(t *testing.T, content string) { |
| 75 |
t.Helper() |
| 76 |
p := filepath.Join(e.librarianDir, "generate-request.json") |
| 77 |
if err := os.WriteFile(p, []byte(content), 0644); err != nil { |
| 78 |
t.Fatalf("failed to write request file: %v", err) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
// writeBazelFile writes a BUILD.bazel file. |
| 83 |
func (e *testEnv) writeBazelFile(t *testing.T, apiPath, content string) { |
| 84 |
t.Helper() |
| 85 |
apiDir := filepath.Join(e.sourceDir, apiPath) |
| 86 |
if err := os.MkdirAll(apiDir, 0755); err != nil { |
| 87 |
t.Fatalf("failed to create api dir: %v", err) |
| 88 |
} |
| 89 |
// Create a fake .proto file, which is required by the protoc command builder. |
| 90 |
if err := os.WriteFile(filepath.Join(apiDir, "fake.proto"), nil, 0644); err != nil { |
| 91 |
t.Fatalf("failed to write fake proto file: %v", err) |
| 92 |
} |
| 93 |
p := filepath.Join(apiDir, "BUILD.bazel") |
| 94 |
if err := os.WriteFile(p, []byte(content), 0644); err != nil { |
| 95 |
t.Fatalf("failed to write bazel file: %v", err) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
// writeServiceYAML writes a service.yaml file. |
| 100 |
func (e *testEnv) writeServiceYAML(t *testing.T, apiPath, title string) { |
| 101 |
t.Helper() |
| 102 |
apiDir := filepath.Join(e.sourceDir, apiPath) |
| 103 |
content := "title: " + title |
| 104 |
p := filepath.Join(apiDir, "service.yaml") |
| 105 |
if err := os.WriteFile(p, []byte(content), 0644); err != nil { |
| 106 |
t.Fatalf("failed to write service yaml: %v", err) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
type postProcessRecorder struct { |
| 111 |
called bool |
| 112 |
} |
| 113 |
|
| 114 |
func (r *postProcessRecorder) record(ctx context.Context, req *request.Library, outputDir, moduleDir string, moduleConfig *config.ModuleConfig) error { |
| 115 |
r.called = true |
| 116 |
return nil |
| 117 |
} |
| 118 |
|
| 119 |
func TestGenerate(t *testing.T) { |
| 120 |
singleAPIRequest := `{"id": "foo", "apis": [{"path": "api/v1"}]}` |
| 121 |
multiAPIRequest := `{"id": "foo", "apis": [{"path": "api/v1"}, {"path": "api/v2"}]}` |
| 122 |
validBazel := ` |
| 123 |
go_gapic_library( |
| 124 |
name = "v1_gapic", |
| 125 |
importpath = "path/to/v1;v1", |
| 126 |
grpc_service_config = "service_config.json", |
| 127 |
service_yaml = "service.yaml", |
| 128 |
transport = "grpc", |
| 129 |
) |
| 130 |
` |
| 131 |
invalidBazel := ` |
| 132 |
go_gapic_library( |
| 133 |
name = "v1_gapic", |
| 134 |
importpath = "path/to/v1;v1", |
| 135 |
) |
| 136 |
` |
| 137 |
tests := []struct { |
| 138 |
name string |
| 139 |
setup func(e *testEnv, t *testing.T) |
| 140 |
protocErr error |
| 141 |
wantErr bool |
| 142 |
wantProtocRunCount int |
| 143 |
}{ |
| 144 |
{ |
| 145 |
name: "happy path", |
| 146 |
setup: func(e *testEnv, t *testing.T) { |
| 147 |
e.writeRequestFile(t, singleAPIRequest) |
| 148 |
e.writeBazelFile(t, "api/v1", validBazel) |
| 149 |
e.writeServiceYAML(t, "api/v1", "My API") |
| 150 |
}, |
| 151 |
wantErr: false, |
| 152 |
wantProtocRunCount: 1, |
| 153 |
}, |
| 154 |
{ |
| 155 |
name: "multi-api request uses first api config", |
| 156 |
setup: func(e *testEnv, t *testing.T) { |
| 157 |
e.writeRequestFile(t, multiAPIRequest) |
| 158 |
e.writeBazelFile(t, "api/v1", validBazel) |
| 159 |
e.writeServiceYAML(t, "api/v1", "First API") |
| 160 |
e.writeBazelFile(t, "api/v2", validBazel) |
| 161 |
e.writeServiceYAML(t, "api/v2", "Second API") |
| 162 |
}, |
| 163 |
wantErr: false, |
| 164 |
wantProtocRunCount: 2, |
| 165 |
}, |
| 166 |
{ |
| 167 |
name: "missing request file", |
| 168 |
setup: func(e *testEnv, t *testing.T) { |
| 169 |
e.writeBazelFile(t, "api/v1", validBazel) |
| 170 |
}, |
| 171 |
wantErr: true, |
| 172 |
}, |
| 173 |
{ |
| 174 |
name: "missing bazel file", |
| 175 |
setup: func(e *testEnv, t *testing.T) { |
| 176 |
e.writeRequestFile(t, singleAPIRequest) |
| 177 |
}, |
| 178 |
wantErr: true, |
| 179 |
}, |
| 180 |
{ |
| 181 |
name: "invalid bazel config", |
| 182 |
setup: func(e *testEnv, t *testing.T) { |
| 183 |
e.writeRequestFile(t, singleAPIRequest) |
| 184 |
e.writeBazelFile(t, "api/v1", invalidBazel) |
| 185 |
}, |
| 186 |
wantErr: true, |
| 187 |
}, |
| 188 |
{ |
| 189 |
name: "protoc fails", |
| 190 |
setup: func(e *testEnv, t *testing.T) { |
| 191 |
e.writeRequestFile(t, singleAPIRequest) |
| 192 |
e.writeBazelFile(t, "api/v1", validBazel) |
| 193 |
e.writeServiceYAML(t, "api/v1", "My API") |
| 194 |
}, |
| 195 |
protocErr: errors.New("protoc failed"), |
| 196 |
wantErr: true, |
| 197 |
wantProtocRunCount: 1, |
| 198 |
}, |
| 199 |
} |
| 200 |
|
| 201 |
for _, tt := range tests { |
| 202 |
t.Run(tt.name, func(t *testing.T) { |
| 203 |
e := newTestEnv(t) |
| 204 |
defer e.cleanup(t) |
| 205 |
|
| 206 |
tt.setup(e, t) |
| 207 |
|
| 208 |
var protocRunCount int |
| 209 |
execvRun = func(ctx context.Context, args []string, dir string) error { |
| 210 |
want := "protoc" |
| 211 |
if args[0] != want { |
| 212 |
t.Errorf("protocRun called with %s; want %s", args[0], want) |
| 213 |
} |
| 214 |
if tt.protocErr == nil { |
| 215 |
// Simulate protoc creating the nested directory. |
| 216 |
if err := os.MkdirAll(filepath.Join(e.outputDir, "cloud.google.com", "go"), 0755); err != nil { |
| 217 |
t.Fatalf("failed to create nested dir: %v", err) |
| 218 |
} |
| 219 |
} |
| 220 |
protocRunCount++ |
| 221 |
return tt.protocErr |
| 222 |
} |
| 223 |
recorder := &postProcessRecorder{} |
| 224 |
postProcess = recorder.record |
| 225 |
|
| 226 |
cfg := &Config{ |
| 227 |
LibrarianDir: e.librarianDir, |
| 228 |
InputDir: "fake-input", |
| 229 |
OutputDir: e.outputDir, |
| 230 |
SourceDir: e.sourceDir, |
| 231 |
DisablePostProcessor: tt.name != "happy path" && tt.name != "multi-api request uses first api config", |
| 232 |
} |
| 233 |
|
| 234 |
if err := Generate(context.Background(), cfg); (err != nil) != tt.wantErr { |
| 235 |
t.Errorf("Generate() error = %v, wantErr %v", err, tt.wantErr) |
| 236 |
} |
| 237 |
|
| 238 |
if protocRunCount != tt.wantProtocRunCount { |
| 239 |
t.Errorf("protocRun called = %v; want %v", protocRunCount, tt.wantProtocRunCount) |
| 240 |
} |
| 241 |
}) |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
func TestFixPermissions(t *testing.T) { |
| 246 |
// Create a temporary directory for the test. |
| 247 |
tmpDir, err := os.MkdirTemp("", "permissions-test") |
| 248 |
if err != nil { |
| 249 |
t.Fatalf("failed to create temp dir: %v", err) |
| 250 |
} |
| 251 |
defer os.RemoveAll(tmpDir) |
| 252 |
|
| 253 |
// Create a subdirectory to test recursive behavior. |
| 254 |
subDir := filepath.Join(tmpDir, "subdir") |
| 255 |
if err := os.Mkdir(subDir, 0755); err != nil { |
| 256 |
t.Fatalf("failed to create subdir: %v", err) |
| 257 |
} |
| 258 |
|
| 259 |
// Create test files with incorrect permissions. |
| 260 |
goFile1 := filepath.Join(tmpDir, "file1.go") |
| 261 |
goFile2 := filepath.Join(subDir, "file2.go") |
| 262 |
otherFile := filepath.Join(tmpDir, "other.txt") |
| 263 |
|
| 264 |
if err := os.WriteFile(goFile1, []byte("package main"), 0777); err != nil { |
| 265 |
t.Fatalf("failed to write goFile1: %v", err) |
| 266 |
} |
| 267 |
if err := os.WriteFile(goFile2, []byte("package main"), 0777); err != nil { |
| 268 |
t.Fatalf("failed to write goFile2: %v", err) |
| 269 |
} |
| 270 |
if err := os.WriteFile(otherFile, []byte("some text"), 0777); err != nil { |
| 271 |
t.Fatalf("failed to write otherFile: %v", err) |
| 272 |
} |
| 273 |
|
| 274 |
// Run the function to fix permissions. |
| 275 |
if err := fixPermissions(tmpDir); err != nil { |
| 276 |
t.Fatalf("fixPermissions() failed: %v", err) |
| 277 |
} |
| 278 |
|
| 279 |
// Check permissions of the .go files. |
| 280 |
for _, f := range []string{goFile1, goFile2} { |
| 281 |
info, err := os.Stat(f) |
| 282 |
if err != nil { |
| 283 |
t.Fatalf("failed to stat %s: %v", f, err) |
| 284 |
} |
| 285 |
if info.Mode().Perm() != 0644 { |
| 286 |
t.Errorf("permission of %s is %v, want 0644", f, info.Mode().Perm()) |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
// Check that the permission of the other file is unchanged. |
| 291 |
info, err := os.Stat(otherFile) |
| 292 |
if err != nil { |
| 293 |
t.Fatalf("failed to stat %s: %v", otherFile, err) |
| 294 |
} |
| 295 |
if info.Mode().Perm() == 0644 { |
| 296 |
t.Errorf("permission of %s was changed, should not have been", otherFile) |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
func TestFlattenOutput(t *testing.T) { |
| 301 |
// Create a temporary directory for the test. |
| 302 |
tmpDir, err := os.MkdirTemp("", "flatten-test") |
| 303 |
if err != nil { |
| 304 |
t.Fatalf("failed to create temp dir: %v", err) |
| 305 |
} |
| 306 |
defer os.RemoveAll(tmpDir) |
| 307 |
|
| 308 |
// Create the nested directory structure. |
| 309 |
goDir := filepath.Join(tmpDir, "cloud.google.com", "go") |
| 310 |
if err := os.MkdirAll(goDir, 0755); err != nil { |
| 311 |
t.Fatalf("failed to create goDir: %v", err) |
| 312 |
} |
| 313 |
|
| 314 |
// Create a file to be moved. |
| 315 |
filePath := filepath.Join(goDir, "file.txt") |
| 316 |
if err := os.WriteFile(filePath, []byte("test"), 0644); err != nil { |
| 317 |
t.Fatalf("failed to write file: %v", err) |
| 318 |
} |
| 319 |
|
| 320 |
// Run the flatten function. |
| 321 |
if err := flattenOutput(tmpDir); err != nil { |
| 322 |
t.Fatalf("flattenOutput() failed: %v", err) |
| 323 |
} |
| 324 |
|
| 325 |
// Check that the file was moved to the top level. |
| 326 |
newFilePath := filepath.Join(tmpDir, "file.txt") |
| 327 |
if _, err := os.Stat(newFilePath); os.IsNotExist(err) { |
| 328 |
t.Errorf("file was not moved to the top level") |
| 329 |
} |
| 330 |
|
| 331 |
// Check that the old directory was removed. |
| 332 |
if _, err := os.Stat(filepath.Join(tmpDir, "cloud.google.com")); !os.IsNotExist(err) { |
| 333 |
t.Errorf("old directory was not removed") |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
func TestConfig_Validate(t *testing.T) { |
| 338 |
tests := []struct { |
| 339 |
name string |
| 340 |
cfg *Config |
| 341 |
wantErr bool |
| 342 |
}{ |
| 343 |
{ |
| 344 |
name: "valid", |
| 345 |
cfg: &Config{ |
| 346 |
LibrarianDir: "a", |
| 347 |
InputDir: "b", |
| 348 |
OutputDir: "c", |
| 349 |
SourceDir: "d", |
| 350 |
}, |
| 351 |
wantErr: false, |
| 352 |
}, |
| 353 |
{ |
| 354 |
name: "missing librarian dir", |
| 355 |
cfg: &Config{ |
| 356 |
InputDir: "b", |
| 357 |
OutputDir: "c", |
| 358 |
SourceDir: "d", |
| 359 |
}, |
| 360 |
wantErr: true, |
| 361 |
}, |
| 362 |
{ |
| 363 |
name: "missing input dir", |
| 364 |
cfg: &Config{ |
| 365 |
LibrarianDir: "a", |
| 366 |
OutputDir: "c", |
| 367 |
SourceDir: "d", |
| 368 |
}, |
| 369 |
wantErr: true, |
| 370 |
}, |
| 371 |
{ |
| 372 |
name: "missing output dir", |
| 373 |
cfg: &Config{ |
| 374 |
LibrarianDir: "a", |
| 375 |
InputDir: "b", |
| 376 |
SourceDir: "d", |
| 377 |
}, |
| 378 |
wantErr: true, |
| 379 |
}, |
| 380 |
{ |
| 381 |
name: "missing source dir", |
| 382 |
cfg: &Config{ |
| 383 |
LibrarianDir: "a", |
| 384 |
InputDir: "b", |
| 385 |
OutputDir: "c", |
| 386 |
}, |
| 387 |
wantErr: true, |
| 388 |
}, |
| 389 |
} |
| 390 |
for _, tt := range tests { |
| 391 |
t.Run(tt.name, func(t *testing.T) { |
| 392 |
if err := tt.cfg.Validate(); (err != nil) != tt.wantErr { |
| 393 |
t.Errorf("Config.Validate() error = %v, wantErr %v", err, tt.wantErr) |
| 394 |
} |
| 395 |
}) |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
func TestApplyModuleVersion(t *testing.T) { |
| 400 |
tests := []struct { |
| 401 |
name string |
| 402 |
moduleName string |
| 403 |
modulePath string |
| 404 |
setupFiles []string |
| 405 |
wantErr bool |
| 406 |
wantPresent []string |
| 407 |
wantAbsent []string |
| 408 |
}{ |
| 409 |
{ |
| 410 |
name: "v1", |
| 411 |
moduleName: "functions", |
| 412 |
modulePath: "cloud.google.com/go/functions", |
| 413 |
setupFiles: []string{"functions/apiv1/client.go", "internal/generated/snippets/functions/apiv1/snippets.go"}, |
| 414 |
wantPresent: []string{"functions/apiv1/client.go", "internal/generated/snippets/functions/apiv1/snippets.go"}, |
| 415 |
}, |
| 416 |
{ |
| 417 |
name: "v2", |
| 418 |
moduleName: "functions", |
| 419 |
modulePath: "cloud.google.com/go/functions/v2", |
| 420 |
setupFiles: []string{"functions/v2/apiv1/client.go", "internal/generated/snippets/functions/v2/apiv1/snippets.go"}, |
| 421 |
wantPresent: []string{"functions/apiv1/client.go", "internal/generated/snippets/functions/apiv1/snippets.go"}, |
| 422 |
wantAbsent: []string{"functions/v2/apiv1/client.go", "internal/generated/snippets/functions/v2/apiv1/snippets.go"}, |
| 423 |
}, |
| 424 |
{ |
| 425 |
name: "unexpected module path format", |
| 426 |
moduleName: "bogus", |
| 427 |
modulePath: "cloud.google.com/go/bogus/v1/v2", |
| 428 |
wantErr: true, |
| 429 |
}, |
| 430 |
} |
| 431 |
|
| 432 |
for _, tt := range tests { |
| 433 |
t.Run(tt.name, func(t *testing.T) { |
| 434 |
tmpDir, err := os.MkdirTemp("", "apply-module-version-test") |
| 435 |
if err != nil { |
| 436 |
t.Fatalf("failed to create temp dir: %v", err) |
| 437 |
} |
| 438 |
defer os.RemoveAll(tmpDir) |
| 439 |
|
| 440 |
for _, file := range tt.setupFiles { |
| 441 |
fullFile := filepath.Join(tmpDir, file) |
| 442 |
fullDir := filepath.Dir(fullFile) |
| 443 |
if err := os.MkdirAll(fullDir, 0755); err != nil { |
| 444 |
t.Fatalf("failed to create dir %s: %v", fullDir, err) |
| 445 |
} |
| 446 |
if err := os.WriteFile(fullFile, []byte("test"), 0644); err != nil { |
| 447 |
t.Fatalf("failed to write file %s: %v", fullFile, err) |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
if err := applyModuleVersion(tmpDir, tt.modulePath); (err != nil) != tt.wantErr { |
| 452 |
t.Errorf("applyModuleVersion() error = %v, wantErr %v", err, tt.wantErr) |
| 453 |
} |
| 454 |
|
| 455 |
if tt.wantErr { |
| 456 |
return |
| 457 |
} |
| 458 |
|
| 459 |
for _, wantPresent := range tt.wantPresent { |
| 460 |
if _, err := os.Stat(filepath.Join(tmpDir, wantPresent)); err != nil { |
| 461 |
t.Errorf("wanted present, was absent: %s", wantPresent) |
| 462 |
} |
| 463 |
} |
| 464 |
for _, wantAbsent := range tt.wantAbsent { |
| 465 |
if _, err := os.Stat(filepath.Join(tmpDir, wantAbsent)); !os.IsNotExist(err) { |
| 466 |
t.Errorf("wanted absent, was missing: %s", wantAbsent) |
| 467 |
} |
| 468 |
} |
| 469 |
}) |
| 470 |
} |
| 471 |
} |