@@ -19,12 +19,16 @@ import (
19
19
"fmt"
20
20
"net/http"
21
21
"net/http/httptest"
22
+ "os"
22
23
"testing"
23
24
25
+ "github.com/arduino/arduino-cli/arduino/cores/packagemanager"
24
26
"github.com/arduino/arduino-cli/commands"
25
27
"github.com/arduino/arduino-cli/configuration"
28
+ "github.com/arduino/go-paths-helper"
26
29
"github.com/arduino/go-properties-orderedmap"
27
30
"github.com/stretchr/testify/require"
31
+ semver "go.bug.st/relaxed-semver"
28
32
)
29
33
30
34
func init () {
@@ -109,3 +113,53 @@ func TestBoardDetectionViaAPIWithNonUSBPort(t *testing.T) {
109
113
require .Equal (t , err , ErrNotFound )
110
114
require .Empty (t , items )
111
115
}
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