Skip to content

Fix path in error messages on sketch loading #1805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix path in error messages on sketch loading
  • Loading branch information
cmaglie committed Jul 14, 2022
commit 79dd87850e1d00b930310e323388b5af43115299
7 changes: 4 additions & 3 deletions arduino/sketch/sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func New(path *paths.Path) (*Sketch, error) {
}

path = path.Canonical()
if !path.IsDir() {
if _, validIno := globals.MainFileValidExtensions[path.Ext()]; validIno && !path.IsDir() {
path = path.Parent()
}

Expand All @@ -82,6 +82,9 @@ func New(path *paths.Path) (*Sketch, error) {
}
}
}
if mainFile == nil {
return nil, fmt.Errorf(tr("main file missing from sketch: %s", path.Join(path.Base()+globals.MainFileValidExtension)))
}

sketch := &Sketch{
Name: path.Base(),
Expand Down Expand Up @@ -269,7 +272,6 @@ func (s *Sketch) checkSketchCasing() error {
return &InvalidSketchFolderNameError{
SketchFolder: s.FullPath,
SketchFile: sketchFile,
Sketch: s,
}
}

Expand All @@ -280,7 +282,6 @@ func (s *Sketch) checkSketchCasing() error {
type InvalidSketchFolderNameError struct {
SketchFolder *paths.Path
SketchFile *paths.Path
Sketch *Sketch
}

func (e *InvalidSketchFolderNameError) Error() string {
Expand Down
43 changes: 33 additions & 10 deletions arduino/sketch/sketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,39 @@ func TestNewSketchWrongMain(t *testing.T) {
}

func TestNewSketchCasingWrong(t *testing.T) {
sketchPath := paths.New("testdata", "SketchWithWrongMain")
sketch, err := New(sketchPath)
assert.Nil(t, sketch)
assert.Error(t, err)
assert.IsType(t, &InvalidSketchFolderNameError{}, err)
e := err.(*InvalidSketchFolderNameError)
assert.NotNil(t, e.Sketch)
sketchPath, _ = sketchPath.Abs()
expectedError := fmt.Sprintf("no valid sketch found in %s: missing %s", sketchPath.String(), sketchPath.Join(sketchPath.Base()+".ino"))
assert.EqualError(t, err, expectedError)
{
sketchPath := paths.New("testdata", "SketchWithWrongMain")
sketch, err := New(sketchPath)
assert.Nil(t, sketch)
assert.Error(t, err)
_, ok := err.(*InvalidSketchFolderNameError)
assert.False(t, ok)
sketchPath, _ = sketchPath.Abs()
expectedError := fmt.Sprintf("main file missing from sketch: %s", sketchPath.Join(sketchPath.Base()+".ino"))
assert.EqualError(t, err, expectedError)
}
{
sketchPath := paths.New("testdata", "SketchWithWrongMain", "main.ino")
sketch, err := New(sketchPath)
assert.Nil(t, sketch)
assert.Error(t, err)
_, ok := err.(*InvalidSketchFolderNameError)
assert.False(t, ok)
sketchPath, _ = sketchPath.Parent().Abs()
expectedError := fmt.Sprintf("main file missing from sketch: %s", sketchPath.Join(sketchPath.Base()+".ino"))
assert.EqualError(t, err, expectedError)
}
{
sketchPath := paths.New("testdata", "non-existent")
sketch, skerr := New(sketchPath)
require.Nil(t, sketch)
require.Error(t, skerr)
_, ok := skerr.(*InvalidSketchFolderNameError)
assert.False(t, ok)
sketchPath, _ = sketchPath.Abs()
expectedError := fmt.Sprintf("main file missing from sketch: %s", sketchPath.Join(sketchPath.Base()+".ino"))
require.EqualError(t, skerr, expectedError)
}
}

func TestNewSketchCasingCorrect(t *testing.T) {
Expand Down
12 changes: 1 addition & 11 deletions legacy/builder/container_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
package builder

import (
"fmt"

sk "github.com/arduino/arduino-cli/arduino/sketch"
"github.com/arduino/arduino-cli/legacy/builder/types"
"github.com/pkg/errors"
Expand Down Expand Up @@ -61,17 +59,9 @@ func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context)

// load sketch
sketch, err := sk.New(sketchLocation)
if e, ok := err.(*sk.InvalidSketchFolderNameError); ctx.IgnoreSketchFolderNameErrors && ok {
// ignore error
// This is only done by the arduino-builder since the Arduino Java IDE
// supports sketches with invalid names
sketch = e.Sketch
} else if err != nil {
if err != nil {
return errors.WithStack(err)
}
if sketch.MainFile == nil {
return fmt.Errorf(tr("main file missing from sketch"))
}
sketch.BuildPath = ctx.BuildPath
ctx.SketchLocation = sketch.MainFile
ctx.Sketch = sketch
Expand Down