forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_compile_part_1.py
439 lines (367 loc) · 15.7 KB
/
test_compile_part_1.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# 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 os
import platform
import tempfile
import hashlib
from pathlib import Path
import simplejson as json
import pytest
from .common import running_on_ci
def test_compile_without_fqbn(run_command):
# Init the environment explicitly
run_command(["core", "update-index"])
# Install Arduino AVR Boards
run_command(["core", "install", "arduino:[email protected]"])
# Build sketch without FQBN
result = run_command(["compile"])
assert result.failed
def test_compile_error_message(run_command, working_dir):
# Init the environment explicitly
run_command(["core", "update-index"])
# Download latest AVR
run_command(["core", "install", "arduino:avr"])
# Run a batch of bogus compile in a temp dir to check the error messages
with tempfile.TemporaryDirectory() as tmp_dir:
tmp = Path(tmp_dir)
abcdef = tmp / "ABCDEF"
res = run_command(["compile", "-b", "arduino:avr:uno", abcdef])
assert res.failed
assert "no such file or directory:" in res.stderr
res = run_command(["compile", "-b", "arduino:avr:uno", abcdef / "ABCDEF.ino"])
assert res.failed
assert "no such file or directory:" in res.stderr
res = run_command(["compile", "-b", "arduino:avr:uno", abcdef / "QWERTY"])
assert res.failed
assert "no such file or directory:" in res.stderr
abcdef.mkdir()
res = run_command(["compile", "-b", "arduino:avr:uno", abcdef])
assert res.failed
assert "main file missing from sketch:" in res.stderr
res = run_command(["compile", "-b", "arduino:avr:uno", abcdef / "ABCDEF.ino"])
assert res.failed
assert "no such file or directory:" in res.stderr
qwerty_ino = abcdef / "QWERTY.ino"
qwerty_ino.touch()
res = run_command(["compile", "-b", "arduino:avr:uno", qwerty_ino])
assert res.failed
assert "main file missing from sketch:" in res.stderr
def test_compile_with_simple_sketch(run_command, data_dir, working_dir):
# Init the environment explicitly
run_command(["core", "update-index"])
# Download latest AVR
run_command(["core", "install", "arduino:avr"])
sketch_name = "CompileIntegrationTest"
sketch_path = Path(data_dir, sketch_name)
fqbn = "arduino:avr:uno"
# Create a test sketch
result = run_command(["sketch", "new", sketch_path])
assert result.ok
assert f"Sketch created in: {sketch_path}" in result.stdout
# Build sketch for arduino:avr:uno
result = run_command(["compile", "-b", fqbn, sketch_path])
assert result.ok
# Build sketch for arduino:avr:uno with json output
result = run_command(["compile", "-b", fqbn, sketch_path, "--format", "json"])
assert result.ok
# check is a valid json and contains requested data
compile_output = json.loads(result.stdout)
assert compile_output["compiler_out"] != ""
assert compile_output["compiler_err"] == ""
# Verifies expected binaries have been built
sketch_path_md5 = hashlib.md5(bytes(sketch_path)).hexdigest().upper()
build_dir = Path(tempfile.gettempdir(), f"arduino-sketch-{sketch_path_md5}")
assert (build_dir / f"{sketch_name}.ino.eep").exists()
assert (build_dir / f"{sketch_name}.ino.elf").exists()
assert (build_dir / f"{sketch_name}.ino.hex").exists()
assert (build_dir / f"{sketch_name}.ino.with_bootloader.bin").exists()
assert (build_dir / f"{sketch_name}.ino.with_bootloader.hex").exists()
# Verifies binaries are not exported by default to Sketch folder
sketch_build_dir = Path(sketch_path, "build", fqbn.replace(":", "."))
assert not (sketch_build_dir / f"{sketch_name}.ino.eep").exists()
assert not (sketch_build_dir / f"{sketch_name}.ino.elf").exists()
assert not (sketch_build_dir / f"{sketch_name}.ino.hex").exists()
assert not (sketch_build_dir / f"{sketch_name}.ino.with_bootloader.bin").exists()
assert not (sketch_build_dir / f"{sketch_name}.ino.with_bootloader.hex").exists()
@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
run_command(["core", "update-index"])
# Install Arduino AVR Boards
run_command(["core", "install", "arduino:[email protected]"])
# 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", sketch_path])
assert result.ok
# Test the --output-dir flag defaulting to current working dir
result = run_command(["compile", "-b", fqbn, sketch_path, "--output-dir", "test"])
assert result.ok
target = os.path.join(working_dir, "test")
assert os.path.exists(target) and os.path.isdir(target)
def test_compile_with_sketch_with_symlink_selfloop(run_command, data_dir):
# Init the environment explicitly
run_command(["core", "update-index"])
# Install Arduino AVR Boards
run_command(["core", "install", "arduino:[email protected]"])
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", 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])
# 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 opening sketch:" 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", 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])
# 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 opening sketch:" in result.stderr
assert not result.ok
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
run_command(["core", "update-index"])
# Install Arduino AVR Boards
run_command(["core", "install", "arduino:[email protected]"])
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", sketch_path])
assert result.ok
assert "Sketch created in: {}".format(sketch_path) in result.stdout
# Build sketch for arduino:avr:uno
result = run_command(["compile", "-b", fqbn, sketch_path])
assert result.ok
def test_compile_without_precompiled_libraries(run_command, data_dir):
# Init the environment explicitly
url = "https://adafruit.github.io/arduino-board-index/package_adafruit_index.json"
assert run_command(["core", "update-index", f"--additional-urls={url}"])
assert run_command(["core", "install", "arduino:[email protected]", f"--additional-urls={url}"])
# # Precompiled version of Arduino_TensorflowLite
# assert run_command(["lib", "install", "Arduino_LSM9DS1"])
# assert run_command(["lib", "install", "[email protected]"])
#
# sketch_path = Path(data_dir, "libraries", "Arduino_TensorFlowLite", "examples", "hello_world")
# assert run_command(["compile", "-b", "arduino:mbed:nano33ble", sketch_path])
assert run_command(["core", "install", "arduino:[email protected]", f"--additional-urls={url}"])
# assert run_command(["core", "install", "adafruit:[email protected]", f"--additional-urls={url}"])
# # should work on adafruit too after https://github.com/arduino/arduino-cli/pull/1134
# assert run_command(["compile", "-b", "adafruit:samd:adafruit_feather_m4", sketch_path])
#
# # Non-precompiled version of Arduino_TensorflowLite
# assert run_command(["lib", "install", "[email protected]"])
# assert run_command(["compile", "-b", "arduino:mbed:nano33ble", sketch_path])
# assert run_command(["compile", "-b", "adafruit:samd:adafruit_feather_m4", sketch_path])
# Bosch sensor library
assert run_command(["lib", "install", "BSEC Software [email protected]"])
sketch_path = Path(data_dir, "libraries", "BSEC_Software_Library", "examples", "basic")
assert run_command(["compile", "-b", "arduino:samd:mkr1000", sketch_path])
assert run_command(["compile", "-b", "arduino:mbed:nano33ble", sketch_path])
# USBBlaster library
assert run_command(["lib", "install", "[email protected]"])
sketch_path = Path(data_dir, "libraries", "USBBlaster", "examples", "USB_Blaster")
assert run_command(["compile", "-b", "arduino:samd:mkrvidor4000", sketch_path])
def test_compile_with_build_properties_flag(run_command, data_dir, copy_sketch):
# Init the environment explicitly
assert run_command(["core", "update-index"])
# Install Arduino AVR Boards
assert run_command(["core", "install", "arduino:[email protected]"])
sketch_path = copy_sketch("sketch_with_single_string_define")
fqbn = "arduino:avr:uno"
# Compile using a build property with quotes
res = run_command(
[
"compile",
"-b",
fqbn,
'--build-properties="build.extra_flags=\\"-DMY_DEFINE=\\"hello world\\"\\""',
sketch_path,
"--verbose",
"--clean",
]
)
assert res.failed
assert "Flag --build-properties has been deprecated, please use --build-property instead." not in res.stderr
# Try again with quotes
res = run_command(
[
"compile",
"-b",
fqbn,
'--build-properties="build.extra_flags=-DMY_DEFINE=\\"hello\\""',
sketch_path,
"--verbose",
"--clean",
]
)
assert res.failed
assert "Flag --build-properties has been deprecated, please use --build-property instead." not in res.stderr
# Try without quotes
sketch_path = copy_sketch("sketch_with_single_int_define")
res = run_command(
[
"compile",
"-b",
fqbn,
'--build-properties="build.extra_flags=-DMY_DEFINE=1"',
sketch_path,
"--verbose",
"--clean",
]
)
assert res.ok
assert "Flag --build-properties has been deprecated, please use --build-property instead." in res.stderr
assert "-DMY_DEFINE=1" in res.stdout
sketch_path = copy_sketch("sketch_with_multiple_int_defines")
res = run_command(
[
"compile",
"-b",
fqbn,
'--build-properties="build.extra_flags=-DFIRST_PIN=1,compiler.cpp.extra_flags=-DSECOND_PIN=2"',
sketch_path,
"--verbose",
"--clean",
]
)
assert res.ok
assert "Flag --build-properties has been deprecated, please use --build-property instead." in res.stderr
assert "-DFIRST_PIN=1" in res.stdout
assert "-DSECOND_PIN=2" in res.stdout
def test_compile_with_build_property_containing_quotes(run_command, data_dir, copy_sketch):
# Init the environment explicitly
assert run_command(["core", "update-index"])
# Install Arduino AVR Boards
assert run_command(["core", "install", "arduino:[email protected]"])
sketch_path = copy_sketch("sketch_with_single_string_define")
fqbn = "arduino:avr:uno"
# Compile using a build property with quotes
res = run_command(
[
"compile",
"-b",
fqbn,
'--build-property=build.extra_flags=\\"-DMY_DEFINE=\\"hello world\\"\\"',
sketch_path,
"--verbose",
]
)
assert res.ok
assert '-DMY_DEFINE=\\"hello world\\"' in res.stdout
def test_compile_with_multiple_build_property_flags(run_command, data_dir, copy_sketch, working_dir):
# Init the environment explicitly
assert run_command(["core", "update-index"])
# Install Arduino AVR Boards
assert run_command(["core", "install", "arduino:[email protected]"])
sketch_path = copy_sketch("sketch_with_multiple_defines")
fqbn = "arduino:avr:uno"
# Compile using multiple build properties separated by a space
res = run_command(
[
"compile",
"-b",
fqbn,
'--build-property=compiler.cpp.extra_flags=\\"-DPIN=2 -DSSID=\\"This is a String\\"\\"',
sketch_path,
"--verbose",
"--clean",
]
)
assert res.failed
# Compile using multiple build properties separated by a space and properly quoted
res = run_command(
[
"compile",
"-b",
fqbn,
'--build-property=compiler.cpp.extra_flags=-DPIN=2 \\"-DSSID=\\"This is a String\\"\\"',
sketch_path,
"--verbose",
"--clean",
]
)
assert res.ok
assert '-DPIN=2 "-DSSID=\\"This is a String\\""' in res.stdout
# Tries compilation using multiple build properties separated by a comma
res = run_command(
[
"compile",
"-b",
fqbn,
'--build-property=compiler.cpp.extra_flags=\\"-DPIN=2,-DSSID=\\"This is a String\\"\\"',
sketch_path,
"--verbose",
"--clean",
]
)
assert res.failed
res = run_command(
[
"compile",
"-b",
fqbn,
'--build-property=compiler.cpp.extra_flags=\\"-DPIN=2\\"',
'--build-property=compiler.cpp.extra_flags=\\"-DSSID=\\"This is a String\\"\\"',
sketch_path,
"--verbose",
"--clean",
]
)
assert res.failed
assert "-DPIN=2" not in res.stdout
assert '-DSSID=\\"This is a String\\"' in res.stdout
res = run_command(
[
"compile",
"-b",
fqbn,
'--build-property=compiler.cpp.extra_flags=\\"-DPIN=2\\"',
'--build-property=build.extra_flags=\\"-DSSID=\\"hello world\\"\\"',
sketch_path,
"--verbose",
"--clean",
]
)
assert res.ok
assert "-DPIN=2" in res.stdout
assert '-DSSID=\\"hello world\\"' in res.stdout