Skip to content

Commit 49897ef

Browse files
federicobondMassimiliano Pippi
authored and
Massimiliano Pippi
committed
Improve error message when port or fqbn flags are not set (#523)
* Improve error message when port or fqbn flags are missing * Redirect stderr for integration tests too
1 parent 4f4c98d commit 49897ef

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

Diff for: cli/cli_test.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
"github.com/spf13/viper"
3232

33+
"github.com/arduino/arduino-cli/cli/errorcodes"
3334
"github.com/arduino/arduino-cli/cli/feedback"
3435

3536
"bou.ke/monkey"
@@ -42,26 +43,28 @@ var (
4243
// Redirecting stdOut so we can analyze output line by
4344
// line and check with what we want.
4445
stdOut = os.Stdout
46+
stdErr = os.Stderr
4547

4648
currDownloadDir string
4749
currDataDir string
4850
currUserDir string
4951
)
5052

51-
type stdOutRedirect struct {
53+
type outputRedirect struct {
5254
tempFile *os.File
5355
}
5456

55-
func (grabber *stdOutRedirect) Open() {
57+
func (grabber *outputRedirect) Open() {
5658
tempFile, err := ioutil.TempFile(os.TempDir(), "test")
5759
if err != nil {
5860
panic("Opening temp output file")
5961
}
6062
os.Stdout = tempFile
63+
os.Stderr = tempFile
6164
grabber.tempFile = tempFile
6265
}
6366

64-
func (grabber *stdOutRedirect) GetOutput() []byte {
67+
func (grabber *outputRedirect) GetOutput() []byte {
6568
_, err := grabber.tempFile.Seek(0, 0)
6669
if err != nil {
6770
panic("Rewinding temp output file")
@@ -75,13 +78,14 @@ func (grabber *stdOutRedirect) GetOutput() []byte {
7578
return output
7679
}
7780

78-
func (grabber *stdOutRedirect) Close() {
81+
func (grabber *outputRedirect) Close() {
7982
grabber.tempFile.Close()
8083
err := os.Remove(grabber.tempFile.Name())
8184
if err != nil {
8285
panic("Removing temp output file")
8386
}
8487
os.Stdout = stdOut
88+
os.Stderr = stdErr
8589
}
8690

8791
func TestMain(m *testing.M) {
@@ -136,7 +140,7 @@ func executeWithArgs(args ...string) (int, []byte) {
136140

137141
// This closure is here because we won't that the defer are executed after the end of the "executeWithArgs" method
138142
func() {
139-
redirect := &stdOutRedirect{}
143+
redirect := &outputRedirect{}
140144
redirect.Open()
141145
// re-init feedback so it'll write to our grabber
142146
feedback.SetDefaultFeedback(feedback.New(os.Stdout, os.Stdout, feedback.Text))
@@ -170,7 +174,9 @@ func executeWithArgs(args ...string) (int, []byte) {
170174
ArduinoCli.ResetFlags()
171175
createCliCommandTree(ArduinoCli)
172176
ArduinoCli.SetArgs(args)
173-
ArduinoCli.Execute()
177+
if err := ArduinoCli.Execute(); err != nil {
178+
exitCode = errorcodes.ErrGeneric
179+
}
174180
}()
175181

176182
return exitCode, output
@@ -307,7 +313,7 @@ func TestCompileCommandsIntegration(t *testing.T) {
307313
// Build sketch without FQBN
308314
exitCode, d = executeWithArgs("compile", sketchPath)
309315
require.NotZero(t, exitCode)
310-
require.Contains(t, string(d), "no FQBN provided")
316+
require.Contains(t, string(d), "required flag(s) \"fqbn\" not set")
311317

312318
// Build sketch for arduino:avr:uno
313319
exitCode, d = executeWithArgs("compile", "-b", "arduino:avr:uno", sketchPath)

Diff for: cli/compile/compile.go

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ func NewCommand() *cobra.Command {
8080
command.Flags().BoolVarP(&verify, "verify", "t", false, "Verify uploaded binary after the upload.")
8181
command.Flags().StringVar(&vidPid, "vid-pid", "", "When specified, VID/PID specific build properties are used, if boards supports them.")
8282

83+
command.MarkFlagRequired("fqbn")
84+
8385
return command
8486
}
8587

Diff for: cli/upload/upload.go

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ func NewCommand() *cobra.Command {
5656
uploadCommand.Flags().BoolVarP(&verify, "verify", "t", false, "Verify uploaded binary after the upload.")
5757
uploadCommand.Flags().BoolVarP(&verbose, "verbose", "v", false, "Optional, turns on verbose mode.")
5858

59+
uploadCommand.MarkFlagRequired("fqbn")
60+
uploadCommand.MarkFlagRequired("port")
61+
5962
return uploadCommand
6063
}
6164

0 commit comments

Comments
 (0)