Skip to content

Commit 6188338

Browse files
authored
fix(internal/config): validate library IDs are unique (#2187)
Fixes #2186 (Additionally, fixes a panic if a test that wants an error doesn't produce one.)
1 parent 3cdc998 commit 6188338

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

internal/config/state.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,15 @@ func (s *LibrarianState) Validate() error {
4747
if len(s.Libraries) == 0 {
4848
return fmt.Errorf("libraries cannot be empty")
4949
}
50+
seenLibraryIDs := make(map[string]bool)
5051
for i, l := range s.Libraries {
5152
if l == nil {
5253
return fmt.Errorf("library at index %d cannot be nil", i)
5354
}
55+
if _, exists := seenLibraryIDs[l.ID]; exists {
56+
return fmt.Errorf("duplicate library ID %s", l.ID)
57+
}
58+
seenLibraryIDs[l.ID] = true
5459
if err := l.Validate(); err != nil {
5560
return fmt.Errorf("invalid library at index %d: %w", i, err)
5661
}

internal/config/state_test.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,31 @@ func TestLibrarianState_Validate(t *testing.T) {
7171
wantErr: true,
7272
wantErrMsg: "libraries cannot be empty",
7373
},
74+
{
75+
name: "duplicate library IDs",
76+
state: &LibrarianState{
77+
Image: "gcr.io/test/image:v1.2.3",
78+
Libraries: []*LibraryState{
79+
{
80+
ID: "x",
81+
SourceRoots: []string{"src/x"},
82+
},
83+
{
84+
ID: "x",
85+
SourceRoots: []string{"src/x"},
86+
},
87+
},
88+
},
89+
wantErr: true,
90+
wantErrMsg: "duplicate library ID",
91+
},
7492
} {
7593
t.Run(test.name, func(t *testing.T) {
7694
err := test.state.Validate()
7795
if test.wantErr {
7896
if err == nil {
7997
t.Error("Librarian.Validate() should fail")
80-
}
81-
if !strings.Contains(err.Error(), test.wantErrMsg) {
98+
} else if !strings.Contains(err.Error(), test.wantErrMsg) {
8299
t.Errorf("want error message %q, got %q", test.wantErrMsg, err.Error())
83100
}
84101

0 commit comments

Comments
 (0)