Unverified Commit 38a0dfd5 authored by Luca Bianconi's avatar Luca Bianconi Committed by GitHub

fix: consistent boards list ordering across output formats (#2025)

parent 50918b49
...@@ -17,9 +17,11 @@ package board ...@@ -17,9 +17,11 @@ package board
import ( import (
"context" "context"
"sort"
"strings" "strings"
"github.com/arduino/arduino-cli/arduino" "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/arduino/utils"
"github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/commands"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
...@@ -36,8 +38,8 @@ func ListAll(ctx context.Context, req *rpc.BoardListAllRequest) (*rpc.BoardListA ...@@ -36,8 +38,8 @@ func ListAll(ctx context.Context, req *rpc.BoardListAllRequest) (*rpc.BoardListA
searchArgs := strings.Join(req.GetSearchArgs(), " ") searchArgs := strings.Join(req.GetSearchArgs(), " ")
list := &rpc.BoardListAllResponse{Boards: []*rpc.BoardListItem{}} list := &rpc.BoardListAllResponse{Boards: []*rpc.BoardListItem{}}
for _, targetPackage := range pme.GetPackages() { for _, targetPackage := range toSortedPackageArray(pme.GetPackages()) {
for _, platform := range targetPackage.Platforms { for _, platform := range toSortedPlatformArray(targetPackage.Platforms) {
installedPlatformRelease := pme.GetInstalledPlatformRelease(platform) installedPlatformRelease := pme.GetInstalledPlatformRelease(platform)
// We only want to list boards for installed platforms // We only want to list boards for installed platforms
if installedPlatformRelease == nil { if installedPlatformRelease == nil {
...@@ -93,3 +95,37 @@ func ListAll(ctx context.Context, req *rpc.BoardListAllRequest) (*rpc.BoardListA ...@@ -93,3 +95,37 @@ func ListAll(ctx context.Context, req *rpc.BoardListAllRequest) (*rpc.BoardListA
return list, nil 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
}
...@@ -33,7 +33,10 @@ func TestCorrectBoardListOrdering(t *testing.T) { ...@@ -33,7 +33,10 @@ func TestCorrectBoardListOrdering(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp() 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) require.NoError(t, err)
jsonOut, _, err := cli.Run("board", "listall", "--format", "json") jsonOut, _, err := cli.Run("board", "listall", "--format", "json")
require.NoError(t, err) require.NoError(t, err)
...@@ -64,7 +67,9 @@ func TestCorrectBoardListOrdering(t *testing.T) { ...@@ -64,7 +67,9 @@ func TestCorrectBoardListOrdering(t *testing.T) {
"arduino:avr:yunmini", "arduino:avr:yunmini",
"arduino:avr:chiwawa", "arduino:avr:chiwawa",
"arduino:avr:one", "arduino:avr:one",
"arduino:avr:unowifi" "arduino:avr:unowifi",
"arduino:sam:arduino_due_x_dbg",
"arduino:sam:arduino_due_x"
]`) ]`)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment