forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpackage_manager_test.go
319 lines (286 loc) · 12.6 KB
/
package_manager_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// This file is part of arduino-cli.
//
// Copyright 2020 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 [email protected].
package packagemanager_test
import (
"fmt"
"net/url"
"os"
"testing"
"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
"github.com/arduino/arduino-cli/configuration"
"github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
semver "go.bug.st/relaxed-semver"
)
var customHardware = paths.New("testdata", "custom_hardware")
var dataDir1 = paths.New("testdata", "data_dir_1")
// Intended to be used alongside dataDir1
var extraHardware = paths.New("testdata", "extra_hardware")
func TestFindBoardWithFQBN(t *testing.T) {
pm := packagemanager.NewPackageManager(customHardware, customHardware, customHardware, customHardware)
pm.LoadHardwareFromDirectory(customHardware)
_, _, board, err := pm.FindBoard("arduino:avr:uno", nil)
require.Nil(t, err)
require.NotNil(t, board)
require.Equal(t, board.Name(), "Arduino/Genuino Uno")
_, _, board, err = pm.FindBoard("arduino:avr:mega", nil)
require.Nil(t, err)
require.NotNil(t, board)
require.Equal(t, board.Name(), "Arduino/Genuino Mega or Mega 2560")
}
func TestResolveFQBN(t *testing.T) {
// Pass nil, since these paths are only used for installing
pm := packagemanager.NewPackageManager(nil, nil, nil, nil)
// Hardware from main packages directory
pm.LoadHardwareFromDirectory(dataDir1.Join("packages"))
// This contains the arduino:avr core
pm.LoadHardwareFromDirectory(customHardware)
// This contains the referenced:avr core
pm.LoadHardwareFromDirectory(extraHardware)
fqbn, err := cores.ParseFQBN("arduino:avr:uno")
require.Nil(t, err)
require.NotNil(t, fqbn)
pkg, platformRelease, board, props, buildPlatformRelease, err := pm.ResolveFQBN(fqbn)
require.Nil(t, err)
require.Equal(t, pkg, platformRelease.Platform.Package)
require.NotNil(t, platformRelease)
require.NotNil(t, platformRelease.Platform)
require.Equal(t, platformRelease.Platform.String(), "arduino:avr")
require.NotNil(t, board)
require.Equal(t, board.Name(), "Arduino/Genuino Uno")
require.NotNil(t, props)
require.Equal(t, platformRelease, buildPlatformRelease)
fqbn, err = cores.ParseFQBN("arduino:avr:mega")
require.Nil(t, err)
require.NotNil(t, fqbn)
pkg, platformRelease, board, props, buildPlatformRelease, err = pm.ResolveFQBN(fqbn)
require.Nil(t, err)
require.Equal(t, pkg, platformRelease.Platform.Package)
require.NotNil(t, platformRelease)
require.NotNil(t, platformRelease.Platform)
require.Equal(t, platformRelease.Platform.String(), "arduino:avr")
require.NotNil(t, board)
require.Equal(t, board.Name(), "Arduino/Genuino Mega or Mega 2560")
require.NotNil(t, props)
require.Equal(t, platformRelease, buildPlatformRelease)
// Test a board referenced from the main AVR arduino platform
fqbn, err = cores.ParseFQBN("referenced:avr:uno")
require.Nil(t, err)
require.NotNil(t, fqbn)
pkg, platformRelease, board, props, buildPlatformRelease, err = pm.ResolveFQBN(fqbn)
require.Nil(t, err)
require.Equal(t, pkg, platformRelease.Platform.Package)
require.NotNil(t, platformRelease)
require.NotNil(t, platformRelease.Platform)
require.Equal(t, platformRelease.Platform.String(), "referenced:avr")
require.NotNil(t, board)
require.Equal(t, board.Name(), "Referenced Uno")
require.NotNil(t, props)
require.NotNil(t, buildPlatformRelease)
require.NotNil(t, buildPlatformRelease.Platform)
require.Equal(t, buildPlatformRelease.Platform.String(), "arduino:avr")
// Test a board referenced from the Adafruit SAMD core (this tests
// deriving where the package and core name are different)
fqbn, err = cores.ParseFQBN("referenced:samd:feather_m0")
require.Nil(t, err)
require.NotNil(t, fqbn)
pkg, platformRelease, board, props, buildPlatformRelease, err = pm.ResolveFQBN(fqbn)
require.Nil(t, err)
require.Equal(t, pkg, platformRelease.Platform.Package)
require.NotNil(t, platformRelease)
require.NotNil(t, platformRelease.Platform)
require.Equal(t, platformRelease.Platform.String(), "referenced:samd")
require.NotNil(t, board)
require.Equal(t, board.Name(), "Referenced Feather M0")
require.NotNil(t, props)
require.NotNil(t, buildPlatformRelease)
require.NotNil(t, buildPlatformRelease.Platform)
require.Equal(t, buildPlatformRelease.Platform.String(), "adafruit:samd")
// Test a board referenced from a non-existent package
fqbn, err = cores.ParseFQBN("referenced:avr:dummy_invalid_package")
require.Nil(t, err)
require.NotNil(t, fqbn)
pkg, platformRelease, board, props, buildPlatformRelease, err = pm.ResolveFQBN(fqbn)
require.NotNil(t, err)
require.Equal(t, pkg, platformRelease.Platform.Package)
require.NotNil(t, platformRelease)
require.NotNil(t, platformRelease.Platform)
require.Equal(t, platformRelease.Platform.String(), "referenced:avr")
require.NotNil(t, board)
require.Equal(t, board.Name(), "Referenced dummy with invalid package")
require.NotNil(t, props)
require.Nil(t, buildPlatformRelease)
// Test a board referenced from a non-existent platform/architecture
fqbn, err = cores.ParseFQBN("referenced:avr:dummy_invalid_platform")
require.Nil(t, err)
require.NotNil(t, fqbn)
pkg, platformRelease, board, props, buildPlatformRelease, err = pm.ResolveFQBN(fqbn)
require.NotNil(t, err)
require.Equal(t, pkg, platformRelease.Platform.Package)
require.NotNil(t, platformRelease)
require.NotNil(t, platformRelease.Platform)
require.Equal(t, platformRelease.Platform.String(), "referenced:avr")
require.NotNil(t, board)
require.Equal(t, board.Name(), "Referenced dummy with invalid platform")
require.NotNil(t, props)
require.Nil(t, buildPlatformRelease)
// Test a board referenced from a non-existent core
// Note that ResolveFQBN does not actually check this currently
fqbn, err = cores.ParseFQBN("referenced:avr:dummy_invalid_core")
require.Nil(t, err)
require.NotNil(t, fqbn)
pkg, platformRelease, board, props, buildPlatformRelease, err = pm.ResolveFQBN(fqbn)
require.Nil(t, err)
require.Equal(t, pkg, platformRelease.Platform.Package)
require.NotNil(t, platformRelease)
require.NotNil(t, platformRelease.Platform)
require.Equal(t, platformRelease.Platform.String(), "referenced:avr")
require.NotNil(t, board)
require.Equal(t, board.Name(), "Referenced dummy with invalid core")
require.NotNil(t, props)
require.NotNil(t, buildPlatformRelease)
require.NotNil(t, buildPlatformRelease.Platform)
require.Equal(t, buildPlatformRelease.Platform.String(), "arduino:avr")
}
func TestBoardOptionsFunctions(t *testing.T) {
pm := packagemanager.NewPackageManager(customHardware, customHardware, customHardware, customHardware)
pm.LoadHardwareFromDirectory(customHardware)
_, _, nano, err := pm.FindBoard("arduino:avr:nano", nil)
require.Nil(t, err)
require.NotNil(t, nano)
require.Equal(t, nano.Name(), "Arduino Nano")
nanoOptions := nano.GetConfigOptions()
require.Equal(t, "Processor", nanoOptions.Get("cpu"))
require.Equal(t, 1, nanoOptions.Size())
nanoCPUValues := nano.GetConfigOptionValues("cpu")
expectedNanoCPUValues := properties.NewMap()
expectedNanoCPUValues.Set("atmega328", "ATmega328P")
expectedNanoCPUValues.Set("atmega328old", "ATmega328P (Old Bootloader)")
expectedNanoCPUValues.Set("atmega168", "ATmega168")
require.EqualValues(t, expectedNanoCPUValues, nanoCPUValues)
_, _, esp8266, err := pm.FindBoard("esp8266:esp8266:generic", nil)
require.Nil(t, err)
require.NotNil(t, esp8266)
require.Equal(t, esp8266.Name(), "Generic ESP8266 Module")
esp8266Options := esp8266.GetConfigOptions()
require.Equal(t, 13, esp8266Options.Size())
require.Equal(t, "Builtin Led", esp8266Options.Get("led"))
require.Equal(t, "Upload Speed", esp8266Options.Get("UploadSpeed"))
esp8266UploadSpeedValues := esp8266.GetConfigOptionValues("UploadSpeed")
for k, v := range esp8266UploadSpeedValues.AsMap() {
// Some option values are missing for a particular OS: check that only the available options are listed
require.Equal(t, k, v)
}
}
func TestFindToolsRequiredForBoard(t *testing.T) {
os.Setenv("ARDUINO_DATA_DIR", dataDir1.String())
configuration.Init("")
pm := packagemanager.NewPackageManager(
dataDir1,
configuration.PackagesDir(),
paths.New(viper.GetString("directories.Downloads")),
dataDir1,
)
loadIndex := func(addr string) {
res, err := url.Parse(addr)
require.NoError(t, err)
require.NoError(t, pm.LoadPackageIndex(res))
}
loadIndex("https://dl.espressif.com/dl/package_esp32_index.json")
loadIndex("http://arduino.esp8266.com/stable/package_esp8266com_index.json")
loadIndex("https://adafruit.github.io/arduino-board-index/package_adafruit_index.json")
require.NoError(t, pm.LoadHardware())
_, _, esp32, err := pm.FindBoard("esp32:esp32:esp32", nil)
require.NoError(t, err)
esptool231 := pm.FindToolDependency(&cores.ToolDependency{
ToolPackager: "esp32",
ToolName: "esptool",
ToolVersion: semver.ParseRelaxed("2.3.1"),
})
require.NotNil(t, esptool231)
esptool0413 := pm.FindToolDependency(&cores.ToolDependency{
ToolPackager: "esp8266",
ToolName: "esptool",
ToolVersion: semver.ParseRelaxed("0.4.13"),
})
require.NotNil(t, esptool0413)
testConflictingToolsInDifferentPackages := func() {
tools, err := pm.FindToolsRequiredForBoard(esp32)
require.NoError(t, err)
require.Contains(t, tools, esptool231)
require.NotContains(t, tools, esptool0413)
}
// As seen in https://github.com/arduino/arduino-cli/issues/73 the map randomess
// may make the function fail half of the times. Repeating the test 10 times
// greatly increases the chances to trigger the bad case.
testConflictingToolsInDifferentPackages()
testConflictingToolsInDifferentPackages()
testConflictingToolsInDifferentPackages()
testConflictingToolsInDifferentPackages()
testConflictingToolsInDifferentPackages()
testConflictingToolsInDifferentPackages()
testConflictingToolsInDifferentPackages()
testConflictingToolsInDifferentPackages()
testConflictingToolsInDifferentPackages()
testConflictingToolsInDifferentPackages()
_, _, feather, err := pm.FindBoard("adafruit:samd:adafruit_feather_m0_express", nil)
require.NoError(t, err)
require.NotNil(t, feather)
featherTools, err := pm.FindToolsRequiredForBoard(feather)
require.NoError(t, err)
require.NotNil(t, featherTools)
// Test when a package index requires two different version of the same tool
// See: https://github.com/arduino/arduino-cli/issues/166#issuecomment-528295989
bossac17 := pm.FindToolDependency(&cores.ToolDependency{
ToolPackager: "arduino",
ToolName: "bossac",
ToolVersion: semver.ParseRelaxed("1.7.0"),
})
require.NotNil(t, bossac17)
bossac18 := pm.FindToolDependency(&cores.ToolDependency{
ToolPackager: "arduino",
ToolName: "bossac",
ToolVersion: semver.ParseRelaxed("1.8.0-48-gb176eee"),
})
require.NotNil(t, bossac18)
require.Contains(t, featherTools, bossac17)
require.Contains(t, featherTools, bossac18)
// Check if the runtime variable is set correctly to the latest version
uploadProperties := properties.NewMap()
for _, requiredTool := range featherTools {
uploadProperties.Merge(requiredTool.RuntimeProperties())
}
require.Equal(t, bossac18.InstallDir.String(), uploadProperties.Get("runtime.tools.bossac.path"))
}
func TestIdentifyBoard(t *testing.T) {
pm := packagemanager.NewPackageManager(customHardware, customHardware, customHardware, customHardware)
pm.LoadHardwareFromDirectory(customHardware)
identify := func(vid, pid string) []*cores.Board {
return pm.IdentifyBoard(properties.NewFromHashmap(map[string]string{
"vid": vid, "pid": pid,
}))
}
require.Equal(t, "[arduino:avr:uno]", fmt.Sprintf("%v", identify("0x2341", "0x0001")))
// Check indexed vid/pid format (vid.0/pid.0)
require.Equal(t, "[test:avr:a]", fmt.Sprintf("%v", identify("0x9999", "0x0001")))
require.Equal(t, "[test:avr:b]", fmt.Sprintf("%v", identify("0x9999", "0x0002")))
require.Equal(t, "[test:avr:c]", fmt.Sprintf("%v", identify("0x9999", "0x0003")))
require.Equal(t, "[test:avr:c]", fmt.Sprintf("%v", identify("0x9999", "0x0004")))
// https://github.com/arduino/arduino-cli/issues/456
require.Equal(t, "[test:avr:d]", fmt.Sprintf("%v", identify("0x9999", "0x0005")))
}