Skip to content

Commit c3fe4ef

Browse files
authored
feat(internal/librarian/python): bump snippet metadata versions (#4030)
Towards #3389
1 parent 105fd65 commit c3fe4ef

File tree

4 files changed

+51
-15
lines changed

4 files changed

+51
-15
lines changed

internal/librarian/python/bump.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"os"
2222
"path/filepath"
2323
"strings"
24+
25+
"github.com/googleapis/librarian/internal/snippetmetadata"
2426
)
2527

2628
const (
@@ -37,7 +39,11 @@ var (
3739
// Bump updates the version number in the library with the given output
3840
// directory.
3941
func Bump(output, version string) error {
40-
return bumpGapicVersions(output, version)
42+
if err := bumpGapicVersions(output, version); err != nil {
43+
return err
44+
}
45+
snippetsDir := filepath.Join(output, "samples", "generated_samples")
46+
return snippetmetadata.UpdateAllLibraryVersions(snippetsDir, version)
4147
}
4248

4349
// bumpGapicVersion finds all gapic_version.py files under output. For each

internal/librarian/python/bump_test.go

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,34 @@ import (
2222
"testing"
2323

2424
"github.com/google/go-cmp/cmp"
25+
"github.com/googleapis/librarian/internal/snippetmetadata"
2526
)
2627

2728
func TestBump(t *testing.T) {
2829
const readme = "This is a readme file"
2930
const versionBefore = "line1\n" + gapicVersionLinePrefix + "\"1.2.2\"# Other stuff\n" + "line3"
3031
const versionAfter = "line1\n" + gapicVersionLinePrefix + "\"1.2.3\"\n" + "line3"
32+
const snippetMetadataBefore = `{
33+
"clientLibrary": {
34+
"otherField": "x",
35+
"version": "1.2.2"
36+
},
37+
"otherField": "y"
38+
}`
39+
const snippetMetadataAfter = `{
40+
"clientLibrary": {
41+
"otherField": "x",
42+
"version": "1.2.3"
43+
},
44+
"otherField": "y"
45+
}`
3146
initial := map[string]string{
32-
"README.txt": readme,
33-
"docs/README.txt": readme,
34-
"google/cloud/iam/" + gapicVersionFile: versionBefore,
35-
"google/cloud/iam_v1/" + gapicVersionFile: versionBefore,
36-
"other/" + gapicVersionFile: versionBefore,
47+
"README.txt": readme,
48+
"docs/README.txt": readme,
49+
"google/cloud/iam/" + gapicVersionFile: versionBefore,
50+
"google/cloud/iam_v1/" + gapicVersionFile: versionBefore,
51+
"other/" + gapicVersionFile: versionBefore,
52+
"samples/generated_samples/snippet_metadata.json": snippetMetadataBefore,
3753
}
3854
dir := t.TempDir()
3955
for file, content := range initial {
@@ -51,11 +67,12 @@ func TestBump(t *testing.T) {
5167
}
5268

5369
wantAfter := map[string]string{
54-
"README.txt": readme,
55-
"docs/README.txt": readme,
56-
"google/cloud/iam/gapic_version.py": versionAfter,
57-
"google/cloud/iam_v1/gapic_version.py": versionAfter,
58-
"other/gapic_version.py": versionAfter,
70+
"README.txt": readme,
71+
"docs/README.txt": readme,
72+
"google/cloud/iam/gapic_version.py": versionAfter,
73+
"google/cloud/iam_v1/gapic_version.py": versionAfter,
74+
"other/gapic_version.py": versionAfter,
75+
"samples/generated_samples/snippet_metadata.json": snippetMetadataAfter,
5976
}
6077
for file, want := range wantAfter {
6178
got, err := os.ReadFile(filepath.Join(dir, file))
@@ -95,6 +112,17 @@ func TestBump_Error(t *testing.T) {
95112
},
96113
wantErr: errNoVersionFound,
97114
},
115+
{
116+
name: "snippet metadata file is invalid",
117+
setup: func(dir string) error {
118+
snippetMetadataPath := filepath.Join(dir, "samples", "generated_samples", "snippet_metadata.json")
119+
if err := os.MkdirAll(filepath.Dir(snippetMetadataPath), 0755); err != nil {
120+
t.Fatal(err)
121+
}
122+
return os.WriteFile(snippetMetadataPath, []byte("{}"), 0644)
123+
},
124+
wantErr: snippetmetadata.ErrNoClientLibraryField,
125+
},
98126
} {
99127
t.Run(test.name, func(t *testing.T) {
100128
dir := t.TempDir()
@@ -103,7 +131,7 @@ func TestBump_Error(t *testing.T) {
103131
}
104132
gotErr := Bump(dir, "1.2.3")
105133
if !errors.Is(gotErr, test.wantErr) {
106-
t.Errorf("bumpSingleGapicVersion() error = %v, wantErr %v", gotErr, test.wantErr)
134+
t.Errorf("bump() error = %v, wantErr %v", gotErr, test.wantErr)
107135
}
108136
})
109137
}

internal/snippetmetadata/snippetmetadata.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import (
2626
)
2727

2828
var (
29-
errNoClientLibraryField = errors.New("no clientLibrary field at the top level")
29+
// ErrNoClientLibraryField is the error returned if a snippet metadata file
30+
// has no clientLibrary field at the top level.
31+
ErrNoClientLibraryField = errors.New("no clientLibrary field at the top level")
3032
errSnippetMetadataDirectory = errors.New("expected file; was a directory")
3133
errSnippetMetadataLink = errors.New("expected regular file; was a link")
3234
)
@@ -65,7 +67,7 @@ func updateLibraryVersion(path, version string) error {
6567
}
6668
clientLibrary, ok := metadata["clientLibrary"].(map[string]any)
6769
if !ok {
68-
return fmt.Errorf("error updating snippet metadata file %s: %w", path, errNoClientLibraryField)
70+
return fmt.Errorf("error updating snippet metadata file %s: %w", path, ErrNoClientLibraryField)
6971
}
7072
clientLibrary["version"] = version
7173
return writeMetadata(path, metadata)

internal/snippetmetadata/snippetmetadata_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestUpdateLibraryVersion_Error(t *testing.T) {
7171
t.Fatal(err)
7272
}
7373
},
74-
wantErr: errNoClientLibraryField,
74+
wantErr: ErrNoClientLibraryField,
7575
},
7676
{
7777
name: "readonly file",

0 commit comments

Comments
 (0)