1616package core_test
1717
1818import (
19- "encoding/json"
2019 "fmt"
2120 "strings"
2221 "testing"
2322
2423 "github.com/arduino/arduino-cli/internal/integrationtest"
2524 "github.com/arduino/go-paths-helper"
2625 "github.com/stretchr/testify/require"
27- "github.com/tidwall/gjson"
26+ "go.bug.st/testsuite"
27+ "go.bug.st/testsuite/requirejson"
2828)
2929
3030func TestCoreSearch (t * testing.T ) {
31- env := integrationtest .NewEnvironment (t )
31+ env := testsuite .NewEnvironment (t )
3232 defer env .CleanUp ()
3333
34- cli := integrationtest .NewArduinoCliWithinEnvironment (t , & integrationtest.ArduinoCLIConfig {
34+ cli := integrationtest .NewArduinoCliWithinEnvironment (env , & integrationtest.ArduinoCLIConfig {
3535 ArduinoCLIPath : paths .New (".." , ".." , ".." , "arduino-cli" ),
3636 UseSharedStagingFolder : true ,
37- }, env )
38- defer cli .CleanUp ()
37+ })
3938
4039 // Set up an http server to serve our custom index file
4140 test_index := paths .New (".." , "testdata" , "test_index.json" )
42- url , httpClose := integrationtest .HTTPServeFile (t , 8000 , test_index )
43- defer httpClose ()
41+ url := env .HTTPServeFile (8000 , test_index )
4442
4543 // Run update-index with our test index
4644 _ , _ , err := cli .Run ("core" , "update-index" , "--additional-urls=" + url .String ())
@@ -53,47 +51,22 @@ func TestCoreSearch(t *testing.T) {
5351
5452 out , _ , err = cli .Run ("core" , "search" , "avr" , "--format" , "json" )
5553 require .NoError (t , err )
56- data := make ([]interface {}, 0 )
57- require .NoError (t , json .Unmarshal (out , & data ))
58- require .NotEmpty (t , data )
59- // same check using gjson lib
60- require .NotEmpty (t , gjson .ParseBytes (out ).Array ())
54+ requirejson .NotEmpty (t , out )
6155
6256 // additional URL
6357 out , _ , err = cli .Run ("core" , "search" , "test_core" , "--format" , "json" , "--additional-urls=" + url .String ())
6458 require .NoError (t , err )
65- require .NoError (t , json .Unmarshal (out , & data ))
66- require .Len (t , data , 1 )
59+ requirejson .Len (t , out , 1 )
6760
6861 // show all versions
6962 out , _ , err = cli .Run ("core" , "search" , "test_core" , "--all" , "--format" , "json" , "--additional-urls=" + url .String ())
7063 require .NoError (t , err )
71- require .NoError (t , json .Unmarshal (out , & data ))
72- require .Len (t , data , 2 )
73- // alternative check using gjson:
74- require .Len (t , gjson .ParseBytes (out ).Array (), 2 )
75- // alternative using gojq:
76- integrationtest .JQQuery (t , out , "length" , 2 )
64+ requirejson .Len (t , out , 2 )
65+ // requirejson.Len(t, out, 3) // Test failure
7766
7867 checkPlatformIsInJSONOutput := func (stdout []byte , id , version string ) {
79- // Alternative solution with gojq
80- jqquery := fmt .Sprintf (`contains( [{id:"%s", latest:"%s"}] )` , id , version )
81- integrationtest .JQQuery (t , out , jqquery , true , "platform %s@%s is missing from the output" , id , version )
82-
83- // Alternative solution with gjson
84- // query := fmt.Sprintf("#(id=%s)#|#(latest=%s)", id, version)
85- // if gjson.ParseBytes(out).Get(query).Exists() {
86- // return
87- // }
88- // require.FailNowf(t, "Wrong output", "platform %s@%s is missing from the output", id, version)
89-
90- // Alternative solution:
91- // for _, platform := range gjson.ParseBytes(out).Array() {
92- // if platform.Get("id").Str == id && platform.Get("latest").Str == version {
93- // return
94- // }
95- // }
96- // require.FailNowf(t, "Wrong output", "platform %s@%s is missing from the output", id, version)
68+ jqquery := fmt .Sprintf (`[{id:"%s", latest:"%s"}]` , id , version )
69+ requirejson .Contains (t , out , jqquery , "platform %s@%s is missing from the output" , id , version )
9770 }
9871
9972 // Search all Retrokit platforms
@@ -127,20 +100,10 @@ func TestCoreSearch(t *testing.T) {
127100 out , _ , err := cli .Run (args ... )
128101 require .NoError (t , err )
129102
130- // Alternative solution with gojq
131103 for _ , id := range expectedIDs {
132- jqquery := fmt .Sprintf (`contains( [{id:"%s"}] ) ` , id )
133- integrationtest . JQQuery (t , out , jqquery , true , "platform %s is missing from the output" , id )
104+ jqquery := fmt .Sprintf (`[{id:"%s"}]` , id )
105+ requirejson . Contains (t , out , jqquery , "platform %s is missing from the output" , id )
134106 }
135-
136- // Alternative solution with gjson
137- // data := gjson.ParseBytes(out)
138- // for _, expectedID := range expectedIDs {
139- // query := fmt.Sprintf("#(id=%s)", expectedID)
140- // if !data.Get(query).Exists() {
141- // require.FailNowf(t, "Wrong output", "platform %s is missing from the output", expectedID)
142- // }
143- // }
144107 }
145108
146109 runSearch ("mkr1000" , "arduino:samd" )
0 commit comments