Skip to content

Commit 6718cf4

Browse files
authored
Sketch path can now be specified by folder (path/to/sketch/) or by file (path/to/sketch/sketch.ino) (#690)
* Added test for different cases of sketch path case 1: /path/to/sketch case 2: /path/to/sketch/sketch.ino in the case 2 a compile+upload combo gives the following error: Error during Upload: cannot open sketch: stat /tmp/pytest-of-megabug/pytest-35/ArduinoTest1/CompileAndUploadIntegrationTest/CompileAndUploadIntegrationTest.ino/CompileAndUploadIntegrationTest.ino.arduino.samd.mkr1000.bin: not a directory * Fix sketch loading with different kind of input paths * Added unit-test for sketches module * Small test fix for Windows Paths are converted to \ when extracting Parent(). === RUN TestSketchLoadingFromFolderOrMainFile testdata/Sketch1 == testdata/Sketch1 testdata\Sketch1 == testdata/Sketch1
1 parent b62c339 commit 6718cf4

File tree

5 files changed

+88
-34
lines changed

5 files changed

+88
-34
lines changed

Diff for: arduino/sketches/sketches.go

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ type BoardMetadata struct {
4343

4444
// NewSketchFromPath loads a sketch from the specified path
4545
func NewSketchFromPath(path *paths.Path) (*Sketch, error) {
46+
if !path.IsDir() {
47+
path = path.Parent()
48+
}
4649
sketch := &Sketch{
4750
FullPath: path,
4851
Name: path.Base(),

Diff for: arduino/sketches/sketches_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package sketches
17+
18+
import (
19+
"fmt"
20+
"testing"
21+
22+
"github.com/arduino/go-paths-helper"
23+
"github.com/stretchr/testify/require"
24+
)
25+
26+
func TestSketchLoadingFromFolderOrMainFile(t *testing.T) {
27+
skFolder := paths.New("testdata/Sketch1")
28+
skMainIno := skFolder.Join("Sketch1.ino")
29+
30+
{
31+
sk, err := NewSketchFromPath(skFolder)
32+
require.NoError(t, err)
33+
require.Equal(t, sk.Name, "Sketch1")
34+
fmt.Println(sk.FullPath.String(), "==", skFolder.String())
35+
require.True(t, sk.FullPath.EquivalentTo(skFolder))
36+
}
37+
38+
{
39+
sk, err := NewSketchFromPath(skMainIno)
40+
require.NoError(t, err)
41+
require.Equal(t, sk.Name, "Sketch1")
42+
fmt.Println(sk.FullPath.String(), "==", skFolder.String())
43+
require.True(t, sk.FullPath.EquivalentTo(skFolder))
44+
}
45+
}

Diff for: arduino/sketches/testdata/Sketch1/Sketch1.ino

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
void setup() {}
3+
void loop() {}

Diff for: commands/compile/compile.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
211211
var exportPath *paths.Path
212212
var exportFile string
213213
if req.GetExportFile() == "" {
214-
if sketch.FullPath.IsDir() {
215-
exportPath = sketch.FullPath
216-
} else {
217-
exportPath = sketch.FullPath.Parent()
218-
}
214+
exportPath = sketch.FullPath
219215
exportFile = sketch.Name + "." + fqbnSuffix // "sketch.arduino.avr.uno"
220216
} else {
221217
exportPath = paths.New(req.GetExportFile()).Parent()

Diff for: test/test_compile.py

+36-29
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def test_compile_with_sketch_with_symlink_selfloop(run_command, data_dir):
180180

181181

182182
@pytest.mark.skipif(running_on_ci(), reason="VMs have no serial ports")
183-
def test_compile_and_compile_combo(run_command, data_dir, detected_boards):
183+
def test_compile_and_upload_combo(run_command, data_dir, detected_boards):
184184
# Init the environment explicitly
185185
result = run_command("core update-index")
186186
assert result.ok
@@ -193,6 +193,7 @@ def test_compile_and_compile_combo(run_command, data_dir, detected_boards):
193193
# Create a test sketch
194194
sketch_name = "CompileAndUploadIntegrationTest"
195195
sketch_path = os.path.join(data_dir, sketch_name)
196+
sketch_main_file = os.path.join(sketch_path, sketch_name+".ino")
196197
result = run_command("sketch new {}".format(sketch_path))
197198
assert result.ok
198199
assert "Sketch created in: {}".format(sketch_path) in result.stdout
@@ -204,35 +205,41 @@ def test_compile_and_compile_combo(run_command, data_dir, detected_boards):
204205
command_log_flags = "--log-format json --log-file {} --log-level trace".format(
205206
log_file_path
206207
)
207-
result = run_command(
208-
"compile -b {fqbn} --upload -p {address} {sketch_path} {log_flags}".format(
209-
fqbn=board.fqbn,
210-
address=board.address,
211-
sketch_path=sketch_path,
212-
log_flags=command_log_flags,
208+
209+
def run_test(s):
210+
result = run_command(
211+
"compile -b {fqbn} --upload -p {address} {sketch_path} {log_flags}".format(
212+
fqbn=board.fqbn,
213+
address=board.address,
214+
sketch_path=s,
215+
log_flags=command_log_flags,
216+
)
213217
)
214-
)
215-
assert result.ok
216-
# check from the logs if the bin file were uploaded on the current board
217-
log_json = open(log_file_path, "r")
218-
json_log_lines = log_json.readlines()
219-
expected_trace_sequence = [
220-
"Compile {sketch} for {fqbn} started".format(
221-
sketch=sketch_path, fqbn=board.fqbn
222-
),
223-
"Compile {sketch} for {fqbn} successful".format(
224-
sketch=sketch_name, fqbn=board.fqbn
225-
),
226-
"Upload {sketch} on {fqbn} started".format(
227-
sketch=sketch_path, fqbn=board.fqbn
228-
),
229-
"Upload {sketch} on {fqbn} successful".format(
230-
sketch=sketch_name, fqbn=board.fqbn
231-
),
232-
]
233-
assert is_message_sequence_in_json_log_traces(
234-
expected_trace_sequence, json_log_lines
235-
)
218+
assert result.ok
219+
220+
# check from the logs if the bin file were uploaded on the current board
221+
log_json = open(log_file_path, "r")
222+
json_log_lines = log_json.readlines()
223+
expected_trace_sequence = [
224+
"Compile {sketch} for {fqbn} started".format(
225+
sketch=sketch_path, fqbn=board.fqbn
226+
),
227+
"Compile {sketch} for {fqbn} successful".format(
228+
sketch=sketch_name, fqbn=board.fqbn
229+
),
230+
"Upload {sketch} on {fqbn} started".format(
231+
sketch=sketch_path, fqbn=board.fqbn
232+
),
233+
"Upload {sketch} on {fqbn} successful".format(
234+
sketch=sketch_name, fqbn=board.fqbn
235+
),
236+
]
237+
assert is_message_sequence_in_json_log_traces(
238+
expected_trace_sequence, json_log_lines
239+
)
240+
241+
run_test(sketch_path)
242+
run_test(sketch_main_file)
236243

237244

238245
def is_message_sequence_in_json_log_traces(message_sequence, log_json_lines):

0 commit comments

Comments
 (0)