Skip to content

[WE-168] Path bugfix win #270

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 7 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
76 changes: 41 additions & 35 deletions tools/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -226,7 +227,7 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
// Decompress
t.Logger("Unpacking tool " + name)

location := path.Join(dir(), pack, correctTool.Name, correctTool.Version)
location := path.Join("tmp", dir(), pack, correctTool.Name, correctTool.Version)
err = os.RemoveAll(location)

if err != nil {
Expand All @@ -240,12 +241,12 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {

switch srcType {
case "application/zip":
location, err = extractZip(body, location)
location, err = extractZip(t.Logger, body, location)
case "application/x-bz2":
case "application/octet-stream":
location, err = extractBz2(body, location)
location, err = extractBz2(t.Logger, body, location)
case "application/x-gzip":
location, err = extractTarGz(body, location)
location, err = extractTarGz(t.Logger, body, location)
default:
return errors.New("Unknown extension for file " + correctSystem.URL)
}
Expand Down Expand Up @@ -392,7 +393,7 @@ func findBaseDir(dirList []string) string {
return commonBaseDir
}

func extractZip(body []byte, location string) (string, error) {
func extractZip(log func(msg string) , body []byte, location string) (string, error) {
path, err := utilities.SaveFileonTempDir("tooldownloaded.zip", bytes.NewReader(body))
r, err := zip.OpenReader(path)
if err != nil {
Expand All @@ -406,6 +407,7 @@ func extractZip(body []byte, location string) (string, error) {
}

basedir := findBaseDir(dirList)
log(fmt.Sprintf("selected baseDir %s from Zip Archive Content: %v", basedir, dirList))

for _, f := range r.File {
fullname := filepath.Join(location, strings.Replace(f.Name, basedir, "", -1))
Expand Down Expand Up @@ -439,7 +441,7 @@ func extractZip(body []byte, location string) (string, error) {
return location, nil
}

func extractTarGz(body []byte, location string) (string, error) {
func extractTarGz(log func(msg string),body []byte, location string) (string, error) {
bodyCopy := make([]byte, len(body))
copy(bodyCopy, body)
tarFile, _ := gzip.NewReader(bytes.NewReader(body))
Expand All @@ -456,6 +458,7 @@ func extractTarGz(body []byte, location string) (string, error) {
}

basedir := findBaseDir(dirList)
log(fmt.Sprintf("selected baseDir %s from TarGz Archive Content: %v", basedir, dirList))

tarFile, _ = gzip.NewReader(bytes.NewReader(bodyCopy))
tarReader = tar.NewReader(tarFile)
Expand Down Expand Up @@ -502,36 +505,8 @@ func extractTarGz(body []byte, location string) (string, error) {
return location, nil
}

func (t *Tools) installDrivers(location string) error {
OK_PRESSED := 6
extension := ".bat"
preamble := ""
if runtime.GOOS != "windows" {
extension = ".sh"
// add ./ to force locality
preamble = "./"
}
if _, err := os.Stat(filepath.Join(location, "post_install"+extension)); err == nil {
t.Logger("Installing drivers")
ok := MessageBox("Installing drivers", "We are about to install some drivers needed to use Arduino/Genuino boards\nDo you want to continue?")
if ok == OK_PRESSED {
os.Chdir(location)
t.Logger(preamble + "post_install" + extension)
oscmd := exec.Command(preamble + "post_install" + extension)
if runtime.GOOS != "linux" {
// spawning a shell could be the only way to let the user type his password
TellCommandNotToSpawnShell(oscmd)
}
err = oscmd.Run()
return err
} else {
return errors.New("Could not install drivers")
}
}
return nil
}

func extractBz2(body []byte, location string) (string, error) {
func extractBz2(log func(msg string),body []byte, location string) (string, error) {
bodyCopy := make([]byte, len(body))
copy(bodyCopy, body)
tarFile := bzip2.NewReader(bytes.NewReader(body))
Expand All @@ -548,6 +523,7 @@ func extractBz2(body []byte, location string) (string, error) {
}

basedir := findBaseDir(dirList)
log(fmt.Sprintf("selected baseDir %s from Bz2 Archive Content: %v", basedir, dirList))

tarFile = bzip2.NewReader(bytes.NewReader(bodyCopy))
tarReader = tar.NewReader(tarFile)
Expand Down Expand Up @@ -596,6 +572,36 @@ func extractBz2(body []byte, location string) (string, error) {
return location, nil
}


func (t *Tools) installDrivers(location string) error {
OK_PRESSED := 6
extension := ".bat"
preamble := ""
if runtime.GOOS != "windows" {
extension = ".sh"
// add ./ to force locality
preamble = "./"
}
if _, err := os.Stat(filepath.Join(location, "post_install"+extension)); err == nil {
t.Logger("Installing drivers")
ok := MessageBox("Installing drivers", "We are about to install some drivers needed to use Arduino/Genuino boards\nDo you want to continue?")
if ok == OK_PRESSED {
os.Chdir(location)
t.Logger(preamble + "post_install" + extension)
oscmd := exec.Command(preamble + "post_install" + extension)
if runtime.GOOS != "linux" {
// spawning a shell could be the only way to let the user type his password
TellCommandNotToSpawnShell(oscmd)
}
err = oscmd.Run()
return err
} else {
return errors.New("Could not install drivers")
}
}
return nil
}

func makeExecutable(location string) error {
location = path.Join(location, "bin")
files, err := ioutil.ReadDir(location)
Expand Down
72 changes: 70 additions & 2 deletions tools/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package tools

import (
"fmt"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"os"
"path"
"testing"
)

Expand All @@ -14,8 +19,7 @@ func Test_findBaseDir(t *testing.T) {
{[]string{"bin/", "bin/bossac"}, "bin/"},
{[]string{"bin/", "bin/bossac", "example"}, ""},
{[]string{"avrdude/bin/avrdude", "avrdude/etc/avrdude.conf"}, "avrdude/"},
{[]string{"pax_global_header","bin/", "bin/bossac"}, "bin/"},

{[]string{"pax_global_header", "bin/", "bin/bossac"}, "bin/"},
}
for _, tt := range cases {
t.Run(fmt.Sprintln(tt.dirList), func(t *testing.T) {
Expand All @@ -25,3 +29,67 @@ func Test_findBaseDir(t *testing.T) {
})
}
}

func TestTools_DownloadAndUnpackBehaviour(t *testing.T) {
urls := []string{
"http://downloads.arduino.cc/tools/avrdude-6.3.0-arduino14-armhf-pc-linux-gnu.tar.bz2",
"http://downloads.arduino.cc/tools/avrdude-6.3.0-arduino14-aarch64-pc-linux-gnu.tar.bz2",
"http://downloads.arduino.cc/tools/avrdude-6.3.0-arduino14-i386-apple-darwin11.tar.bz2",
"http://downloads.arduino.cc/tools/avrdude-6.3.0-arduino14-x86_64-pc-linux-gnu.tar.bz2",
"http://downloads.arduino.cc/tools/avrdude-6.3.0-arduino14-i686-pc-linux-gnu.tar.bz2",
"http://downloads.arduino.cc/tools/avrdude-6.3.0-arduino14-i686-w64-mingw32.zip",
}
expectedDirList := []string{"bin", "etc"}

for _, url := range urls {
t.Log("Downloading tool from " + url)
resp, err := http.Get(url)
if err != nil {
t.Errorf("%v", err)
}
defer resp.Body.Close()

// Read the body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("%v", err)
}

location := path.Join("/tmp", dir(), "arduino", "avrdude", "6.3.0-arduino14")
os.MkdirAll(location, os.ModePerm)
err = os.RemoveAll(location)

if err != nil {
t.Errorf("%v", err)
}

srcType, err := mimeType(body)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we want to test the internal function that takes care of format/mime type here?

if err != nil {
t.Errorf("%v", err)
}

switch srcType {
case "application/zip":
location, err = extractZip(func(msg string) { t.Log(msg) }, body, location)
case "application/x-bz2":
case "application/octet-stream":
location, err = extractBz2(func(msg string) { t.Log(msg) }, body, location)
case "application/x-gzip":
location, err = extractTarGz(func(msg string) { t.Log(msg) }, body, location)
default:
t.Errorf("no suitable type found")
}
files, err := ioutil.ReadDir(location)
if err != nil {
t.Errorf("%v", err)
}
dirList := []string{}
for _, f := range files {
dirList = append(dirList, f.Name())
}

assert.ElementsMatchf(t, dirList, expectedDirList, "error message %s", "formatted")

}

}