Skip to content

Commit d3c3ef6

Browse files
author
Luca Bianconi
committed
fix: consistent boards list ordering across formats
1 parent 558130b commit d3c3ef6

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

Diff for: commands/board/listall.go

+38-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ package board
1717

1818
import (
1919
"context"
20+
"sort"
2021
"strings"
2122

2223
"github.com/arduino/arduino-cli/arduino"
24+
"github.com/arduino/arduino-cli/arduino/cores"
2325
"github.com/arduino/arduino-cli/arduino/utils"
2426
"github.com/arduino/arduino-cli/commands"
2527
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
3638
searchArgs := strings.Join(req.GetSearchArgs(), " ")
3739

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

9496
return list, nil
9597
}
98+
99+
// TODO use a generic function instead of the two below once go >1.18 is adopted.
100+
// Without generics we either have to create multiple functions for different map types
101+
// or resort to type assertions on the caller side
102+
103+
// toSortedPackageArray takes a packages map and returns its values as array
104+
// ordered by the map keys alphabetically
105+
func toSortedPackageArray(sourceMap cores.Packages) []*cores.Package {
106+
keys := []string{}
107+
for key := range sourceMap {
108+
keys = append(keys, key)
109+
}
110+
sort.Strings(keys)
111+
sortedValues := make([]*cores.Package, len(keys))
112+
for i, key := range keys {
113+
sortedValues[i] = sourceMap[key]
114+
}
115+
return sortedValues
116+
}
117+
118+
// toSortedPlatformArray takes a packages map and returns its values as array
119+
// ordered by the map keys alphabetically
120+
func toSortedPlatformArray(sourceMap map[string]*cores.Platform) []*cores.Platform {
121+
keys := []string{}
122+
for key := range sourceMap {
123+
keys = append(keys, key)
124+
}
125+
sort.Strings(keys)
126+
sortedValues := make([]*cores.Platform, len(keys))
127+
for i, key := range keys {
128+
sortedValues[i] = sourceMap[key]
129+
}
130+
return sortedValues
131+
}

Diff for: internal/integrationtest/board/board_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ func TestCorrectBoardListOrdering(t *testing.T) {
3333
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
3434
defer env.CleanUp()
3535

36-
_, _, err := cli.Run("core", "install", "arduino:avr")
36+
// install two cores, boards must be ordered by package name and platform name
37+
_, _, err := cli.Run("core", "install", "arduino:sam")
38+
require.NoError(t, err)
39+
_, _, err = cli.Run("core", "install", "arduino:avr")
3740
require.NoError(t, err)
3841
jsonOut, _, err := cli.Run("board", "listall", "--format", "json")
3942
require.NoError(t, err)
@@ -64,7 +67,9 @@ func TestCorrectBoardListOrdering(t *testing.T) {
6467
"arduino:avr:yunmini",
6568
"arduino:avr:chiwawa",
6669
"arduino:avr:one",
67-
"arduino:avr:unowifi"
70+
"arduino:avr:unowifi",
71+
"arduino:sam:arduino_due_x_dbg",
72+
"arduino:sam:arduino_due_x"
6873
]`)
6974
}
7075

0 commit comments

Comments
 (0)