Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added PackageManager.NormalizeFQBN() method.
  • Loading branch information
cmaglie committed Mar 29, 2023
commit fe0b1b4dece382bb16b8fc2070bf3054e81efa84
23 changes: 23 additions & 0 deletions arduino/cores/packagemanager/package_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -826,3 +826,26 @@ func (pme *Explorer) FindMonitorDependency(discovery *cores.MonitorDependency) *
return toolRelease.GetLatestInstalled()
}
}

// NormalizeFQBN return a normalized copy of the given FQBN, that is the same
// FQBN but with the unneeded or invalid options removed.
func (pme *Explorer) NormalizeFQBN(fqbn *cores.FQBN) (*cores.FQBN, error) {
_, _, board, _, _, err := pme.ResolveFQBN(fqbn)
if err != nil {
return nil, err
}
normalizedFqbn := fqbn.Clone()
for _, option := range fqbn.Configs.Keys() {
values := board.GetConfigOptionValues(option)
if values == nil || values.Size() == 0 {
normalizedFqbn.Configs.Remove(option)
continue
}

defaultValue := values.Keys()[0]
if fqbn.Configs.Get(option) == defaultValue {
normalizedFqbn.Configs.Remove(option)
}
}
return normalizedFqbn, nil
}
24 changes: 24 additions & 0 deletions arduino/cores/packagemanager/package_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ func TestResolveFQBN(t *testing.T) {
pme, release := pm.NewExplorer()
defer release()

{
testNormalization := func(in, expected string) {
fqbn, err := cores.ParseFQBN(in)
require.Nil(t, err)
require.NotNil(t, fqbn)
normalized, err := pme.NormalizeFQBN(fqbn)
if expected == "ERROR" {
require.Error(t, err)
require.Nil(t, normalized)
} else {
require.NoError(t, err)
require.NotNil(t, normalized)
require.Equal(t, expected, normalized.String())
}
}
testNormalization("arduino:avr:mega", "arduino:avr:mega")
testNormalization("arduino:avr:mega:cpu=atmega2560", "arduino:avr:mega")
testNormalization("arduino:avr:mega:cpu=atmega1280", "arduino:avr:mega:cpu=atmega1280")
testNormalization("esp8266:esp8266:generic:baud=57600,wipe=sdk", "esp8266:esp8266:generic:baud=57600,wipe=sdk")
testNormalization("esp8266:esp8266:generic:baud=115200,wipe=sdk", "esp8266:esp8266:generic:wipe=sdk")
testNormalization("arduino:avr:mega:cpu=nonexistent", "ERROR")
testNormalization("arduino:avr:mega:nonexistent=blah", "ERROR")
}

{
fqbn, err := cores.ParseFQBN("arduino:avr:uno")
require.Nil(t, err)
Expand Down