Skip to content

Commit 455ea5c

Browse files
committed
Make engine configuration fixture for integration tests
This will make writing tests easier and avoid code duplication.
1 parent e48f728 commit 455ea5c

File tree

2 files changed

+55
-51
lines changed

2 files changed

+55
-51
lines changed

test/conftest.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,46 @@
2929
import json
3030

3131

32+
@pytest.fixture
33+
def configuration(working_dir):
34+
"""Create a libraries-repository-engine configuration file and return an object containing its data and path."""
35+
working_dir_path = pathlib.Path(working_dir)
36+
37+
# This is based on the `Librariesv2` production job's config.
38+
data = {
39+
"BaseDownloadUrl": "https://downloads.arduino.cc/libraries/",
40+
"LibrariesFolder": working_dir_path.joinpath("libraries").as_posix(),
41+
"LogsFolder": working_dir_path.joinpath("ci-logs", "libraries", "logs").as_posix(),
42+
"LibrariesDB": working_dir_path.joinpath("db.json").as_posix(),
43+
"LibrariesIndex": working_dir_path.joinpath("libraries", "library_index.json").as_posix(),
44+
"GitClonesFolder": working_dir_path.joinpath("gitclones").as_posix(),
45+
# I was unable to get clamdscan working in the GitHub Actions runner, but the tests should pass with this set to
46+
# False when run on a machine with ClamAV installed.
47+
"DoNotRunClamav": True,
48+
# Arduino Lint should be installed under PATH
49+
"ArduinoLintPath": "",
50+
}
51+
52+
# Generate configuration file
53+
path = working_dir_path.joinpath("config.json")
54+
with path.open("w", encoding="utf-8") as configuration_file:
55+
json.dump(obj=data, fp=configuration_file, indent=2)
56+
57+
class Object:
58+
"""Container for libraries-repository-engine configuration data.
59+
60+
Keyword arguments:
61+
data -- dictionary of configuration data
62+
path -- path of the configuration file
63+
"""
64+
65+
def __init__(self, data, path):
66+
self.data = data
67+
self.path = path
68+
69+
return Object(data=data, path=path)
70+
71+
3272
@pytest.fixture(scope="function")
3373
def run_command(pytestconfig, working_dir) -> typing.Callable[..., invoke.runners.Result]:
3474
"""Provide a wrapper around invoke's `run` API so that every test will work in the same temporary folder.

test/test_sync.py

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,11 @@
3131
size_comparison_tolerance = 0.03 # Maximum allowed archive size difference ratio
3232

3333

34-
def test_sync(run_command, working_dir):
35-
working_dir_path = pathlib.Path(working_dir)
36-
configuration = {
37-
"BaseDownloadUrl": "http://www.example.com/libraries/",
38-
"LibrariesFolder": working_dir_path.joinpath("libraries").as_posix(),
39-
"LogsFolder": working_dir_path.joinpath("logs").as_posix(),
40-
"LibrariesDB": working_dir_path.joinpath("libraries_db.json").as_posix(),
41-
"LibrariesIndex": working_dir_path.joinpath("libraries", "library_index.json").as_posix(),
42-
"GitClonesFolder": working_dir_path.joinpath("gitclones").as_posix(),
43-
# I was unable to get clamdscan working in the GitHub Actions runner, but the tests should pass with this set to
44-
# False when run on a machine with ClamAV installed.
45-
"DoNotRunClamav": True,
46-
# Arduino Lint should be installed under PATH
47-
"ArduinoLintPath": "",
48-
}
49-
50-
# Generate configuration file
51-
with working_dir_path.joinpath("config.json").open("w", encoding="utf-8") as configuration_file:
52-
json.dump(obj=configuration, fp=configuration_file, indent=2)
53-
34+
def test_sync(configuration, run_command):
5435
libraries_repository_engine_command = [
5536
"sync",
5637
"--config-file",
57-
working_dir_path.joinpath("config.json"),
38+
configuration.path,
5839
test_data_path.joinpath("test_sync", "repos.txt"),
5940
]
6041

@@ -63,38 +44,38 @@ def test_sync(run_command, working_dir):
6344
assert result.ok
6445

6546
# Test fresh output
66-
check_libraries(configuration=configuration)
47+
check_libraries(configuration=configuration.data)
6748
check_logs(
68-
configuration=configuration,
49+
configuration=configuration.data,
6950
golden_logs_parent_path=test_data_path.joinpath("test_sync", "golden", "logs", "generate"),
7051
logs_subpath=pathlib.Path("github.com", "arduino-libraries", "ArduinoCloudThing", "index.html"),
7152
)
7253
check_logs(
73-
configuration=configuration,
54+
configuration=configuration.data,
7455
golden_logs_parent_path=test_data_path.joinpath("test_sync", "golden", "logs", "generate"),
7556
logs_subpath=pathlib.Path("github.com", "arduino-libraries", "SpacebrewYun", "index.html"),
7657
)
77-
check_db(configuration=configuration)
78-
check_index(configuration=configuration)
58+
check_db(configuration=configuration.data)
59+
check_index(configuration=configuration.data)
7960

8061
# Run the engine again
8162
result = run_command(cmd=libraries_repository_engine_command)
8263
assert result.ok
8364

8465
# Test the updated output
85-
check_libraries(configuration=configuration)
66+
check_libraries(configuration=configuration.data)
8667
check_logs(
87-
configuration=configuration,
68+
configuration=configuration.data,
8869
golden_logs_parent_path=test_data_path.joinpath("test_sync", "golden", "logs", "update"),
8970
logs_subpath=pathlib.Path("github.com", "arduino-libraries", "ArduinoCloudThing", "index.html"),
9071
)
9172
check_logs(
92-
configuration=configuration,
73+
configuration=configuration.data,
9374
golden_logs_parent_path=test_data_path.joinpath("test_sync", "golden", "logs", "update"),
9475
logs_subpath=pathlib.Path("github.com", "arduino-libraries", "SpacebrewYun", "index.html"),
9576
)
96-
check_db(configuration=configuration)
97-
check_index(configuration=configuration)
77+
check_db(configuration=configuration.data)
78+
check_index(configuration=configuration.data)
9879

9980

10081
def check_libraries(configuration):
@@ -276,28 +257,11 @@ def check_index(configuration):
276257

277258

278259
# The engine's Git code struggles to get a clean checkout of releases under some circumstances.
279-
def test_clean_checkout(run_command, working_dir):
280-
working_dir_path = pathlib.Path(working_dir)
281-
configuration = {
282-
"BaseDownloadUrl": "http://www.example.com/libraries/",
283-
"LibrariesFolder": working_dir_path.joinpath("libraries").as_posix(),
284-
"LogsFolder": working_dir_path.joinpath("logs").as_posix(),
285-
"LibrariesDB": working_dir_path.joinpath("libraries_db.json").as_posix(),
286-
"LibrariesIndex": working_dir_path.joinpath("libraries", "library_index.json").as_posix(),
287-
"GitClonesFolder": working_dir_path.joinpath("gitclones").as_posix(),
288-
"DoNotRunClamav": True,
289-
# Arduino Lint should be installed under PATH
290-
"ArduinoLintPath": "",
291-
}
292-
293-
# Generate configuration file
294-
with working_dir_path.joinpath("config.json").open("w", encoding="utf-8") as configuration_file:
295-
json.dump(obj=configuration, fp=configuration_file, indent=2)
296-
260+
def test_clean_checkout(configuration, run_command):
297261
libraries_repository_engine_command = [
298262
"sync",
299263
"--config-file",
300-
working_dir_path.joinpath("config.json"),
264+
configuration.path,
301265
test_data_path.joinpath("test_clean_checkout", "repos.txt"),
302266
]
303267

@@ -306,7 +270,7 @@ def test_clean_checkout(run_command, working_dir):
306270
assert result.ok
307271

308272
# Load generated index
309-
with pathlib.Path(configuration["LibrariesIndex"]).open(mode="r", encoding="utf-8") as library_index_file:
273+
with pathlib.Path(configuration.data["LibrariesIndex"]).open(mode="r", encoding="utf-8") as library_index_file:
310274
library_index = json.load(fp=library_index_file)
311275

312276
for release in library_index["libraries"]:

0 commit comments

Comments
 (0)