forked from spinalcordtoolbox/spinalcordtoolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
80 lines (60 loc) · 2.49 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
#############################################################################
#
# Configure tests so that pytest downloads testing data every time
# pytest is run from the sct directory
#
# ----------------------------------------------------------------------------
# Copyright (c) 2020 Polytechnique Montreal <www.neuro.polymtl.ca>
# Author: Julien Cohen-Adad, Chris Hammill
#
# About the license: see the file LICENSE.TXT
###############################################################################
import sys
import os
import logging
from typing import Mapping
from hashlib import md5
import pytest
from spinalcordtoolbox.utils.sys import sct_dir_local_path, sct_test_path
from spinalcordtoolbox.scripts import sct_download_data as downloader
logger = logging.getLogger(__name__)
def pytest_sessionstart():
""" Download sct_testing_data prior to test collection. """
logger.info("Downloading sct test data")
downloader.main(['-d', 'sct_testing_data', '-o', sct_test_path()])
@pytest.fixture(scope="session", autouse=True)
def test_data_integrity(request):
files_checksums = dict()
for root, _, files in os.walk(sct_test_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 = []
for root, _, files in os.walk(sct_test_path()):
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 sct_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