Skip to content

Commit f0b218a

Browse files
authored
Fix path in error messages on sketch loading (#1805)
* Fix path in error messages on sketch loading * Added integration tests * Updated unit-test * Updated integration test * Even better error messages
1 parent 63b53c0 commit f0b218a

File tree

7 files changed

+87
-33
lines changed

7 files changed

+87
-33
lines changed

Diff for: arduino/sketch/sketch.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ func New(path *paths.Path) (*Sketch, error) {
6464
}
6565

6666
path = path.Canonical()
67-
if !path.IsDir() {
67+
if exist, err := path.ExistCheck(); err != nil {
68+
return nil, fmt.Errorf("%s: %s", tr("sketch path is not valid"), err)
69+
} else if !exist {
70+
return nil, fmt.Errorf("%s: %s", tr("no such file or directory"), path)
71+
}
72+
if _, validIno := globals.MainFileValidExtensions[path.Ext()]; validIno && !path.IsDir() {
6873
path = path.Parent()
6974
}
7075

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

8694
sketch := &Sketch{
8795
Name: path.Base(),
@@ -269,7 +277,6 @@ func (s *Sketch) checkSketchCasing() error {
269277
return &InvalidSketchFolderNameError{
270278
SketchFolder: s.FullPath,
271279
SketchFile: sketchFile,
272-
Sketch: s,
273280
}
274281
}
275282

@@ -280,7 +287,6 @@ func (s *Sketch) checkSketchCasing() error {
280287
type InvalidSketchFolderNameError struct {
281288
SketchFolder *paths.Path
282289
SketchFile *paths.Path
283-
Sketch *Sketch
284290
}
285291

286292
func (e *InvalidSketchFolderNameError) Error() string {

Diff for: arduino/sketch/sketch_test.go

+35-13
Original file line numberDiff line numberDiff line change
@@ -100,30 +100,52 @@ func TestNewSketchWrongMain(t *testing.T) {
100100
require.Error(t, err)
101101
sketchFolderPath, _ = sketchFolderPath.Abs()
102102
expectedMainFile := sketchFolderPath.Join(sketchName)
103-
expectedError := fmt.Sprintf("no valid sketch found in %s: missing %s", sketchFolderPath, expectedMainFile)
103+
expectedError := fmt.Sprintf("main file missing from sketch: %s", expectedMainFile)
104104
require.Contains(t, err.Error(), expectedError)
105105

106106
sketchFolderPath = paths.New("testdata", sketchName)
107107
mainFilePath := sketchFolderPath.Join(fmt.Sprintf("%s.ino", sketchName))
108108
sketch, err = New(mainFilePath)
109109
require.Nil(t, sketch)
110110
require.Error(t, err)
111-
sketchFolderPath, _ = sketchFolderPath.Abs()
112-
expectedError = fmt.Sprintf("no valid sketch found in %s: missing %s", sketchFolderPath, expectedMainFile)
111+
expectedError = fmt.Sprintf("no such file or directory: %s", expectedMainFile)
113112
require.Contains(t, err.Error(), expectedError)
114113
}
115114

116115
func TestNewSketchCasingWrong(t *testing.T) {
117-
sketchPath := paths.New("testdata", "SketchWithWrongMain")
118-
sketch, err := New(sketchPath)
119-
assert.Nil(t, sketch)
120-
assert.Error(t, err)
121-
assert.IsType(t, &InvalidSketchFolderNameError{}, err)
122-
e := err.(*InvalidSketchFolderNameError)
123-
assert.NotNil(t, e.Sketch)
124-
sketchPath, _ = sketchPath.Abs()
125-
expectedError := fmt.Sprintf("no valid sketch found in %s: missing %s", sketchPath.String(), sketchPath.Join(sketchPath.Base()+".ino"))
126-
assert.EqualError(t, err, expectedError)
116+
{
117+
sketchPath := paths.New("testdata", "SketchWithWrongMain")
118+
sketch, err := New(sketchPath)
119+
assert.Nil(t, sketch)
120+
assert.Error(t, err)
121+
_, ok := err.(*InvalidSketchFolderNameError)
122+
assert.False(t, ok)
123+
sketchPath, _ = sketchPath.Abs()
124+
expectedError := fmt.Sprintf("main file missing from sketch: %s", sketchPath.Join(sketchPath.Base()+".ino"))
125+
assert.EqualError(t, err, expectedError)
126+
}
127+
{
128+
sketchPath := paths.New("testdata", "SketchWithWrongMain", "main.ino")
129+
sketch, err := New(sketchPath)
130+
assert.Nil(t, sketch)
131+
assert.Error(t, err)
132+
_, ok := err.(*InvalidSketchFolderNameError)
133+
assert.False(t, ok)
134+
sketchPath, _ = sketchPath.Parent().Abs()
135+
expectedError := fmt.Sprintf("main file missing from sketch: %s", sketchPath.Join(sketchPath.Base()+".ino"))
136+
assert.EqualError(t, err, expectedError)
137+
}
138+
{
139+
sketchPath := paths.New("testdata", "non-existent")
140+
sketch, skerr := New(sketchPath)
141+
require.Nil(t, sketch)
142+
require.Error(t, skerr)
143+
_, ok := skerr.(*InvalidSketchFolderNameError)
144+
assert.False(t, ok)
145+
sketchPath, _ = sketchPath.Abs()
146+
expectedError := fmt.Sprintf("no such file or directory: %s", sketchPath)
147+
require.EqualError(t, skerr, expectedError)
148+
}
127149
}
128150

129151
func TestNewSketchCasingCorrect(t *testing.T) {

Diff for: legacy/builder/container_setup.go

+1-11
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
package builder
1717

1818
import (
19-
"fmt"
20-
2119
sk "github.com/arduino/arduino-cli/arduino/sketch"
2220
"github.com/arduino/arduino-cli/legacy/builder/types"
2321
"github.com/pkg/errors"
@@ -61,17 +59,9 @@ func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context)
6159

6260
// load sketch
6361
sketch, err := sk.New(sketchLocation)
64-
if e, ok := err.(*sk.InvalidSketchFolderNameError); ctx.IgnoreSketchFolderNameErrors && ok {
65-
// ignore error
66-
// This is only done by the arduino-builder since the Arduino Java IDE
67-
// supports sketches with invalid names
68-
sketch = e.Sketch
69-
} else if err != nil {
62+
if err != nil {
7063
return errors.WithStack(err)
7164
}
72-
if sketch.MainFile == nil {
73-
return fmt.Errorf(tr("main file missing from sketch"))
74-
}
7565
sketch.BuildPath = ctx.BuildPath
7666
ctx.SketchLocation = sketch.MainFile
7767
ctx.Sketch = sketch

Diff for: test/test_compile_part_1.py

+36
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,42 @@ def test_compile_without_fqbn(run_command):
3737
assert result.failed
3838

3939

40+
def test_compile_error_message(run_command, working_dir):
41+
# Init the environment explicitly
42+
run_command(["core", "update-index"])
43+
44+
# Download latest AVR
45+
run_command(["core", "install", "arduino:avr"])
46+
47+
# Run a batch of bogus compile in a temp dir to check the error messages
48+
with tempfile.TemporaryDirectory() as tmp_dir:
49+
tmp = Path(tmp_dir)
50+
abcdef = tmp / "ABCDEF"
51+
res = run_command(["compile", "-b", "arduino:avr:uno", abcdef])
52+
assert res.failed
53+
assert "no such file or directory:" in res.stderr
54+
res = run_command(["compile", "-b", "arduino:avr:uno", abcdef / "ABCDEF.ino"])
55+
assert res.failed
56+
assert "no such file or directory:" in res.stderr
57+
res = run_command(["compile", "-b", "arduino:avr:uno", abcdef / "QWERTY"])
58+
assert res.failed
59+
assert "no such file or directory:" in res.stderr
60+
61+
abcdef.mkdir()
62+
res = run_command(["compile", "-b", "arduino:avr:uno", abcdef])
63+
assert res.failed
64+
assert "main file missing from sketch:" in res.stderr
65+
res = run_command(["compile", "-b", "arduino:avr:uno", abcdef / "ABCDEF.ino"])
66+
assert res.failed
67+
assert "no such file or directory:" in res.stderr
68+
69+
qwerty_ino = abcdef / "QWERTY.ino"
70+
qwerty_ino.touch()
71+
res = run_command(["compile", "-b", "arduino:avr:uno", qwerty_ino])
72+
assert res.failed
73+
assert "main file missing from sketch:" in res.stderr
74+
75+
4076
def test_compile_with_simple_sketch(run_command, data_dir, working_dir):
4177
# Init the environment explicitly
4278
run_command(["core", "update-index"])

Diff for: test/test_compile_part_3.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,15 @@ def test_compile_sketch_case_mismatch_fails(run_command, data_dir):
124124
# * Compiling with sketch path
125125
res = run_command(["compile", "--clean", "-b", fqbn, sketch_path])
126126
assert res.failed
127-
assert "Error opening sketch: no valid sketch found" in res.stderr
127+
assert "Error opening sketch:" in res.stderr
128128
# * Compiling with sketch main file
129129
res = run_command(["compile", "--clean", "-b", fqbn, sketch_main_file])
130130
assert res.failed
131-
assert "Error opening sketch: no valid sketch found" in res.stderr
131+
assert "Error opening sketch:" in res.stderr
132132
# * Compiling in sketch path
133133
res = run_command(["compile", "--clean", "-b", fqbn], custom_working_dir=sketch_path)
134134
assert res.failed
135-
assert "Error opening sketch: no valid sketch found" in res.stderr
135+
assert "Error opening sketch:" in res.stderr
136136

137137

138138
def test_compile_with_only_compilation_database_flag(run_command, data_dir):

Diff for: test/test_sketch.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -896,4 +896,4 @@ def test_sketch_archive_case_mismatch_fails(run_command, data_dir):
896896

897897
res = run_command(["sketch", "archive", sketch_path])
898898
assert res.failed
899-
assert "Error archiving: Can't open sketch: no valid sketch found" in res.stderr
899+
assert "Error archiving: Can't open sketch:" in res.stderr

Diff for: test/test_upload.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ def test_compile_and_upload_combo_sketch_with_mismatched_casing(run_command, dat
364364
# Try to compile
365365
res = run_command(["compile", "--clean", "-b", board.fqbn, "-u", "-p", board.address, sketch_path])
366366
assert res.failed
367-
assert "Error opening sketch: no valid sketch found in" in res.stderr
367+
assert "Error opening sketch:" in res.stderr
368368

369369

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

392392

393393
def test_upload_to_port_with_board_autodetect(run_command, data_dir, detected_boards):

0 commit comments

Comments
 (0)