1
1
# This file is part of arduino-cli.
2
-
2
+ #
3
3
# Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
4
-
4
+ #
5
5
# This software is released under the GNU General Public License version 3,
6
6
# which covers the main part of arduino-cli.
7
7
# The terms of this license can be found at:
8
8
# https://www.gnu.org/licenses/gpl-3.0.en.html
9
-
9
+ #
10
10
# You can be released from the requirements of the above licenses by purchasing
11
11
# a commercial license. Buying such a license is mandatory if you want to modify or
12
12
# otherwise use the software for commercial activities involving the Arduino
13
13
# software without disclosing the source code of your own applications. To purchase
14
14
# a commercial license, send an email to [email protected] .
15
- import pytest
16
15
import json
17
16
import os
17
+ import pytest
18
18
19
19
from .common import running_on_ci
20
20
@@ -42,33 +42,47 @@ def test_compile_with_simple_sketch(run_command, data_dir):
42
42
result = run_command ("core install arduino:avr" )
43
43
assert result .ok
44
44
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"
46
48
47
49
# Create a test sketch
48
- result = run_command ("sketch new CompileIntegrationTest" )
50
+ result = run_command ("sketch new {}" . format ( sketch_name ) )
49
51
assert result .ok
50
52
assert "Sketch created in: {}" .format (sketch_path ) in result .stdout
51
53
52
54
# 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 ))
54
60
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 )
56
70
57
71
58
72
@pytest .mark .skipif (running_on_ci (), reason = "VMs have no serial ports" )
59
73
def test_compile_and_compile_combo (run_command , data_dir ):
60
-
61
74
# Init the environment explicitly
62
75
result = run_command ("core update-index" )
63
76
assert result .ok
64
77
65
78
# Install required core(s)
66
79
result = run_command ("core install arduino:avr" )
67
- # result = run_command("core install arduino:samd")
80
+ result = run_command ("core install arduino:samd" )
68
81
assert result .ok
69
82
70
83
# 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 )
72
86
result = run_command ("sketch new CompileAndUploadIntegrationTest" )
73
87
assert result .ok
74
88
assert "Sketch created in: {}" .format (sketch_path ) in result .stdout
@@ -96,7 +110,7 @@ def test_compile_and_compile_combo(run_command, data_dir):
96
110
# }
97
111
# ]
98
112
99
- detected_boards = []
113
+ detected_boards = []
100
114
101
115
ports = json .loads (result .stdout )
102
116
assert isinstance (ports , list )
@@ -109,12 +123,34 @@ def test_compile_and_compile_combo(run_command, data_dir):
109
123
assert len (detected_boards ) >= 1 , "There are no boards available for testing"
110
124
111
125
# 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
+ ))
119
136
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