forked from dials/dials
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conftest.py
99 lines (75 loc) · 2.83 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
#
# See https://github.com/dials/dials/wiki/pytest for documentation on how to
# write and run pytest tests, and an overview of the available features.
#
from __future__ import annotations
import multiprocessing
import os
import sys
import warnings
from pathlib import Path
import pytest
from _pytest.outcomes import Skipped
# https://stackoverflow.com/a/40846742
warnings.filterwarnings("ignore", message="numpy.dtype size changed")
warnings.filterwarnings("ignore", message="numpy.ufunc size changed")
if sys.version_info[:2] == (3, 7) and sys.platform == "darwin":
multiprocessing.set_start_method("forkserver")
collect_ignore = []
def pytest_configure(config):
if not config.pluginmanager.hasplugin("dials_data"):
@pytest.fixture(scope="session")
def dials_data():
pytest.skip("This test requires the dials_data package to be installed")
globals()["dials_data"] = dials_data
config.addinivalue_line(
"markers", "xfel: Mark test to run xfail if xfel module is missing"
)
def pytest_collection_modifyitems(config, items):
# Attempt to import xfel
try:
import xfel # noqa: F401
except (Skipped, ModuleNotFoundError):
# We don't have XFEL
xfail_marker = pytest.mark.xfail(reason="XFEL module not present")
for item in items:
if item.get_closest_marker("xfel"):
item.add_marker(xfail_marker)
@pytest.fixture(scope="session")
def dials_regression():
"""Return the absolute path to the dials_regression module as a string.
Skip the test if dials_regression is not installed."""
if "DIALS_REGRESSION" in os.environ:
return os.environ["DIALS_REGRESSION"]
try:
import dials_regression as dr
return os.path.dirname(dr.__file__)
except ImportError:
pass # dials_regression not configured
try:
import socket
reference_copy = "/dls/science/groups/scisoft/DIALS/repositories/git-reference/dials_regression"
if (
os.name == "posix"
and socket.gethostname().endswith(".diamond.ac.uk")
and os.path.exists(reference_copy)
):
return reference_copy
except ImportError:
pass # Cannot tell whether in DLS network or not
pytest.skip("dials_regression required for this test")
@pytest.fixture
def run_in_tmp_path(tmp_path) -> Path:
"""
A fixture to change the working directory for the test to a temporary directory.
The original working directory is restored upon teardown of the fixture.
Args:
tmp_path: Pytest tmp_path fixture, see
https://docs.pytest.org/en/latest/how-to/tmp_path.html
Yields:
The path to the temporary working directory defined by tmp_path.
"""
cwd = Path.cwd()
os.chdir(tmp_path)
yield tmp_path
os.chdir(cwd)