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
1615import json
1716import os
17+ import pytest
1818
1919from .common import running_on_ci
2020
@@ -42,33 +42,47 @@ def test_compile_with_simple_sketch(run_command, data_dir):
4242 result = run_command ("core install arduino:avr" )
4343 assert result .ok
4444
45- sketch_path = os .path .join (data_dir , "CompileIntegrationTest" )
45+ sketch_name = "CompileIntegrationTest"
46+ sketch_path = os .path .join (data_dir , sketch_name )
47+ fqbn = "arduino:avr:uno"
4648
4749 # Create a test sketch
48- result = run_command ("sketch new CompileIntegrationTest" )
50+ result = run_command ("sketch new {}" . format ( sketch_name ) )
4951 assert result .ok
5052 assert "Sketch created in: {}" .format (sketch_path ) in result .stdout
5153
5254 # Build sketch for arduino:avr:uno
53- result = run_command ("compile -b arduino:avr:uno {}" .format (sketch_path ))
55+ log_file_name = "compile.log"
56+ log_file_path = os .path .join (data_dir , log_file_name )
57+ result = run_command (
58+ "compile -b {fqbn} {sketch_path} --log-format json --log-file {log_file} --log-level trace" .format (
59+ fqbn = fqbn , sketch_path = sketch_path , log_file = log_file_path ))
5460 assert result .ok
55- assert "Sketch uses" in result .stdout
61+
62+ # let's test from the logs if the hex file produced by successful compile is moved to our sketch folder
63+ log_json = open (log_file_path , 'r' )
64+ json_log_lines = log_json .readlines ()
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 )
5670
5771
5872@pytest .mark .skipif (running_on_ci (), reason = "VMs have no serial ports" )
5973def test_compile_and_compile_combo (run_command , data_dir ):
60-
6174 # Init the environment explicitly
6275 result = run_command ("core update-index" )
6376 assert result .ok
6477
6578 # Install required core(s)
6679 result = run_command ("core install arduino:avr" )
67- # result = run_command("core install arduino:samd")
80+ result = run_command ("core install arduino:samd" )
6881 assert result .ok
6982
7083 # Create a test sketch
71- sketch_path = os .path .join (data_dir , "CompileAndUploadIntegrationTest" )
84+ sketch_name = "CompileAndUploadIntegrationTest"
85+ sketch_path = os .path .join (data_dir , sketch_name )
7286 result = run_command ("sketch new CompileAndUploadIntegrationTest" )
7387 assert result .ok
7488 assert "Sketch created in: {}" .format (sketch_path ) in result .stdout
@@ -96,7 +110,7 @@ def test_compile_and_compile_combo(run_command, data_dir):
96110 # }
97111 # ]
98112
99- detected_boards = []
113+ detected_boards = []
100114
101115 ports = json .loads (result .stdout )
102116 assert isinstance (ports , list )
@@ -109,12 +123,34 @@ def test_compile_and_compile_combo(run_command, data_dir):
109123 assert len (detected_boards ) >= 1 , "There are no boards available for testing"
110124
111125 # Build sketch for each detected board
112- for board in detected_boards :
113- result = run_command (
114- "compile -b {fqbn} --upload -p {address} {sketch_path}" .format (
115- fqbn = board .get ('fqbn' ),
116- address = board .get ('address' ),
117- sketch_path = sketch_path )
118- )
126+ for board in detected_boards :
127+ log_file_name = "{fqbn}-compile.log" .format (fqbn = board .get ('fqbn' ))
128+ log_file_path = os .path .join (data_dir , log_file_name )
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+ ))
119136 assert result .ok
120- assert "Verify successful" in result .stdout
137+ # check from the logs if the bin file were uploaded on the current board
138+ log_json = open (log_file_path , 'r' )
139+ json_log_lines = log_json .readlines ()
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