Skip to content

Commit

Permalink
updated the files lock.go and update.go
Browse files Browse the repository at this point in the history
Signed-off-by: BalaadityaPatanjali <[email protected]>

added the required changes

Signed-off-by: BalaadityaPatanjali <[email protected]>

added the required changes

Signed-off-by: BalaadityaPatanjali <[email protected]>
  • Loading branch information
BalaadityaPatanjali committed Jul 8, 2024
1 parent 0cb31b1 commit f7cb3a3
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 3 deletions.
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)
}
})
}
}

0 comments on commit f7cb3a3

Please sign in to comment.