diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 79052528..aa8304f8 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -12,4 +12,5 @@ We contributors to BuildSafe: * Rakshit(@rakshitgondwal) * Praful(@Horiodino) * Sanyam(@sanyamjain04) -* Hanshal(@hanshal101) \ No newline at end of file +* Hanshal(@hanshal101) +* Balaaditya(@BalaadityaPatanjali) \ No newline at end of file diff --git a/cmd/update/update.go b/cmd/update/update.go index b28366c0..743621e3 100644 --- a/cmd/update/update.go +++ b/cmd/update/update.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "sync" + "sort" "github.com/spf13/cobra" "golang.org/x/mod/semver" @@ -66,6 +67,10 @@ var UpdateCmd = &cobra.Command{ devVersions := parsePackagesForUpdates(devVersionMap) runtimeVersions := parsePackagesForUpdates(runtimeVersionMap) + // Sorted the versions + sort.Strings(devVersions) + sort.Strings(runtimeVersions) + newPackages := hcl2nix.Packages{ Development: devVersions, Runtime: runtimeVersions, diff --git a/pkg/hcl2nix/lock.go b/pkg/hcl2nix/lock.go index a7796c81..96996291 100644 --- a/pkg/hcl2nix/lock.go +++ b/pkg/hcl2nix/lock.go @@ -9,6 +9,7 @@ import ( "slices" "strings" "sync" + "sort" buildsafev1 "github.com/buildsafedev/bsf-apis/go/buildsafe/v1" bstrings "github.com/buildsafedev/bsf/pkg/strings" @@ -145,10 +146,16 @@ func ResolvePackages(ctx context.Context, sc buildsafev1.SearchServiceClient, pa if errStr != "" { return nil, fmt.Errorf(errStr) } + sort.Slice(resolvedPackages, func(i, j int) bool { + pi, pj := resolvedPackages[i].Package, resolvedPackages[j].Package + if pi.Name != pj.Name { + return pi.Name < pj.Name + } + return pi.Version < pj.Version + }) return resolvedPackages, nil } - // ResolvePackage resolves package name func resolvePackage(ctx context.Context, sc buildsafev1.SearchServiceClient, pkg string) (*buildsafev1.Package, error) { var desiredVersion *buildsafev1.FetchPackageVersionResponse diff --git a/pkg/hcl2nix/lock_test.go b/pkg/hcl2nix/lock_test.go index e1eb4511..38d02ed3 100644 --- a/pkg/hcl2nix/lock_test.go +++ b/pkg/hcl2nix/lock_test.go @@ -3,6 +3,7 @@ package hcl2nix import ( "reflect" "testing" + "sort" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" @@ -130,7 +131,7 @@ func TestMapPackageCategory(t *testing.T) { }, { - name: "Test Case 2", + name: "Test Case 3", packages: Packages{ Runtime: []string{"go@1.20", "node@14.0"}, Development: []string{"go@1.20", "node@14.0"}, @@ -150,3 +151,79 @@ func TestMapPackageCategory(t *testing.T) { }) } } + +func TestResolvePackagesSorting(t *testing.T) { + tests := []struct { + name string + pkgVersions []LockPackage + sortedExpected []LockPackage + }{ + { + name: "test sorting", + pkgVersions: []LockPackage{ + { + Package: &buildsafev1.Package{ + Name: "pkgB", + Version: "1.1.0", + }, + Runtime: true, + }, + { + Package: &buildsafev1.Package{ + Name: "pkgA", + Version: "1.2.0", + }, + Runtime: false, + }, + { + Package: &buildsafev1.Package{ + Name: "pkgA", + Version: "1.0.0", + }, + Runtime: false, + }, + }, + sortedExpected: []LockPackage{ + { + Package: &buildsafev1.Package{ + Name: "pkgA", + Version: "1.0.0", + }, + Runtime: false, + }, + { + Package: &buildsafev1.Package{ + Name: "pkgA", + Version: "1.2.0", + }, + Runtime: false, + }, + { + Package: &buildsafev1.Package{ + Name: "pkgB", + Version: "1.1.0", + }, + Runtime: true, + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Sort the package versions + sortedPackages := tt.pkgVersions + sort.Slice(sortedPackages, func(i, j int) bool { + pi, pj := sortedPackages[i].Package, sortedPackages[j].Package + if pi.Name != pj.Name { + return pi.Name < pj.Name + } + return pi.Version < pj.Version + }) + + if !reflect.DeepEqual(sortedPackages, tt.sortedExpected) { + t.Errorf("sortedPackages = %v, want %v", sortedPackages, tt.sortedExpected) + } + }) + } +}