File tree Expand file tree Collapse file tree 2 files changed +64
-2
lines changed Expand file tree Collapse file tree 2 files changed +64
-2
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ import (
2424 "os"
2525 "path"
2626 "path/filepath"
27+ "sort"
2728 "strings"
2829
2930 "github.com/googleapis/librarian/internal/config"
@@ -186,15 +187,23 @@ func findServiceConfigIn(path string) (string, error) {
186187}
187188
188189func saveLibrarianState (repoDir string , state * config.LibrarianState ) error {
189- path := filepath .Join (repoDir , config .LibrarianDir , librarianStateFile )
190+ sortByLibraryID (state )
191+ stateFile := filepath .Join (repoDir , config .LibrarianDir , librarianStateFile )
190192 var buffer bytes.Buffer
191193 encoder := yaml .NewEncoder (& buffer )
192194 encoder .SetIndent (2 )
193195 err := encoder .Encode (state )
194196 if err != nil {
195197 return err
196198 }
197- return os .WriteFile (path , buffer .Bytes (), 0644 )
199+ return os .WriteFile (stateFile , buffer .Bytes (), 0644 )
200+ }
201+
202+ // sortByLibraryID sorts config.LibraryState with respect to ID.
203+ func sortByLibraryID (state * config.LibrarianState ) {
204+ sort .Slice (state .Libraries , func (i , j int ) bool {
205+ return state .Libraries [i ].ID < state .Libraries [j ].ID
206+ })
198207}
199208
200209// readLibraryState reads the library state from a container response, if it exists.
Original file line number Diff line number Diff line change @@ -493,3 +493,56 @@ func TestLoadRepoStateFromGitHub(t *testing.T) {
493493 })
494494 }
495495}
496+
497+ func TestSortByLibraryID (t * testing.T ) {
498+ t .Parallel ()
499+ for _ , test := range []struct {
500+ name string
501+ state * config.LibrarianState
502+ want * config.LibrarianState
503+ }{
504+ {
505+ name : "sort_with_library_id" ,
506+ state : & config.LibrarianState {
507+ Image : "test-image" ,
508+ Libraries : []* config.LibraryState {
509+ {
510+ ID : "b-library" ,
511+ },
512+ {
513+ ID : "a-library" ,
514+ },
515+ },
516+ },
517+ want : & config.LibrarianState {
518+ Image : "test-image" ,
519+ Libraries : []* config.LibraryState {
520+ {
521+ ID : "a-library" ,
522+ },
523+ {
524+ ID : "b-library" ,
525+ },
526+ },
527+ },
528+ },
529+ {
530+ name : "nil_libraries" ,
531+ state : & config.LibrarianState {
532+ Image : "test-image" ,
533+ Libraries : nil ,
534+ },
535+ want : & config.LibrarianState {
536+ Image : "test-image" ,
537+ Libraries : nil ,
538+ },
539+ },
540+ } {
541+ t .Run (test .name , func (t * testing.T ) {
542+ sortByLibraryID (test .state )
543+ if diff := cmp .Diff (test .want , test .state ); diff != "" {
544+ t .Errorf ("sortByLibraryID mismatch (-want +got):\n %s" , diff )
545+ }
546+ })
547+ }
548+ }
You can’t perform that action at this time.
0 commit comments