Skip to content

fix: consistent boards list ordering #2025

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

Merged
merged 1 commit into from
Jan 4, 2023
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
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