-
-
Notifications
You must be signed in to change notification settings - Fork 398
/
Copy pathtest_lib.py
385 lines (311 loc) · 14 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
# 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 simplejson as json
from pathlib import Path
def test_list(run_command):
# Init the environment explicitly
run_command("core update-index")
# When output is empty, nothing is printed out, no matter the output format
result = run_command("lib list")
assert result.ok
assert "" == result.stderr
assert "No libraries installed." == result.stdout.strip()
result = run_command("lib list --format json")
assert result.ok
assert "" == result.stderr
assert 0 == len(json.loads(result.stdout))
# Install something we can list at a version older than latest
result = run_command("lib install [email protected]")
assert result.ok
# Look at the plain text output
result = run_command("lib list")
assert result.ok
assert "" == result.stderr
lines = result.stdout.strip().splitlines()
assert 2 == len(lines)
toks = [t.strip() for t in lines[1].split(maxsplit=4)]
# Verifies the expected number of field
assert 5 == len(toks)
# be sure line contain the current version AND the available version
assert "" != toks[1]
assert "" != toks[2]
# Verifies library sentence
assert "An efficient and elegant JSON library..." == toks[4]
# Look at the JSON output
result = run_command("lib list --format json")
assert result.ok
assert "" == result.stderr
data = json.loads(result.stdout)
assert 1 == len(data)
# be sure data contains the available version
assert "" != data[0]["release"]["version"]
# Install something we can list without provides_includes field given in library.properties
result = run_command("lib install [email protected]")
assert result.ok
# Look at the JSON output
result = run_command("lib list Arduino_APDS9960 --format json")
assert result.ok
assert "" == result.stderr
data = json.loads(result.stdout)
assert 1 == len(data)
# be sure data contains the correct provides_includes field
assert "Arduino_APDS9960.h" == data[0]["library"]["provides_includes"][0]
def test_list_exit_code(run_command):
# Init the environment explicitly
assert run_command("core update-index")
assert run_command("core list")
# Verifies lib list doesn't fail when platform is not specified
result = run_command("lib list")
assert result.ok
assert result.stderr.strip() == ""
# Verify lib list command fails because specified platform is not installed
result = run_command("lib list -b arduino:samd:mkr1000")
assert result.failed
assert (
result.stderr.strip() == "Error listing Libraries: loading board data: platform arduino:samd is not installed"
)
assert run_command('lib install "AllThingsTalk LoRaWAN SDK"')
# Verifies lib list command keeps failing
result = run_command("lib list -b arduino:samd:mkr1000")
assert result.failed
assert (
result.stderr.strip() == "Error listing Libraries: loading board data: platform arduino:samd is not installed"
)
assert run_command("core install arduino:samd")
# Verifies lib list command now works since platform has been installed
result = run_command("lib list -b arduino:samd:mkr1000")
assert result.ok
assert result.stderr.strip() == ""
def test_list_with_fqbn(run_command):
# Init the environment explicitly
assert run_command("core update-index")
# Install core
assert run_command("core install arduino:avr")
# Install some library
assert run_command("lib install ArduinoJson")
assert run_command("lib install wm8978-esp32")
# Look at the plain text output
result = run_command("lib list -b arduino:avr:uno")
assert result.ok
assert "" == result.stderr
lines = result.stdout.strip().splitlines()
assert 2 == len(lines)
# Verifies library is compatible
toks = [t.strip() for t in lines[1].split(maxsplit=4)]
assert 5 == len(toks)
assert "ArduinoJson" == toks[0]
# Look at the JSON output
result = run_command("lib list -b arduino:avr:uno --format json")
assert result.ok
assert "" == result.stderr
data = json.loads(result.stdout)
assert 1 == len(data)
# Verifies library is compatible
assert data[0]["library"]["name"] == "ArduinoJson"
assert data[0]["library"]["compatible_with"]["arduino:avr:uno"]
def test_lib_download(run_command, downloads_dir):
# Download a specific lib version
assert run_command("lib download [email protected]")
assert Path(downloads_dir, "libraries", "AudioZero-1.0.0.zip").exists()
# Wrong lib version
result = run_command("lib download [email protected]")
assert result.failed
# Wrong lib
result = run_command("lib download AudioZ")
assert result.failed
def test_install(run_command):
libs = ['"AzureIoTProtocol_MQTT"', '"CMMC MQTT Connector"', '"WiFiNINA"']
# Should be safe to run install multiple times
assert run_command("lib install {}".format(" ".join(libs)))
assert run_command("lib install {}".format(" ".join(libs)))
# Test failing-install of library with wrong dependency
# (https://github.com/arduino/arduino-cli/issues/534)
result = run_command("lib install [email protected]")
assert "Error resolving dependencies for [email protected]: dependency 'MD_MAX72xx' is not available" in result.stderr
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(f"lib install --git-url {git_url}")
assert res.failed
assert "Error: unknown flag: --git-url" in res.stderr
assert run_command("lib download [email protected]")
zip_path = Path(downloads_dir, "libraries", "AudioZero-1.0.0.zip")
res = run_command(f"lib install --zip-path {zip_path}")
assert res.failed
assert "Error: unknown flag: --zip-path" 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(f"lib install --git-url {git_url}", custom_env=env)
assert res.ok
assert "--git-url and --zip-path flags are dangerous, use it at your own risk." in res.stdout
res = run_command(f"lib install --zip-path {zip_path}", custom_env=env)
assert res.ok
assert "--git-url and --zip-path flags are dangerous, 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(f"lib install --git-url {git_url}")
assert res.ok
assert "--git-url and --zip-path flags are dangerous, use it at your own risk." in res.stdout
res = run_command(f"lib install --zip-path {zip_path}")
assert res.ok
assert "--git-url and --zip-path flags are dangerous, use it at your own risk." in res.stdout
def test_install_with_git_url(run_command, data_dir, downloads_dir):
# Initialize configs to enable --git-url 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)
# Test git-url library install
res = run_command("lib install --git-url https://github.com/arduino-libraries/WiFi101.git")
assert res.ok
assert "--git-url and --zip-path flags are dangerous, use it at your own risk." in res.stdout
# Test failing-install as repository already exists
res = run_command("lib install --git-url https://github.com/arduino-libraries/WiFi101.git")
assert "--git-url and --zip-path flags are dangerous, use it at your own risk." in res.stdout
assert "Error installing Git Library: repository already exists" in res.stderr
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
assert run_command("lib download [email protected]")
zip_path = Path(downloads_dir, "libraries", "AudioZero-1.0.0.zip")
# Test zip-path install
res = run_command(f"lib install --zip-path {zip_path}")
assert res.ok
assert "--git-url and --zip-path flags are dangerous, use it at your own risk." in res.stdout
def test_update_index(run_command):
result = run_command("lib update-index")
assert result.ok
assert "Updating index: library_index.json downloaded" == result.stdout.splitlines()[-1].strip()
def test_uninstall(run_command):
libs = ['"AzureIoTProtocol_MQTT"', '"WiFiNINA"']
assert run_command("lib install {}".format(" ".join(libs)))
result = run_command("lib uninstall {}".format(" ".join(libs)))
assert result.ok
def test_uninstall_spaces(run_command):
key = '"LiquidCrystal I2C"'
assert run_command("lib install {}".format(key))
assert run_command("lib uninstall {}".format(key))
result = run_command("lib list --format json")
assert result.ok
assert len(json.loads(result.stdout)) == 0
def test_lib_ops_caseinsensitive(run_command):
"""
This test is supposed to (un)install the following library,
As you can see the name is all caps:
Name: "PCM"
Author: David Mellis <[email protected]>, Michael Smith <[email protected]>
Maintainer: David Mellis <[email protected]>
Sentence: Playback of short audio samples.
Paragraph: These samples are encoded directly in the Arduino sketch as an array of numbers.
Website: http://highlowtech.org/?p=1963
Category: Signal Input/Output
Architecture: avr
Types: Contributed
Versions: [1.0.0]
"""
key = "pcm"
assert run_command("lib install {}".format(key))
assert run_command("lib uninstall {}".format(key))
result = run_command("lib list --format json")
assert result.ok
assert len(json.loads(result.stdout)) == 0
def test_search(run_command):
assert run_command("lib update-index")
result = run_command("lib search --names")
assert result.ok
lines = [l.strip() for l in result.stdout.strip().splitlines()]
assert "Updating index: library_index.json downloaded" in lines
libs = [l[6:].strip('"') for l in lines if "Name:" in l]
expected = {"WiFi101", "WiFi101OTA", "Firebase Arduino based on WiFi101"}
assert expected == {lib for lib in libs if "WiFi101" in lib}
result = run_command("lib search --names --format json")
assert result.ok
libs_json = json.loads(result.stdout)
assert len(libs) == len(libs_json.get("libraries"))
result = run_command("lib search")
assert result.ok
# Search for a specific target
result = run_command("lib search ArduinoJson --format json")
assert result.ok
libs_json = json.loads(result.stdout)
assert len(libs_json.get("libraries")) >= 1
def test_search_paragraph(run_command):
"""
Search for a string that's only present in the `paragraph` field
within the index file.
"""
assert run_command("lib update-index")
result = run_command('lib search "A simple and efficient JSON library" --format json')
assert result.ok
libs_json = json.loads(result.stdout)
assert 1 == len(libs_json.get("libraries"))
def test_lib_list_with_updatable_flag(run_command):
# Init the environment explicitly
run_command("lib update-index")
# No libraries to update
result = run_command("lib list --updatable")
assert result.ok
assert "" == result.stderr
assert "No updates available." == result.stdout.strip()
# No library to update in json
result = run_command("lib list --updatable --format json")
assert result.ok
assert "" == result.stderr
assert 0 == len(json.loads(result.stdout))
# Install outdated library
assert run_command("lib install [email protected]")
# Install latest version of library
assert run_command("lib install WiFi101")
res = run_command("lib list --updatable")
assert res.ok
assert "" == res.stderr
# lines = res.stdout.strip().splitlines()
lines = [l.strip().split(maxsplit=4) for l in res.stdout.strip().splitlines()]
assert 2 == len(lines)
assert ["Name", "Installed", "Available", "Location", "Description"] in lines
line = lines[1]
assert "ArduinoJson" == line[0]
assert "6.11.0" == line[1]
# Verifies available version is not equal to installed one and not empty
assert "6.11.0" != line[2]
assert "" != line[2]
assert "An efficient and elegant JSON library..." == line[4]
# Look at the JSON output
res = run_command("lib list --updatable --format json")
assert res.ok
assert "" == res.stderr
data = json.loads(res.stdout)
assert 1 == len(data)
# be sure data contains the available version
assert "6.11.0" == data[0]["library"]["version"]
assert "6.11.0" != data[0]["release"]["version"]
assert "" != data[0]["release"]["version"]