forked from data-apis/array-api-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
208 lines (190 loc) · 7.2 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from functools import lru_cache
from pathlib import Path
import warnings
import os
from hypothesis import settings
from pytest import mark
from array_api_tests import _array_module as xp
from array_api_tests import api_version
from array_api_tests._array_module import _UndefinedStub
from reporting import pytest_metadata, pytest_json_modifyreport, add_extra_json_metadata # noqa
settings.register_profile("xp_default", deadline=800)
def pytest_addoption(parser):
# Hypothesis max examples
# See https://github.com/HypothesisWorks/hypothesis/issues/2434
parser.addoption(
"--hypothesis-max-examples",
"--max-examples",
action="store",
default=None,
help="set the Hypothesis max_examples setting",
)
# Hypothesis deadline
parser.addoption(
"--hypothesis-disable-deadline",
"--disable-deadline",
action="store_true",
help="disable the Hypothesis deadline",
)
# disable extensions
parser.addoption(
"--disable-extension",
metavar="ext",
nargs="+",
default=[],
help="disable testing for Array API extension(s)",
)
# data-dependent shape
parser.addoption(
"--disable-data-dependent-shapes",
"--disable-dds",
action="store_true",
help="disable testing functions with output shapes dependent on input",
)
# CI
parser.addoption(
"--ci",
action="store_true",
help="run just the tests appropriate for CI",
)
parser.addoption(
"--skips-file",
action="store",
help="file with tests to skip. Defaults to skips.txt"
)
parser.addoption(
"--xfails-file",
action="store",
help="file with tests to skip. Defaults to xfails.txt"
)
def pytest_configure(config):
config.addinivalue_line(
"markers", "xp_extension(ext): tests an Array API extension"
)
config.addinivalue_line(
"markers", "data_dependent_shapes: output shapes are dependent on inputs"
)
config.addinivalue_line("markers", "ci: primary test")
config.addinivalue_line(
"markers",
"min_version(api_version): run when greater or equal to api_version",
)
# Hypothesis
hypothesis_max_examples = config.getoption("--hypothesis-max-examples")
disable_deadline = config.getoption("--hypothesis-disable-deadline")
profile_settings = {}
if hypothesis_max_examples is not None:
profile_settings["max_examples"] = int(hypothesis_max_examples)
if disable_deadline:
profile_settings["deadline"] = None
if profile_settings:
settings.register_profile("xp_override", **profile_settings)
settings.load_profile("xp_override")
else:
settings.load_profile("xp_default")
@lru_cache
def xp_has_ext(ext: str) -> bool:
try:
return not isinstance(getattr(xp, ext), _UndefinedStub)
except AttributeError:
return False
def pytest_collection_modifyitems(config, items):
skips_file = skips_path = config.getoption('--skips-file')
if skips_file is None:
skips_file = Path(__file__).parent / "skips.txt"
if skips_file.exists():
skips_path = skips_file
skip_ids = []
if skips_path:
with open(os.path.expanduser(skips_path)) as f:
for line in f:
if line.startswith("array_api_tests"):
id_ = line.strip("\n")
skip_ids.append(id_)
xfails_file = xfails_path = config.getoption('--xfails-file')
if xfails_file is None:
xfails_file = Path(__file__).parent / "xfails.txt"
if xfails_file.exists():
xfails_path = xfails_file
xfail_ids = []
if xfails_path:
with open(os.path.expanduser(xfails_path)) as f:
for line in f:
if not line.strip() or line.startswith('#'):
continue
id_ = line.strip("\n")
xfail_ids.append(id_)
skip_id_matched = {id_: False for id_ in skip_ids}
xfail_id_matched = {id_: False for id_ in xfail_ids}
disabled_exts = config.getoption("--disable-extension")
disabled_dds = config.getoption("--disable-data-dependent-shapes")
ci = config.getoption("--ci")
for item in items:
markers = list(item.iter_markers())
# skip if specified in skips file
for id_ in skip_ids:
if id_ in item.nodeid:
item.add_marker(mark.skip(reason=f"--skips-file ({skips_file})"))
skip_id_matched[id_] = True
break
# xfail if specified in xfails file
for id_ in xfail_ids:
if id_ in item.nodeid:
item.add_marker(mark.xfail(reason=f"--xfails-file ({xfails_file})"))
xfail_id_matched[id_] = True
break
# skip if disabled or non-existent extension
ext_mark = next((m for m in markers if m.name == "xp_extension"), None)
if ext_mark is not None:
ext = ext_mark.args[0]
if ext in disabled_exts:
item.add_marker(
mark.skip(reason=f"{ext} disabled in --disable-extensions")
)
elif not xp_has_ext(ext):
item.add_marker(mark.skip(reason=f"{ext} not found in array module"))
# skip if disabled by dds flag
if disabled_dds:
for m in markers:
if m.name == "data_dependent_shapes":
item.add_marker(
mark.skip(reason="disabled via --disable-data-dependent-shapes")
)
break
# skip if test not appropriate for CI
if ci:
ci_mark = next((m for m in markers if m.name == "ci"), None)
if ci_mark is None:
item.add_marker(mark.skip(reason="disabled via --ci"))
# skip if test is for greater api_version
ver_mark = next((m for m in markers if m.name == "min_version"), None)
if ver_mark is not None:
min_version = ver_mark.args[0]
if api_version < min_version:
item.add_marker(
mark.skip(
reason=f"requires ARRAY_API_TESTS_VERSION=>{min_version}"
)
)
bad_ids_end_msg = (
"Note the relevant tests might not of been collected by pytest, or "
"another specified id might have already matched a test."
)
bad_skip_ids = [id_ for id_, matched in skip_id_matched.items() if not matched]
if bad_skip_ids:
f_bad_ids = "\n".join(f" {id_}" for id_ in bad_skip_ids)
warnings.warn(
f"{len(bad_skip_ids)} ids in skips file don't match any collected tests: \n"
f"{f_bad_ids}\n"
f"(skips file: {skips_file})\n"
f"{bad_ids_end_msg}"
)
bad_xfail_ids = [id_ for id_, matched in xfail_id_matched.items() if not matched]
if bad_xfail_ids:
f_bad_ids = "\n".join(f" {id_}" for id_ in bad_xfail_ids)
warnings.warn(
f"{len(bad_xfail_ids)} ids in xfails file don't match any collected tests: \n"
f"{f_bad_ids}\n"
f"(xfails file: {xfails_file})\n"
f"{bad_ids_end_msg}"
)