16
16
package core_test
17
17
18
18
import (
19
- "encoding/json"
20
19
"fmt"
21
20
"strings"
22
21
"testing"
23
22
24
23
"github.com/arduino/arduino-cli/internal/integrationtest"
25
24
"github.com/arduino/go-paths-helper"
26
25
"github.com/stretchr/testify/require"
27
- "github.com/tidwall/gjson"
26
+ "go.bug.st/testsuite"
27
+ "go.bug.st/testsuite/requirejson"
28
28
)
29
29
30
30
func TestCoreSearch (t * testing.T ) {
31
- env := integrationtest .NewEnvironment (t )
31
+ env := testsuite .NewEnvironment (t )
32
32
defer env .CleanUp ()
33
33
34
- cli := integrationtest .NewArduinoCliWithinEnvironment (t , & integrationtest.ArduinoCLIConfig {
34
+ cli := integrationtest .NewArduinoCliWithinEnvironment (env , & integrationtest.ArduinoCLIConfig {
35
35
ArduinoCLIPath : paths .New (".." , ".." , ".." , "arduino-cli" ),
36
36
UseSharedStagingFolder : true ,
37
- }, env )
38
- defer cli .CleanUp ()
37
+ })
39
38
40
39
// Set up an http server to serve our custom index file
41
40
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 )
44
42
45
43
// Run update-index with our test index
46
44
_ , _ , err := cli .Run ("core" , "update-index" , "--additional-urls=" + url .String ())
@@ -53,47 +51,22 @@ func TestCoreSearch(t *testing.T) {
53
51
54
52
out , _ , err = cli .Run ("core" , "search" , "avr" , "--format" , "json" )
55
53
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 )
61
55
62
56
// additional URL
63
57
out , _ , err = cli .Run ("core" , "search" , "test_core" , "--format" , "json" , "--additional-urls=" + url .String ())
64
58
require .NoError (t , err )
65
- require .NoError (t , json .Unmarshal (out , & data ))
66
- require .Len (t , data , 1 )
59
+ requirejson .Len (t , out , 1 )
67
60
68
61
// show all versions
69
62
out , _ , err = cli .Run ("core" , "search" , "test_core" , "--all" , "--format" , "json" , "--additional-urls=" + url .String ())
70
63
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
77
66
78
67
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 )
97
70
}
98
71
99
72
// Search all Retrokit platforms
@@ -127,20 +100,10 @@ func TestCoreSearch(t *testing.T) {
127
100
out , _ , err := cli .Run (args ... )
128
101
require .NoError (t , err )
129
102
130
- // Alternative solution with gojq
131
103
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 )
134
106
}
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
- // }
144
107
}
145
108
146
109
runSearch ("mkr1000" , "arduino:samd" )
0 commit comments