Skip to content

Commit 5a1d6e9

Browse files
author
Roberto Sora
committed
check log traces for exact sequences
1 parent 50de65a commit 5a1d6e9

File tree

3 files changed

+40
-40
lines changed

3 files changed

+40
-40
lines changed

commands/compile/compile.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
4747
return nil, errors.New("invalid instance")
4848
}
4949

50-
logrus.Info("Executing `arduino compile`")
50+
logrus.Tracef("Compile %s for %s started", req.GetSketchPath(), req.GetFqbn())
5151
if req.GetSketchPath() == "" {
5252
return nil, fmt.Errorf("missing sketchPath")
5353
}
@@ -202,20 +202,20 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
202202
// Copy .hex file to sketch directory
203203
srcHex := paths.New(outputPath)
204204
dstHex := exportPath.Join(exportFile + ext)
205-
logrus.WithField("from", srcHex).WithField("to", dstHex).Print("copying sketch build output")
205+
logrus.WithField("from", srcHex).WithField("to", dstHex).Debug("copying sketch build output")
206206
if err = srcHex.CopyTo(dstHex); err != nil {
207207
return nil, fmt.Errorf("copying output file: %s", err)
208208
}
209209

210210
// Copy .elf file to sketch directory
211211
srcElf := paths.New(outputPath[:len(outputPath)-3] + "elf")
212212
dstElf := exportPath.Join(exportFile + ".elf")
213-
logrus.WithField("from", srcElf).WithField("to", dstElf).Print("copying sketch build output")
213+
logrus.WithField("from", srcElf).WithField("to", dstElf).Debug("copying sketch build output")
214214
if err = srcElf.CopyTo(dstElf); err != nil {
215215
return nil, fmt.Errorf("copying elf file: %s", err)
216216
}
217217

218-
logrus.Infof("Compile %s for %s successful", sketch.Name, fqbnIn)
218+
logrus.Tracef("Compile %s for %s successful", sketch.Name, fqbnIn)
219219

220220
return &rpc.CompileResp{}, nil
221221
}

