Skip to content

Commit 3d5a87e

Browse files
authored
board listall (in json/machine readable output) return boards sorted with the same order as in the original boards.txt (#1903)
* Simplified internal boards menu maps construction * Redesign boards build loop to iterate following boards.txt boards order * Return board list with the same ordering as in original boards.txt * Added integration test
1 parent 2ad24a7 commit 3d5a87e

File tree

5 files changed

+130
-12
lines changed

5 files changed

+130
-12
lines changed

Diff for: arduino/cores/cores.go

+8
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type PlatformRelease struct {
6363
Platform *Platform `json:"-"`
6464
Properties *properties.Map `json:"-"`
6565
Boards map[string]*Board `json:"-"`
66+
orderedBoards []*Board `json:"-"` // The Boards of this platform, in the order they are defined in the boards.txt file.
6667
Programmers map[string]*Programmer `json:"-"`
6768
Menus *properties.Map `json:"-"`
6869
InstallDir *paths.Path `json:"-"`
@@ -292,9 +293,16 @@ func (release *PlatformRelease) GetOrCreateBoard(boardID string) *Board {
292293
PlatformRelease: release,
293294
}
294295
release.Boards[boardID] = board
296+
release.orderedBoards = append(release.orderedBoards, board)
295297
return board
296298
}
297299

300+
// GetBoards returns the boards in this platforms in the order they
301+
// are defined in the platform.txt file.
302+
func (release *PlatformRelease) GetBoards() []*Board {
303+
return release.orderedBoards
304+
}
305+
298306
// RequiresToolRelease returns true if the PlatformRelease requires the
299307
// toolReleased passed as parameter
300308
func (release *PlatformRelease) RequiresToolRelease(toolRelease *ToolRelease) bool {

Diff for: arduino/cores/packagemanager/loader.go

+8-11
Original file line numberDiff line numberDiff line change
@@ -470,18 +470,15 @@ func (pm *Builder) loadBoards(platform *cores.PlatformRelease) error {
470470
return err
471471
}
472472

473-
propertiesByBoard := boardsProperties.FirstLevelOf()
473+
platform.Menus = boardsProperties.SubTree("menu")
474474

475-
if menus, ok := propertiesByBoard["menu"]; ok {
476-
platform.Menus = menus
477-
} else {
478-
platform.Menus = properties.NewMap()
479-
}
480-
// This is not a board id so we remove it to correctly
481-
// set all other boards properties
482-
delete(propertiesByBoard, "menu")
483-
484-
for boardID, boardProperties := range propertiesByBoard {
475+
// Build to boards structure following the boards.txt board ordering
476+
for _, boardID := range boardsProperties.FirstLevelKeys() {
477+
if boardID == "menu" {
478+
// This is not a board id so we remove it to correctly set all other boards properties
479+
continue
480+
}
481+
boardProperties := boardsProperties.SubTree(boardID)
485482
var board *cores.Board
486483
if !platform.PluggableDiscoveryAware {
487484
convertVidPidIdentificationPropertiesToPluggableDiscovery(boardProperties)

Diff for: arduino/cores/packagemanager/package_manager_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,56 @@ func TestBoardOptionsFunctions(t *testing.T) {
218218
}
219219
}
220220

221+
func TestBoardOrdering(t *testing.T) {
222+
pmb := packagemanager.NewBuilder(dataDir1, dataDir1.Join("packages"), nil, nil, "")
223+
_ = pmb.LoadHardwareFromDirectories(paths.NewPathList(dataDir1.Join("packages").String()))
224+
pm := pmb.Build()
225+
pme, release := pm.NewExplorer()
226+
defer release()
227+
228+
pl := pme.FindPlatform(&packagemanager.PlatformReference{
229+
Package: "arduino",
230+
PlatformArchitecture: "avr",
231+
})
232+
require.NotNil(t, pl)
233+
plReleases := pl.GetAllInstalled()
234+
require.NotEmpty(t, plReleases)
235+
avr := plReleases[0]
236+
res := []string{}
237+
for _, board := range avr.GetBoards() {
238+
res = append(res, board.Name())
239+
}
240+
expected := []string{
241+
"Arduino Yún",
242+
"Arduino Uno",
243+
"Arduino Duemilanove or Diecimila",
244+
"Arduino Nano",
245+
"Arduino Mega or Mega 2560",
246+
"Arduino Mega ADK",
247+
"Arduino Leonardo",
248+
"Arduino Leonardo ETH",
249+
"Arduino Micro",
250+
"Arduino Esplora",
251+
"Arduino Mini",
252+
"Arduino Ethernet",
253+
"Arduino Fio",
254+
"Arduino BT",
255+
"LilyPad Arduino USB",
256+
"LilyPad Arduino",
257+
"Arduino Pro or Pro Mini",
258+
"Arduino NG or older",
259+
"Arduino Robot Control",
260+
"Arduino Robot Motor",
261+
"Arduino Gemma",
262+
"Adafruit Circuit Playground",
263+
"Arduino Yún Mini",
264+
"Arduino Industrial 101",
265+
"Linino One",
266+
"Arduino Uno WiFi",
267+
}
268+
require.Equal(t, expected, res)
269+
}
270+
221271
func TestFindToolsRequiredForBoard(t *testing.T) {
222272
os.Setenv("ARDUINO_DATA_DIR", dataDir1.String())
223273
configuration.Settings = configuration.Init("")

Diff for: commands/board/listall.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func ListAll(ctx context.Context, req *rpc.BoardListAllRequest) (*rpc.BoardListA
7070
targetPackage.Maintainer,
7171
}
7272

73-
for _, board := range installedPlatformRelease.Boards {
73+
for _, board := range installedPlatformRelease.GetBoards() {
7474
if !req.GetIncludeHiddenBoards() && board.IsHidden() {
7575
continue
7676
}

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

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2022 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package board_test
17+
18+
import (
19+
"testing"
20+
21+
"github.com/arduino/arduino-cli/internal/integrationtest"
22+
"github.com/stretchr/testify/require"
23+
"go.bug.st/testifyjson/requirejson"
24+
)
25+
26+
func TestCorrectBoardListOrdering(t *testing.T) {
27+
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
28+
defer env.CleanUp()
29+
30+
_, _, err := cli.Run("core", "install", "arduino:avr")
31+
require.NoError(t, err)
32+
jsonOut, _, err := cli.Run("board", "listall", "--format", "json")
33+
require.NoError(t, err)
34+
requirejson.Query(t, jsonOut, "[.boards[] | .fqbn]", `[
35+
"arduino:avr:yun",
36+
"arduino:avr:uno",
37+
"arduino:avr:unomini",
38+
"arduino:avr:diecimila",
39+
"arduino:avr:nano",
40+
"arduino:avr:mega",
41+
"arduino:avr:megaADK",
42+
"arduino:avr:leonardo",
43+
"arduino:avr:leonardoeth",
44+
"arduino:avr:micro",
45+
"arduino:avr:esplora",
46+
"arduino:avr:mini",
47+
"arduino:avr:ethernet",
48+
"arduino:avr:fio",
49+
"arduino:avr:bt",
50+
"arduino:avr:LilyPadUSB",
51+
"arduino:avr:lilypad",
52+
"arduino:avr:pro",
53+
"arduino:avr:atmegang",
54+
"arduino:avr:robotControl",
55+
"arduino:avr:robotMotor",
56+
"arduino:avr:gemma",
57+
"arduino:avr:circuitplay32u4cat",
58+
"arduino:avr:yunmini",
59+
"arduino:avr:chiwawa",
60+
"arduino:avr:one",
61+
"arduino:avr:unowifi"
62+
]`)
63+
}

0 commit comments

Comments
 (0)