-
-
Notifications
You must be signed in to change notification settings - Fork 398
/
Copy pathtest_compile.py
123 lines (101 loc) · 4.78 KB
/
test_compile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# This file is part of arduino-cli.
#
# Copyright 2019 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 [email protected].
import json
import os
import pytest
from .common import running_on_ci
def test_compile_without_fqbn(run_command):
# Init the environment explicitly
result = run_command("core update-index")
assert result.ok
# Download latest AVR
result = run_command("core install arduino:avr")
assert result.ok
# Build sketch without FQBN
result = run_command("compile")
assert result.failed
def test_compile_with_simple_sketch(run_command, data_dir):
# Init the environment explicitly
result = run_command("core update-index")
assert result.ok
# # Download latest AVR
result = run_command("core install arduino:avr")
assert result.ok
sketch_name = "CompileIntegrationTest"
sketch_path = os.path.join(data_dir, sketch_name)
fqbn = "arduino:avr:uno"
# Create a test sketch
result = run_command("sketch new {}".format(sketch_path))
assert result.ok
assert "Sketch created in: {}".format(sketch_path) in result.stdout
# Build sketch for arduino:avr:uno
log_file_name = "compile.log"
log_file_path = os.path.join(data_dir, log_file_name)
result = run_command(
"compile -b {fqbn} {sketch_path} --log-format json --log-file {log_file} --log-level trace".format(
fqbn=fqbn, sketch_path=sketch_path, log_file=log_file_path))
assert result.ok
# let's test from the logs if the hex file produced by successful compile is moved to our sketch folder
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=fqbn),
"Compile {sketch} for {fqbn} successful".format(sketch=sketch_name, fqbn=fqbn)
]
assert is_message_sequence_in_json_log_traces(expected_trace_sequence, json_log_lines)
@pytest.mark.skipif(running_on_ci(), reason="VMs have no serial ports")
def test_compile_and_compile_combo(run_command, detected_boards, data_dir):
# Create a test sketch
sketch_name = "CompileAndUploadIntegrationTest"
sketch_path = os.path.join(data_dir, sketch_name)
result = run_command("sketch new {}".format(sketch_path))
assert result.ok
assert "Sketch created in: {}".format(sketch_path) in result.stdout
# Build sketch for each detected board
for board in detected_boards:
# Init the environment explicitly
result = run_command("core update-index")
assert result.ok
# Install required core(s)
result = run_command("core install {}".format(board.core))
assert result.ok
log_file_name = "{fqbn}-compile.log".format(fqbn=board.fqbn.replace(":", "-"))
log_file_path = os.path.join(data_dir, log_file_name)
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
))
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)
def is_message_sequence_in_json_log_traces(message_sequence, log_json_lines):
trace_entries = []
for entry in log_json_lines:
entry = json.loads(entry)
if entry.get("level") == "trace":
if entry.get("msg") in message_sequence:
trace_entries.append(entry.get("msg"))
return message_sequence == trace_entries