commands/upload/upload.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import (
4040

4141
// Upload FIXMEDOC
4242
func Upload(ctx context.Context, req *rpc.UploadReq, outStream io.Writer, errStream io.Writer) (*rpc.UploadResp, error) {
43-
logrus.Info("Executing `arduino upload`")
43+
logrus.Tracef("Upload %s on %s started", req.GetSketchPath(), req.GetFqbn())
4444

4545
// TODO: make a generic function to extract sketch from request
4646
// and remove duplication in commands/compile.go
@@ -249,7 +249,7 @@ func Upload(ctx context.Context, req *rpc.UploadReq, outStream io.Writer, errStr
249249
return nil, fmt.Errorf("uploading error: %s", err)
250250
}
251251

252-
logrus.Infof("Upload %s on %s successful", sketch.Name, fqbnIn)
252+
logrus.Tracef("Upload %s on %s successful", sketch.Name, fqbnIn)
253253

254254
return &rpc.UploadResp{}, nil
255255
}

test/test_compile.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
# This file is part of arduino-cli.
2-
2+
#
33
# Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
4-
4+
#
55
# This software is released under the GNU General Public License version 3,
66
# which covers the main part of arduino-cli.
77
# The terms of this license can be found at:
88
# https://www.gnu.org/licenses/gpl-3.0.en.html
9-
9+
#
1010
# You can be released from the requirements of the above licenses by purchasing
1111
# a commercial license. Buying such a license is mandatory if you want to modify or
1212
# otherwise use the software for commercial activities involving the Arduino
1313
# software without disclosing the source code of your own applications. To purchase
1414
# a commercial license, send an email to license@arduino.cc.
15-
import pytest
1615
import json
1716
import os
17+
import pytest
1818

1919
from .common import running_on_ci
2020

@@ -62,11 +62,11 @@ def test_compile_with_simple_sketch(run_command, data_dir):
6262
# let's test from the logs if the hex file produced by successful compile is moved to our sketch folder
6363
log_json = open(log_file_path, 'r')
6464
json_log_lines = log_json.readlines()
65-
assert is_message_in_json_log_lines("Executing `arduino compile`", json_log_lines)
66-
assert is_message_in_json_log_lines(
67-
"Compile {sketch} for {fqbn} successful".format(sketch=sketch_name,
68-
fqbn=fqbn),
69-
json_log_lines)
65+
expected_trace_sequence = [
66+
"Compile {sketch} for {fqbn} started".format(sketch=sketch_path, fqbn=fqbn),
67+
"Compile {sketch} for {fqbn} successful".format(sketch=sketch_name, fqbn=fqbn)
68+
]
69+
assert is_message_sequence_in_json_log_traces(expected_trace_sequence, json_log_lines)
7070

7171

7272
@pytest.mark.skipif(running_on_ci(), reason="VMs have no serial ports")
@@ -77,7 +77,7 @@ def test_compile_and_compile_combo(run_command, data_dir):
7777

7878
# Install required core(s)
7979
result = run_command("core install arduino:avr")
80-
# result = run_command("core install arduino:samd")
80+
result = run_command("core install arduino:samd")
8181
assert result.ok
8282

8383
# Create a test sketch
@@ -126,31 +126,31 @@ def test_compile_and_compile_combo(run_command, data_dir):
126126
for board in detected_boards:
127127
log_file_name = "{fqbn}-compile.log".format(fqbn=board.get('fqbn'))
128128
log_file_path = os.path.join(data_dir, log_file_name)
129-
result = run_command(
130-
"compile -b {fqbn} --upload -p {address} {sketch_path} --log-format json --log-file {log_file} --log-level trace".format(
131-
fqbn=board.get('fqbn'),
132-
address=board.get('address'),
133-
sketch_path=sketch_path,
134-
log_file=log_file_path
135-
)
136-
)
129+
command_log_flags = "--log-format json --log-file {} --log-level trace".format(log_file_path)
130+
result = run_command("compile -b {fqbn} --upload -p {address} {sketch_path} {log_flags}".format(
131+
fqbn=board.get('fqbn'),
132+
address=board.get('address'),
133+
sketch_path=sketch_path,
134+
log_flags=command_log_flags
135+
))
137136
assert result.ok
138137
# check from the logs if the bin file were uploaded on the current board
139138
log_json = open(log_file_path, 'r')
140139
json_log_lines = log_json.readlines()
141-
assert is_message_in_json_log_lines("Executing `arduino compile`", json_log_lines)
142-
assert is_message_in_json_log_lines(
143-
"Compile {sketch} for {fqbn} successful".format(sketch=sketch_name,
144-
fqbn=board.get(
145-
'fqbn')),
146-
json_log_lines)
147-
assert is_message_in_json_log_lines("Executing `arduino upload`", json_log_lines)
148-
assert is_message_in_json_log_lines(
149-
"Upload {sketch} on {fqbn} successful".format(sketch=sketch_name,
150-
fqbn=board.get(
151-
'fqbn')),
152-
json_log_lines)
153-
154-
155-
def is_message_in_json_log_lines(message, log_json_lines):
156-
return len([index for index, entry in enumerate(log_json_lines) if json.loads(entry).get("msg") == message]) == 1
140+
expected_trace_sequence = [
141+
"Compile {sketch} for {fqbn} started".format(sketch=sketch_path, fqbn=board.get('fqbn')),
142+
"Compile {sketch} for {fqbn} successful".format(sketch=sketch_name, fqbn=board.get('fqbn')),
143+
"Upload {sketch} on {fqbn} started".format(sketch=sketch_path, fqbn=board.get('fqbn')),
144+
"Upload {sketch} on {fqbn} successful".format(sketch=sketch_name, fqbn=board.get('fqbn'))
145+
]
146+
assert is_message_sequence_in_json_log_traces(expected_trace_sequence, json_log_lines)
147+
148+
149+
def is_message_sequence_in_json_log_traces(message_sequence, log_json_lines):
150+
trace_entries = []
151+
for entry in log_json_lines:
152+
entry = json.loads(entry)
153+
if entry.get("level") == "trace":
154+
if entry.get("msg") in message_sequence:
155+
trace_entries.append(entry.get("msg"))
156+
return message_sequence == trace_entries

0 commit comments

Comments
 (0)