@@ -19,12 +19,15 @@ import (
19
19
"context"
20
20
"fmt"
21
21
"io"
22
+ "os"
22
23
"testing"
23
24
"time"
24
25
26
+ "github.com/arduino/arduino-cli/arduino"
25
27
"github.com/arduino/arduino-cli/internal/integrationtest"
26
28
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
27
29
"github.com/arduino/go-paths-helper"
30
+
28
31
"github.com/stretchr/testify/require"
29
32
)
30
33
@@ -215,34 +218,18 @@ func TestDaemonCoreUpdateIndex(t *testing.T) {
215
218
` "http://downloads.arduino.cc/package_inexistent_index.json"]` )
216
219
require .NoError (t , err )
217
220
218
- analyzeUpdateIndexClient := func (cl commands.ArduinoCoreService_UpdateIndexClient ) (map [string ]* commands.DownloadProgressEnd , error ) {
219
- analyzer := NewDownloadProgressAnalyzer (t )
220
- for {
221
- msg , err := cl .Recv ()
222
- // fmt.Println("DOWNLOAD>", msg)
223
- if err == io .EOF {
224
- return analyzer .Results , nil
225
- }
226
- if err != nil {
227
- return analyzer .Results , err
228
- }
229
- require .NoError (t , err )
230
- analyzer .Process (msg .GetDownloadProgress ())
231
- }
232
- }
233
-
234
221
{
235
222
cl , err := grpcInst .UpdateIndex (context .Background (), true )
236
223
require .NoError (t , err )
237
- res , err := analyzeUpdateIndexClient (cl )
224
+ res , err := analyzeUpdateIndexClient (t , cl )
238
225
require .NoError (t , err )
239
226
require .Len (t , res , 1 )
240
227
require .True (t , res ["https://downloads.arduino.cc/packages/package_index.tar.bz2" ].Success )
241
228
}
242
229
{
243
230
cl , err := grpcInst .UpdateIndex (context .Background (), false )
244
231
require .NoError (t , err )
245
- res , err := analyzeUpdateIndexClient (cl )
232
+ res , err := analyzeUpdateIndexClient (t , cl )
246
233
require .Error (t , err )
247
234
require .Len (t , res , 3 )
248
235
require .True (t , res ["https://downloads.arduino.cc/packages/package_index.tar.bz2" ].Success )
@@ -413,3 +400,158 @@ func TestDaemonLibrariesRescanOnInstall(t *testing.T) {
413
400
}
414
401
415
402
}
403
+
404
+ func TestDaemonCoreUpgradePlatform (t * testing.T ) {
405
+ refreshInstance := func (t * testing.T , grpcInst * integrationtest.ArduinoCLIInstance ) {
406
+ require .NoError (t , grpcInst .Init ("" , "" , func (ir * commands.InitResponse ) {}))
407
+ }
408
+ updateIndexAndInstallPlatform := func (cli * integrationtest.ArduinoCLI , grpcInst * integrationtest.ArduinoCLIInstance , version string ) {
409
+ refreshInstance (t , grpcInst )
410
+
411
+ // adding the additional urls
412
+ err := cli .SetValue ("board_manager.additional_urls" , `["https://arduino.esp8266.com/stable/package_esp8266com_index.json"]` )
413
+ require .NoError (t , err )
414
+
415
+ cl , err := grpcInst .UpdateIndex (context .Background (), false )
416
+ require .NoError (t , err )
417
+ res , err := analyzeUpdateIndexClient (t , cl )
418
+ require .NoError (t , err )
419
+ require .Len (t , res , 2 )
420
+ require .True (t , res ["https://arduino.esp8266.com/stable/package_esp8266com_index.json" ].Success )
421
+
422
+ refreshInstance (t , grpcInst )
423
+
424
+ // installing outdated version
425
+ plInst , err := grpcInst .PlatformInstall (context .Background (), "esp8266" , "esp8266" , version , true )
426
+ require .NoError (t , err )
427
+ for {
428
+ _ , err := plInst .Recv ()
429
+ if err == io .EOF {
430
+ break
431
+ }
432
+ require .NoError (t , err )
433
+ }
434
+ }
435
+
436
+ t .Run ("upgraded successfully with additional urls" , func (t * testing.T ) {
437
+ t .Run ("and install.json is present" , func (t * testing.T ) {
438
+ env , cli := createEnvForDaemon (t )
439
+ defer env .CleanUp ()
440
+
441
+ grpcInst := cli .Create ()
442
+ updateIndexAndInstallPlatform (cli , grpcInst , "3.1.0" )
443
+
444
+ plUpgrade , err := grpcInst .PlatformUpgrade (context .Background (), "esp8266" , "esp8266" , true )
445
+ require .NoError (t , err )
446
+
447
+ platform , upgradeError := analyzePlatformUpgradeClient (plUpgrade )
448
+ require .NoError (t , upgradeError )
449
+ require .NotNil (t , platform )
450
+ require .True (t , platform .Indexed ) // the esp866 is present in the additional-urls
451
+ require .False (t , platform .MissingMetadata ) // install.json is present
452
+ })
453
+ t .Run ("and install.json is missing" , func (t * testing.T ) {
454
+ env , cli := createEnvForDaemon (t )
455
+ defer env .CleanUp ()
456
+
457
+ grpcInst := cli .Create ()
458
+ updateIndexAndInstallPlatform (cli , grpcInst , "3.1.0" )
459
+
460
+ // remove installed.json{
461
+ x := env .RootDir ().Join ("A/packages/esp8266/hardware/esp8266/3.1.0/installed.json" )
462
+ require .NoError (t , os .Remove (x .String ()))
463
+
464
+ plUpgrade , err := grpcInst .PlatformUpgrade (context .Background (), "esp8266" , "esp8266" , true )
465
+ require .NoError (t , err )
466
+
467
+ platform , upgradeError := analyzePlatformUpgradeClient (plUpgrade )
468
+ require .NoError (t , upgradeError )
469
+ require .NotNil (t , platform )
470
+ require .True (t , platform .Indexed ) // the esp866 is not present in the additional-urls
471
+ require .False (t , platform .MissingMetadata ) // install.json is present because the old version got upgraded
472
+
473
+ })
474
+ })
475
+
476
+ t .Run ("upgrade failed" , func (t * testing.T ) {
477
+ t .Run ("without additional URLs" , func (t * testing.T ) {
478
+ env , cli := createEnvForDaemon (t )
479
+ defer env .CleanUp ()
480
+
481
+ grpcInst := cli .Create ()
482
+ updateIndexAndInstallPlatform (cli , grpcInst , "3.1.0" )
483
+
484
+ // remove esp8266 from the additional-urls
485
+ require .NoError (t , cli .SetValue ("board_manager.additional_urls" , `[]` ))
486
+ refreshInstance (t , grpcInst )
487
+
488
+ plUpgrade , err := grpcInst .PlatformUpgrade (context .Background (), "esp8266" , "esp8266" , true )
489
+ require .NoError (t , err )
490
+
491
+ platform , upgradeError := analyzePlatformUpgradeClient (plUpgrade )
492
+ require .ErrorIs (t , upgradeError , (& arduino.PlatformAlreadyAtTheLatestVersionError {Platform : "esp8266:esp8266" }).ToRPCStatus ().Err ())
493
+ require .NotNil (t , platform )
494
+ require .False (t , platform .Indexed ) // the esp866 is not present in the additional-urls
495
+ require .False (t , platform .MissingMetadata ) // install.json is present
496
+ })
497
+ t .Run ("missing both additional URLs and install.json" , func (t * testing.T ) {
498
+ env , cli := createEnvForDaemon (t )
499
+ defer env .CleanUp ()
500
+
501
+ grpcInst := cli .Create ()
502
+ updateIndexAndInstallPlatform (cli , grpcInst , "3.1.0" )
503
+
504
+ // remove additional urls and installed.json
505
+ {
506
+ require .NoError (t , cli .SetValue ("board_manager.additional_urls" , `[]` ))
507
+ refreshInstance (t , grpcInst )
508
+
509
+ x := env .RootDir ().Join ("A/packages/esp8266/hardware/esp8266/3.1.0/installed.json" )
510
+ require .NoError (t , os .Remove (x .String ()))
511
+ }
512
+
513
+ plUpgrade , err := grpcInst .PlatformUpgrade (context .Background (), "esp8266" , "esp8266" , true )
514
+ require .NoError (t , err )
515
+
516
+ platform , upgradeError := analyzePlatformUpgradeClient (plUpgrade )
517
+ require .ErrorIs (t , upgradeError , (& arduino.PlatformAlreadyAtTheLatestVersionError {Platform : "esp8266:esp8266" }).ToRPCStatus ().Err ())
518
+ require .NotNil (t , platform )
519
+ require .False (t , platform .Indexed ) // the esp866 is not present in the additional-urls
520
+ require .True (t , platform .MissingMetadata ) // install.json is present
521
+ })
522
+ })
523
+ }
524
+
525
+ func analyzeUpdateIndexClient (t * testing.T , cl commands.ArduinoCoreService_UpdateIndexClient ) (map [string ]* commands.DownloadProgressEnd , error ) {
526
+ analyzer := NewDownloadProgressAnalyzer (t )
527
+ for {
528
+ msg , err := cl .Recv ()
529
+ if err == io .EOF {
530
+ return analyzer .Results , nil
531
+ }
532
+ if err != nil {
533
+ return analyzer .Results , err
534
+ }
535
+ require .NoError (t , err )
536
+ analyzer .Process (msg .GetDownloadProgress ())
537
+ }
538
+ }
539
+
540
+ func analyzePlatformUpgradeClient (cl commands.ArduinoCoreService_PlatformUpgradeClient ) (* commands.Platform , error ) {
541
+ var platform * commands.Platform
542
+ var upgradeError error
543
+ for {
544
+ msg , err := cl .Recv ()
545
+ if err == io .EOF {
546
+ break
547
+ }
548
+ if msg .GetPlatform () != nil {
549
+ platform = msg .GetPlatform ()
550
+ }
551
+ if err != nil {
552
+ upgradeError = err
553
+ break
554
+ }
555
+ }
556
+ return platform , upgradeError
557
+ }
0 commit comments