-
-
Notifications
You must be signed in to change notification settings - Fork 398
/
Copy pathtest_lib.py
480 lines (387 loc) · 18.8 KB
/
test_lib.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# 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 platform
import simplejson as json
import pytest
import shutil
from git import Repo
from pathlib import Path
import tempfile
import requests
import zipfile
import io
import re
# Util function to download library from URL
def download_lib(url, download_dir):
tmp = Path(tempfile.TemporaryDirectory().name)
tmp.mkdir(parents=True, exist_ok=True)
regex = re.compile(r"^(.*)-[0-9]+.[0-9]+.[0-9]")
response = requests.get(url)
# Download and unzips library removing version suffix
with zipfile.ZipFile(io.BytesIO(response.content)) as thezip:
for zipinfo in thezip.infolist():
with thezip.open(zipinfo) as f:
dest_dir = tmp / regex.sub("\\g<1>", zipinfo.filename)
if zipinfo.is_dir():
dest_dir.mkdir(parents=True, exist_ok=True)
else:
dest_dir.write_bytes(f.read())
# Recreates zip with folder without version suffix
z = zipfile.ZipFile(download_dir, "w")
for f in tmp.glob("**/*"):
z.write(f, arcname=f.relative_to(tmp))
z.close()
def test_install_git_url_and_zip_path_flags_visibility(run_command, data_dir, downloads_dir):
# Verifies installation fail because flags are not found
git_url = "https://github.com/arduino-libraries/WiFi101.git"
res = run_command(["lib", "install", "--git-url", git_url])
assert res.failed
assert "--git-url and --zip-path are disabled by default, for more information see:" in res.stderr
# Download library
url = "https://github.com/arduino-libraries/AudioZero/archive/refs/tags/1.1.1.zip"
zip_path = Path(downloads_dir, "libraries", "AudioZero.zip")
zip_path.parent.mkdir(parents=True, exist_ok=True)
download_lib(url, zip_path)
res = run_command(["lib", "install", "--zip-path", zip_path])
assert res.failed
assert "--git-url and --zip-path are disabled by default, for more information see:" in res.stderr
env = {
"ARDUINO_DATA_DIR": data_dir,
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
"ARDUINO_SKETCHBOOK_DIR": data_dir,
"ARDUINO_ENABLE_UNSAFE_LIBRARY_INSTALL": "true",
}
# Verifies installation is successful when flags are enabled with env var
res = run_command(["lib", "install", "--git-url", git_url], custom_env=env)
assert res.ok
assert "--git-url and --zip-path flags allow installing untrusted files, use it at your own risk." in res.stdout
res = run_command(["lib", "install", "--zip-path", zip_path], custom_env=env)
assert res.ok
assert "--git-url and --zip-path flags allow installing untrusted files, use it at your own risk." in res.stdout
# Uninstall libraries to install them again
assert run_command(["lib", "uninstall", "WiFi101", "AudioZero"])
# Verifies installation is successful when flags are enabled with settings file
assert run_command(["config", "init", "--dest-dir", "."], custom_env=env)
res = run_command(["lib", "install", "--git-url", git_url])
assert res.ok
assert "--git-url and --zip-path flags allow installing untrusted files, use it at your own risk." in res.stdout
res = run_command(["lib", "install", "--zip-path", zip_path])
assert res.ok
assert "--git-url and --zip-path flags allow installing untrusted files, use it at your own risk." in res.stdout
def test_install_with_zip_path(run_command, data_dir, downloads_dir):
# Initialize configs to enable --zip-path flag
env = {
"ARDUINO_DATA_DIR": data_dir,
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
"ARDUINO_SKETCHBOOK_DIR": data_dir,
"ARDUINO_ENABLE_UNSAFE_LIBRARY_INSTALL": "true",
}
assert run_command(["config", "init", "--dest-dir", "."], custom_env=env)
# Download a specific lib version
# Download library
url = "https://github.com/arduino-libraries/AudioZero/archive/refs/tags/1.1.1.zip"
zip_path = Path(downloads_dir, "libraries", "AudioZero.zip")
zip_path.parent.mkdir(parents=True, exist_ok=True)
download_lib(url, zip_path)
lib_install_dir = Path(data_dir, "libraries", "AudioZero")
# Verifies library is not already installed
assert not lib_install_dir.exists()
# Test zip-path install
res = run_command(["lib", "install", "--zip-path", zip_path])
assert res.ok
assert "--git-url and --zip-path flags allow installing untrusted files, use it at your own risk." in res.stdout
# Verifies library is installed in expected path
assert lib_install_dir.exists()
files = list(lib_install_dir.glob("**/*"))
assert lib_install_dir / "examples" / "SimpleAudioPlayerZero" / "SimpleAudioPlayerZero.ino" in files
assert lib_install_dir / "src" / "AudioZero.h" in files
assert lib_install_dir / "src" / "AudioZero.cpp" in files
assert lib_install_dir / "keywords.txt" in files
assert lib_install_dir / "library.properties" in files
assert lib_install_dir / "README.adoc" in files
# Reinstall library
assert run_command(["lib", "install", "--zip-path", zip_path])
# Verifies library remains installed
assert lib_install_dir.exists()
files = list(lib_install_dir.glob("**/*"))
assert lib_install_dir / "examples" / "SimpleAudioPlayerZero" / "SimpleAudioPlayerZero.ino" in files
assert lib_install_dir / "src" / "AudioZero.h" in files
assert lib_install_dir / "src" / "AudioZero.cpp" in files
assert lib_install_dir / "keywords.txt" in files
assert lib_install_dir / "library.properties" in files
assert lib_install_dir / "README.adoc" in files
@pytest.mark.skipif(
platform.system() == "Windows",
reason="Using a file uri as git url doesn't work on Windows, "
+ "this must be removed when this issue is fixed: https://github.com/go-git/go-git/issues/247",
)
def test_install_with_git_url_local_file_uri(run_command, downloads_dir, data_dir):
assert run_command(["update"])
env = {
"ARDUINO_DATA_DIR": data_dir,
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
"ARDUINO_SKETCHBOOK_DIR": data_dir,
"ARDUINO_ENABLE_UNSAFE_LIBRARY_INSTALL": "true",
}
lib_install_dir = Path(data_dir, "libraries", "WiFi101")
# Verifies library is not installed
assert not lib_install_dir.exists()
# Clone repository locally
git_url = "https://github.com/arduino-libraries/WiFi101.git"
repo_dir = Path(data_dir, "WiFi101")
assert Repo.clone_from(git_url, repo_dir)
assert run_command(["lib", "install", "--git-url", repo_dir.as_uri()], custom_env=env)
# Verifies library is installed
assert lib_install_dir.exists()
def test_install_with_zip_path_multiple_libraries(run_command, downloads_dir, data_dir):
assert run_command(["update"])
env = {
"ARDUINO_DATA_DIR": data_dir,
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
"ARDUINO_SKETCHBOOK_DIR": data_dir,
"ARDUINO_ENABLE_UNSAFE_LIBRARY_INSTALL": "true",
}
# Downloads zip to be installed later
wifi_zip_path = Path(downloads_dir, "libraries", "WiFi101-0.16.1.zip")
ble_zip_path = Path(downloads_dir, "libraries", "ArduinoBLE-1.1.3.zip")
download_lib("https://github.com/arduino-libraries/WiFi101/archive/refs/tags/0.16.1.zip", wifi_zip_path)
download_lib("https://github.com/arduino-libraries/ArduinoBLE/archive/refs/tags/1.1.3.zip", ble_zip_path)
wifi_install_dir = Path(data_dir, "libraries", "WiFi101")
ble_install_dir = Path(data_dir, "libraries", "ArduinoBLE")
# Verifies libraries are not installed
assert not wifi_install_dir.exists()
assert not ble_install_dir.exists()
assert run_command(["lib", "install", "--zip-path", wifi_zip_path, ble_zip_path], custom_env=env)
# Verifies library are installed
assert wifi_install_dir.exists()
assert ble_install_dir.exists()
def test_lib_examples_with_case_mismatch(run_command, data_dir):
assert run_command(["update"])
assert run_command(["lib", "install", "[email protected]"])
res = run_command(["lib", "examples", "WiFiManager", "--format", "json"])
assert res.ok
data = json.loads(res.stdout)
assert len(data) == 1
examples = data[0]["examples"]
assert len(examples) == 14
examples_path = Path(data_dir, "libraries", "WiFiManager", "examples")
# Verifies sketches with correct casing are listed
assert str(examples_path / "Advanced") in examples
assert str(examples_path / "AutoConnect" / "AutoConnectWithFeedbackLED") in examples
assert str(examples_path / "AutoConnect" / "AutoConnectWithFSParameters") in examples
assert str(examples_path / "AutoConnect" / "AutoConnectWithFSParametersAndCustomIP") in examples
assert str(examples_path / "Basic") in examples
assert str(examples_path / "DEV" / "OnDemandConfigPortal") in examples
assert str(examples_path / "NonBlocking" / "AutoConnectNonBlocking") in examples
assert str(examples_path / "NonBlocking" / "AutoConnectNonBlockingwParams") in examples
assert str(examples_path / "Old_examples" / "AutoConnectWithFeedback") in examples
assert str(examples_path / "Old_examples" / "AutoConnectWithReset") in examples
assert str(examples_path / "Old_examples" / "AutoConnectWithStaticIP") in examples
assert str(examples_path / "Old_examples" / "AutoConnectWithTimeout") in examples
assert str(examples_path / "OnDemand" / "OnDemandConfigPortal") in examples
assert str(examples_path / "ParamsChildClass") in examples
# Verifies sketches with wrong casing are not returned
assert str(examples_path / "NonBlocking" / "OnDemandNonBlocking") not in examples
assert str(examples_path / "OnDemand" / "OnDemandWebPortal") not in examples
def test_lib_list_using_library_with_invalid_version(run_command, data_dir):
assert run_command(["update"])
# Install a library
assert run_command(["lib", "install", "[email protected]"])
# Verifies library is correctly returned
res = run_command(["lib", "list", "--format", "json"])
assert res.ok
data = json.loads(res.stdout)
assert len(data) == 1
assert "0.16.1" == data[0]["library"]["version"]
# Changes the version of the currently installed library so that it's
# invalid
lib_path = Path(data_dir, "libraries", "WiFi101")
Path(lib_path, "library.properties").write_text("name=WiFi101\nversion=1.0001")
# Verifies version is now empty
res = run_command(["lib", "list", "--format", "json"])
assert res.ok
data = json.loads(res.stdout)
assert len(data) == 1
assert "version" not in data[0]["library"]
def test_lib_upgrade_using_library_with_invalid_version(run_command, data_dir):
assert run_command(["update"])
# Install a library
assert run_command(["lib", "install", "[email protected]"])
# Verifies library is correctly returned
res = run_command(["lib", "list", "--format", "json"])
assert res.ok
data = json.loads(res.stdout)
assert len(data) == 1
assert "0.16.1" == data[0]["library"]["version"]
# Changes the version of the currently installed library so that it's
# invalid
lib_path = Path(data_dir, "libraries", "WiFi101")
Path(lib_path, "library.properties").write_text("name=WiFi101\nversion=1.0001")
# Verifies version is now empty
res = run_command(["lib", "list", "--format", "json"])
assert res.ok
data = json.loads(res.stdout)
assert len(data) == 1
assert "version" not in data[0]["library"]
# Upgrade library
assert run_command(["lib", "upgrade", "WiFi101"])
# Verifies library has been updated
res = run_command(["lib", "list", "--format", "json"])
assert res.ok
data = json.loads(res.stdout)
assert len(data) == 1
assert "" != data[0]["library"]["version"]
def test_install_zip_lib_with_macos_metadata(run_command, data_dir, downloads_dir):
# Initialize configs to enable --zip-path flag
env = {
"ARDUINO_DATA_DIR": data_dir,
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
"ARDUINO_SKETCHBOOK_DIR": data_dir,
"ARDUINO_ENABLE_UNSAFE_LIBRARY_INSTALL": "true",
}
assert run_command(["config", "init", "--dest-dir", "."], custom_env=env)
lib_install_dir = Path(data_dir, "libraries", "fake-lib")
# Verifies library is not already installed
assert not lib_install_dir.exists()
zip_path = Path(__file__).parent / "testdata" / "fake-lib.zip"
# Test zip-path install
res = run_command(["lib", "install", "--zip-path", zip_path])
assert res.ok
assert "--git-url and --zip-path flags allow installing untrusted files, use it at your own risk." in res.stdout
# Verifies library is installed in expected path
assert lib_install_dir.exists()
files = list(lib_install_dir.glob("**/*"))
assert lib_install_dir / "library.properties" in files
assert lib_install_dir / "src" / "fake-lib.h" in files
# Reinstall library
assert run_command(["lib", "install", "--zip-path", zip_path])
# Verifies library remains installed
assert lib_install_dir.exists()
files = list(lib_install_dir.glob("**/*"))
assert lib_install_dir / "library.properties" in files
assert lib_install_dir / "src" / "fake-lib.h" in files
def test_install_zip_invalid_library(run_command, data_dir, downloads_dir):
# Initialize configs to enable --zip-path flag
env = {
"ARDUINO_DATA_DIR": data_dir,
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
"ARDUINO_SKETCHBOOK_DIR": data_dir,
"ARDUINO_ENABLE_UNSAFE_LIBRARY_INSTALL": "true",
}
assert run_command(["config", "init", "--dest-dir", "."], custom_env=env)
lib_install_dir = Path(data_dir, "libraries", "lib-without-header")
# Verifies library is not already installed
assert not lib_install_dir.exists()
zip_path = Path(__file__).parent / "testdata" / "lib-without-header.zip"
# Test zip-path install
res = run_command(["lib", "install", "--zip-path", zip_path])
assert res.failed
assert "library not valid" in res.stderr
lib_install_dir = Path(data_dir, "libraries", "lib-without-properties")
# Verifies library is not already installed
assert not lib_install_dir.exists()
zip_path = Path(__file__).parent / "testdata" / "lib-without-properties.zip"
# Test zip-path install
res = run_command(["lib", "install", "--zip-path", zip_path])
assert res.failed
assert "library not valid" in res.stderr
def test_install_git_invalid_library(run_command, data_dir, downloads_dir):
# Initialize configs to enable --zip-path flag
env = {
"ARDUINO_DATA_DIR": data_dir,
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
"ARDUINO_SKETCHBOOK_DIR": data_dir,
"ARDUINO_ENABLE_UNSAFE_LIBRARY_INSTALL": "true",
}
assert run_command(["config", "init", "--dest-dir", "."], custom_env=env)
# Create fake library repository
repo_dir = Path(data_dir, "lib-without-header")
with Repo.init(repo_dir) as repo:
lib_properties = Path(repo_dir, "library.properties")
lib_properties.touch()
repo.index.add([str(lib_properties)])
repo.index.commit("First commit")
lib_install_dir = Path(data_dir, "libraries", "lib-without-header")
# Verifies library is not already installed
assert not lib_install_dir.exists()
res = run_command(["lib", "install", "--git-url", repo_dir], custom_env=env)
assert res.failed
assert "library not valid" in res.stderr
assert not lib_install_dir.exists()
# Create another fake library repository
repo_dir = Path(data_dir, "lib-without-properties")
with Repo.init(repo_dir) as repo:
lib_header = Path(repo_dir, "src", "lib-without-properties.h")
lib_header.parent.mkdir(parents=True, exist_ok=True)
lib_header.touch()
repo.index.add([str(lib_header)])
repo.index.commit("First commit")
lib_install_dir = Path(data_dir, "libraries", "lib-without-properties")
# Verifies library is not already installed
assert not lib_install_dir.exists()
res = run_command(["lib", "install", "--git-url", repo_dir], custom_env=env)
assert res.failed
assert "library not valid" in res.stderr
assert not lib_install_dir.exists()
def test_upgrade_does_not_try_to_upgrade_bundled_core_libraries_in_sketchbook(run_command, data_dir):
test_platform_name = "platform_with_bundled_library"
platform_install_dir = Path(data_dir, "hardware", "arduino-beta-dev", test_platform_name)
platform_install_dir.mkdir(parents=True)
# Install platform in Sketchbook hardware dir
shutil.copytree(
Path(__file__).parent / "testdata" / test_platform_name,
platform_install_dir,
dirs_exist_ok=True,
)
assert run_command(["update"])
# Install latest version of library identical to one
# bundled with test platform
assert run_command(["lib", "install", "USBHost"])
res = run_command(["lib", "list", "--all", "--format", "json"])
assert res.ok
libs = json.loads(res.stdout)
assert len(libs) == 2
# Verify both libraries have the same name
assert libs[0]["library"]["name"] == "USBHost"
assert libs[1]["library"]["name"] == "USBHost"
res = run_command(["lib", "upgrade"])
assert res.ok
# Empty output means nothing has been updated as expected
assert res.stdout == ""
def test_upgrade_does_not_try_to_upgrade_bundled_core_libraries(run_command, data_dir):
test_platform_name = "platform_with_bundled_library"
platform_install_dir = Path(data_dir, "packages", "arduino", "hardware", "arch", "4.2.0")
platform_install_dir.mkdir(parents=True)
# Simulate installation of a platform with arduino-cli
shutil.copytree(
Path(__file__).parent / "testdata" / test_platform_name,
platform_install_dir,
dirs_exist_ok=True,
)
assert run_command(["update"])
# Install latest version of library identical to one
# bundled with test platform
assert run_command(["lib", "install", "USBHost"])
res = run_command(["lib", "list", "--all", "--format", "json"])
assert res.ok
libs = json.loads(res.stdout)
assert len(libs) == 2
# Verify both libraries have the same name
assert libs[0]["library"]["name"] == "USBHost"
assert libs[1]["library"]["name"] == "USBHost"
res = run_command(["lib", "upgrade"])
assert res.ok
# Empty output means nothing has been updated as expected
assert res.stdout == ""