File tree Expand file tree Collapse file tree 2 files changed +24
-2
lines changed Expand file tree Collapse file tree 2 files changed +24
-2
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments