Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Making sure of alphabetical sorting in packages #59

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ We contributors to BuildSafe:
* Rakshit(@rakshitgondwal)
* Praful(@Horiodino)
* Sanyam(@sanyamjain04)
* Hanshal(@hanshal101)
* Hanshal(@hanshal101)
* Balaaditya(@BalaadityaPatanjali)
5 changes: 5 additions & 0 deletions cmd/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"sync"
"sort"

"github.com/spf13/cobra"
"golang.org/x/mod/semver"
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion pkg/hcl2nix/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
79 changes: 78 additions & 1 deletion pkg/hcl2nix/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hcl2nix
import (
"reflect"
"testing"
"sort"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
Expand Down Expand Up @@ -130,7 +131,7 @@ func TestMapPackageCategory(t *testing.T) {
},

{
name: "Test Case 2",
name: "Test Case 3",
packages: Packages{
Runtime: []string{"[email protected]", "[email protected]"},
Development: []string{"[email protected]", "[email protected]"},
Expand All @@ -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)
}
})
}
}
Loading