Skip to content
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

Fix sketch metadata loader #690

Merged
merged 4 commits into from
May 7, 2020
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
3 changes: 3 additions & 0 deletions arduino/sketches/sketches.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
45 changes: 45 additions & 0 deletions arduino/sketches/sketches_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
3 changes: 3 additions & 0 deletions arduino/sketches/testdata/Sketch1/Sketch1.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

void setup() {}
void loop() {}
6 changes: 1 addition & 5 deletions commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
65 changes: 36 additions & 29 deletions test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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):
Expand Down