Skip to content

Let board listall return boards sorted with the same order as in the original boards.txt #1903

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 4 commits into from
Oct 4, 2022
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
8 changes: 8 additions & 0 deletions arduino/cores/cores.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type PlatformRelease struct {
Platform *Platform `json:"-"`
Properties *properties.Map `json:"-"`
Boards map[string]*Board `json:"-"`
orderedBoards []*Board `json:"-"` // The Boards of this platform, in the order they are defined in the boards.txt file.
Programmers map[string]*Programmer `json:"-"`
Menus *properties.Map `json:"-"`
InstallDir *paths.Path `json:"-"`
Expand Down Expand Up @@ -292,9 +293,16 @@ func (release *PlatformRelease) GetOrCreateBoard(boardID string) *Board {
PlatformRelease: release,
}
release.Boards[boardID] = board
release.orderedBoards = append(release.orderedBoards, board)
return board
}

// GetBoards returns the boards in this platforms in the order they
// are defined in the platform.txt file.
func (release *PlatformRelease) GetBoards() []*Board {
return release.orderedBoards
}

// RequiresToolRelease returns true if the PlatformRelease requires the
// toolReleased passed as parameter
func (release *PlatformRelease) RequiresToolRelease(toolRelease *ToolRelease) bool {
Expand Down
19 changes: 8 additions & 11 deletions arduino/cores/packagemanager/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,18 +470,15 @@ func (pm *Builder) loadBoards(platform *cores.PlatformRelease) error {
return err
}

propertiesByBoard := boardsProperties.FirstLevelOf()
platform.Menus = boardsProperties.SubTree("menu")

if menus, ok := propertiesByBoard["menu"]; ok {
platform.Menus = menus
} else {
platform.Menus = properties.NewMap()
}
// This is not a board id so we remove it to correctly
// set all other boards properties
delete(propertiesByBoard, "menu")

for boardID, boardProperties := range propertiesByBoard {
// Build to boards structure following the boards.txt board ordering
for _, boardID := range boardsProperties.FirstLevelKeys() {
if boardID == "menu" {
// This is not a board id so we remove it to correctly set all other boards properties
continue
}
boardProperties := boardsProperties.SubTree(boardID)
var board *cores.Board
if !platform.PluggableDiscoveryAware {
convertVidPidIdentificationPropertiesToPluggableDiscovery(boardProperties)
Expand Down
50 changes: 50 additions & 0 deletions arduino/cores/packagemanager/package_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,56 @@ func TestBoardOptionsFunctions(t *testing.T) {
}
}

func TestBoardOrdering(t *testing.T) {
pmb := packagemanager.NewBuilder(dataDir1, dataDir1.Join("packages"), nil, nil, "")
_ = pmb.LoadHardwareFromDirectories(paths.NewPathList(dataDir1.Join("packages").String()))
pm := pmb.Build()
pme, release := pm.NewExplorer()
defer release()

pl := pme.FindPlatform(&packagemanager.PlatformReference{
Package: "arduino",
PlatformArchitecture: "avr",
})
require.NotNil(t, pl)
plReleases := pl.GetAllInstalled()
require.NotEmpty(t, plReleases)
avr := plReleases[0]
res := []string{}
for _, board := range avr.GetBoards() {
res = append(res, board.Name())
}
expected := []string{
"Arduino Yún",
"Arduino Uno",
"Arduino Duemilanove or Diecimila",
"Arduino Nano",
"Arduino Mega or Mega 2560",
"Arduino Mega ADK",
"Arduino Leonardo",
"Arduino Leonardo ETH",
"Arduino Micro",
"Arduino Esplora",
"Arduino Mini",
"Arduino Ethernet",
"Arduino Fio",
"Arduino BT",
"LilyPad Arduino USB",
"LilyPad Arduino",
"Arduino Pro or Pro Mini",
"Arduino NG or older",
"Arduino Robot Control",
"Arduino Robot Motor",
"Arduino Gemma",
"Adafruit Circuit Playground",
"Arduino Yún Mini",
"Arduino Industrial 101",
"Linino One",
"Arduino Uno WiFi",
}
require.Equal(t, expected, res)
}

func TestFindToolsRequiredForBoard(t *testing.T) {
os.Setenv("ARDUINO_DATA_DIR", dataDir1.String())
configuration.Settings = configuration.Init("")
Expand Down
2 changes: 1 addition & 1 deletion commands/board/listall.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func ListAll(ctx context.Context, req *rpc.BoardListAllRequest) (*rpc.BoardListA
targetPackage.Maintainer,
}

for _, board := range installedPlatformRelease.Boards {
for _, board := range installedPlatformRelease.GetBoards() {
if !req.GetIncludeHiddenBoards() && board.IsHidden() {
continue
}
Expand Down
63 changes: 63 additions & 0 deletions internal/integrationtest/board/board_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// This file is part of arduino-cli.
//
// Copyright 2022 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.

package board_test

import (
"testing"

"github.com/arduino/arduino-cli/internal/integrationtest"
"github.com/stretchr/testify/require"
"go.bug.st/testifyjson/requirejson"
)

func TestCorrectBoardListOrdering(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()

_, _, err := cli.Run("core", "install", "arduino:avr")
require.NoError(t, err)
jsonOut, _, err := cli.Run("board", "listall", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, jsonOut, "[.boards[] | .fqbn]", `[
"arduino:avr:yun",
"arduino:avr:uno",
"arduino:avr:unomini",
"arduino:avr:diecimila",
"arduino:avr:nano",
"arduino:avr:mega",
"arduino:avr:megaADK",
"arduino:avr:leonardo",
"arduino:avr:leonardoeth",
"arduino:avr:micro",
"arduino:avr:esplora",
"arduino:avr:mini",
"arduino:avr:ethernet",
"arduino:avr:fio",
"arduino:avr:bt",
"arduino:avr:LilyPadUSB",
"arduino:avr:lilypad",
"arduino:avr:pro",
"arduino:avr:atmegang",
"arduino:avr:robotControl",
"arduino:avr:robotMotor",
"arduino:avr:gemma",
"arduino:avr:circuitplay32u4cat",
"arduino:avr:yunmini",
"arduino:avr:chiwawa",
"arduino:avr:one",
"arduino:avr:unowifi"
]`)
}