-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_modify.py
392 lines (357 loc) · 13.3 KB
/
test_modify.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
# Copyright 2021 ARDUINO SA (http://www.arduino.cc/)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# 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 pathlib
import pytest
test_data_path = pathlib.Path(__file__).resolve().parent.joinpath("testdata")
def test_help(run_command):
"""Test the command line help."""
# Run the `help modify` command
engine_command = [
"help",
"modify",
]
result = run_command(cmd=engine_command)
assert result.ok
assert "help for modify" in result.stdout
# --help flag
engine_command = [
"modify",
"--help",
]
result = run_command(cmd=engine_command)
assert result.ok
assert "help for modify" in result.stdout
def test_invalid_flag(configuration, run_command):
"""Test the command's handling of invalid flags."""
invalid_flag = "--some-bad-flag"
engine_command = [
"modify",
invalid_flag,
"--config-file",
configuration.path,
"SpacebrewYun",
]
result = run_command(cmd=engine_command)
assert not result.ok
assert f"unknown flag: {invalid_flag}" in result.stderr
def test_missing_library_name_arg(configuration, run_command):
"""Test the command's handling of missing LIBRARY_NAME argument."""
engine_command = [
"modify",
"--config-file",
configuration.path,
"--repo-url",
"https://github.com/Foo/Bar.git",
]
result = run_command(cmd=engine_command)
assert not result.ok
assert "accepts 1 arg(s), received 0" in result.stderr
def test_multiple_library_name_arg(configuration, run_command):
"""Test the command's handling of multiple LIBRARY_NAME arguments."""
engine_command = [
"modify",
"--config-file",
configuration.path,
"--repo-url",
"https://github.com/Foo/Bar.git",
"ArduinoIoTCloudBearSSL",
"SpacebrewYun",
]
result = run_command(cmd=engine_command)
assert not result.ok
assert "accepts 1 arg(s), received 2" in result.stderr
def test_database_file_not_found(configuration, run_command):
"""Test the command's handling of incorrect LibrariesDB configuration."""
engine_command = [
"modify",
"--config-file",
configuration.path,
"--repo-url",
"https://github.com/Foo/Bar.git",
"SpacebrewYun",
]
result = run_command(cmd=engine_command)
assert not result.ok
assert "Database file not found at {db_path}".format(db_path=configuration.data["LibrariesDB"]) in result.stderr
def test_repo_url_basic(configuration, run_command):
"""Test the basic functionality of the `--repo-url` modification flag."""
# Run the sync command to generate test data
engine_command = [
"sync",
"--config-file",
configuration.path,
test_data_path.joinpath("test_modify", "test_repo_url_basic", "repos.txt"),
]
result = run_command(cmd=engine_command)
assert result.ok
assert pathlib.Path(configuration.data["LibrariesDB"]).exists()
# Library not in DB
nonexistent_library_name = "nonexistent"
engine_command = [
"modify",
"--config-file",
configuration.path,
"--repo-url",
"https://github.com/Foo/Bar.git",
nonexistent_library_name,
]
result = run_command(cmd=engine_command)
assert not result.ok
assert f"{nonexistent_library_name} not found" in result.stderr
# No local flag
engine_command = [
"modify",
"--config-file",
configuration.path,
"SpacebrewYun",
]
result = run_command(cmd=engine_command)
assert not result.ok
assert "No modification flags" in result.stderr
# Invalid URL format
invalid_url = "https://github.com/Foo/Bar"
engine_command = [
"modify",
"--config-file",
configuration.path,
"--repo-url",
invalid_url,
"SpacebrewYun",
]
result = run_command(cmd=engine_command)
assert not result.ok
assert f"{invalid_url} does not have a valid format" in result.stderr
# Same URL as already in DB
library_name = "SpacebrewYun"
library_repo_url = "https://github.com/arduino-libraries/SpacebrewYun.git"
engine_command = [
"modify",
"--config-file",
configuration.path,
"--repo-url",
library_repo_url,
library_name,
]
result = run_command(cmd=engine_command)
assert not result.ok
assert f"{library_name} already has URL {library_repo_url}" in result.stderr
@pytest.mark.parametrize(
"name, releases, old_host, old_owner, old_repo_name, new_host, new_owner, new_repo_name",
[
(
"Arduino Uno WiFi Dev Ed Library",
["0.0.3"],
"github.com",
"arduino-libraries",
"UnoWiFi-Developer-Edition-Lib",
"gitlab.com",
"foo-owner",
"bar-repo",
),
(
"SpacebrewYun",
["1.0.0", "1.0.1", "1.0.2"],
"github.com",
"arduino-libraries",
"SpacebrewYun",
"gitlab.com",
"foo-owner",
"bar-repo",
),
],
)
def test_repo_url(
configuration,
run_command,
working_dir,
name,
releases,
old_host,
old_owner,
old_repo_name,
new_host,
new_owner,
new_repo_name,
):
"""Test the `--repo-url` modification flag in action."""
sanitized_name = name.replace(" ", "_")
old_library_release_archives_folder = pathlib.Path(configuration.data["LibrariesFolder"]).joinpath(
old_host, old_owner
)
old_git_clone_path = pathlib.Path(configuration.data["GitClonesFolder"]).joinpath(
old_host, old_owner, old_repo_name
)
new_repo_url = f"https://{new_host}/{new_owner}/{new_repo_name}.git"
new_library_release_archives_folder = pathlib.Path(configuration.data["LibrariesFolder"]).joinpath(
new_host, new_owner
)
# The "canary" library is not modified and so all its content should remain unchanged after running the command
canary_name = "ArduinoIoTCloudBearSSL"
sanitized_canary_name = "ArduinoIoTCloudBearSSL"
canary_release = "1.1.2"
canary_host = "github.com"
canary_owner = "arduino-libraries"
canary_repo_name = "ArduinoIoTCloudBearSSL"
canary_repo_url = f"https://{canary_host}/{canary_owner}/{canary_repo_name}.git"
canary_release_filename = f"{sanitized_canary_name}-{canary_release}.zip"
canary_release_archive_path = pathlib.Path(configuration.data["LibrariesFolder"]).joinpath(
canary_host, canary_owner, canary_release_filename
)
canary_git_clone_path = pathlib.Path(configuration.data["GitClonesFolder"]).joinpath(
canary_host, canary_owner, canary_repo_name
)
canary_release_archive_url = "{base}{host}/{owner}/{filename}".format(
base=configuration.data["BaseDownloadUrl"],
host=canary_host,
owner=canary_owner,
filename=canary_release_filename,
)
# Run the sync command to generate test data
engine_command = [
"sync",
"--config-file",
configuration.path,
test_data_path.joinpath("test_modify", "test_repo_url", "repos.txt"),
]
result = run_command(cmd=engine_command)
assert result.ok
assert pathlib.Path(configuration.data["LibrariesDB"]).exists()
# Verify the pre-command environment is as expected
def get_library_repo_url(name):
with pathlib.Path(configuration.data["LibrariesDB"]).open(mode="r", encoding="utf-8") as library_db_file:
library_db = json.load(fp=library_db_file)
for library in library_db["Libraries"]:
if library["Name"] == name:
return library["Repository"]
raise
def get_release_archive_url(name, version):
with pathlib.Path(configuration.data["LibrariesDB"]).open(mode="r", encoding="utf-8") as library_db_file:
library_db = json.load(fp=library_db_file)
for release in library_db["Releases"]:
if release["LibraryName"] == name and release["Version"] == version:
return release["URL"]
raise
assert old_git_clone_path.exists()
assert canary_git_clone_path.exists()
assert get_library_repo_url(name=name) != new_repo_url
assert get_library_repo_url(name=canary_name) == canary_repo_url
for release in releases:
assert old_library_release_archives_folder.joinpath(f"{sanitized_name}-{release}.zip").exists()
assert not new_library_release_archives_folder.joinpath(f"{sanitized_name}-{release}.zip").exists()
assert get_release_archive_url(name=name, version=release) == (
"{base}{host}/{owner}/{name}-{release}.zip".format(
base=configuration.data["BaseDownloadUrl"],
host=old_host,
owner=old_owner,
name=sanitized_name,
release=release,
)
)
assert canary_release_archive_path.exists()
assert get_release_archive_url(name=canary_name, version=canary_release) == canary_release_archive_url
# Run the repository URL modification command
engine_command = [
"modify",
"--config-file",
configuration.path,
"--repo-url",
new_repo_url,
name,
]
result = run_command(cmd=engine_command)
assert result.ok
# Verify the effect of the command was as expected
assert not old_git_clone_path.exists()
assert canary_release_archive_path.exists()
assert canary_git_clone_path.exists()
assert get_library_repo_url(name=name) == new_repo_url
assert get_library_repo_url(name=canary_name) == canary_repo_url
for release in releases:
assert not old_library_release_archives_folder.joinpath(f"{sanitized_name}-{release}.zip").exists()
assert new_library_release_archives_folder.joinpath(f"{sanitized_name}-{release}.zip").exists()
assert get_release_archive_url(name=name, version=release) == (
"{base}{host}/{owner}/{name}-{release}.zip".format(
base=configuration.data["BaseDownloadUrl"],
host=new_host,
owner=new_owner,
name=sanitized_name,
release=release,
)
)
assert canary_release_archive_path.exists()
assert get_release_archive_url(name=canary_name, version=canary_release) == canary_release_archive_url
def test_types(configuration, run_command):
"""Test the `--types` modification flag in action."""
name = "SpacebrewYun"
raw_old_types = "Arduino"
raw_new_types = "Arduino, Retired"
canary_name = "Arduino Uno WiFi Dev Ed Library"
raw_canary_types = "Partner"
# Run the sync command to generate test data
engine_command = [
"sync",
"--config-file",
configuration.path,
test_data_path.joinpath("test_modify", "test_types", "repos.txt"),
]
result = run_command(cmd=engine_command)
assert result.ok
assert pathlib.Path(configuration.data["LibrariesDB"]).exists()
def assert_types(name, raw_types):
with pathlib.Path(configuration.data["LibrariesDB"]).open(mode="r", encoding="utf-8") as library_db_file:
library_db = json.load(fp=library_db_file)
for release in library_db["Releases"]:
if release["LibraryName"] == name and release["Types"] != [
raw_type.strip() for raw_type in raw_types.split(sep=",")
]:
return False
return True
# Verify the pre-command DB is as expected
assert assert_types(name=name, raw_types=raw_old_types)
assert assert_types(name=canary_name, raw_types=raw_canary_types)
# Run the modification command with existing types
engine_command = [
"modify",
"--config-file",
configuration.path,
"--types",
raw_old_types,
name,
]
result = run_command(cmd=engine_command)
assert not result.ok
assert f"{name} already has types {raw_old_types}" in result.stderr
# Run the modification command with existing types
engine_command = [
"modify",
"--config-file",
configuration.path,
"--types",
raw_new_types,
name,
]
result = run_command(cmd=engine_command)
assert result.ok
# Verify the effect of the command was as expected
assert assert_types(name=name, raw_types=raw_new_types)
assert assert_types(name=canary_name, raw_types=raw_canary_types)