Skip to content

Commit 4191f9f

Browse files
committed
replace test with a meaningful one
The tools are the one coming with the cores installed in the builder
1 parent ba3283c commit 4191f9f

File tree

2 files changed

+421
-68
lines changed

2 files changed

+421
-68
lines changed

tools/download_test.go

+37-68
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@ package tools
1818
import (
1919
"encoding/json"
2020
"fmt"
21-
"io"
22-
"net/http"
23-
"os"
24-
"path"
2521
"testing"
22+
"time"
2623

24+
"github.com/arduino/arduino-create-agent/index"
2725
"github.com/arduino/arduino-create-agent/v2/pkgs"
2826
"github.com/arduino/go-paths-helper"
29-
"github.com/stretchr/testify/assert"
3027
"github.com/stretchr/testify/require"
3128
)
3229

@@ -126,71 +123,43 @@ func Test_findBaseDir(t *testing.T) {
126123
}
127124
}
128125

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"},
137140
}
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(),
143147
}
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) })
163149

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+
})
194164
}
195-
196165
}

0 commit comments

Comments
 (0)