Skip to content

Commit

Permalink
fix: consistent boards list ordering across output formats (arduino#2025
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Bikappa authored Jan 4, 2023
1 parent 50918b4 commit 38a0dfd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
40 changes: 38 additions & 2 deletions commands/board/listall.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ package board

import (
"context"
"sort"
"strings"

"github.com/arduino/arduino-cli/arduino"
"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/utils"
"github.com/arduino/arduino-cli/commands"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
Expand All @@ -36,8 +38,8 @@ func ListAll(ctx context.Context, req *rpc.BoardListAllRequest) (*rpc.BoardListA
searchArgs := strings.Join(req.GetSearchArgs(), " ")

list := &rpc.BoardListAllResponse{Boards: []*rpc.BoardListItem{}}
for _, targetPackage := range pme.GetPackages() {
for _, platform := range targetPackage.Platforms {
for _, targetPackage := range toSortedPackageArray(pme.GetPackages()) {
for _, platform := range toSortedPlatformArray(targetPackage.Platforms) {
installedPlatformRelease := pme.GetInstalledPlatformRelease(platform)
// We only want to list boards for installed platforms
if installedPlatformRelease == nil {
Expand Down Expand Up @@ -93,3 +95,37 @@ func ListAll(ctx context.Context, req *rpc.BoardListAllRequest) (*rpc.BoardListA

return list, nil
}

// TODO use a generic function instead of the two below once go >1.18 is adopted.
// Without generics we either have to create multiple functions for different map types
// or resort to type assertions on the caller side

// toSortedPackageArray takes a packages map and returns its values as array
// ordered by the map keys alphabetically
func toSortedPackageArray(sourceMap cores.Packages) []*cores.Package {
keys := []string{}
for key := range sourceMap {
keys = append(keys, key)
}
sort.Strings(keys)
sortedValues := make([]*cores.Package, len(keys))
for i, key := range keys {
sortedValues[i] = sourceMap[key]
}
return sortedValues
}

// toSortedPlatformArray takes a packages map and returns its values as array
// ordered by the map keys alphabetically
func toSortedPlatformArray(sourceMap map[string]*cores.Platform) []*cores.Platform {
keys := []string{}
for key := range sourceMap {
keys = append(keys, key)
}
sort.Strings(keys)
sortedValues := make([]*cores.Platform, len(keys))
for i, key := range keys {
sortedValues[i] = sourceMap[key]
}
return sortedValues
}
9 changes: 7 additions & 2 deletions internal/integrationtest/board/board_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ func TestCorrectBoardListOrdering(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()

_, _, err := cli.Run("core", "install", "arduino:avr")
// install two cores, boards must be ordered by package name and platform name
_, _, err := cli.Run("core", "install", "arduino:sam")
require.NoError(t, err)
_, _, err = cli.Run("core", "install", "arduino:avr")
require.NoError(t, err)
jsonOut, _, err := cli.Run("board", "listall", "--format", "json")
require.NoError(t, err)
Expand Down Expand Up @@ -64,7 +67,9 @@ func TestCorrectBoardListOrdering(t *testing.T) {
"arduino:avr:yunmini",
"arduino:avr:chiwawa",
"arduino:avr:one",
"arduino:avr:unowifi"
"arduino:avr:unowifi",
"arduino:sam:arduino_due_x_dbg",
"arduino:sam:arduino_due_x"
]`)
}

Expand Down

0 comments on commit 38a0dfd

Please sign in to comment.