Skip to content

Commit 82be52f

Browse files
author
Roberto Sora
committed
created unarchive tests and injecting logger in unarchive functions
1 parent 282ae8d commit 82be52f

File tree

2 files changed

+111
-37
lines changed

2 files changed

+111
-37
lines changed

tools/download.go

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"encoding/hex"
1111
"encoding/json"
1212
"errors"
13+
"fmt"
1314
"io"
1415
"io/ioutil"
1516
"net/http"
@@ -226,7 +227,7 @@ func (t *Tools) Download(pack, name, version, behaviour string) error {
226227
// Decompress
227228
t.Logger("Unpacking tool " + name)
228229

229-
location := path.Join(dir(), pack, correctTool.Name, correctTool.Version)
230+
location := path.Join("tmp", dir(), pack, correctTool.Name, correctTool.Version)
230231
err = os.RemoveAll(location)
231232

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

241242
switch srcType {
242243
case "application/zip":
243-
location, err = extractZip(body, location)
244+
location, err = extractZip(t.Logger, body, location)
244245
case "application/x-bz2":
245246
case "application/octet-stream":
246-
location, err = extractBz2(body, location)
247+
location, err = extractBz2(t.Logger, body, location)
247248
case "application/x-gzip":
248-
location, err = extractTarGz(body, location)
249+
location, err = extractTarGz(t.Logger, body, location)
249250
default:
250251
return errors.New("Unknown extension for file " + correctSystem.URL)
251252
}
@@ -392,7 +393,7 @@ func findBaseDir(dirList []string) string {
392393
return commonBaseDir
393394
}
394395

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

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

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

442-
func extractTarGz(body []byte, location string) (string, error) {
444+
func extractTarGz(log func(msg string),body []byte, location string) (string, error) {
443445
bodyCopy := make([]byte, len(body))
444446
copy(bodyCopy, body)
445447
tarFile, _ := gzip.NewReader(bytes.NewReader(body))
@@ -456,6 +458,7 @@ func extractTarGz(body []byte, location string) (string, error) {
456458
}
457459

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

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

505-
func (t *Tools) installDrivers(location string) error {
506-
OK_PRESSED := 6
507-
extension := ".bat"
508-
preamble := ""
509-
if runtime.GOOS != "windows" {
510-
extension = ".sh"
511-
// add ./ to force locality
512-
preamble = "./"
513-
}
514-
if _, err := os.Stat(filepath.Join(location, "post_install"+extension)); err == nil {
515-
t.Logger("Installing drivers")
516-
ok := MessageBox("Installing drivers", "We are about to install some drivers needed to use Arduino/Genuino boards\nDo you want to continue?")
517-
if ok == OK_PRESSED {
518-
os.Chdir(location)
519-
t.Logger(preamble + "post_install" + extension)
520-
oscmd := exec.Command(preamble + "post_install" + extension)
521-
if runtime.GOOS != "linux" {
522-
// spawning a shell could be the only way to let the user type his password
523-
TellCommandNotToSpawnShell(oscmd)
524-
}
525-
err = oscmd.Run()
526-
return err
527-
} else {
528-
return errors.New("Could not install drivers")
529-
}
530-
}
531-
return nil
532-
}
533508

534-
func extractBz2(body []byte, location string) (string, error) {
509+
func extractBz2(log func(msg string),body []byte, location string) (string, error) {
535510
bodyCopy := make([]byte, len(body))
536511
copy(bodyCopy, body)
537512
tarFile := bzip2.NewReader(bytes.NewReader(body))
@@ -548,6 +523,7 @@ func extractBz2(body []byte, location string) (string, error) {
548523
}
549524

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

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

575+
576+
func (t *Tools) installDrivers(location string) error {
577+
OK_PRESSED := 6
578+
extension := ".bat"
579+
preamble := ""
580+
if runtime.GOOS != "windows" {
581+
extension = ".sh"
582+
// add ./ to force locality
583+
preamble = "./"
584+
}
585+
if _, err := os.Stat(filepath.Join(location, "post_install"+extension)); err == nil {
586+
t.Logger("Installing drivers")
587+
ok := MessageBox("Installing drivers", "We are about to install some drivers needed to use Arduino/Genuino boards\nDo you want to continue?")
588+
if ok == OK_PRESSED {
589+
os.Chdir(location)
590+
t.Logger(preamble + "post_install" + extension)
591+
oscmd := exec.Command(preamble + "post_install" + extension)
592+
if runtime.GOOS != "linux" {
593+
// spawning a shell could be the only way to let the user type his password
594+
TellCommandNotToSpawnShell(oscmd)
595+
}
596+
err = oscmd.Run()
597+
return err
598+
} else {
599+
return errors.New("Could not install drivers")
600+
}
601+
}
602+
return nil
603+
}
604+
599605
func makeExecutable(location string) error {
600606
location = path.Join(location, "bin")
601607
files, err := ioutil.ReadDir(location)

tools/download_test.go

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ package tools
22

33
import (
44
"fmt"
5+
"github.com/stretchr/testify/assert"
6+
"io/ioutil"
7+
"net/http"
8+
"os"
9+
"path"
510
"testing"
611
)
712

@@ -14,8 +19,7 @@ func Test_findBaseDir(t *testing.T) {
1419
{[]string{"bin/", "bin/bossac"}, "bin/"},
1520
{[]string{"bin/", "bin/bossac", "example"}, ""},
1621
{[]string{"avrdude/bin/avrdude", "avrdude/etc/avrdude.conf"}, "avrdude/"},
17-
{[]string{"pax_global_header","bin/", "bin/bossac"}, "bin/"},
18-
22+
{[]string{"pax_global_header", "bin/", "bin/bossac"}, "bin/"},
1923
}
2024
for _, tt := range cases {
2125
t.Run(fmt.Sprintln(tt.dirList), func(t *testing.T) {
@@ -25,3 +29,67 @@ func Test_findBaseDir(t *testing.T) {
2529
})
2630
}
2731
}
32+
33+
func TestTools_DownloadAndUnpackBehaviour(t *testing.T) {
34+
urls := []string{
35+
"http://downloads.arduino.cc/tools/avrdude-6.3.0-arduino14-armhf-pc-linux-gnu.tar.bz2",
36+
"http://downloads.arduino.cc/tools/avrdude-6.3.0-arduino14-aarch64-pc-linux-gnu.tar.bz2",
37+
"http://downloads.arduino.cc/tools/avrdude-6.3.0-arduino14-i386-apple-darwin11.tar.bz2",
38+
"http://downloads.arduino.cc/tools/avrdude-6.3.0-arduino14-x86_64-pc-linux-gnu.tar.bz2",
39+
"http://downloads.arduino.cc/tools/avrdude-6.3.0-arduino14-i686-pc-linux-gnu.tar.bz2",
40+
"http://downloads.arduino.cc/tools/avrdude-6.3.0-arduino14-i686-w64-mingw32.zip",
41+
}
42+
expectedDirList := []string{"bin", "etc"}
43+
44+
for _, url := range urls {
45+
t.Log("Downloading tool from " + url)
46+
resp, err := http.Get(url)
47+
if err != nil {
48+
t.Errorf("%v", err)
49+
}
50+
defer resp.Body.Close()
51+
52+
// Read the body
53+
body, err := ioutil.ReadAll(resp.Body)
54+
if err != nil {
55+
t.Errorf("%v", err)
56+
}
57+
58+
location := path.Join("/tmp", dir(), "arduino", "avrdude", "6.3.0-arduino14")
59+
os.MkdirAll(location, os.ModePerm)
60+
err = os.RemoveAll(location)
61+
62+
if err != nil {
63+
t.Errorf("%v", err)
64+
}
65+
66+
srcType, err := mimeType(body)
67+
if err != nil {
68+
t.Errorf("%v", err)
69+
}
70+
71+
switch srcType {
72+
case "application/zip":
73+
location, err = extractZip(func(msg string) { t.Log(msg) }, body, location)
74+
case "application/x-bz2":
75+
case "application/octet-stream":
76+
location, err = extractBz2(func(msg string) { t.Log(msg) }, body, location)
77+
case "application/x-gzip":
78+
location, err = extractTarGz(func(msg string) { t.Log(msg) }, body, location)
79+
default:
80+
t.Errorf("no suitable type found")
81+
}
82+
files, err := ioutil.ReadDir(location)
83+
if err != nil {
84+
t.Errorf("%v", err)
85+
}
86+
dirList := []string{}
87+
for _, f := range files {
88+
dirList = append(dirList, f.Name())
89+
}
90+
91+
assert.ElementsMatchf(t, dirList, expectedDirList, "error message %s", "formatted")
92+
93+
}
94+
95+
}

0 commit comments

Comments
 (0)