forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_compile.py
283 lines (236 loc) · 9.94 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# 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 [email protected].
import json
import os
import platform
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, working_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
)
# Test the --output flag with absolute path
target = os.path.join(data_dir, "test.hex")
result = run_command(
"compile -b {fqbn} {sketch_path} -o {target}".format(
fqbn=fqbn, sketch_path=sketch_path, target=target
)
)
assert result.ok
assert os.path.exists(target)
@pytest.mark.skipif(
running_on_ci() and platform.system() == "Windows",
reason="Test disabled on Github Actions Win VM until tmpdir inconsistent behavior bug is fixed",
)
def test_output_flag_default_path(run_command, data_dir, working_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
# Create a test sketch
sketch_path = os.path.join(data_dir, "test_output_flag_default_path")
fqbn = "arduino:avr:uno"
result = run_command("sketch new {}".format(sketch_path))
assert result.ok
# Test the --output flag defaulting to current working dir
result = run_command(
"compile -b {fqbn} {sketch_path} -o test".format(
fqbn=fqbn, sketch_path=sketch_path
)
)
assert result.ok
assert os.path.exists(os.path.join(working_dir, "test.hex"))
# Test extension won't be added if already present
result = run_command(
"compile -b {fqbn} {sketch_path} -o test2.hex".format(
fqbn=fqbn, sketch_path=sketch_path
)
)
assert result.ok
assert os.path.exists(os.path.join(working_dir, "test2.hex"))
def test_compile_with_sketch_with_symlink_selfloop(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 = "CompileIntegrationTestSymlinkSelfLoop"
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
# create a symlink that loops on himself
loop_file_path = os.path.join(sketch_path, "loop")
os.symlink(loop_file_path, loop_file_path)
# Build sketch for arduino:avr:uno
result = run_command(
"compile -b {fqbn} {sketch_path}".format(fqbn=fqbn, sketch_path=sketch_path)
)
# The assertion is a bit relaxed in this case because win behaves differently from macOs and linux
# returning a different error detailed message
assert "Error during sketch processing" in result.stderr
assert not result.ok
sketch_name = "CompileIntegrationTestSymlinkDirLoop"
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
# create a symlink that loops on the upper level
loop_dir_path = os.path.join(sketch_path, "loop_dir")
os.mkdir(loop_dir_path)
loop_dir_symlink_path = os.path.join(loop_dir_path, "loop_dir_symlink")
os.symlink(loop_dir_path, loop_dir_symlink_path)
# Build sketch for arduino:avr:uno
result = run_command(
"compile -b {fqbn} {sketch_path}".format(fqbn=fqbn, sketch_path=sketch_path)
)
# The assertion is a bit relaxed also in this case because macOS behaves differently from win and linux:
# the cli does not follow recursively the symlink til breaking
assert "Error during sketch processing" in result.stderr
assert not result.ok
@pytest.mark.skipif(running_on_ci(), reason="VMs have no serial ports")
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
# Install required core(s)
result = run_command("core install arduino:avr")
result = run_command("core install arduino:samd")
assert result.ok
# 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
# Build sketch for each detected board
for board in detected_boards:
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
)
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
)
run_test(sketch_path)
run_test(sketch_main_file)
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
def test_compile_blacklisted_sketchname(run_command, data_dir):
"""
Compile should ignore folders named `RCS`, `.git` and the likes, but
it should be ok for a sketch to be named like RCS.ino
"""
# 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 = "RCS"
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}".format(fqbn=fqbn, sketch_path=sketch_path)
)
assert result.ok