forked from dials/dials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
109 lines (83 loc) · 3.09 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
106
107
108
109
#
# 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
if sys.version_info[:2] == (3, 7) and sys.platform == "darwin":
multiprocessing.set_start_method("forkserver")
collect_ignore = []
def _build_filterwarnings_string() -> str:
"""
Given the set of active warnings, build a PYTHONWARNINGS string.
This lets us set the PYTHONWARNINGS environment variable so that
warning filters are passed down to subprocesses.
"""
filter_parts = []
for action, regex, category, modregex, line in warnings.filters:
if action != "ignore":
continue
if category.__module__ != "builtins":
continue
this_action = [
action,
regex.pattern if regex else "",
category.__name__,
]
if modregex is not None:
this_action.append(modregex.pattern)
if line:
this_action.append(line)
filter_parts.append(":".join(str(x) for x in this_action))
return ",".join(filter_parts)
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
# Ensure that subprocesses get the warnings filters
os.environ["PYTHONWARNINGS"] = _build_filterwarnings_string()
@pytest.fixture(scope="session")
def dials_regression() -> Path:
"""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 Path(os.environ["DIALS_REGRESSION"].rstrip("/"))
try:
import dials_regression as dr
return Path(dr.__file__).parent
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 Path(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)