Skip to content

Commit b6f40b0

Browse files
committed
Added unti tests for utils.GetCompatibleWith
1 parent 27a77e8 commit b6f40b0

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ replace go.bug.st/serial => github.com/cmaglie/go-serial v0.0.0-20200923162623-b
77

88
require (
99
github.com/arduino/arduino-cli v0.0.0-20200924151007-69ac12c98b2b
10+
github.com/arduino/go-paths-helper v1.3.2
1011
github.com/pkg/errors v0.9.1
12+
github.com/stretchr/testify v1.6.1
1113
go.bug.st/serial v1.1.1
1214
)

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func main() {
3434
flag.Parse()
3535

3636
if ctx.Compatible != "" {
37-
el, _ := json.Marshal(utils.GetCompatibleWith(ctx.Compatible))
37+
el, _ := json.Marshal(utils.GetCompatibleWith(ctx.Compatible, ""))
3838
fmt.Println(string(el))
3939
os.Exit(0)
4040
}

utils/utils.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func isPreferred(existing bool, path string, board combo) bool {
3434
return true
3535
}
3636

37-
func GetCompatibleWith(name string) map[string][]firmware {
37+
func GetCompatibleWith(name string, rootPath string) map[string][]firmware {
3838

3939
files := make(map[string][]firmware)
4040

@@ -51,8 +51,10 @@ func GetCompatibleWith(name string) map[string][]firmware {
5151
if knownBoards[strings.ToLower(name)].match == "" {
5252
listAll = true
5353
}
54-
55-
exePath, _ := os.Executable()
54+
exePath := rootPath
55+
if exePath == "" {
56+
exePath, _ = os.Executable()
57+
}
5658
root := filepath.Dir(exePath)
5759
root = filepath.Join(root, "firmwares")
5860
loader := regexp.MustCompile(knownBoards[name].loader)

utils/utils_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package utils
2+
3+
import (
4+
"testing"
5+
6+
"github.com/arduino/go-paths-helper"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestGetCompatibleWith(t *testing.T) {
11+
root, err := paths.Getwd()
12+
require.NoError(t, err)
13+
require.NoError(t, root.ToAbs())
14+
testrunner := func(board string) {
15+
res := GetCompatibleWith(board, root.String())
16+
require.NotNil(t, res)
17+
hasLoader := false
18+
for _, e := range res {
19+
for _, i := range e {
20+
if i.IsLoader {
21+
require.False(t, hasLoader, "loader must be unique")
22+
hasLoader = true
23+
require.NotEmpty(t, i.Name)
24+
require.NotEmpty(t, i.Path)
25+
}
26+
}
27+
}
28+
require.True(t, hasLoader, "loader must be present")
29+
}
30+
31+
testrunner("mkrwifi1010")
32+
testrunner("mkr1000")
33+
testrunner("nano_33_iot")
34+
}

0 commit comments

Comments
 (0)