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/arduino/sketches/sketches_test.go b/arduino/sketches/sketches_test.go new file mode 100644 index 00000000000..daae6d370e3 --- /dev/null +++ b/arduino/sketches/sketches_test.go @@ -0,0 +1,45 @@ +// 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 ( + "fmt" + "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") + 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") + fmt.Println(sk.FullPath.String(), "==", skFolder.String()) + require.True(t, sk.FullPath.EquivalentTo(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() {} 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() 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):