Skip to content

Download the correct tools for Windows 64-bit #813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 6 additions & 45 deletions tools/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,40 +40,10 @@ import (
"golang.org/x/crypto/openpgp"

"github.com/arduino/arduino-create-agent/utilities"
"github.com/arduino/arduino-create-agent/v2/pkgs"
"github.com/blang/semver"
"github.com/xrash/smetrics"
)

type system struct {
Host string `json:"host"`
URL string `json:"url"`
Name string `json:"archiveFileName"`
CheckSum string `json:"checksum"`
}

type tool struct {
Name string `json:"name"`
Version string `json:"version"`
Systems []system `json:"systems"`
}

type index struct {
Packages []struct {
Name string `json:"name"`
Tools []tool `json:"tools"`
} `json:"packages"`
}

var systems = map[string]string{
"linuxamd64": "x86_64-linux-gnu",
"linux386": "i686-linux-gnu",
"darwinamd64": "i686-apple-darwin",
"darwinarm64": "arm64-apple-darwin",
"windows386": "i686-mingw32",
"windowsamd64": "i686-mingw32",
"linuxarm": "arm-linux-gnueabihf",
}

// public vars to allow override in the tests
var (
OS = runtime.GOOS
Expand Down Expand Up @@ -199,7 +169,7 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
return err
}

var data index
var data pkgs.Index
json.Unmarshal(body, &data)

// Find the tool by name
Expand Down Expand Up @@ -245,7 +215,7 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
checksum := sha256.Sum256(body)
checkSumString := "SHA-256:" + hex.EncodeToString(checksum[:sha256.Size])

if checkSumString != correctSystem.CheckSum {
if checkSumString != correctSystem.Checksum {
return errors.New("checksum doesn't match")
}

Expand Down Expand Up @@ -299,8 +269,8 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
return t.writeMap()
}

func findTool(pack, name, version string, data index) (tool, system) {
var correctTool tool
func findTool(pack, name, version string, data pkgs.Index) (pkgs.Tool, pkgs.System) {
var correctTool pkgs.Tool
correctTool.Version = "0.0"

for _, p := range data.Packages {
Expand All @@ -324,16 +294,7 @@ func findTool(pack, name, version string, data index) (tool, system) {
}

// Find the url based on system
var correctSystem system
maxSimilarity := 0.7

for _, s := range correctTool.Systems {
similarity := smetrics.Jaro(s.Host, systems[OS+Arch])
if similarity > maxSimilarity {
correctSystem = s
maxSimilarity = similarity
}
}
correctSystem := correctTool.GetFlavourCompatibleWith(OS, Arch)

return correctTool, correctSystem
}
Expand Down
37 changes: 35 additions & 2 deletions tools/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"path"
"testing"

"github.com/arduino/arduino-create-agent/v2/pkgs"
"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -40,14 +41,14 @@ func TestDownloadCorrectPlatform(t *testing.T) {
{"darwin", "amd64", "x86_64-apple-darwin"},
{"darwin", "arm64", "arm64-apple-darwin"},
{"windows", "386", "i686-mingw32"},
{"windows", "amd64", "i686-mingw32"},
{"windows", "amd64", "x86_64-mingw32"},
{"linux", "arm", "arm-linux-gnueabihf"},
}
testIndex := paths.New("testdata", "test_tool_index.json")
buf, err := testIndex.ReadFile()
require.NoError(t, err)

var data index
var data pkgs.Index
err = json.Unmarshal(buf, &data)
require.NoError(t, err)
for _, tc := range testCases {
Expand All @@ -65,6 +66,38 @@ func TestDownloadCorrectPlatform(t *testing.T) {
}
}

func TestDownloadFallbackPlatform(t *testing.T) {
testCases := []struct {
hostOS string
hostArch string
correctOSArch string
}{
{"darwin", "amd64", "i386-apple-darwin11"},
{"darwin", "arm64", "i386-apple-darwin11"},
{"windows", "amd64", "i686-mingw32"},
}
testIndex := paths.New("testdata", "test_tool_index.json")
buf, err := testIndex.ReadFile()
require.NoError(t, err)

var data pkgs.Index
err = json.Unmarshal(buf, &data)
require.NoError(t, err)
for _, tc := range testCases {
t.Run(tc.hostOS+tc.hostArch, func(t *testing.T) {
OS = tc.hostOS // override `runtime.OS` for testing purposes
Arch = tc.hostArch // override `runtime.ARCH` for testing purposes
// Find the tool by name
correctTool, correctSystem := findTool("arduino-test", "arduino-fwuploader", "2.2.0", data)
require.NotNil(t, correctTool)
require.NotNil(t, correctSystem)
require.Equal(t, correctTool.Name, "arduino-fwuploader")
require.Equal(t, correctTool.Version, "2.2.0")
require.Equal(t, correctSystem.Host, tc.correctOSArch)
})
}
}

func Test_findBaseDir(t *testing.T) {
cases := []struct {
dirList []string
Expand Down
Loading