@@ -18,6 +18,7 @@ package core_test
18
18
import (
19
19
"fmt"
20
20
"runtime"
21
+ "sort"
21
22
"strconv"
22
23
"strings"
23
24
"testing"
@@ -582,3 +583,75 @@ func TestCoreListWithInstalledJson(t *testing.T) {
582
583
}
583
584
]` )
584
585
}
586
+
587
+ func TestCoreSearchSortedResults (t * testing.T ) {
588
+ env , cli := integrationtest .CreateArduinoCLIWithEnvironment (t )
589
+ defer env .CleanUp ()
590
+
591
+ // Set up the server to serve our custom index file
592
+ testIndex := paths .New (".." , "testdata" , "test_index.json" )
593
+ url := env .HTTPServeFile (8000 , testIndex )
594
+
595
+ // update custom index
596
+ _ , _ , err := cli .Run ("core" , "update-index" , "--additional-urls=" + url .String ())
597
+ require .NoError (t , err )
598
+
599
+ // This is done only to avoid index update output when calling core search
600
+ // since it automatically updates them if they're outdated and it makes it
601
+ // harder to parse the list of cores
602
+ _ , _ , err = cli .Run ("core" , "search" )
603
+ require .NoError (t , err )
604
+
605
+ // list all with additional url specified
606
+ stdout , _ , err := cli .Run ("core" , "search" , "--additional-urls=" + url .String ())
607
+ require .NoError (t , err )
608
+
609
+ out := strings .Split (strings .TrimSpace (string (stdout )), "\n " )
610
+ var lines , deprecated , notDeprecated [][]string
611
+ for i , v := range out {
612
+ if i > 0 {
613
+ v = strings .Join (strings .Fields (v ), " " )
614
+ lines = append (lines , strings .SplitN (v , " " , 3 ))
615
+ }
616
+ }
617
+ for _ , v := range lines {
618
+ if strings .HasPrefix (v [2 ], "[DEPRECATED]" ) {
619
+ deprecated = append (deprecated , v )
620
+ } else {
621
+ notDeprecated = append (notDeprecated , v )
622
+ }
623
+ }
624
+
625
+ // verify that results are already sorted correctly
626
+ require .True (t , sort .SliceIsSorted (deprecated , func (i , j int ) bool {
627
+ return strings .ToLower (deprecated [i ][2 ]) < strings .ToLower (deprecated [j ][2 ])
628
+ }))
629
+ require .True (t , sort .SliceIsSorted (notDeprecated , func (i , j int ) bool {
630
+ return strings .ToLower (notDeprecated [i ][2 ]) < strings .ToLower (notDeprecated [j ][2 ])
631
+ }))
632
+
633
+ // verify that deprecated platforms are the last ones
634
+ require .Equal (t , lines , append (notDeprecated , deprecated ... ))
635
+
636
+ // test same behaviour with json output
637
+ stdout , _ , err = cli .Run ("core" , "search" , "--additional-urls=" + url .String (), "--format=json" )
638
+ require .NoError (t , err )
639
+
640
+ // verify that results are already sorted correctly
641
+ sortedDeprecated := requirejson .Parse (t , stdout ).Query (
642
+ "[ .[] | select(.deprecated == true) | .name |=ascii_downcase | .name ] | sort" ).String ()
643
+ notSortedDeprecated := requirejson .Parse (t , stdout ).Query (
644
+ "[.[] | select(.deprecated == true) | .name |=ascii_downcase | .name]" ).String ()
645
+ require .Equal (t , sortedDeprecated , notSortedDeprecated )
646
+
647
+ sortedNotDeprecated := requirejson .Parse (t , stdout ).Query (
648
+ "[ .[] | select(.deprecated != true) | .name |=ascii_downcase | .name ] | sort" ).String ()
649
+ notSortedNotDeprecated := requirejson .Parse (t , stdout ).Query (
650
+ "[.[] | select(.deprecated != true) | .name |=ascii_downcase | .name]" ).String ()
651
+ require .Equal (t , sortedNotDeprecated , notSortedNotDeprecated )
652
+
653
+ // verify that deprecated platforms are the last ones
654
+ platform := requirejson .Parse (t , stdout ).Query (
655
+ "[.[] | .name |=ascii_downcase | .name]" ).String ()
656
+ require .True (t , strings .HasSuffix (platform , strings .TrimLeft (notSortedDeprecated , "[" )))
657
+ }
0 commit comments