@@ -655,3 +655,76 @@ func TestCoreSearchSortedResults(t *testing.T) {
655655 "[.[] | .name |=ascii_downcase | .name]" ).String ()
656656 require .True (t , strings .HasSuffix (platform , strings .TrimLeft (notSortedDeprecated , "[" )))
657657}
658+
659+ func TestCoreListSortedResults (t * testing.T ) {
660+ env , cli := integrationtest .CreateArduinoCLIWithEnvironment (t )
661+ defer env .CleanUp ()
662+
663+ // Set up the server to serve our custom index file
664+ testIndex := paths .New (".." , "testdata" , "test_index.json" )
665+ url := env .HTTPServeFile (8000 , testIndex )
666+
667+ // update custom index
668+ _ , _ , err := cli .Run ("core" , "update-index" , "--additional-urls=" + url .String ())
669+ require .NoError (t , err )
670+
671+ // install some core for testing
672+ _ , _ , err = cli .Run ("core" , "install" , "test:x86" , "Retrokits-RK002:arm" , "Package:x86" , "--additional-urls=" + url .String ())
673+ require .NoError (t , err )
674+
675+ // list all with additional url specified
676+ stdout , _ , err := cli .Run ("core" , "list" , "--additional-urls=" + url .String ())
677+ require .NoError (t , err )
678+
679+ out := strings .Split (strings .TrimSpace (string (stdout )), "\n " )
680+ var lines , deprecated , notDeprecated [][]string
681+ for i , v := range out {
682+ if i > 0 {
683+ v = strings .Join (strings .Fields (v ), " " )
684+ lines = append (lines , strings .SplitN (v , " " , 4 ))
685+ }
686+ }
687+ require .Len (t , lines , 3 )
688+ for _ , v := range lines {
689+ if strings .HasPrefix (v [3 ], "[DEPRECATED]" ) {
690+ deprecated = append (deprecated , v )
691+ } else {
692+ notDeprecated = append (notDeprecated , v )
693+ }
694+ }
695+
696+ // verify that results are already sorted correctly
697+ require .True (t , sort .SliceIsSorted (deprecated , func (i , j int ) bool {
698+ return strings .ToLower (deprecated [i ][3 ]) < strings .ToLower (deprecated [j ][3 ])
699+ }))
700+ require .True (t , sort .SliceIsSorted (notDeprecated , func (i , j int ) bool {
701+ return strings .ToLower (notDeprecated [i ][3 ]) < strings .ToLower (notDeprecated [j ][3 ])
702+ }))
703+
704+ // verify that deprecated platforms are the last ones
705+ require .Equal (t , lines , append (notDeprecated , deprecated ... ))
706+
707+ // test same behaviour with json output
708+ stdout , _ , err = cli .Run ("core" , "list" , "--additional-urls=" + url .String (), "--format=json" )
709+ require .NoError (t , err )
710+ requirejson .Len (t , stdout , 3 )
711+
712+ // verify that results are already sorted correctly
713+ sortedDeprecated := requirejson .Parse (t , stdout ).Query (
714+ "[ .[] | select(.deprecated == true) | .name |=ascii_downcase | .name ] | sort" ).String ()
715+ notSortedDeprecated := requirejson .Parse (t , stdout ).Query (
716+ "[.[] | select(.deprecated == true) | .name |=ascii_downcase | .name]" ).String ()
717+ require .Equal (t , sortedDeprecated , notSortedDeprecated )
718+
719+ sortedNotDeprecated := requirejson .Parse (t , stdout ).Query (
720+ "[ .[] | select(.deprecated != true) | .name |=ascii_downcase | .name ] | sort" ).String ()
721+ notSortedNotDeprecated := requirejson .Parse (t , stdout ).Query (
722+ "[.[] | select(.deprecated != true) | .name |=ascii_downcase | .name]" ).String ()
723+ require .Equal (t , sortedNotDeprecated , notSortedNotDeprecated )
724+
725+ // verify that deprecated platforms are the last ones
726+ platform := requirejson .Parse (t , stdout ).Query (
727+ "[.[] | .name |=ascii_downcase | .name]" ).String ()
728+ require .True (t , strings .HasSuffix (platform , strings .TrimLeft (notSortedDeprecated , "[" )))
729+
730+ }
0 commit comments