Skip to content

Commit 75698ba

Browse files
Download the correct tools for Windows 64-bit (#813)
* Download the correct tools for Windows 64-bit * Change the OS/Arch matching criteria to find the correct fallback tool * Add TestDownloadFallbackPlatform to unit tests * Use public types defined in `v2/pkgs` package and move their methods there * Use imported `v2/pkgs` function to find the correct system
1 parent cc9a238 commit 75698ba

File tree

5 files changed

+280
-194
lines changed

5 files changed

+280
-194
lines changed

tools/download.go

+6-45
Original file line numberDiff line numberDiff line change
@@ -39,40 +39,10 @@ import (
3939
"golang.org/x/crypto/openpgp"
4040

4141
"github.com/arduino/arduino-create-agent/utilities"
42+
"github.com/arduino/arduino-create-agent/v2/pkgs"
4243
"github.com/blang/semver"
43-
"github.com/xrash/smetrics"
4444
)
4545

46-
type system struct {
47-
Host string `json:"host"`
48-
URL string `json:"url"`
49-
Name string `json:"archiveFileName"`
50-
CheckSum string `json:"checksum"`
51-
}
52-
53-
type tool struct {
54-
Name string `json:"name"`
55-
Version string `json:"version"`
56-
Systems []system `json:"systems"`
57-
}
58-
59-
type index struct {
60-
Packages []struct {
61-
Name string `json:"name"`
62-
Tools []tool `json:"tools"`
63-
} `json:"packages"`
64-
}
65-
66-
var systems = map[string]string{
67-
"linuxamd64": "x86_64-linux-gnu",
68-
"linux386": "i686-linux-gnu",
69-
"darwinamd64": "i686-apple-darwin",
70-
"darwinarm64": "arm64-apple-darwin",
71-
"windows386": "i686-mingw32",
72-
"windowsamd64": "i686-mingw32",
73-
"linuxarm": "arm-linux-gnueabihf",
74-
}
75-
7646
// public vars to allow override in the tests
7747
var (
7848
OS = runtime.GOOS
@@ -198,7 +168,7 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
198168
return err
199169
}
200170

201-
var data index
171+
var data pkgs.Index
202172
json.Unmarshal(body, &data)
203173

204174
// Find the tool by name
@@ -244,7 +214,7 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
244214
checksum := sha256.Sum256(body)
245215
checkSumString := "SHA-256:" + hex.EncodeToString(checksum[:sha256.Size])
246216

247-
if checkSumString != correctSystem.CheckSum {
217+
if checkSumString != correctSystem.Checksum {
248218
return errors.New("checksum doesn't match")
249219
}
250220

@@ -298,8 +268,8 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
298268
return t.writeMap()
299269
}
300270

301-
func findTool(pack, name, version string, data index) (tool, system) {
302-
var correctTool tool
271+
func findTool(pack, name, version string, data pkgs.Index) (pkgs.Tool, pkgs.System) {
272+
var correctTool pkgs.Tool
303273
correctTool.Version = "0.0"
304274

305275
for _, p := range data.Packages {
@@ -323,16 +293,7 @@ func findTool(pack, name, version string, data index) (tool, system) {
323293
}
324294

325295
// Find the url based on system
326-
var correctSystem system
327-
maxSimilarity := 0.7
328-
329-
for _, s := range correctTool.Systems {
330-
similarity := smetrics.Jaro(s.Host, systems[OS+Arch])
331-
if similarity > maxSimilarity {
332-
correctSystem = s
333-
maxSimilarity = similarity
334-
}
335-
}
296+
correctSystem := correctTool.GetFlavourCompatibleWith(OS, Arch)
336297

337298
return correctTool, correctSystem
338299
}

tools/download_test.go

+35-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"path"
2525
"testing"
2626

27+
"github.com/arduino/arduino-create-agent/v2/pkgs"
2728
"github.com/arduino/go-paths-helper"
2829
"github.com/stretchr/testify/assert"
2930
"github.com/stretchr/testify/require"
@@ -40,14 +41,14 @@ func TestDownloadCorrectPlatform(t *testing.T) {
4041
{"darwin", "amd64", "x86_64-apple-darwin"},
4142
{"darwin", "arm64", "arm64-apple-darwin"},
4243
{"windows", "386", "i686-mingw32"},
43-
{"windows", "amd64", "i686-mingw32"},
44+
{"windows", "amd64", "x86_64-mingw32"},
4445
{"linux", "arm", "arm-linux-gnueabihf"},
4546
}
4647
testIndex := paths.New("testdata", "test_tool_index.json")
4748
buf, err := testIndex.ReadFile()
4849
require.NoError(t, err)
4950

50-
var data index
51+
var data pkgs.Index
5152
err = json.Unmarshal(buf, &data)
5253
require.NoError(t, err)
5354
for _, tc := range testCases {
@@ -65,6 +66,38 @@ func TestDownloadCorrectPlatform(t *testing.T) {
6566
}
6667
}
6768

69+
func TestDownloadFallbackPlatform(t *testing.T) {
70+
testCases := []struct {
71+
hostOS string
72+
hostArch string
73+
correctOSArch string
74+
}{
75+
{"darwin", "amd64", "i386-apple-darwin11"},
76+
{"darwin", "arm64", "i386-apple-darwin11"},
77+
{"windows", "amd64", "i686-mingw32"},
78+
}
79+
testIndex := paths.New("testdata", "test_tool_index.json")
80+
buf, err := testIndex.ReadFile()
81+
require.NoError(t, err)
82+
83+
var data pkgs.Index
84+
err = json.Unmarshal(buf, &data)
85+
require.NoError(t, err)
86+
for _, tc := range testCases {
87+
t.Run(tc.hostOS+tc.hostArch, func(t *testing.T) {
88+
OS = tc.hostOS // override `runtime.OS` for testing purposes
89+
Arch = tc.hostArch // override `runtime.ARCH` for testing purposes
90+
// Find the tool by name
91+
correctTool, correctSystem := findTool("arduino-test", "arduino-fwuploader", "2.2.0", data)
92+
require.NotNil(t, correctTool)
93+
require.NotNil(t, correctSystem)
94+
require.Equal(t, correctTool.Name, "arduino-fwuploader")
95+
require.Equal(t, correctTool.Version, "2.2.0")
96+
require.Equal(t, correctSystem.Host, tc.correctOSArch)
97+
})
98+
}
99+
}
100+
68101
func Test_findBaseDir(t *testing.T) {
69102
cases := []struct {
70103
dirList []string

0 commit comments

Comments
 (0)