Skip to content

Adding unit tests #14

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 4 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
35 changes: 35 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: test

on:
push:
branches:
- master
pull_request:

jobs:
native-os-build:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v1
- uses: actions/setup-go@v1
with:
go-version: "1.15"
- name: Build native
run: go build -v ./...
shell: bash
- name: Run unit tests
run: go test -v -race ./...
shell: bash
- name: Cross-build for 386
if: matrix.os != 'macOS-latest'
run: GOARCH=386 go build -v ./...
shell: bash
- name: Cross-build for arm
if: matrix.os != 'macOS-latest'
run: GOARCH=arm go build -v ./...
shell: bash
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ replace go.bug.st/serial => github.com/cmaglie/go-serial v0.0.0-20200923162623-b

require (
github.com/arduino/arduino-cli v0.0.0-20200924151007-69ac12c98b2b
github.com/arduino/go-paths-helper v1.3.2
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.6.1
go.bug.st/serial v1.1.1
)
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
flag.Parse()

if ctx.Compatible != "" {
el, _ := json.Marshal(utils.GetCompatibleWith(ctx.Compatible))
el, _ := json.Marshal(utils.GetCompatibleWith(ctx.Compatible, ""))
fmt.Println(string(el))
os.Exit(0)
}
Expand Down
10 changes: 6 additions & 4 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func isPreferred(existing bool, path string, board combo) bool {
return true
}

func GetCompatibleWith(name string) map[string][]firmware {
func GetCompatibleWith(name string, rootPath string) map[string][]firmware {

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

Expand All @@ -51,8 +51,10 @@ func GetCompatibleWith(name string) map[string][]firmware {
if knownBoards[strings.ToLower(name)].match == "" {
listAll = true
}

exePath, _ := os.Executable()
exePath := rootPath
if exePath == "" {
exePath, _ = os.Executable()
}
root := filepath.Dir(exePath)
root = filepath.Join(root, "firmwares")
loader := regexp.MustCompile(knownBoards[name].loader)
Expand All @@ -68,7 +70,7 @@ func GetCompatibleWith(name string) map[string][]firmware {
f := firmware{
Path: path,
Name: fancyName,
IsLoader: loader.MatchString(path) && !listAll,
IsLoader: loader.MatchString(unixPath) && !listAll,
}
folder := filepath.Dir(path)
lowerPath, _ := filepath.Rel(root, path)
Expand Down
36 changes: 36 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package utils

import (
"testing"

"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/require"
)

func TestGetCompatibleWith(t *testing.T) {
root, err := paths.Getwd()
require.NoError(t, err)
require.NoError(t, root.ToAbs())
testrunner := func(board string) {
t.Run(board, func(t *testing.T) {
res := GetCompatibleWith(board, root.String())
require.NotNil(t, res)
hasLoader := false
for _, e := range res {
for _, i := range e {
if i.IsLoader {
require.False(t, hasLoader, "loader must be unique")
hasLoader = true
require.NotEmpty(t, i.Name)
require.NotEmpty(t, i.Path)
}
}
}
require.True(t, hasLoader, "loader must be present")
})
}

testrunner("mkrwifi1010")
testrunner("mkr1000")
testrunner("nano_33_iot")
}