@@ -18,15 +18,12 @@ package tools
18
18
import (
19
19
"encoding/json"
20
20
"fmt"
21
- "io"
22
- "net/http"
23
- "os"
24
- "path"
25
21
"testing"
22
+ "time"
26
23
24
+ "github.com/arduino/arduino-create-agent/index"
27
25
"github.com/arduino/arduino-create-agent/v2/pkgs"
28
26
"github.com/arduino/go-paths-helper"
29
- "github.com/stretchr/testify/assert"
30
27
"github.com/stretchr/testify/require"
31
28
)
32
29
@@ -126,71 +123,43 @@ func Test_findBaseDir(t *testing.T) {
126
123
}
127
124
}
128
125
129
- func TestTools_DownloadAndUnpackBehaviour (t * testing.T ) {
130
- urls := []string {
131
- "https://downloads.arduino.cc/tools/avrdude-6.3.0-arduino14-armhf-pc-linux-gnu.tar.bz2" ,
132
- "https://downloads.arduino.cc/tools/avrdude-6.3.0-arduino14-aarch64-pc-linux-gnu.tar.bz2" ,
133
- "https://downloads.arduino.cc/tools/avrdude-6.3.0-arduino14-i386-apple-darwin11.tar.bz2" ,
134
- "https://downloads.arduino.cc/tools/avrdude-6.3.0-arduino14-x86_64-pc-linux-gnu.tar.bz2" ,
135
- "https://downloads.arduino.cc/tools/avrdude-6.3.0-arduino14-i686-pc-linux-gnu.tar.bz2" ,
136
- "https://downloads.arduino.cc/tools/avrdude-6.3.0-arduino14-i686-w64-mingw32.zip" ,
126
+ func TestDownload (t * testing.T ) {
127
+ testCases := []struct {
128
+ name string
129
+ version string
130
+ }{
131
+ {"avrdude" , "6.3.0-arduino17" },
132
+ {"bossac" , "1.6.1-arduino" },
133
+ {"bossac" , "1.7.0-arduino3" },
134
+ {"bossac" , "1.9.1-arduino2" },
135
+ {"openocd" , "0.11.0-arduino2" },
136
+ {"dfu-util" , "0.10.0-arduino1" },
137
+ {"rp2040tools" , "1.0.6" },
138
+ {"esptool_py" , "4.5.1" },
139
+ {"arduino-fwuploader" , "2.2.2" },
137
140
}
138
- expectedDirList := []string {"bin" , "etc" }
139
-
140
- tmpDir , err := os .MkdirTemp ("" , "download_test" )
141
- if err != nil {
142
- t .Fatal (err )
141
+ // prepare the test environment
142
+ tempDir := t .TempDir ()
143
+ tempDirPath := paths .New (tempDir )
144
+ testIndex := index.Resource {
145
+ IndexFile : * paths .New ("testdata" , "test_tool_index.json" ),
146
+ LastRefresh : time .Now (),
143
147
}
144
- defer os .RemoveAll (tmpDir )
145
-
146
- for _ , url := range urls {
147
- t .Log ("Downloading tool from " + url )
148
- resp , err := http .Get (url )
149
- if err != nil {
150
- t .Errorf ("%v" , err )
151
- }
152
- defer resp .Body .Close ()
153
-
154
- // Read the body
155
- body , err := io .ReadAll (resp .Body )
156
- if err != nil {
157
- t .Errorf ("%v" , err )
158
- }
159
-
160
- location := path .Join (tmpDir , "username" , "arduino" , "avrdude" , "6.3.0-arduino14" )
161
- os .MkdirAll (location , os .ModePerm )
162
- err = os .RemoveAll (location )
148
+ testTools := New (tempDirPath , & testIndex , func (msg string ) { t .Log (msg ) })
163
149
164
- if err != nil {
165
- t .Errorf ("%v" , err )
166
- }
167
-
168
- srcType , err := mimeType (body )
169
- if err != nil {
170
- t .Errorf ("%v" , err )
171
- }
172
-
173
- switch srcType {
174
- case "application/zip" :
175
- location , err = extractZip (func (msg string ) { t .Log (msg ) }, body , location )
176
- case "application/x-bz2" :
177
- case "application/octet-stream" :
178
- location , err = extractBz2 (func (msg string ) { t .Log (msg ) }, body , location )
179
- case "application/x-gzip" :
180
- location , err = extractTarGz (func (msg string ) { t .Log (msg ) }, body , location )
181
- default :
182
- t .Errorf ("no suitable type found" )
183
- }
184
- files , err := os .ReadDir (location )
185
- if err != nil {
186
- t .Errorf ("%v" , err )
187
- }
188
- dirList := []string {}
189
- for _ , f := range files {
190
- dirList = append (dirList , f .Name ())
191
- }
192
-
193
- assert .ElementsMatchf (t , dirList , expectedDirList , "error message %s" , "formatted" )
150
+ for _ , tc := range testCases {
151
+ t .Run (tc .name + "-" + tc .version , func (t * testing.T ) {
152
+ // Download the tool
153
+ err := testTools .Download ("arduino-test" , tc .name , tc .version , "replace" )
154
+ require .NoError (t , err )
155
+
156
+ // Check that the tool has been downloaded
157
+ toolDir := tempDirPath .Join ("arduino-test" , tc .name , tc .version )
158
+ require .DirExists (t , toolDir .String ())
159
+
160
+ // Check that the tool has been installed
161
+ _ , ok := testTools .getMapValue (tc .name + "-" + tc .version )
162
+ require .True (t , ok )
163
+ })
194
164
}
195
-
196
165
}
0 commit comments