Skip to content

Commit 242fa79

Browse files
matteosuppofacchinm
authored andcommitted
Refactor, resolve is now public
1 parent 1ed3b66 commit 242fa79

File tree

4 files changed

+71
-66
lines changed

4 files changed

+71
-66
lines changed

programmer/programmer.go

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package programmer
22

33
import (
4+
"path/filepath"
5+
"regexp"
6+
"strings"
47
"time"
58

69
"github.com/facchinm/go-serial"
@@ -24,6 +27,11 @@ func info(l logger, args ...interface{}) {
2427
}
2528
}
2629

30+
// tools can return the location of a tool in the system
31+
type tools interface {
32+
GetLocation(command string) (string, error)
33+
}
34+
2735
// Auth contains username and password used for a network upload
2836
type Auth struct {
2937
Username string `json:"username"`
@@ -41,19 +49,49 @@ type Extra struct {
4149
ParamsQuiet string `json:"params_quiet"`
4250
}
4351

52+
// Resolve replaces some symbols in the commandline with the appropriate values
53+
// it can return an error when looking a variable in the tools
54+
func Resolve(port, board, file, commandline string, extra Extra, tools tools) (string, error) {
55+
commandline = strings.Replace(commandline, "{build.path}", filepath.ToSlash(filepath.Dir(file)), -1)
56+
commandline = strings.Replace(commandline, "{build.project_name}", strings.TrimSuffix(filepath.Base(file), filepath.Ext(filepath.Base(file))), -1)
57+
commandline = strings.Replace(commandline, "{serial.port}", port, -1)
58+
commandline = strings.Replace(commandline, "{serial.port.file}", filepath.Base(port), -1)
59+
60+
if extra.Verbose == true {
61+
commandline = strings.Replace(commandline, "{upload.verbose}", extra.ParamsVerbose, -1)
62+
} else {
63+
commandline = strings.Replace(commandline, "{upload.verbose}", extra.ParamsQuiet, -1)
64+
}
65+
66+
// search for runtime variables and replace with values from globalToolsMap
67+
var runtimeRe = regexp.MustCompile("\\{(.*?)\\}")
68+
runtimeVars := runtimeRe.FindAllString(commandline, -1)
69+
70+
for _, element := range runtimeVars {
71+
72+
location, err := tools.GetLocation(element)
73+
if err != nil {
74+
return "", errors.Wrapf(err, "get location of %s", element)
75+
}
76+
commandline = strings.Replace(commandline, element, location, 1)
77+
}
78+
79+
return commandline, nil
80+
}
81+
4482
// Do performs a command on a port with a board attached to it
45-
func Do(port, board, file, commandline string, extra Extra, l logger) {
83+
func Do(port, board, file, commandline string, extra Extra, t tools, l logger) {
4684
debug(l, port, board, file, commandline)
4785
if extra.Network {
4886
doNetwork()
4987
} else {
50-
doSerial(port, board, file, commandline, extra, l)
88+
doSerial(port, board, file, commandline, extra, t, l)
5189
}
5290
}
5391

5492
func doNetwork() {}
5593

56-
func doSerial(port, board, file, commandline string, extra Extra, l logger) error {
94+
func doSerial(port, board, file, commandline string, extra Extra, t tools, l logger) error {
5795
// some boards needs to be resetted
5896
if extra.Use1200bpsTouch {
5997
var err error
@@ -63,11 +101,6 @@ func doSerial(port, board, file, commandline string, extra Extra, l logger) erro
63101
}
64102
}
65103

66-
// resolve commandline
67-
info(l, "unresolved commandline ", commandline)
68-
commandline = resolve(port, board, file, commandline, extra)
69-
info(l, "resolved commandline ", commandline)
70-
71104
return nil
72105
}
73106

programmer/programmer_test.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@ import (
55

66
"github.com/Sirupsen/logrus"
77
"github.com/arduino/arduino-create-agent/programmer"
8-
"github.com/stretchr/testify/suite"
98
)
109

11-
type ProgrammerTestSuite struct {
12-
suite.Suite
13-
}
10+
type mockTools struct{}
1411

15-
func TestProgrammer(t *testing.T) {
16-
suite.Run(t, new(ProgrammerTestSuite))
12+
func (mockTools) GetLocation(el string) (string, error) {
13+
return "$loc" + el, nil
1714
}
1815

1916
var TestSerialData = []struct {
@@ -28,12 +25,35 @@ var TestSerialData = []struct {
2825
`"{runtime.tools.avrdude.path}/bin/avrdude" "-C{runtime.tools.avrdude.path}/etc/avrdude.conf" {upload.verbose} {upload.verify} -patmega32u4 -cavr109 -P{serial.port} -b57600 -D "-Uflash:w:{build.path}/{build.project_name}.hex:i"`, programmer.Extra{Use1200bpsTouch: true, WaitForUploadPort: true}},
2926
}
3027

31-
func (t *ProgrammerTestSuite) TestSerial() {
28+
func TestSerial(t *testing.T) {
3229
logger := logrus.New()
3330
logger.Level = logrus.DebugLevel
3431

3532
for _, test := range TestSerialData {
36-
programmer.Do(test.Port, test.Board, test.File, test.Commandline, test.Extra, logger)
33+
programmer.Do(test.Port, test.Board, test.File, test.Commandline, test.Extra, mockTools{}, logger)
34+
}
35+
t.Fail()
36+
}
37+
38+
var TestResolveData = []struct {
39+
Port string
40+
Board string
41+
File string
42+
Commandline string
43+
Extra programmer.Extra
44+
Result string
45+
}{
46+
{"/dev/ttyACM0", "arduino:avr:leonardo", "./programmer_test.hex",
47+
`"{runtime.tools.avrdude.path}/bin/avrdude" "-C{runtime.tools.avrdude.path}/etc/avrdude.conf" {upload.verbose} {upload.verify} -patmega32u4 -cavr109 -P{serial.port} -b57600 -D "-Uflash:w:{build.path}/{build.project_name}.hex:i"`, programmer.Extra{Use1200bpsTouch: true, WaitForUploadPort: true},
48+
`"$loc$loc{runtime.tools.avrdude.path}/bin/avrdude" "-C{runtime.tools.avrdude.path}/etc/avrdude.conf" $loc{upload.verify} -patmega32u4 -cavr109 -P/dev/ttyACM0 -b57600 -D "-Uflash:w:./programmer_test.hex:i"`},
49+
}
50+
51+
func TestResolve(t *testing.T) {
52+
for _, test := range TestResolveData {
53+
result, _ := programmer.Resolve(test.Port, test.Board, test.File, test.Commandline, test.Extra, mockTools{})
54+
if result != test.Result {
55+
t.Error("expected " + test.Result + ", got " + result)
56+
continue
57+
}
3758
}
38-
t.Fail("")
3959
}

programmer/utils.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package programmer
22

3-
import (
4-
"path/filepath"
5-
"strings"
6-
)
7-
83
// differ returns the first item that differ between the two input slices
94
func differ(slice1 []string, slice2 []string) string {
105
m := map[string]int{}
@@ -24,18 +19,3 @@ func differ(slice1 []string, slice2 []string) string {
2419

2520
return ""
2621
}
27-
28-
// resolve replaces some symbols in the commandline with the appropriate values
29-
func resolve(port, board, file, commandline string, extra Extra) string {
30-
commandline = strings.Replace(commandline, "{build.path}", filepath.ToSlash(filepath.Dir(file)), -1)
31-
commandline = strings.Replace(commandline, "{build.project_name}", strings.TrimSuffix(filepath.Base(file), filepath.Ext(filepath.Base(file))), -1)
32-
commandline = strings.Replace(commandline, "{serial.port}", port, -1)
33-
commandline = strings.Replace(commandline, "{serial.port.file}", filepath.Base(port), -1)
34-
35-
if extra.Verbose == true {
36-
commandline = strings.Replace(commandline, "{upload.verbose}", extra.ParamsVerbose, -1)
37-
} else {
38-
commandline = strings.Replace(commandline, "{upload.verbose}", extra.ParamsQuiet, -1)
39-
}
40-
return commandline
41-
}

programmer/utils_test.go

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package programmer
22

3-
import (
4-
"log"
5-
"testing"
6-
)
3+
import "testing"
74

85
var TestDifferData = []struct {
96
Before []string
@@ -24,28 +21,3 @@ func TestDiffer(t *testing.T) {
2421
}
2522
}
2623
}
27-
28-
var TestResolveData = []struct {
29-
Port string
30-
Board string
31-
File string
32-
Commandline string
33-
Extra Extra
34-
Result string
35-
}{
36-
{"/dev/ttyACM0", "arduino:avr:leonardo", "./programmer_test.hex",
37-
`"{runtime.tools.avrdude.path}/bin/avrdude" "-C{runtime.tools.avrdude.path}/etc/avrdude.conf" {upload.verbose} {upload.verify} -patmega32u4 -cavr109 -P{serial.port} -b57600 -D "-Uflash:w:{build.path}/{build.project_name}.hex:i"`, Extra{Use1200bpsTouch: true, WaitForUploadPort: true},
38-
`"{runtime.tools.avrdude.path}/bin/avrdude" "-C{runtime.tools.avrdude.path}/etc/avrdude.conf" {upload.verify} -patmega32u4 -cavr109 -P/dev/ttyACM0 -b57600 -D "-Uflash:w:./programmer_test.hex:i"`},
39-
}
40-
41-
func TestResolve(t *testing.T) {
42-
for _, test := range TestResolveData {
43-
result := resolve(test.Port, test.Board, test.File, test.Commandline, test.Extra)
44-
if result != test.Result {
45-
log.Println(result)
46-
log.Println(test.Result)
47-
t.Error("expected " + test.Result + ", got " + result)
48-
continue
49-
}
50-
}
51-
}

0 commit comments

Comments
 (0)