forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_test.go
1060 lines (874 loc) · 40.9 KB
/
core_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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of arduino-cli.
//
// Copyright 2022 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 core_test
import (
"crypto/md5"
"encoding/hex"
"fmt"
"os"
"runtime"
"sort"
"strconv"
"strings"
"testing"
"time"
"github.com/arduino/arduino-cli/internal/integrationtest"
"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/require"
semver "go.bug.st/relaxed-semver"
"go.bug.st/testifyjson/requirejson"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
)
func TestCorrectHandlingOfPlatformVersionProperty(t *testing.T) {
// See: https://github.com/arduino/arduino-cli/issues/1823
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Copy test platform
testPlatform := paths.New("testdata", "issue_1823", "DxCore-dev")
require.NoError(t, testPlatform.CopyDirTo(cli.SketchbookDir().Join("hardware", "DxCore-dev")))
// Trigger problematic call
out, _, err := cli.Run("core", "list", "--format", "json")
require.NoError(t, err)
requirejson.Contains(t, out, `[{"id":"DxCore-dev:megaavr","installed":"1.4.10","name":"DxCore"}]`)
}
func TestCoreSearch(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Set up an http server to serve our custom index file
test_index := paths.New("..", "testdata", "test_index.json")
url := env.HTTPServeFile(8000, test_index)
// Run update-index with our test index
_, _, err := cli.Run("core", "update-index", "--additional-urls="+url.String())
require.NoError(t, err)
// Search a specific core
out, _, err := cli.Run("core", "search", "avr")
require.NoError(t, err)
require.Greater(t, len(strings.Split(string(out), "\n")), 2)
_, _, err = cli.Run("core", "install", "arduino:[email protected]")
require.NoError(t, err)
out, _, err = cli.Run("core", "search", "avr", "--format", "json")
require.NoError(t, err)
requirejson.NotEmpty(t, out)
// Verify that "installed" is set
requirejson.Contains(t, out, `[{installed: "1.8.6"}]`)
// additional URL
out, _, err = cli.Run("core", "search", "test_core", "--format", "json", "--additional-urls="+url.String())
require.NoError(t, err)
requirejson.Len(t, out, 1)
// show all versions
out, _, err = cli.Run("core", "search", "test_core", "--all", "--format", "json", "--additional-urls="+url.String())
require.NoError(t, err)
requirejson.Len(t, out, 3)
checkPlatformIsInJSONOutput := func(stdout []byte, id, version string) {
jqquery := fmt.Sprintf(`[{id:"%s", latest:"%s"}]`, id, version)
requirejson.Contains(t, out, jqquery, "platform %s@%s is missing from the output", id, version)
}
// Search all Retrokit platforms
out, _, err = cli.Run("core", "search", "retrokit", "--all", "--additional-urls="+url.String(), "--format", "json")
require.NoError(t, err)
checkPlatformIsInJSONOutput(out, "Retrokits-RK002:arm", "1.0.5")
checkPlatformIsInJSONOutput(out, "Retrokits-RK002:arm", "1.0.6")
// Search using Retrokit Package Maintainer
out, _, err = cli.Run("core", "search", "Retrokits-RK002", "--all", "--additional-urls="+url.String(), "--format", "json")
require.NoError(t, err)
checkPlatformIsInJSONOutput(out, "Retrokits-RK002:arm", "1.0.5")
checkPlatformIsInJSONOutput(out, "Retrokits-RK002:arm", "1.0.6")
// Search using the Retrokit Platform name
out, _, err = cli.Run("core", "search", "rk002", "--all", "--additional-urls="+url.String(), "--format", "json")
require.NoError(t, err)
checkPlatformIsInJSONOutput(out, "Retrokits-RK002:arm", "1.0.5")
checkPlatformIsInJSONOutput(out, "Retrokits-RK002:arm", "1.0.6")
// Search using board names
out, _, err = cli.Run("core", "search", "myboard", "--all", "--additional-urls="+url.String(), "--format", "json")
require.NoError(t, err)
checkPlatformIsInJSONOutput(out, "Package:x86", "1.2.3")
runSearch := func(searchArgs string, expectedIDs ...string) {
args := []string{"core", "search", "--format", "json"}
args = append(args, strings.Split(searchArgs, " ")...)
out, _, err := cli.Run(args...)
require.NoError(t, err)
for _, id := range expectedIDs {
jqquery := fmt.Sprintf(`[{id:"%s"}]`, id)
requirejson.Contains(t, out, jqquery, "platform %s is missing from the output", id)
}
}
// Check search with case, accents and spaces
runSearch("mkr 1000", "arduino:samd")
runSearch("yún", "arduino:avr")
runSearch("yùn", "arduino:avr")
runSearch("yun", "arduino:avr")
runSearch("nano 33", "arduino:samd", "arduino:mbed_nano")
runSearch("nano ble", "arduino:mbed_nano")
runSearch("ble", "arduino:mbed_nano")
runSearch("ble nano", "arduino:mbed_nano")
runSearch("nano", "arduino:avr", "arduino:megaavr", "arduino:samd", "arduino:mbed_nano")
}
func TestCoreSearchNoArgs(t *testing.T) {
// This tests `core search` with and without additional URLs in case no args
// are passed (i.e. all results are shown).
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Set up an http server to serve our custom index file
testIndex := paths.New("..", "testdata", "test_index.json")
url := env.HTTPServeFile(8000, testIndex)
// update custom index and install test core (installed cores affect `core search`)
_, _, err := cli.Run("core", "update-index", "--additional-urls="+url.String())
require.NoError(t, err)
_, _, err = cli.Run("core", "install", "test:[email protected]", "--additional-urls="+url.String())
require.NoError(t, err)
// list all with no additional urls, ensure the test core won't show up
stdout, _, err := cli.Run("core", "search")
require.NoError(t, err)
var lines [][]string
for _, v := range strings.Split(strings.TrimSpace(string(stdout)), "\n") {
lines = append(lines, strings.Fields(strings.TrimSpace(v)))
}
// Check the presence of test:[email protected]
require.Contains(t, lines, []string{"test:x86", "2.0.0", "test_core"})
numPlatforms := len(lines) - 1
// same thing in JSON format, also check the number of platforms found is the same
stdout, _, err = cli.Run("core", "search", "--format", "json")
require.NoError(t, err)
requirejson.Contains(t, stdout, `[ { "name":"test_core" } ]`)
requirejson.Query(t, stdout, "length", fmt.Sprint(numPlatforms))
// list all with additional urls, check the test core is there
stdout, _, err = cli.Run("core", "search", "--additional-urls="+url.String())
require.NoError(t, err)
lines = nil
for _, v := range strings.Split(strings.TrimSpace(string(stdout)), "\n") {
lines = append(lines, strings.Fields(strings.TrimSpace(v)))
}
// Check the presence of test:[email protected]
require.Contains(t, lines, []string{"test:x86", "3.0.0", "test_core"})
numPlatforms = len(lines) - 1
// same thing in JSON format, also check the number of platforms found is the same
stdout, _, err = cli.Run("core", "search", "--format", "json", "--additional-urls="+url.String())
require.NoError(t, err)
requirejson.Contains(t, stdout, `[ { "name":"test_core" } ]`)
requirejson.Query(t, stdout, "length", fmt.Sprint(numPlatforms))
}
func TestCoreUpdateIndexUrlNotFound(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
_, _, err := cli.Run("core", "update-index")
require.NoError(t, err)
// Brings up a local server to fake a failure
url := env.HTTPServeFileError(8000, paths.New("test_index.json"), 404)
stdout, stderr, err := cli.Run("core", "update-index", "--additional-urls="+url.String())
require.Error(t, err)
require.Contains(t, string(stdout), "Downloading index: test_index.json Server responded with: 404 Not Found")
require.Contains(t, string(stderr), "Some indexes could not be updated.")
}
func TestCoreUpdateIndexInternalServerError(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
_, _, err := cli.Run("core", "update-index")
require.NoError(t, err)
// Brings up a local server to fake a failure
url := env.HTTPServeFileError(8000, paths.New("test_index.json"), 500)
stdout, _, err := cli.Run("core", "update-index", "--additional-urls="+url.String())
require.Error(t, err)
require.Contains(t, string(stdout), "Downloading index: test_index.json Server responded with: 500 Internal Server Error")
}
func TestCoreInstallWithoutUpdateIndex(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Missing "core update-index"
// Download samd core pinned to 1.8.6
stdout, _, err := cli.Run("core", "install", "arduino:[email protected]")
require.NoError(t, err)
require.Contains(t, string(stdout), "Downloading index: package_index.tar.bz2 downloaded")
}
func TestCoreInstallEsp32(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// update index
url := "https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json"
_, _, err := cli.Run("core", "update-index", "--additional-urls="+url)
require.NoError(t, err)
// install 3rd-party core
_, _, err = cli.Run("core", "install", "esp32:[email protected]", "--additional-urls="+url)
require.NoError(t, err)
// create a sketch and compile to double check the core was successfully installed
sketchName := "test_core_install_esp32"
sketchPath := cli.SketchbookDir().Join(sketchName)
_, _, err = cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)
_, _, err = cli.Run("compile", "-b", "esp32:esp32:esp32", sketchPath.String())
require.NoError(t, err)
// prevent regressions for https://github.com/arduino/arduino-cli/issues/163
md5 := md5.Sum(([]byte(sketchPath.String())))
sketchPathMd5 := strings.ToUpper(hex.EncodeToString(md5[:]))
require.NotEmpty(t, sketchPathMd5)
buildDir := paths.TempDir().Join("arduino", "sketches", sketchPathMd5)
require.FileExists(t, buildDir.Join(sketchName+".ino.partitions.bin").String())
}
func TestCoreDownload(t *testing.T) {
env := integrationtest.NewEnvironment(t)
defer env.CleanUp()
cli := integrationtest.NewArduinoCliWithinEnvironment(env, &integrationtest.ArduinoCLIConfig{
ArduinoCLIPath: integrationtest.FindArduinoCLIPath(t),
UseSharedStagingFolder: false,
})
_, _, err := cli.Run("core", "update-index")
require.NoError(t, err)
// Download a specific core version
_, _, err = cli.Run("core", "download", "arduino:[email protected]")
require.NoError(t, err)
require.FileExists(t, cli.DownloadDir().Join("packages", "avr-1.6.16.tar.bz2").String())
// Wrong core version
_, _, err = cli.Run("core", "download", "arduino:[email protected]")
require.Error(t, err)
// Wrong core
_, _, err = cli.Run("core", "download", "bananas:avr")
require.Error(t, err)
// Wrong casing
_, _, err = cli.Run("core", "download", "Arduino:[email protected]")
require.NoError(t, err)
require.FileExists(t, cli.DownloadDir().Join("packages", "core-ArduinoCore-samd-1.8.12.tar.bz2").String())
}
func TestCoreInstall(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
_, _, err := cli.Run("core", "update-index")
require.NoError(t, err)
// Install a specific core version
_, _, err = cli.Run("core", "install", "arduino:[email protected]")
require.NoError(t, err)
stdout, _, err := cli.Run("core", "list", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, `.[] | select(.id == "arduino:avr") | .installed`, `"1.6.16"`)
// Replace it with the same with --no-overwrite (should NOT fail)
_, _, err = cli.Run("core", "install", "arduino:[email protected]", "--no-overwrite")
require.NoError(t, err)
// Replace it with a more recent one with --no-overwrite (should fail)
_, _, err = cli.Run("core", "install", "arduino:[email protected]", "--no-overwrite")
require.Error(t, err)
// Replace it with a more recent one without --no-overwrite (should succeed)
_, _, err = cli.Run("core", "install", "arduino:[email protected]")
require.NoError(t, err)
stdout, _, err = cli.Run("core", "list", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, `.[] | select(.id == "arduino:avr") | .installed`, `"1.6.17"`)
// Confirm core is listed as "updatable"
stdout, _, err = cli.Run("core", "list", "--updatable", "--format", "json")
require.NoError(t, err)
jsonout := requirejson.Parse(t, stdout)
q := jsonout.Query(`.[] | select(.id == "arduino:avr")`)
q.Query(".installed").MustEqual(`"1.6.17"`)
latest := q.Query(".latest")
// Upgrade the core to latest version
_, _, err = cli.Run("core", "upgrade", "arduino:avr")
require.NoError(t, err)
stdout, _, err = cli.Run("core", "list", "--format", "json")
require.NoError(t, err)
requirejson.Query(t, stdout, `.[] | select(.id == "arduino:avr") | .installed`, latest.String())
// double check the core isn't updatable anymore
stdout, _, err = cli.Run("core", "list", "--updatable", "--format", "json")
require.NoError(t, err)
requirejson.Empty(t, stdout)
}
func TestCoreUninstall(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
_, _, err := cli.Run("core", "update-index")
require.NoError(t, err)
_, _, err = cli.Run("core", "install", "arduino:avr")
require.NoError(t, err)
stdout, _, err := cli.Run("core", "list", "--format", "json")
require.NoError(t, err)
requirejson.Contains(t, stdout, `[ { "id": "arduino:avr" } ]`)
_, _, err = cli.Run("core", "uninstall", "arduino:avr")
require.NoError(t, err)
stdout, _, err = cli.Run("core", "list", "--format", "json")
require.NoError(t, err)
requirejson.Empty(t, stdout)
}
func TestCoreUninstallToolDependencyRemoval(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// These platforms both have a dependency on the arduino:[email protected] tool
// arduino:[email protected] has a dependency on arduino:[email protected]
_, _, err := cli.Run("core", "install", "arduino:[email protected]")
require.NoError(t, err)
// arduino:[email protected] has a dependency on arduino:[email protected]
_, _, err = cli.Run("core", "install", "arduino:[email protected]")
require.NoError(t, err)
_, _, err = cli.Run("core", "uninstall", "arduino:avr")
require.NoError(t, err)
arduinoToolsPath := cli.DataDir().Join("packages", "arduino", "tools")
avrGccBinariesPath := arduinoToolsPath.Join("avr-gcc", "7.3.0-atmel3.6.1-arduino5", "bin")
// The tool arduino:[email protected] that is a dep of another installed platform should remain
require.True(t, avrGccBinariesPath.Join("avr-gcc").Exist() || avrGccBinariesPath.Join("avr-gcc.exe").Exist())
avrDudeBinariesPath := arduinoToolsPath.Join("avrdude", "6.3.0-arduino17", "bin")
// The tool arduino:[email protected] that is only a dep of arduino:avr should have been removed
require.False(t, avrDudeBinariesPath.Join("avrdude").Exist() || avrDudeBinariesPath.Join("avrdude.exe").Exist())
}
func TestCoreZipslip(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
url := "https://raw.githubusercontent.com/arduino/arduino-cli/master/internal/integrationtest/testdata/test_index.json"
_, _, err := cli.Run("core", "update-index", "--additional-urls="+url)
require.NoError(t, err)
// Install a core and check if malicious content has been extracted.
_, _, err = cli.Run("core", "install", "zipslip:x86", "--additional-urls="+url)
require.Error(t, err)
require.NoFileExists(t, "/tmp/evil.txt")
}
func TestCoreBrokenInstall(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
url := "https://raw.githubusercontent.com/arduino/arduino-cli/master/internal/integrationtest/testdata/test_index.json"
_, _, err := cli.Run("core", "update-index", "--additional-urls="+url)
require.NoError(t, err)
_, _, err = cli.Run("core", "install", "brokenchecksum:x86", "--additional-urls="+url)
require.Error(t, err)
}
func TestCoreUpdateWithLocalUrl(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
wd, _ := paths.Getwd()
testIndex := wd.Parent().Join("testdata", "test_index.json").String()
if runtime.GOOS == "windows" {
testIndex = "/" + strings.ReplaceAll(testIndex, "\\", "/")
}
stdout, _, err := cli.Run("core", "update-index", "--additional-urls=file://"+testIndex)
require.NoError(t, err)
require.Contains(t, string(stdout), "Downloading index: test_index.json downloaded")
}
func TestCoreSearchManuallyInstalledCoresNotPrinted(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
_, _, err := cli.Run("core", "update-index")
require.NoError(t, err)
// Verifies only cores in board manager are shown
stdout, _, err := cli.Run("core", "search", "--format", "json")
require.NoError(t, err)
requirejson.NotEmpty(t, stdout)
oldJson := stdout
// Manually installs a core in sketchbooks hardware folder
gitUrl := "https://github.com/arduino/ArduinoCore-avr.git"
repoDir := cli.SketchbookDir().Join("hardware", "arduino-beta-development", "avr")
_, err = git.PlainClone(repoDir.String(), false, &git.CloneOptions{
URL: gitUrl,
ReferenceName: plumbing.NewTagReferenceName("1.8.3"),
})
require.NoError(t, err)
// Verifies manually installed core is not shown
stdout, _, err = cli.Run("core", "search", "--format", "json")
require.NoError(t, err)
requirejson.NotContains(t, stdout, `[{"id": "arduino-beta-development:avr"}]`)
require.Equal(t, oldJson, stdout)
}
func TestCoreListAllManuallyInstalledCore(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
_, _, err := cli.Run("core", "update-index")
require.NoError(t, err)
// Verifies only cores in board manager are shown
stdout, _, err := cli.Run("core", "list", "--all", "--format", "json")
require.NoError(t, err)
requirejson.NotEmpty(t, stdout)
len, err := strconv.Atoi(requirejson.Parse(t, stdout).Query("length").String())
require.NoError(t, err)
// Manually installs a core in sketchbooks hardware folder
gitUrl := "https://github.com/arduino/ArduinoCore-avr.git"
repoDir := cli.SketchbookDir().Join("hardware", "arduino-beta-development", "avr")
_, err = git.PlainClone(repoDir.String(), false, &git.CloneOptions{
URL: gitUrl,
ReferenceName: plumbing.NewTagReferenceName("1.8.3"),
})
require.NoError(t, err)
// Verifies manually installed core is shown
stdout, _, err = cli.Run("core", "list", "--all", "--format", "json")
require.NoError(t, err)
requirejson.Len(t, stdout, len+1)
requirejson.Contains(t, stdout, `[
{
"id": "arduino-beta-development:avr",
"latest": "1.8.3",
"name": "Arduino AVR Boards"
}
]`)
}
func TestCoreListUpdatableAllFlags(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
_, _, err := cli.Run("core", "update-index")
require.NoError(t, err)
// Verifies only cores in board manager are shown
stdout, _, err := cli.Run("core", "list", "--all", "--updatable", "--format", "json")
require.NoError(t, err)
requirejson.NotEmpty(t, stdout)
len, err := strconv.Atoi(requirejson.Parse(t, stdout).Query("length").String())
require.NoError(t, err)
// Manually installs a core in sketchbooks hardware folder
gitUrl := "https://github.com/arduino/ArduinoCore-avr.git"
repoDir := cli.SketchbookDir().Join("hardware", "arduino-beta-development", "avr")
_, err = git.PlainClone(repoDir.String(), false, &git.CloneOptions{
URL: gitUrl,
ReferenceName: plumbing.NewTagReferenceName("1.8.3"),
})
require.NoError(t, err)
// Verifies using both --updatable and --all flags --all takes precedence
stdout, _, err = cli.Run("core", "list", "--all", "--updatable", "--format", "json")
require.NoError(t, err)
requirejson.Len(t, stdout, len+1)
requirejson.Contains(t, stdout, `[
{
"id": "arduino-beta-development:avr",
"latest": "1.8.3",
"name": "Arduino AVR Boards"
}
]`)
}
func TestCoreUpgradeRemovesUnusedTools(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
_, _, err := cli.Run("core", "update-index")
require.NoError(t, err)
// Installs a core
_, _, err = cli.Run("core", "install", "arduino:[email protected]")
require.NoError(t, err)
// Verifies expected tool is installed
toolPath := cli.DataDir().Join("packages", "arduino", "tools", "avr-gcc", "7.3.0-atmel3.6.1-arduino5")
require.DirExists(t, toolPath.String())
// Upgrades core
_, _, err = cli.Run("core", "upgrade", "arduino:avr")
require.NoError(t, err)
// Verifies tool is uninstalled since it's not used by newer core version
require.NoDirExists(t, toolPath.String())
}
func TestCoreInstallRemovesUnusedTools(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
_, _, err := cli.Run("core", "update-index")
require.NoError(t, err)
// Installs a core
_, _, err = cli.Run("core", "install", "arduino:[email protected]")
require.NoError(t, err)
// Verifies expected tool is installed
toolPath := cli.DataDir().Join("packages", "arduino", "tools", "avr-gcc", "7.3.0-atmel3.6.1-arduino5")
require.DirExists(t, toolPath.String())
// Installs newer version of already installed core
_, _, err = cli.Run("core", "install", "arduino:[email protected]")
require.NoError(t, err)
// Verifies tool is uninstalled since it's not used by newer core version
require.NoDirExists(t, toolPath.String())
}
func TestCoreListWithInstalledJson(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
_, _, err := cli.Run("update")
require.NoError(t, err)
// Install core
url := "https://adafruit.github.io/arduino-board-index/package_adafruit_index.json"
_, _, err = cli.Run("core", "update-index", "--additional-urls="+url)
require.NoError(t, err)
_, _, err = cli.Run("core", "install", "adafruit:[email protected]", "--additional-urls="+url)
require.NoError(t, err)
// Verifies installed core is correctly found and name is set
stdout, _, err := cli.Run("core", "list", "--format", "json")
require.NoError(t, err)
requirejson.Len(t, stdout, 1)
requirejson.Contains(t, stdout, `[
{
"id": "adafruit:avr",
"name": "Adafruit AVR Boards"
}
]`)
// Deletes installed.json file, this file stores information about the core,
// that is used mostly when removing package indexes and their cores are still installed;
// this way we don't lose much information about it.
// It might happen that the user has old cores installed before the addition of
// the installed.json file so we need to handle those cases.
installedJson := cli.DataDir().Join("packages", "adafruit", "hardware", "avr", "1.4.13", "installed.json")
require.NoError(t, installedJson.Remove())
// Verifies installed core is still found and name is set
stdout, _, err = cli.Run("core", "list", "--format", "json")
require.NoError(t, err)
requirejson.Len(t, stdout, 1)
// Name for this core changes since if there's installed.json file we read it from
// platform.txt, turns out that this core has different names used in different files
// thus the change.
requirejson.Contains(t, stdout, `[
{
"id": "adafruit:avr",
"name": "Adafruit Boards"
}
]`)
}
func TestCoreSearchUpdateIndexDelay(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Verifies index update is not run
stdout, _, err := cli.Run("core", "search")
require.NoError(t, err)
require.Contains(t, string(stdout), "Downloading index")
// Change edit time of package index file
indexFile := cli.DataDir().Join("package_index.json")
date := time.Now().Local().Add(time.Hour * (-25))
require.NoError(t, os.Chtimes(indexFile.String(), date, date))
// Verifies index update is run
stdout, _, err = cli.Run("core", "search")
require.NoError(t, err)
require.Contains(t, string(stdout), "Downloading index")
// Verifies index update is not run again
stdout, _, err = cli.Run("core", "search")
require.NoError(t, err)
require.NotContains(t, string(stdout), "Downloading index")
}
func TestCoreSearchSortedResults(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Set up the server to serve our custom index file
testIndex := paths.New("..", "testdata", "test_index.json")
url := env.HTTPServeFile(8000, testIndex)
// update custom index
_, _, err := cli.Run("core", "update-index", "--additional-urls="+url.String())
require.NoError(t, err)
// This is done only to avoid index update output when calling core search
// since it automatically updates them if they're outdated and it makes it
// harder to parse the list of cores
_, _, err = cli.Run("core", "search")
require.NoError(t, err)
// list all with additional url specified
stdout, _, err := cli.Run("core", "search", "--additional-urls="+url.String())
require.NoError(t, err)
out := strings.Split(strings.TrimSpace(string(stdout)), "\n")
var lines, deprecated, notDeprecated [][]string
for i, v := range out {
if i > 0 {
v = strings.Join(strings.Fields(v), " ")
lines = append(lines, strings.SplitN(v, " ", 3))
}
}
for _, v := range lines {
if strings.HasPrefix(v[2], "[DEPRECATED]") {
deprecated = append(deprecated, v)
} else {
notDeprecated = append(notDeprecated, v)
}
}
// verify that results are already sorted correctly
require.True(t, sort.SliceIsSorted(deprecated, func(i, j int) bool {
return strings.ToLower(deprecated[i][2]) < strings.ToLower(deprecated[j][2])
}))
require.True(t, sort.SliceIsSorted(notDeprecated, func(i, j int) bool {
return strings.ToLower(notDeprecated[i][2]) < strings.ToLower(notDeprecated[j][2])
}))
// verify that deprecated platforms are the last ones
require.Equal(t, lines, append(notDeprecated, deprecated...))
// test same behaviour with json output
stdout, _, err = cli.Run("core", "search", "--additional-urls="+url.String(), "--format=json")
require.NoError(t, err)
// verify that results are already sorted correctly
sortedDeprecated := requirejson.Parse(t, stdout).Query(
"[ .[] | select(.deprecated == true) | .name |=ascii_downcase | .name ] | sort").String()
notSortedDeprecated := requirejson.Parse(t, stdout).Query(
"[.[] | select(.deprecated == true) | .name |=ascii_downcase | .name]").String()
require.Equal(t, sortedDeprecated, notSortedDeprecated)
sortedNotDeprecated := requirejson.Parse(t, stdout).Query(
"[ .[] | select(.deprecated != true) | .name |=ascii_downcase | .name ] | sort").String()
notSortedNotDeprecated := requirejson.Parse(t, stdout).Query(
"[.[] | select(.deprecated != true) | .name |=ascii_downcase | .name]").String()
require.Equal(t, sortedNotDeprecated, notSortedNotDeprecated)
// verify that deprecated platforms are the last ones
platform := requirejson.Parse(t, stdout).Query(
"[.[] | .name |=ascii_downcase | .name]").String()
require.Equal(t, platform, strings.TrimRight(notSortedNotDeprecated, "]")+","+strings.TrimLeft(notSortedDeprecated, "["))
}
func TestCoreListSortedResults(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Set up the server to serve our custom index file
testIndex := paths.New("..", "testdata", "test_index.json")
url := env.HTTPServeFile(8000, testIndex)
// update custom index
_, _, err := cli.Run("core", "update-index", "--additional-urls="+url.String())
require.NoError(t, err)
// install some core for testing
_, _, err = cli.Run("core", "install", "test:[email protected]", "Retrokits-RK002:arm", "Package:x86", "--additional-urls="+url.String())
require.NoError(t, err)
// list all with additional url specified
stdout, _, err := cli.Run("core", "list", "--additional-urls="+url.String())
require.NoError(t, err)
out := strings.Split(strings.TrimSpace(string(stdout)), "\n")
var lines, deprecated, notDeprecated [][]string
for i, v := range out {
if i > 0 {
v = strings.Join(strings.Fields(v), " ")
lines = append(lines, strings.SplitN(v, " ", 4))
}
}
require.Len(t, lines, 3)
for _, v := range lines {
if strings.HasPrefix(v[3], "[DEPRECATED]") {
deprecated = append(deprecated, v)
} else {
notDeprecated = append(notDeprecated, v)
}
}
// verify that results are already sorted correctly
require.True(t, sort.SliceIsSorted(deprecated, func(i, j int) bool {
return strings.ToLower(deprecated[i][3]) < strings.ToLower(deprecated[j][3])
}))
require.True(t, sort.SliceIsSorted(notDeprecated, func(i, j int) bool {
return strings.ToLower(notDeprecated[i][3]) < strings.ToLower(notDeprecated[j][3])
}))
// verify that deprecated platforms are the last ones
require.Equal(t, lines, append(notDeprecated, deprecated...))
// test same behaviour with json output
stdout, _, err = cli.Run("core", "list", "--additional-urls="+url.String(), "--format=json")
require.NoError(t, err)
requirejson.Len(t, stdout, 3)
// verify that results are already sorted correctly
sortedDeprecated := requirejson.Parse(t, stdout).Query(
"[ .[] | select(.deprecated == true) | .name |=ascii_downcase | .name ] | sort").String()
notSortedDeprecated := requirejson.Parse(t, stdout).Query(
"[.[] | select(.deprecated == true) | .name |=ascii_downcase | .name]").String()
require.Equal(t, sortedDeprecated, notSortedDeprecated)
sortedNotDeprecated := requirejson.Parse(t, stdout).Query(
"[ .[] | select(.deprecated != true) | .name |=ascii_downcase | .name ] | sort").String()
notSortedNotDeprecated := requirejson.Parse(t, stdout).Query(
"[.[] | select(.deprecated != true) | .name |=ascii_downcase | .name]").String()
require.Equal(t, sortedNotDeprecated, notSortedNotDeprecated)
// verify that deprecated platforms are the last ones
platform := requirejson.Parse(t, stdout).Query(
"[.[] | .name |=ascii_downcase | .name]").String()
require.Equal(t, platform, strings.TrimRight(notSortedNotDeprecated, "]")+","+strings.TrimLeft(notSortedDeprecated, "["))
}
func TestCoreListDeprecatedPlatformWithInstalledJson(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Set up the server to serve our custom index file
testIndex := paths.New("..", "testdata", "test_index.json")
url := env.HTTPServeFile(8000, testIndex)
// update custom index
_, _, err := cli.Run("core", "update-index", "--additional-urls="+url.String())
require.NoError(t, err)
// install some core for testing
_, _, err = cli.Run("core", "install", "Package:x86", "--additional-urls="+url.String())
require.NoError(t, err)
installedJsonFile := cli.DataDir().Join("packages", "Package", "hardware", "x86", "1.2.3", "installed.json")
installedJsonData, err := installedJsonFile.ReadFile()
require.NoError(t, err)
installedJson := requirejson.Parse(t, installedJsonData)
updatedInstalledJsonData := installedJson.Query(`del( .packages[0].platforms[0].deprecated )`).String()
require.NoError(t, installedJsonFile.WriteFile([]byte(updatedInstalledJsonData)))
// test same behaviour with json output
stdout, _, err := cli.Run("core", "list", "--additional-urls="+url.String(), "--format=json")
require.NoError(t, err)
requirejson.Len(t, stdout, 1)
requirejson.Query(t, stdout, ".[] | .deprecated", "true")
}
func TestCoreListPlatformWithoutPlatformTxt(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
_, _, err := cli.Run("update")
require.NoError(t, err)
stdout, _, err := cli.Run("core", "list", "--format", "json")
require.NoError(t, err)
requirejson.Len(t, stdout, 0)
// Simulates creation of a new core in the sketchbook hardware folder
// without a platforms.txt
testBoardsTxt := paths.New("..", "testdata", "boards.local.txt")
boardsTxt := cli.SketchbookDir().Join("hardware", "some-packager", "some-arch", "boards.txt")
require.NoError(t, boardsTxt.Parent().MkdirAll())
require.NoError(t, testBoardsTxt.CopyTo(boardsTxt))
// Verifies no core is installed
stdout, _, err = cli.Run("core", "list", "--format", "json")
require.NoError(t, err)
requirejson.Len(t, stdout, 1)
requirejson.Query(t, stdout, ".[] | .id", "\"some-packager:some-arch\"")
requirejson.Query(t, stdout, ".[] | .name", "\"some-packager-some-arch\"")
}
func TestCoreDownloadMultiplePlatforms(t *testing.T) {
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
t.Skip("macOS by default is case insensitive https://github.com/actions/virtual-environments/issues/865 ",
"Windows too is case insensitive",
"https://stackoverflow.com/questions/7199039/file-paths-in-windows-environment-not-case-sensitive")
}
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
_, _, err := cli.Run("update")
require.NoError(t, err)
// Verifies no core is installed
stdout, _, err := cli.Run("core", "list", "--format", "json")
require.NoError(t, err)
requirejson.Len(t, stdout, 0)
// Simulates creation of two new cores in the sketchbook hardware folder
wd, _ := paths.Getwd()
testBoardsTxt := wd.Parent().Join("testdata", "boards.local.txt")
boardsTxt := cli.DataDir().Join("packages", "PACKAGER", "hardware", "ARCH", "1.0.0", "boards.txt")
require.NoError(t, boardsTxt.Parent().MkdirAll())
require.NoError(t, testBoardsTxt.CopyTo(boardsTxt))
boardsTxt1 := cli.DataDir().Join("packages", "packager", "hardware", "arch", "1.0.0", "boards.txt")
require.NoError(t, boardsTxt1.Parent().MkdirAll())
require.NoError(t, testBoardsTxt.CopyTo(boardsTxt1))
// Verifies the two cores are detected
stdout, _, err = cli.Run("core", "list", "--format", "json")
require.NoError(t, err)
requirejson.Len(t, stdout, 2)
// Try to do an operation on the fake cores.
// The cli should not allow it since optimizing the casing results in finding two cores
_, stderr, err := cli.Run("core", "upgrade", "Packager:Arch")
require.Error(t, err)
require.Contains(t, string(stderr), "Invalid argument passed: Found 2 platform for reference")
}
func TestCoreWithMissingCustomBoardOptionsIsLoaded(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Install platform in Sketchbook hardware dir
testPlatformName := "platform_with_missing_custom_board_options"
platformInstallDir := cli.SketchbookDir().Join("hardware", "arduino-beta-dev")
require.NoError(t, platformInstallDir.MkdirAll())
require.NoError(t, paths.New("..", "testdata", testPlatformName).CopyDirTo(platformInstallDir.Join(testPlatformName)))
_, _, err := cli.Run("update")
require.NoError(t, err)
stdout, _, err := cli.Run("core", "list", "--format", "json")
require.NoError(t, err)
requirejson.Len(t, stdout, 1)
// Verifies platform is loaded except excluding board with missing options
requirejson.Contains(t, stdout, `[
{
"id": "arduino-beta-dev:platform_with_missing_custom_board_options"
}
]`)
requirejson.Query(t, stdout, ".[] | select(.id == \"arduino-beta-dev:platform_with_missing_custom_board_options\") | .boards | length", "2")
// Verify board with malformed options is not loaded
// while other board is loaded
requirejson.Contains(t, stdout, `[
{
"id": "arduino-beta-dev:platform_with_missing_custom_board_options",
"boards": [
{
"fqbn": "arduino-beta-dev:platform_with_missing_custom_board_options:nessuno"
},
{
"fqbn": "arduino-beta-dev:platform_with_missing_custom_board_options:altra"
}
]
}
]`)
}
func TestCoreListOutdatedCore(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
_, _, err := cli.Run("update")
require.NoError(t, err)
// Install an old core version
_, _, err = cli.Run("core", "install", "arduino:[email protected]")
require.NoError(t, err)
stdout, _, err := cli.Run("core", "list", "--format", "json")
require.NoError(t, err)
requirejson.Len(t, stdout, 1)
requirejson.Query(t, stdout, ".[0] | .installed", "\"1.8.6\"")
installedVersion, err := semver.Parse(strings.Trim(requirejson.Parse(t, stdout).Query(".[0] | .installed").String(), "\""))
require.NoError(t, err)
latestVersion, err := semver.Parse(strings.Trim(requirejson.Parse(t, stdout).Query(".[0] | .latest").String(), "\""))
require.NoError(t, err)
// Installed version must be older than latest
require.True(t, installedVersion.LessThan(latestVersion))
}
func TestCoreLoadingPackageManager(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
// Create empty architecture folder (this condition is normally produced by `core uninstall`)
require.NoError(t, cli.DataDir().Join("packages", "foovendor", "hardware", "fooarch").MkdirAll())
_, _, err := cli.Run("core", "list", "--all", "--format", "json")
require.NoError(t, err) // this should not make the cli crash
}
func TestCoreIndexWithoutChecksum(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
_, _, err := cli.Run("config", "init", "--dest-dir", ".")
require.NoError(t, err)
url := "https://raw.githubusercontent.com/keyboardio/ArduinoCore-GD32-Keyboardio/ae5938af2f485910729e7d27aa233032a1cb4734/package_gd32_index.json" // noqa: E501
_, _, err = cli.Run("config", "add", "board_manager.additional_urls", url, "--config-file", "arduino-cli.yaml")
require.NoError(t, err)
_, _, err = cli.Run("core", "update-index", "--config-file", "arduino-cli.yaml")
require.NoError(t, err)
_, _, err = cli.Run("core", "list", "--all", "--config-file", "arduino-cli.yaml")
require.NoError(t, err) // this should not make the cli crash
}
func TestCoreInstallCreatesInstalledJson(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
_, _, err := cli.Run("core", "update-index")
require.NoError(t, err)
_, _, err = cli.Run("core", "install", "arduino:[email protected]")
require.NoError(t, err)
installedJsonFile := cli.DataDir().Join("packages", "arduino", "hardware", "avr", "1.6.23", "installed.json")
require.FileExists(t, installedJsonFile.String())
installedJson, err := installedJsonFile.ReadFile()
require.NoError(t, err)
expectedInstalledJson, err := paths.New("..", "testdata", "installed.json").ReadFile()
require.NoError(t, err)
sortedInstalled := requirejson.Parse(t, installedJson).Query("walk(if type == \"array\" then sort else . end)").String()
sortedExpected := requirejson.Parse(t, expectedInstalledJson).Query("walk(if type == \"array\" then sort else . end)").String()
require.JSONEq(t, sortedExpected, sortedInstalled)
}
func TestCoreInstallRunsToolPostInstallScript(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)