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 all commits
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
12 changes: 9 additions & 3 deletions arduino/sketch/sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ func New(path *paths.Path) (*Sketch, error) {
}

path = path.Canonical()
if !path.IsDir() {
if exist, err := path.ExistCheck(); err != nil {
return nil, fmt.Errorf("%s: %s", tr("sketch path is not valid"), err)
} else if !exist {
return nil, fmt.Errorf("%s: %s", tr("no such file or directory"), path)
}
if _, validIno := globals.MainFileValidExtensions[path.Ext()]; validIno && !path.IsDir() {
path = path.Parent()
}

Expand All @@ -82,6 +87,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 +277,6 @@ func (s *Sketch) checkSketchCasing() error {
return &InvalidSketchFolderNameError{
SketchFolder: s.FullPath,
SketchFile: sketchFile,
Sketch: s,
}
}

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

func (e *InvalidSketchFolderNameError) Error() string {
Expand Down
48 changes: 35 additions & 13 deletions arduino/sketch/sketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,30 +100,52 @@ func TestNewSketchWrongMain(t *testing.T) {
require.Error(t, err)
sketchFolderPath, _ = sketchFolderPath.Abs()
expectedMainFile := sketchFolderPath.Join(sketchName)
expectedError := fmt.Sprintf("no valid sketch found in %s: missing %s", sketchFolderPath, expectedMainFile)
expectedError := fmt.Sprintf("main file missing from sketch: %s", expectedMainFile)
require.Contains(t, err.Error(), expectedError)

sketchFolderPath = paths.New("testdata", sketchName)
mainFilePath := sketchFolderPath.Join(fmt.Sprintf("%s.ino", sketchName))
sketch, err = New(mainFilePath)
require.Nil(t, sketch)
require.Error(t, err)
sketchFolderPath, _ = sketchFolderPath.Abs()
expectedError = fmt.Sprintf("no valid sketch found in %s: missing %s", sketchFolderPath, expectedMainFile)
expectedError = fmt.Sprintf("no such file or directory: %s", expectedMainFile)
require.Contains(t, err.Error(), expectedError)
}

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("no such file or directory: %s", sketchPath)
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
36 changes: 36 additions & 0 deletions test/test_compile_part_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,42 @@ def test_compile_without_fqbn(run_command):
assert result.failed


def test_compile_error_message(run_command, working_dir):
# Init the environment explicitly
run_command(["core", "update-index"])

# Download latest AVR
run_command(["core", "install", "arduino:avr"])

# Run a batch of bogus compile in a temp dir to check the error messages
with tempfile.TemporaryDirectory() as tmp_dir:
tmp = Path(tmp_dir)
abcdef = tmp / "ABCDEF"
res = run_command(["compile", "-b", "arduino:avr:uno", abcdef])
assert res.failed
assert "no such file or directory:" in res.stderr
res = run_command(["compile", "-b", "arduino:avr:uno", abcdef / "ABCDEF.ino"])
assert res.failed
assert "no such file or directory:" in res.stderr
res = run_command(["compile", "-b", "arduino:avr:uno", abcdef / "QWERTY"])
assert res.failed
assert "no such file or directory:" in res.stderr

abcdef.mkdir()
res = run_command(["compile", "-b", "arduino:avr:uno", abcdef])
assert res.failed
assert "main file missing from sketch:" in res.stderr
res = run_command(["compile", "-b", "arduino:avr:uno", abcdef / "ABCDEF.ino"])
assert res.failed
assert "no such file or directory:" in res.stderr

qwerty_ino = abcdef / "QWERTY.ino"
qwerty_ino.touch()
res = run_command(["compile", "-b", "arduino:avr:uno", qwerty_ino])
assert res.failed
assert "main file missing from sketch:" in res.stderr


def test_compile_with_simple_sketch(run_command, data_dir, working_dir):
# Init the environment explicitly
run_command(["core", "update-index"])
Expand Down
6 changes: 3 additions & 3 deletions test/test_compile_part_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ def test_compile_sketch_case_mismatch_fails(run_command, data_dir):
# * Compiling with sketch path
res = run_command(["compile", "--clean", "-b", fqbn, sketch_path])
assert res.failed
assert "Error opening sketch: no valid sketch found" in res.stderr
assert "Error opening sketch:" in res.stderr
# * Compiling with sketch main file
res = run_command(["compile", "--clean", "-b", fqbn, sketch_main_file])
assert res.failed
assert "Error opening sketch: no valid sketch found" in res.stderr
assert "Error opening sketch:" in res.stderr
# * Compiling in sketch path
res = run_command(["compile", "--clean", "-b", fqbn], custom_working_dir=sketch_path)
assert res.failed
assert "Error opening sketch: no valid sketch found" in res.stderr
assert "Error opening sketch:" in res.stderr


def test_compile_with_only_compilation_database_flag(run_command, data_dir):
Expand Down
2 changes: 1 addition & 1 deletion test/test_sketch.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,4 +896,4 @@ def test_sketch_archive_case_mismatch_fails(run_command, data_dir):

res = run_command(["sketch", "archive", sketch_path])
assert res.failed
assert "Error archiving: Can't open sketch: no valid sketch found" in res.stderr
assert "Error archiving: Can't open sketch:" in res.stderr
4 changes: 2 additions & 2 deletions test/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def test_compile_and_upload_combo_sketch_with_mismatched_casing(run_command, dat
# Try to compile
res = run_command(["compile", "--clean", "-b", board.fqbn, "-u", "-p", board.address, sketch_path])
assert res.failed
assert "Error opening sketch: no valid sketch found in" in res.stderr
assert "Error opening sketch:" in res.stderr


def test_upload_sketch_with_mismatched_casing(run_command, data_dir, detected_boards, wait_for_board):
Expand All @@ -387,7 +387,7 @@ def test_upload_sketch_with_mismatched_casing(run_command, data_dir, detected_bo
# searching for binaries since the sketch is not valid
res = run_command(["upload", "-b", board.fqbn, "-p", board.address, sketch_path])
assert res.failed
assert "Error during Upload: no valid sketch found in" in res.stderr
assert "Error during Upload:" in res.stderr


def test_upload_to_port_with_board_autodetect(run_command, data_dir, detected_boards):
Expand Down