Skip to content

Commit ea924c1

Browse files
authored
fix(internal/librarian/golang): skip updating snippet metadata in nested module (#4115)
Skip updating snippet metadata in nested module because it belongs to another Go library. For #3617
1 parent 751eac2 commit ea924c1

2 files changed

Lines changed: 92 additions & 1 deletion

File tree

internal/librarian/golang/generate.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ func generateREADME(library *config.Library, api *serviceconfig.API, moduleRoot
363363
}
364364

365365
// updateSnippetMetadata updates the snippet metadata files with the correct library version.
366+
// Skip nested module if exists.
366367
func updateSnippetMetadata(library *config.Library, output string) error {
367368
baseDir := filepath.Join(output, "internal", "generated", "snippets", library.Name)
368369
return filepath.WalkDir(baseDir, func(path string, d fs.DirEntry, err error) error {
@@ -373,7 +374,13 @@ func updateSnippetMetadata(library *config.Library, output string) error {
373374
}
374375
return err
375376
}
376-
if d.IsDir() || !strings.HasPrefix(d.Name(), "snippet_metadata") {
377+
if d.IsDir() {
378+
if library.Go != nil && d.Name() == library.Go.NestedModule {
379+
return fs.SkipDir
380+
}
381+
return nil
382+
}
383+
if !strings.HasPrefix(d.Name(), "snippet_metadata") {
377384
return nil
378385
}
379386
read, err := os.ReadFile(path)

internal/librarian/golang/generate_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,90 @@ func TestUpdateSnippetMetadata(t *testing.T) {
419419
}
420420
}
421421

422+
func TestUpdateSnippetMetadata_Skipped(t *testing.T) {
423+
for _, test := range []struct {
424+
name string
425+
library *config.Library
426+
dir string
427+
fileName string
428+
data string
429+
}{
430+
{
431+
name: "skipped due to nested module",
432+
library: &config.Library{
433+
Name: "bigquery",
434+
Version: "1.2.3",
435+
Go: &config.GoModule{
436+
NestedModule: "v2",
437+
},
438+
},
439+
dir: filepath.Join("internal", "generated", "snippets", "bigquery", "v2", "apiv2"),
440+
fileName: "snippet_metadata.google.cloud.bigquery.v2.json",
441+
data: `{
442+
"clientLibrary": {
443+
"name": "cloud.google.com/go/bigquery/v2/apiv2",
444+
"version": "$VERSION",
445+
"language": "GO",
446+
"apis": [
447+
{
448+
"id": "google.cloud.bigquery.v2",
449+
"version": "v2"
450+
}
451+
]
452+
}
453+
}
454+
`,
455+
},
456+
{
457+
name: "skipped due to non-snippets file name",
458+
library: &config.Library{
459+
Name: "bigquery",
460+
Version: "1.2.3",
461+
},
462+
dir: filepath.Join("internal", "generated", "snippets", "bigquery", "apiv1"),
463+
fileName: "non_metadata.google.cloud.bigquery.v1.json",
464+
data: `{
465+
"clientLibrary": {
466+
"name": "cloud.google.com/go/bigquery/apiv1",
467+
"version": "$VERSION",
468+
"language": "GO",
469+
"apis": [
470+
{
471+
"id": "google.cloud.bigquery.v1",
472+
"version": "v1"
473+
}
474+
]
475+
}
476+
}
477+
`,
478+
},
479+
} {
480+
t.Run(test.name, func(t *testing.T) {
481+
tmpDir := t.TempDir()
482+
err := os.MkdirAll(filepath.Join(tmpDir, test.dir), 0755)
483+
if err != nil {
484+
t.Fatal(err)
485+
}
486+
metadataFile := filepath.Join(tmpDir, test.dir, test.fileName)
487+
if err := os.WriteFile(metadataFile, []byte(test.data), 0755); err != nil {
488+
t.Fatal(err)
489+
}
490+
if err := updateSnippetMetadata(test.library, tmpDir); err != nil {
491+
t.Fatal(err)
492+
}
493+
494+
content, err := os.ReadFile(metadataFile)
495+
if err != nil {
496+
t.Fatal(err)
497+
}
498+
s := string(content)
499+
if !strings.Contains(s, "$VERSION") {
500+
t.Errorf("want version in snippet metadata, got:\n%s", s)
501+
}
502+
})
503+
}
504+
}
505+
422506
func TestBuildGAPICImportPath(t *testing.T) {
423507
for _, test := range []struct {
424508
name string

0 commit comments

Comments
 (0)