Skip to content

Commit a448abb

Browse files
committed
test config added, fixed tests using test config file
1 parent 0ff1182 commit a448abb

File tree

7 files changed

+53
-14
lines changed

7 files changed

+53
-14
lines changed

cmd/.test-config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
proxy_type: auto
2+
sketchbook_path: /home/bcmi/Arduino
3+
arduino_data: /home/bcmi/.arduino15

cmd/arduino.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ arduino core version # for the version of the core component.`,
9999
}
100100

101101
var bundled bool
102+
var testing = false
102103

103104
func init() {
104105
versions[ArduinoCmd.Name()] = ArduinoVersion
@@ -107,6 +108,19 @@ func init() {
107108
InitCommands()
108109
}
109110

111+
// TestInit creates an initialization for tests
112+
func TestInit() {
113+
InitFlags()
114+
InitCommands()
115+
InitConfigs()
116+
117+
cobra.OnInitialize(func() {
118+
viper.SetConfigFile("./test-config.yml")
119+
})
120+
121+
testing = true
122+
}
123+
110124
// InitFlags reinitialize flags (useful for testing too)
111125
func InitFlags() {
112126
ArduinoCmd.ResetFlags()
@@ -197,7 +211,9 @@ func arduinoPreRun(cmd *cobra.Command, args []string) {
197211
})
198212
}
199213

200-
cobra.OnInitialize(initViper)
214+
if !testing {
215+
cobra.OnInitialize(initViper)
216+
}
201217
}
202218

203219
func arduinoRun(cmd *cobra.Command, args []string) error {
@@ -223,6 +239,7 @@ func arduinoRun(cmd *cobra.Command, args []string) error {
223239
func Execute() {
224240
err := ArduinoCmd.Execute()
225241
if err != nil {
242+
formatter.PrintError(err)
226243
os.Exit(1)
227244
}
228245
}
@@ -298,8 +315,8 @@ func initViper() {
298315

299316
err := viper.ReadInConfig()
300317
if err != nil {
318+
formatter.PrintError(err)
301319
formatter.PrintErrorMessage("Cannot read configuration file in any of the default folders")
302-
os.Exit(1) //Exit with error is OK here?
303320
}
304321

305322
viper.SetDefault("paths.sketchbook", defHome)
@@ -315,7 +332,7 @@ func initViper() {
315332
hostname := viper.GetString("proxy.hostname")
316333
if hostname == "" {
317334
formatter.PrintErrorMessage("With manual proxy configuration, hostname is required.")
318-
os.Exit(1)
335+
os.Exit(2)
319336
}
320337

321338
if strings.HasPrefix(hostname, "http") {

cmd/arduino_boards.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ package cmd
22

33
import (
44
"errors"
5+
"fmt"
56
"time"
67

8+
"github.com/bcmi-labs/arduino-cli/common"
9+
10+
"github.com/bcmi-labs/arduino-modules/boards"
11+
712
"github.com/bcmi-labs/arduino-cli/cmd/formatter"
813

914
"github.com/arduino/board-discovery"
@@ -43,7 +48,7 @@ func executeBoardListCommand(cmd *cobra.Command, args []string) {
4348

4449
formatter.Print(*monitor)
4550
//monitor.Stop() //If called will slow like 1sec the program to close after print, with the same result (tested).
46-
// it closes gracefully, but at the end of the command we can't have races.
51+
// it closes ungracefully, but at the end of the command we can't have races.
4752
}
4853

4954
func executeBoardAttachCommand(cmd *cobra.Command, args []string) error {
@@ -58,9 +63,20 @@ func executeBoardAttachCommand(cmd *cobra.Command, args []string) error {
5863

5964
formatter.Print(*monitor)
6065

61-
serialDevices := monitor.Serial()
62-
for name, device := range serialDevices {
66+
packageFolder, err := common.GetDefaultPkgFolder()
67+
if err != nil {
68+
formatter.PrintErrorMessage("Cannot Parse Board Index file")
69+
return nil
70+
}
71+
boards, err := boards.Find(packageFolder)
72+
if err != nil {
73+
formatter.PrintErrorMessage("Cannot Parse Board Index file")
74+
return nil
75+
}
6376

77+
fmt.Println("SUPPORTED BOARDS:")
78+
for _, device := range monitor.Serial() {
79+
fmt.Println(boards.ByVidPid(device.ProductID, device.VendorID))
6480
}
6581

6682
return nil

cmd/cmd_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ package cmd_test
3131

3232
import (
3333
"encoding/json"
34+
"fmt"
3435
"io/ioutil"
3536
"os"
3637
"strings"
@@ -56,6 +57,7 @@ func createTempRedirect(t *testing.T) *os.File {
5657
tempFile, err := ioutil.TempFile(os.TempDir(), "test")
5758
require.NoError(t, err, "Opening temp file")
5859
os.Stdout = tempFile
60+
5961
return tempFile
6062
}
6163

@@ -67,12 +69,13 @@ func cleanTempRedirect(t *testing.T, tempFile *os.File) {
6769
}
6870

6971
func executeWithArgs(t *testing.T, args ...string) {
70-
cmd.InitFlags()
71-
cmd.InitCommands()
72+
cmd.TestInit()
7273
if args != nil {
7374
cmd.ArduinoCmd.SetArgs(args)
7475
}
76+
7577
err := cmd.ArduinoCmd.Execute()
78+
fmt.Fprintln(stdOut, err)
7679
require.NoError(t, err, "Error executing command")
7780
}
7881

cmd/formatter/formatter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ var debug bool
5454
func init() {
5555
formatters = make(map[string]Formatter, 2)
5656
AddCustomFormatter("text", &TextFormatter{})
57-
AddCustomFormatter("json", &JSONFormatter{Debug: true})
57+
AddCustomFormatter("json", &JSONFormatter{})
5858
defaultFormatter = formatters["text"]
5959

6060
printFunc = defaultPrintFunc

configs/configs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ func getFileLocation() string {
6868

6969
// Configs represents the possible configurations for the CLI.
7070
type Configs struct {
71-
ProxyType string `yaml:"proxy_type,required"`
71+
ProxyType string `yaml:"proxy_type"`
7272
ProxyManualConfig *ProxyConfigs `yaml:"manual_configs,omitempty"`
7373
SketchbookPath string `yaml:"sketchbook_path,omitempty"`
7474
ArduinoDataFolder string `yaml:"arduino_data,omitempty"`
7575
}
7676

7777
//ProxyConfigs represents a possible manual proxy configuration.
7878
type ProxyConfigs struct {
79-
Hostname string `yaml:"hostname,required"`
79+
Hostname string `yaml:"hostname"`
8080
Username string `yaml:"username,omitempty"`
8181
Password string `yaml:"password,omitempty"` // can be encrypted, see issue #71
8282
}

libraries/common.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ import "strings"
3232

3333
// NameVersionPair represents a pair Name - Version.
3434
type NameVersionPair struct {
35-
Name string
36-
Version string
35+
Name string // The name of the parsed item.
36+
Version string // The Version of the parsed item.
3737
}
3838

39-
// ParseLibArgs parses a sequence of "item@version" tokens and returns a Name-Version slice.
39+
// ParseArgs parses a sequence of "item@version" tokens and returns a Name-Version slice.
4040
//
4141
// If version is not present it is assumed as "latest" version.
4242
func ParseArgs(args []string) []NameVersionPair {

0 commit comments

Comments
 (0)