@@ -19,12 +19,16 @@ import (
1919 "fmt"
2020 "net/http"
2121 "net/http/httptest"
22+ "os"
2223 "testing"
2324
25+ "github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2426 "github.com/arduino/arduino-cli/commands"
2527 "github.com/arduino/arduino-cli/configuration"
28+ "github.com/arduino/go-paths-helper"
2629 "github.com/arduino/go-properties-orderedmap"
2730 "github.com/stretchr/testify/require"
31+ semver "go.bug.st/relaxed-semver"
2832)
2933
3034func init () {
@@ -109,3 +113,53 @@ func TestBoardDetectionViaAPIWithNonUSBPort(t *testing.T) {
109113 require .Equal (t , err , ErrNotFound )
110114 require .Empty (t , items )
111115}
116+
117+ func TestBoardIdentifySorting (t * testing.T ) {
118+ dataDir := paths .TempDir ().Join ("test" , "data_dir" )
119+ os .Setenv ("ARDUINO_DATA_DIR" , dataDir .String ())
120+ dataDir .MkdirAll ()
121+ defer paths .TempDir ().Join ("test" ).RemoveAll ()
122+
123+ // We don't really care about the paths in this case
124+ pm := packagemanager .NewPackageManager (dataDir , dataDir , dataDir , dataDir )
125+
126+ // Create some boards with identical VID:PID combination
127+ pack := pm .Packages .GetOrCreatePackage ("packager" )
128+ pack .Maintainer = "NotArduino"
129+ platform := pack .GetOrCreatePlatform ("platform" )
130+ platformRelease := platform .GetOrCreateRelease (semver .MustParse ("0.0.0" ))
131+ platformRelease .InstallDir = dataDir
132+ board := platformRelease .GetOrCreateBoard ("boardA" )
133+ board .Properties .Set ("vid" , "0x0000" )
134+ board .Properties .Set ("pid" , "0x0000" )
135+ board = platformRelease .GetOrCreateBoard ("boardB" )
136+ board .Properties .Set ("vid" , "0x0000" )
137+ board .Properties .Set ("pid" , "0x0000" )
138+
139+ // Create some Arduino boards with same VID:PID combination as boards created previously
140+ pack = pm .Packages .GetOrCreatePackage ("arduino" )
141+ pack .Maintainer = "Arduino"
142+ platform = pack .GetOrCreatePlatform ("avr" )
143+ platformRelease = platform .GetOrCreateRelease (semver .MustParse ("0.0.0" ))
144+ platformRelease .InstallDir = dataDir
145+ board = platformRelease .GetOrCreateBoard ("nessuno" )
146+ board .Properties .Set ("vid" , "0x0000" )
147+ board .Properties .Set ("pid" , "0x0000" )
148+ board = platformRelease .GetOrCreateBoard ("assurdo" )
149+ board .Properties .Set ("vid" , "0x0000" )
150+ board .Properties .Set ("pid" , "0x0000" )
151+
152+ idPrefs := properties .NewMap ()
153+ idPrefs .Set ("vid" , "0x0000" )
154+ idPrefs .Set ("pid" , "0x0000" )
155+ res , err := identify (pm , & commands.BoardPort {IdentificationPrefs : idPrefs })
156+ require .NoError (t , err )
157+ require .NotNil (t , res )
158+ require .Len (t , res , 4 )
159+
160+ // Verify expected sorting
161+ require .Equal (t , res [0 ].Fqbn , "arduino:avr:assurdo" )
162+ require .Equal (t , res [1 ].Fqbn , "arduino:avr:nessuno" )
163+ require .Equal (t , res [2 ].Fqbn , "packager:platform:boardA" )
164+ require .Equal (t , res [3 ].Fqbn , "packager:platform:boardB" )
165+ }
0 commit comments