Skip to content

Commit 94a334f

Browse files
authored
Merge pull request #14 from arduino/unit_tests
Adding unit tests
2 parents 27a77e8 + 1f97c7f commit 94a334f

File tree

5 files changed

+80
-5
lines changed

5 files changed

+80
-5
lines changed

.github/workflows/test.yaml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: test
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
9+
jobs:
10+
native-os-build:
11+
strategy:
12+
matrix:
13+
os: [ubuntu-latest, windows-latest, macOS-latest]
14+
15+
runs-on: ${{ matrix.os }}
16+
17+
steps:
18+
- uses: actions/checkout@v1
19+
- uses: actions/setup-go@v1
20+
with:
21+
go-version: "1.15"
22+
- name: Build native
23+
run: go build -v ./...
24+
shell: bash
25+
- name: Run unit tests
26+
run: go test -v -race ./...
27+
shell: bash
28+
- name: Cross-build for 386
29+
if: matrix.os != 'macOS-latest'
30+
run: GOARCH=386 go build -v ./...
31+
shell: bash
32+
- name: Cross-build for arm
33+
if: matrix.os != 'macOS-latest'
34+
run: GOARCH=arm go build -v ./...
35+
shell: bash

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

+6-4
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)
@@ -68,7 +70,7 @@ func GetCompatibleWith(name string) map[string][]firmware {
6870
f := firmware{
6971
Path: path,
7072
Name: fancyName,
71-
IsLoader: loader.MatchString(path) && !listAll,
73+
IsLoader: loader.MatchString(unixPath) && !listAll,
7274
}
7375
folder := filepath.Dir(path)
7476
lowerPath, _ := filepath.Rel(root, path)

utils/utils_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
t.Run(board, func(t *testing.T) {
16+
res := GetCompatibleWith(board, root.String())
17+
require.NotNil(t, res)
18+
hasLoader := false
19+
for _, e := range res {
20+
for _, i := range e {
21+
if i.IsLoader {
22+
require.False(t, hasLoader, "loader must be unique")
23+
hasLoader = true
24+
require.NotEmpty(t, i.Name)
25+
require.NotEmpty(t, i.Path)
26+
}
27+
}
28+
}
29+
require.True(t, hasLoader, "loader must be present")
30+
})
31+
}
32+
33+
testrunner("mkrwifi1010")
34+
testrunner("mkr1000")
35+
testrunner("nano_33_iot")
36+
}

0 commit comments

Comments
 (0)