From 8b76e2e8898db999e3749a0c04264b96e7cbb761 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 7 May 2020 17:29:05 +0200 Subject: [PATCH 1/4] 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 --- test/test_compile.py | 65 ++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/test/test_compile.py b/test/test_compile.py index 9a05a191124..6b3cbe3e784 100644 --- a/test/test_compile.py +++ b/test/test_compile.py @@ -180,7 +180,7 @@ def test_compile_with_sketch_with_symlink_selfloop(run_command, data_dir): @pytest.mark.skipif(running_on_ci(), reason="VMs have no serial ports") -def test_compile_and_compile_combo(run_command, data_dir, detected_boards): +def test_compile_and_upload_combo(run_command, data_dir, detected_boards): # Init the environment explicitly result = run_command("core update-index") assert result.ok @@ -193,6 +193,7 @@ def test_compile_and_compile_combo(run_command, data_dir, detected_boards): # Create a test sketch sketch_name = "CompileAndUploadIntegrationTest" sketch_path = os.path.join(data_dir, sketch_name) + sketch_main_file = os.path.join(sketch_path, sketch_name+".ino") result = run_command("sketch new {}".format(sketch_path)) assert result.ok 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): command_log_flags = "--log-format json --log-file {} --log-level trace".format( log_file_path ) - result = run_command( - "compile -b {fqbn} --upload -p {address} {sketch_path} {log_flags}".format( - fqbn=board.fqbn, - address=board.address, - sketch_path=sketch_path, - log_flags=command_log_flags, + + def run_test(s): + result = run_command( + "compile -b {fqbn} --upload -p {address} {sketch_path} {log_flags}".format( + fqbn=board.fqbn, + address=board.address, + sketch_path=s, + log_flags=command_log_flags, + ) ) - ) - assert result.ok - # check from the logs if the bin file were uploaded on the current board - log_json = open(log_file_path, "r") - json_log_lines = log_json.readlines() - expected_trace_sequence = [ - "Compile {sketch} for {fqbn} started".format( - sketch=sketch_path, fqbn=board.fqbn - ), - "Compile {sketch} for {fqbn} successful".format( - sketch=sketch_name, fqbn=board.fqbn - ), - "Upload {sketch} on {fqbn} started".format( - sketch=sketch_path, fqbn=board.fqbn - ), - "Upload {sketch} on {fqbn} successful".format( - sketch=sketch_name, fqbn=board.fqbn - ), - ] - assert is_message_sequence_in_json_log_traces( - expected_trace_sequence, json_log_lines - ) + assert result.ok + + # check from the logs if the bin file were uploaded on the current board + log_json = open(log_file_path, "r") + json_log_lines = log_json.readlines() + expected_trace_sequence = [ + "Compile {sketch} for {fqbn} started".format( + sketch=sketch_path, fqbn=board.fqbn + ), + "Compile {sketch} for {fqbn} successful".format( + sketch=sketch_name, fqbn=board.fqbn + ), + "Upload {sketch} on {fqbn} started".format( + sketch=sketch_path, fqbn=board.fqbn + ), + "Upload {sketch} on {fqbn} successful".format( + sketch=sketch_name, fqbn=board.fqbn + ), + ] + assert is_message_sequence_in_json_log_traces( + expected_trace_sequence, json_log_lines + ) + + run_test(sketch_path) + run_test(sketch_main_file) def is_message_sequence_in_json_log_traces(message_sequence, log_json_lines): From 90bf1e3d189ed4e44b53e8fbcd6369d0e8f2f921 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 7 May 2020 17:35:14 +0200 Subject: [PATCH 2/4] Fix sketch loading with different kind of input paths --- arduino/sketches/sketches.go | 3 +++ commands/compile/compile.go | 6 +----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/arduino/sketches/sketches.go b/arduino/sketches/sketches.go index 13c0aa181e6..10627920d80 100644 --- a/arduino/sketches/sketches.go +++ b/arduino/sketches/sketches.go @@ -43,6 +43,9 @@ type BoardMetadata struct { // NewSketchFromPath loads a sketch from the specified path func NewSketchFromPath(path *paths.Path) (*Sketch, error) { + if !path.IsDir() { + path = path.Parent() + } sketch := &Sketch{ FullPath: path, Name: path.Base(), diff --git a/commands/compile/compile.go b/commands/compile/compile.go index 0a368a0b3b5..3f67c6da063 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -211,11 +211,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W var exportPath *paths.Path var exportFile string if req.GetExportFile() == "" { - if sketch.FullPath.IsDir() { - exportPath = sketch.FullPath - } else { - exportPath = sketch.FullPath.Parent() - } + exportPath = sketch.FullPath exportFile = sketch.Name + "." + fqbnSuffix // "sketch.arduino.avr.uno" } else { exportPath = paths.New(req.GetExportFile()).Parent() From 9df2c66a587429f157db9884c6d5674b60b64bc6 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 7 May 2020 17:37:50 +0200 Subject: [PATCH 3/4] Added unit-test for sketches module --- arduino/sketches/sketches_test.go | 42 +++++++++++++++++++ arduino/sketches/testdata/Sketch1/Sketch1.ino | 3 ++ 2 files changed, 45 insertions(+) create mode 100644 arduino/sketches/sketches_test.go create mode 100644 arduino/sketches/testdata/Sketch1/Sketch1.ino diff --git a/arduino/sketches/sketches_test.go b/arduino/sketches/sketches_test.go new file mode 100644 index 00000000000..8cca01b1f82 --- /dev/null +++ b/arduino/sketches/sketches_test.go @@ -0,0 +1,42 @@ +// This file is part of arduino-cli. +// +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-cli. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + +package sketches + +import ( + "testing" + + "github.com/arduino/go-paths-helper" + "github.com/stretchr/testify/require" +) + +func TestSketchLoadingFromFolderOrMainFile(t *testing.T) { + skFolder := paths.New("testdata/Sketch1") + skMainIno := skFolder.Join("Sketch1.ino") + + { + sk, err := NewSketchFromPath(skFolder) + require.NoError(t, err) + require.Equal(t, sk.Name, "Sketch1") + require.True(t, sk.FullPath.EqualsTo(skFolder)) + } + + { + sk, err := NewSketchFromPath(skMainIno) + require.NoError(t, err) + require.Equal(t, sk.Name, "Sketch1") + require.True(t, sk.FullPath.EqualsTo(skFolder)) + } +} diff --git a/arduino/sketches/testdata/Sketch1/Sketch1.ino b/arduino/sketches/testdata/Sketch1/Sketch1.ino new file mode 100644 index 00000000000..5054c040393 --- /dev/null +++ b/arduino/sketches/testdata/Sketch1/Sketch1.ino @@ -0,0 +1,3 @@ + +void setup() {} +void loop() {} From 319d045494748a9932472c13345851a9146042de Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 7 May 2020 18:05:33 +0200 Subject: [PATCH 4/4] Small test fix for Windows Paths are converted to \ when extracting Parent(). === RUN TestSketchLoadingFromFolderOrMainFile testdata/Sketch1 == testdata/Sketch1 testdata\Sketch1 == testdata/Sketch1 --- arduino/sketches/sketches_test.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/arduino/sketches/sketches_test.go b/arduino/sketches/sketches_test.go index 8cca01b1f82..daae6d370e3 100644 --- a/arduino/sketches/sketches_test.go +++ b/arduino/sketches/sketches_test.go @@ -16,6 +16,7 @@ package sketches import ( + "fmt" "testing" "github.com/arduino/go-paths-helper" @@ -30,13 +31,15 @@ func TestSketchLoadingFromFolderOrMainFile(t *testing.T) { sk, err := NewSketchFromPath(skFolder) require.NoError(t, err) require.Equal(t, sk.Name, "Sketch1") - require.True(t, sk.FullPath.EqualsTo(skFolder)) + fmt.Println(sk.FullPath.String(), "==", skFolder.String()) + require.True(t, sk.FullPath.EquivalentTo(skFolder)) } { sk, err := NewSketchFromPath(skMainIno) require.NoError(t, err) require.Equal(t, sk.Name, "Sketch1") - require.True(t, sk.FullPath.EqualsTo(skFolder)) + fmt.Println(sk.FullPath.String(), "==", skFolder.String()) + require.True(t, sk.FullPath.EquivalentTo(skFolder)) } }