Skip to content

Commit

Permalink
Respect the order of menu, when computing the config options (#2159)
Browse files Browse the repository at this point in the history
* Respect the order of menu, when computing the config options

* reduce cyclomatic complexity

* fix the comment that was referring to the wrong filename
  • Loading branch information
alessio-perugini committed Apr 27, 2023
1 parent 475d987 commit ad9ddb8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
13 changes: 10 additions & 3 deletions arduino/cores/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,21 @@ func (b *Board) buildConfigOptionsStructures() {

b.configOptions = properties.NewMap()
allConfigs := b.Properties.SubTree("menu")
for _, option := range allConfigs.FirstLevelKeys() {
b.configOptions.Set(option, b.PlatformRelease.Menus.Get(option))
allConfigOptions := allConfigs.FirstLevelOf()

// Used to show the config options in the same order as the menu, defined at the begging of boards.txt
if b.PlatformRelease.Menus != nil {
for _, menuOption := range b.PlatformRelease.Menus.FirstLevelKeys() {
if _, ok := allConfigOptions[menuOption]; ok {
b.configOptions.Set(menuOption, b.PlatformRelease.Menus.Get(menuOption))
}
}
}

b.configOptionValues = map[string]*properties.Map{}
b.configOptionProperties = map[string]*properties.Map{}
b.defaultConfig = properties.NewMap()
for option, optionProps := range allConfigs.FirstLevelOf() {
for option, optionProps := range allConfigOptions {
b.configOptionValues[option] = properties.NewMap()
values := optionProps.FirstLevelKeys()
b.defaultConfig.Set(option, values[0])
Expand Down
36 changes: 36 additions & 0 deletions arduino/cores/board_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package cores

import (
"fmt"
"testing"

properties "github.com/arduino/go-properties-orderedmap"
Expand Down Expand Up @@ -370,6 +371,41 @@ func TestBoardOptions(t *testing.T) {
// fmt.Print(string(data))
}

func TestBoarConfigOptionSortingSameAsMenu(t *testing.T) {
menus := properties.NewMap()
menus.Set("BoardModel", "Model")
menus.Set("xtal", "CPU Frequency")
menus.Set("vt", "VTables")
menus.Set("wipe", "Erase Flash")

props := properties.NewMap()
props.Set("menu.xtal.80", "80 MHz")
props.Set("menu.wipe.none", "Only Sketch")
props.Set("menu.BoardModel.primo", "Primo")
props.Set("menu.BoardModel.primo.build.board", "ESP8266_ARDUINO_PRIMO")
props.Set("menu.vt.flash", "Flash")

esp8266 := &Board{
BoardID: "arduino-esp8266",
Properties: props,
PlatformRelease: &PlatformRelease{
Platform: &Platform{
Architecture: "esp8266",
Package: &Package{
Name: "esp8266",
},
},
Menus: menus,
},
}

config := `xtal=80,wipe=none,BoardModel=primo,vt=flash`

_, err := esp8266.GeneratePropertiesForConfiguration(config)
require.NoError(t, err, fmt.Sprintf("generating %s configuration", config))
require.True(t, esp8266.configOptions.EqualsWithOrder(menus))
}

func TestOSSpecificBoardOptions(t *testing.T) {
boardWihOSSpecificOptionProperties := properties.NewMap()
boardWihOSSpecificOptionProperties.Set("menu.UploadSpeed.115200", "115200")
Expand Down

0 comments on commit ad9ddb8

Please sign in to comment.