Skip to content

Commit 3fff743

Browse files
committed
upload: sketch is ignored if input-dir or input-file is specified
There is no point in overriding the sketch name if the user explicitly give it via command line.
1 parent ef8ba2f commit 3fff743

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

arduino/sketches/sketches.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121

2222
"github.com/arduino/go-paths-helper"
23+
"github.com/pkg/errors"
2324
)
2425

2526
// Sketch is a sketch for Arduino
@@ -43,9 +44,17 @@ type BoardMetadata struct {
4344

4445
// NewSketchFromPath loads a sketch from the specified path
4546
func NewSketchFromPath(path *paths.Path) (*Sketch, error) {
47+
path, err := path.Abs()
48+
if err != nil {
49+
return nil, errors.Errorf("getting sketch path: %s", err)
50+
}
4651
if !path.IsDir() {
4752
path = path.Parent()
4853
}
54+
sketchFile := path.Join(path.Base() + ".ino")
55+
if !sketchFile.Exist() {
56+
return nil, errors.Errorf("no valid sketch found in %s: missing %s", path, sketchFile.Base())
57+
}
4958
sketch := &Sketch{
5059
FullPath: path,
5160
Name: path.Base(),

commands/upload/upload.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,9 @@ func Upload(ctx context.Context, req *rpc.UploadReq, outStream io.Writer, errStr
4444

4545
// TODO: make a generic function to extract sketch from request
4646
// and remove duplication in commands/compile.go
47-
if req.GetSketchPath() == "" {
48-
return nil, fmt.Errorf("missing sketchPath")
49-
}
5047
sketchPath := paths.New(req.GetSketchPath())
5148
sketch, err := sketches.NewSketchFromPath(sketchPath)
52-
if err != nil {
49+
if err != nil && req.GetImportDir() == "" && req.GetImportFile() == "" {
5350
return nil, fmt.Errorf("opening sketch: %s", err)
5451
}
5552

@@ -474,16 +471,9 @@ func determineBuildPathAndSketchName(importFile, importDir string, sketch *sketc
474471
}
475472

476473
if importDir != "" {
477-
// Case 2: importDir flag with a sketch
478-
if sketch != nil {
479-
// In this case we have both the build path and the sketch name given,
480-
// so we just return them as-is
481-
return paths.New(importDir), sketch.Name + ".ino", nil
482-
}
483-
484-
// Case 3: importDir flag without a sketch
474+
// Case 2: importDir flag has been specified
485475

486-
// In this case we have a build path but the sketch name is not given, we may
476+
// In this case we have a build path but ignore the sketch name, we'll
487477
// try to determine the sketch name by applying some euristics to the build folder.
488478
// - "build.path" as importDir
489479
// - "build.project_name" after trying to autodetect it from the build folder.
@@ -495,12 +485,12 @@ func determineBuildPathAndSketchName(importFile, importDir string, sketch *sketc
495485
return buildPath, sketchName, nil
496486
}
497487

498-
// Case 4: nothing given...
488+
// Case 3: nothing given...
499489
if sketch == nil {
500490
return nil, "", fmt.Errorf("no sketch or build directory/file specified")
501491
}
502492

503-
// Case 5: only sketch specified. In this case we use the default sketch build path
493+
// Case 4: only sketch specified. In this case we use the default sketch build path
504494
// and the given sketch name.
505495

506496
// TODO: Create a function to obtain importPath from sketch

commands/upload/upload_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ func TestDetermineBuildPathAndSketchName(t *testing.T) {
6969
{"", "", blonk, nil, "<nil>", "", true},
7070
// 05: use importFile to detect build.path and project_name, sketch is ignored.
7171
{"testdata/build_path_2/Blink.ino.hex", "", blonk, nil, "testdata/build_path_2", "Blink.ino", false},
72-
// 06: use importPath as build.path and Blonk as project name (forced by the sketch)
73-
{"", "testdata/build_path_2", blonk, nil, "testdata/build_path_2", "Blonk.ino", false},
72+
// 06: use importPath as build.path and Blink as project name, ignore the sketch Blonk
73+
{"", "testdata/build_path_2", blonk, nil, "testdata/build_path_2", "Blink.ino", false},
7474
// 07: error: used both importPath and importFile
7575
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", blonk, nil, "<nil>", "", true},
7676

@@ -86,8 +86,8 @@ func TestDetermineBuildPathAndSketchName(t *testing.T) {
8686
{"", "", blonk, fqbn, "testdata/Blonk/build/arduino.samd.mkr1000", "Blonk.ino", false},
8787
// 13: use importFile to detect build.path and project_name, sketch+fqbn is ignored.
8888
{"testdata/build_path_2/Blink.ino.hex", "", blonk, fqbn, "testdata/build_path_2", "Blink.ino", false},
89-
// 14: use importPath as build.path and Blonk as project name (forced by the sketch), fqbn ignored
90-
{"", "testdata/build_path_2", blonk, fqbn, "testdata/build_path_2", "Blonk.ino", false},
89+
// 14: use importPath as build.path and Blink as project name, ignore the sketch Blonk, ignore fqbn
90+
{"", "testdata/build_path_2", blonk, fqbn, "testdata/build_path_2", "Blink.ino", false},
9191
// 15: error: used both importPath and importFile
9292
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", blonk, fqbn, "<nil>", "", true},
9393
}

0 commit comments

Comments
 (0)