Skip to content

Commit

Permalink
Added test TestBoardDetectionViaAPIWithNonUSBPort
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Oct 18, 2019
1 parent 5e1add9 commit f0209c1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
22 changes: 13 additions & 9 deletions commands/board/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ func apiByVidPid(vid, pid string) ([]*rpc.BoardListItem, error) {
return retVal, nil
}

func identifyViaCloudApi(port *commands.BoardPort) ([]*rpc.BoardListItem, error) {
// If the port is not USB do not try identification via cloud
id := port.IdentificationPrefs
if !id.ContainsKey("vid") || !id.ContainsKey("pid") {
return nil, ErrNotFound
}

logrus.Debug("Querying builder API for board identification...")
return apiByVidPid(id.Get("vid"), id.Get("pid"))
}

// List FIXMEDOC
func List(instanceID int32) ([]*rpc.DetectedPort, error) {
m.Lock()
Expand Down Expand Up @@ -120,15 +131,8 @@ func List(instanceID int32) ([]*rpc.DetectedPort, error) {

// if installed cores didn't recognize the board, try querying
// the builder API if the board is a USB device port
if len(b) == 0 &&
port.IdentificationPrefs.ContainsKey("vid") &&
port.IdentificationPrefs.ContainsKey("pid") {

logrus.Debug("Querying builder API for board identification...")
items, err := apiByVidPid(
port.IdentificationPrefs.Get("vid"),
port.IdentificationPrefs.Get("pid"),
)
if len(b) == 0 {
items, err := identifyViaCloudApi(port)
if err == ErrNotFound {
// the board couldn't be detected, print a warning
logrus.Debug("Board not recognized")
Expand Down
11 changes: 11 additions & 0 deletions commands/board/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"net/http/httptest"
"testing"

"github.com/arduino/arduino-cli/commands"
"github.com/arduino/go-properties-orderedmap"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -91,3 +93,12 @@ func TestGetByVidPidMalformedResponse(t *testing.T) {
require.Equal(t, "wrong format in server response", err.Error())
require.Len(t, res, 0)
}

func TestBoardDetectionViaAPIWithNonUSBPort(t *testing.T) {
port := &commands.BoardPort{
IdentificationPrefs: properties.NewMap(),
}
items, err := identifyViaCloudApi(port)
require.Equal(t, err, ErrNotFound)
require.Empty(t, items)
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ require (
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45
golang.org/x/net v0.0.0-20190311183353-d8887717615a
golang.org/x/text v0.3.0
google.golang.org/appengine v1.4.0 // indirect
google.golang.org/genproto v0.0.0-20190327125643-d831d65fe17d // indirect
google.golang.org/grpc v1.21.1
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce // indirect
Expand Down

0 comments on commit f0209c1

Please sign in to comment.