Skip to content

Commit 1c34da6

Browse files
authored
chore(internal/config): remove version map in librarian config (#3073)
Remove version map in librarian config, use `library.Version` instead. Fixes #3070
1 parent 60f79ae commit 1c34da6

File tree

8 files changed

+172
-57
lines changed

8 files changed

+172
-57
lines changed

internal/config/config.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
package config
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"os"
2223

2324
"gopkg.in/yaml.v3"
2425
)
2526

27+
var errLibraryNotFound = errors.New("library not found")
28+
2629
// Config represents the complete librarian.yaml configuration file.
2730
type Config struct {
2831
// Language is the primary language for this repository (go, python, rust).
@@ -39,13 +42,7 @@ type Config struct {
3942

4043
// Libraries contains configuration overrides for libraries that need special handling.
4144
// Only include libraries that differ from defaults.
42-
// Versions are looked up from the Versions map below.
4345
Libraries []*Library `yaml:"libraries,omitempty"`
44-
45-
// Versions contains version numbers for all libraries.
46-
// This is the source of truth for release versions.
47-
// Key is library name, value is version string.
48-
Versions map[string]string `yaml:"versions,omitempty"`
4946
}
5047

5148
// Sources contains references to external source repositories.
@@ -220,3 +217,18 @@ func (c *Config) Write(path string) error {
220217

221218
return nil
222219
}
220+
221+
// LibraryByName returns a library with the given name.
222+
func (c *Config) LibraryByName(name string) (*Library, error) {
223+
if c.Libraries == nil {
224+
return nil, errLibraryNotFound
225+
}
226+
227+
for _, library := range c.Libraries {
228+
if library.Name == name {
229+
return library, nil
230+
}
231+
}
232+
233+
return nil, errLibraryNotFound
234+
}

internal/config/config_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package config
1616

1717
import (
1818
"bytes"
19+
"errors"
1920
"testing"
2021

2122
"github.com/google/go-cmp/cmp"
@@ -47,3 +48,71 @@ func TestReadWrite(t *testing.T) {
4748
t.Errorf("mismatch (-want +got):\n%s", diff)
4849
}
4950
}
51+
52+
func TestLibraryByName(t *testing.T) {
53+
for _, test := range []struct {
54+
name string
55+
libraryName string
56+
config *Config
57+
want *Library
58+
wantErr error
59+
}{
60+
{
61+
name: "find_a_library",
62+
libraryName: "example-library",
63+
config: &Config{
64+
Libraries: []*Library{
65+
{
66+
Name: "example-library",
67+
},
68+
{
69+
Name: "another-library",
70+
},
71+
},
72+
},
73+
want: &Library{
74+
Name: "example-library",
75+
},
76+
},
77+
{
78+
name: "no_library_in_config",
79+
libraryName: "example-library",
80+
config: &Config{},
81+
wantErr: errLibraryNotFound,
82+
},
83+
{
84+
name: "does_not_find_a_library",
85+
libraryName: "non-existent-library",
86+
config: &Config{
87+
Libraries: []*Library{
88+
{
89+
Name: "example-library",
90+
},
91+
{
92+
Name: "another-library",
93+
},
94+
},
95+
},
96+
wantErr: errLibraryNotFound,
97+
},
98+
} {
99+
t.Run(test.name, func(t *testing.T) {
100+
got, err := test.config.LibraryByName(test.libraryName)
101+
if test.wantErr != nil {
102+
if !errors.Is(err, test.wantErr) {
103+
t.Errorf("got error %v, want %v", err, test.wantErr)
104+
}
105+
return
106+
}
107+
108+
if err != nil {
109+
t.Errorf("LibraryByName(%q): %v", test.libraryName, err)
110+
return
111+
}
112+
113+
if diff := cmp.Diff(test.want, got); diff != "" {
114+
t.Errorf("mismatch (-want +got):\n%s", diff)
115+
}
116+
})
117+
}
118+
}

internal/config/testdata/rust/librarian.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ libraries:
4040
- name: google-cloud-secretmanager-v1
4141
channels:
4242
- path: google/cloud/secretmanager/v1
43+
version: 1.2.3
4344
- name: google-cloud-storage-v2
4445
channels:
4546
- path: google/cloud/storage/v2
4647
rust:
4748
disabled_rustdoc_warnings:
4849
- rustdoc::bare_urls
49-
versions:
50-
google-cloud-secretmanager-v1: 0.1.0
51-
google-cloud-storage-v2: 0.2.0
50+
version: 2.3.4

internal/language/internal/rust/release.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ func ReleaseLibrary(cfg *config.Config, name string) (*config.Config, error) {
4646
}
4747

4848
func release(cfg *config.Config, name string) (*config.Config, error) {
49-
if cfg.Versions == nil {
50-
cfg.Versions = make(map[string]string)
51-
}
52-
5349
shouldRelease := func(pkgName string) bool {
5450
// If name is the empty string, release everything.
5551
if name == "" {
@@ -93,7 +89,11 @@ func release(cfg *config.Config, name string) (*config.Config, error) {
9389
if err := rustrelease.UpdateCargoVersion(path, newVersion); err != nil {
9490
return err
9591
}
96-
cfg.Versions[manifest.Package.Name] = newVersion
92+
library, err := cfg.LibraryByName(manifest.Package.Name)
93+
if err != nil {
94+
return err
95+
}
96+
library.Version = newVersion
9797
return nil
9898
})
9999
if err != nil {

internal/language/internal/rust/release_test.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"strings"
2222
"testing"
2323

24-
"github.com/google/go-cmp/cmp"
2524
cmdtest "github.com/googleapis/librarian/internal/command"
2625
"github.com/googleapis/librarian/internal/config"
2726
)
@@ -49,13 +48,8 @@ func TestReleaseAll(t *testing.T) {
4948

5049
checkCargoVersion(t, storageCargo, storageReleased)
5150
checkCargoVersion(t, secretmanagerCargo, secretmanagerReleased)
52-
want := map[string]string{
53-
storageName: storageReleased,
54-
secretmanagerName: secretmanagerReleased,
55-
}
56-
if diff := cmp.Diff(want, got.Versions); diff != "" {
57-
t.Errorf("mismatch (-want +got):\n%s", diff)
58-
}
51+
checkLibraryVersion(t, got, storageName, storageReleased)
52+
checkLibraryVersion(t, got, secretmanagerName, secretmanagerReleased)
5953
}
6054

6155
func TestReleaseOne(t *testing.T) {
@@ -67,13 +61,8 @@ func TestReleaseOne(t *testing.T) {
6761

6862
checkCargoVersion(t, storageCargo, storageReleased)
6963
checkCargoVersion(t, secretmanagerCargo, secretmanagerInitial)
70-
want := map[string]string{
71-
storageName: storageReleased,
72-
secretmanagerName: secretmanagerInitial,
73-
}
74-
if diff := cmp.Diff(want, got.Versions); diff != "" {
75-
t.Errorf("mismatch (-want +got):\n%s", diff)
76-
}
64+
checkLibraryVersion(t, got, storageName, storageReleased)
65+
checkLibraryVersion(t, got, secretmanagerName, secretmanagerInitial)
7766
}
7867

7968
func setupRelease(t *testing.T) *config.Config {
@@ -86,9 +75,15 @@ func setupRelease(t *testing.T) *config.Config {
8675
createCrate(t, storageDir, storageName, storageInitial)
8776
createCrate(t, secretmanagerDir, secretmanagerName, secretmanagerInitial)
8877
return &config.Config{
89-
Versions: map[string]string{
90-
storageName: storageInitial,
91-
secretmanagerName: secretmanagerInitial,
78+
Libraries: []*config.Library{
79+
{
80+
Name: storageName,
81+
Version: storageInitial,
82+
},
83+
{
84+
Name: secretmanagerName,
85+
Version: secretmanagerInitial,
86+
},
9287
},
9388
}
9489
}
@@ -122,3 +117,16 @@ func checkCargoVersion(t *testing.T, path, wantVersion string) {
122117
t.Errorf("%s version mismatch:\nwant line: %q\ngot:\n%s", path, wantLine, got)
123118
}
124119
}
120+
121+
func checkLibraryVersion(t *testing.T, cfg *config.Config, name, wantVersion string) {
122+
t.Helper()
123+
for _, lib := range cfg.Libraries {
124+
if lib.Name == name {
125+
if lib.Version != wantVersion {
126+
t.Errorf("library %q version mismatch: want %q, got %q", name, wantVersion, lib.Version)
127+
}
128+
return
129+
}
130+
}
131+
t.Errorf("library %q not found in config", name)
132+
}

internal/language/release_test.go

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,64 @@ import (
2424
func TestReleaseAll(t *testing.T) {
2525
cfg := &config.Config{
2626
Language: "testhelper",
27-
Versions: map[string]string{
28-
"lib1": "0.1.0",
29-
"lib2": "0.2.0",
27+
Libraries: []*config.Library{
28+
{
29+
Name: "lib-1",
30+
Version: "0.1.0",
31+
},
32+
{
33+
Name: "lib-2",
34+
Version: "0.2.0",
35+
},
3036
},
3137
}
38+
3239
cfg, err := ReleaseAll(cfg)
3340
if err != nil {
3441
t.Fatal(err)
3542
}
43+
gotVersions := make(map[string]string)
44+
for _, lib := range cfg.Libraries {
45+
gotVersions[lib.Name] = lib.Version
46+
}
3647
want := map[string]string{
37-
"lib1": TestReleaseVersion,
38-
"lib2": TestReleaseVersion,
48+
"lib-1": TestReleaseVersion,
49+
"lib-2": TestReleaseVersion,
3950
}
40-
if diff := cmp.Diff(want, cfg.Versions); diff != "" {
51+
if diff := cmp.Diff(want, gotVersions); diff != "" {
4152
t.Errorf("mismatch (-want +got):\n%s", diff)
4253
}
4354
}
4455

4556
func TestReleaseLibrary(t *testing.T) {
4657
cfg := &config.Config{
4758
Language: "testhelper",
48-
Versions: map[string]string{
49-
"lib1": "0.1.0",
50-
"lib2": "0.2.0",
59+
Libraries: []*config.Library{
60+
{
61+
Name: "lib-1",
62+
Version: "0.1.0",
63+
},
64+
{
65+
Name: "lib-2",
66+
Version: "0.2.0",
67+
},
5168
},
5269
}
53-
cfg, err := ReleaseLibrary(cfg, "lib1")
70+
71+
cfg, err := ReleaseLibrary(cfg, "lib-1")
5472
if err != nil {
5573
t.Fatal(err)
5674
}
75+
76+
gotVersions := make(map[string]string)
77+
for _, lib := range cfg.Libraries {
78+
gotVersions[lib.Name] = lib.Version
79+
}
5780
want := map[string]string{
58-
"lib1": TestReleaseVersion,
59-
"lib2": "0.2.0",
81+
"lib-1": TestReleaseVersion,
82+
"lib-2": "0.2.0",
6083
}
61-
if diff := cmp.Diff(want, cfg.Versions); diff != "" {
84+
if diff := cmp.Diff(want, gotVersions); diff != "" {
6285
t.Errorf("mismatch (-want +got):\n%s", diff)
6386
}
6487
}

internal/language/testhelper.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,20 @@ import (
2727
const TestReleaseVersion = "1.2.3"
2828

2929
func testReleaseAll(cfg *config.Config) (*config.Config, error) {
30-
if cfg.Versions == nil {
31-
cfg.Versions = make(map[string]string)
32-
}
33-
for k := range cfg.Versions {
34-
cfg.Versions[k] = TestReleaseVersion
30+
for _, lib := range cfg.Libraries {
31+
lib.Version = TestReleaseVersion
3532
}
3633
return cfg, nil
3734
}
3835

3936
func testReleaseLibrary(cfg *config.Config, name string) (*config.Config, error) {
40-
if cfg.Versions == nil {
41-
cfg.Versions = make(map[string]string)
37+
for _, lib := range cfg.Libraries {
38+
if lib.Name == name {
39+
lib.Version = TestReleaseVersion
40+
return cfg, nil
41+
}
4242
}
43-
cfg.Versions[name] = TestReleaseVersion
44-
return cfg, nil
43+
return nil, fmt.Errorf("library %q not found", name)
4544
}
4645

4746
func testGenerate(library *config.Library) error {

internal/librarian/release_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ func TestReleaseCommand(t *testing.T) {
6666

6767
configPath := filepath.Join(tempDir, librarianConfigPath)
6868
configContent := fmt.Sprintf(`language: testhelper
69-
versions:
70-
%s: 0.1.0
69+
libraries:
70+
- name: %s
71+
version: 0.1.0
7172
`, testlib)
7273
if err := os.WriteFile(configPath, []byte(configContent), 0644); err != nil {
7374
t.Fatal(err)
@@ -86,7 +87,11 @@ versions:
8687
if err != nil {
8788
t.Fatal(err)
8889
}
89-
if diff := cmp.Diff(test.wantVersions, cfg.Versions); diff != "" {
90+
gotVersions := make(map[string]string)
91+
for _, lib := range cfg.Libraries {
92+
gotVersions[lib.Name] = lib.Version
93+
}
94+
if diff := cmp.Diff(test.wantVersions, gotVersions); diff != "" {
9095
t.Errorf("mismatch (-want +got):\n%s", diff)
9196
}
9297
}

0 commit comments

Comments
 (0)