From b6f40b0f3455284ff042573a919df9c4c5d11bf1 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 13 Oct 2020 11:43:20 +0200 Subject: [PATCH 1/4] Added unti tests for utils.GetCompatibleWith --- go.mod | 2 ++ main.go | 2 +- utils/utils.go | 8 +++++--- utils/utils_test.go | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 utils/utils_test.go diff --git a/go.mod b/go.mod index 5a1bc6ab..20d4c579 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/main.go b/main.go index 088a7f68..aaaf99e2 100644 --- a/main.go +++ b/main.go @@ -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) } diff --git a/utils/utils.go b/utils/utils.go index d32cc465..010167bc 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -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) @@ -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) diff --git a/utils/utils_test.go b/utils/utils_test.go new file mode 100644 index 00000000..2a0026af --- /dev/null +++ b/utils/utils_test.go @@ -0,0 +1,34 @@ +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) { + 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") +} From e4f314850250975f70da50415f55206cec4bbfe5 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 13 Oct 2020 11:44:06 +0200 Subject: [PATCH 2/4] Added ghaction runner --- .github/workflows/test.yaml | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 .github/workflows/test.yaml diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 00000000..d202736f --- /dev/null +++ b/.github/workflows/test.yaml @@ -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 From be1bd4fdd288e35802d873793ad7c699d8ab249b Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 13 Oct 2020 10:59:31 +0100 Subject: [PATCH 3/4] Slightly improved test verbosity --- utils/utils_test.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/utils/utils_test.go b/utils/utils_test.go index 2a0026af..3c22aa8d 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -12,20 +12,22 @@ func TestGetCompatibleWith(t *testing.T) { require.NoError(t, err) require.NoError(t, root.ToAbs()) testrunner := func(board string) { - 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) + 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") + require.True(t, hasLoader, "loader must be present") + }) } testrunner("mkrwifi1010") From 1f97c7fe8ee92ad8ca1199a3cc30624ac82981ab Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 13 Oct 2020 10:59:56 +0100 Subject: [PATCH 4/4] Fixed regression in firmware detection --- utils/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/utils.go b/utils/utils.go index 010167bc..eaece2f2 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -70,7 +70,7 @@ func GetCompatibleWith(name string, rootPath 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)