-
Notifications
You must be signed in to change notification settings - Fork 1
/
conftest.py
105 lines (82 loc) · 3.1 KB
/
conftest.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
###############################################################################
# configure testing suite #
###############################################################################
import os
import logging
from pathlib import Path
import pytest
from typing import Mapping
from hashlib import md5
from fsleyes_plugin_shimming_toolbox import __dir_testing__
from shimmingtoolbox.cli.download_data import download_data
logger = logging.getLogger(__name__)
def test_data_path():
# Get the directory where this current file is saved
repo_path = Path(__file__).resolve().parent
# create ./testing_data folder
test_path = repo_path / __dir_testing__
if not test_path.exists():
test_path.mkdir()
return test_path
@pytest.fixture
def test_data_path_fixture():
return test_data_path()
def pytest_sessionstart():
"""Download shimming-toolbox testing_data prior to test collection."""
logger.info("Downloading shimming-toolbox test data")
test_data_location = test_data_path()
print(test_data_location)
try:
download_data(
[
'--verbose',
'--output',
test_data_location,
'testing_data',
]
)
# click sends a SystemExit upon command completion that needs to be caught.
except SystemExit:
return
logger.info("Test data download complete. Beginning testing session...")
@pytest.fixture(scope="session", autouse=True)
def test_data_integrity(request):
files_checksums = dict()
for root, _, files in os.walk(test_data_path()):
for f in files:
fname = os.path.join(root, f)
chksum = checksum(fname)
files_checksums[fname] = chksum
request.addfinalizer(lambda: check_testing_data_integrity(files_checksums))
def checksum(fname: os.PathLike) -> str:
with open(fname, 'rb') as f:
data = f.read()
return md5(data).hexdigest()
def check_testing_data_integrity(files_checksums: Mapping[os.PathLike, str]):
changed = []
new = []
missing = []
after = []
test_data_location = test_data_path()
for root, _, files in os.walk(test_data_location):
for f in files:
fname = os.path.join(root, f)
chksum = checksum(fname)
after.append(fname)
if fname not in files_checksums:
logger.warning(
f"Discovered new file in testing_data that didn't exist before: {(fname, chksum)}"
)
new.append((fname, chksum))
elif files_checksums[fname] != chksum:
logger.error(
f"Checksum mismatch for test data: {fname}. Got {chksum} instead of {files_checksums[fname]}"
)
changed.append((fname, chksum))
for fname, chksum in files_checksums.items():
if fname not in after:
logger.error(f"Test data missing after test:a: {fname}")
missing.append((fname, chksum))
assert not changed
# assert not new
assert not missing