Skip to content

Commit 4f8141c

Browse files
authored
feat: add per-library override configurations to .librarian/config.yaml (#2039)
feat: add LibraryConfigFor(libraryID string) Step 1 towards #1704 Fixes #2040
1 parent f114aa2 commit 4f8141c

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

internal/config/librarian_config.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,14 @@ const (
2626

2727
// LibrarianConfig defines the contract for the config.yaml file.
2828
type LibrarianConfig struct {
29-
GlobalFilesAllowlist []*GlobalFile `yaml:"global_files_allowlist"`
29+
GlobalFilesAllowlist []*GlobalFile `yaml:"global_files_allowlist"`
30+
Libraries []*LibraryConfig `yaml:"libraries"`
31+
}
32+
33+
// LibraryConfig defines configuration for a single library, identified by its ID.
34+
type LibraryConfig struct {
35+
LibraryID string `yaml:"id"`
36+
NextVersion string `yaml:"next_version"`
3037
}
3138

3239
// GlobalFile defines the global files in language repositories.
@@ -55,3 +62,13 @@ func (g *LibrarianConfig) Validate() error {
5562

5663
return nil
5764
}
65+
66+
// LibraryConfigFor finds the LibraryConfig entry for a given LibraryID.
67+
func (g *LibrarianConfig) LibraryConfigFor(LibraryID string) *LibraryConfig {
68+
for _, lib := range g.Libraries {
69+
if lib.LibraryID == LibraryID {
70+
return lib
71+
}
72+
}
73+
return nil
74+
}

internal/config/librarian_config_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ package config
1717
import (
1818
"strings"
1919
"testing"
20+
21+
"github.com/google/go-cmp/cmp"
2022
)
2123

2224
func TestGlobalConfig_Validate(t *testing.T) {
@@ -100,3 +102,67 @@ func TestGlobalConfig_Validate(t *testing.T) {
100102
})
101103
}
102104
}
105+
106+
func TestLibraryConfigFor(t *testing.T) {
107+
cases := []struct {
108+
name string
109+
config *LibrarianConfig
110+
LibraryID string
111+
wantLibrary *LibraryConfig
112+
wantErr bool
113+
wantErrSubstr string
114+
}{
115+
{
116+
name: "library found",
117+
config: &LibrarianConfig{
118+
Libraries: []*LibraryConfig{
119+
{LibraryID: "lib1", NextVersion: "1.0.0"},
120+
{LibraryID: "lib2", NextVersion: "2.0.0"},
121+
},
122+
},
123+
LibraryID: "lib1",
124+
wantLibrary: &LibraryConfig{LibraryID: "lib1", NextVersion: "1.0.0"},
125+
},
126+
{
127+
name: "library not found",
128+
config: &LibrarianConfig{
129+
Libraries: []*LibraryConfig{
130+
{LibraryID: "lib1", NextVersion: "1.0.0"},
131+
{LibraryID: "lib2", NextVersion: "2.0.0"},
132+
},
133+
},
134+
LibraryID: "lib3",
135+
wantLibrary: nil,
136+
},
137+
{
138+
name: "empty libraries",
139+
config: &LibrarianConfig{
140+
Libraries: []*LibraryConfig{},
141+
},
142+
LibraryID: "lib1",
143+
wantLibrary: nil,
144+
},
145+
{
146+
name: "multiple libraries with target in middle",
147+
config: &LibrarianConfig{
148+
Libraries: []*LibraryConfig{
149+
{LibraryID: "lib1", NextVersion: "1.0.0"},
150+
{LibraryID: "lib2", NextVersion: "2.0.0"},
151+
{LibraryID: "lib3", NextVersion: "3.0.0"},
152+
},
153+
},
154+
LibraryID: "lib2",
155+
wantLibrary: &LibraryConfig{LibraryID: "lib2", NextVersion: "2.0.0"},
156+
},
157+
}
158+
159+
for _, tc := range cases {
160+
t.Run(tc.name, func(t *testing.T) {
161+
gotLibrary := tc.config.LibraryConfigFor(tc.LibraryID)
162+
163+
if diff := cmp.Diff(tc.wantLibrary, gotLibrary); diff != "" {
164+
t.Errorf("LibraryConfigFor() mismatch (-want +got):\n%s", diff)
165+
}
166+
})
167+
}
168+
}

0 commit comments

Comments
 (0)