-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
91 lines (79 loc) · 2.69 KB
/
Copy pathconftest.py
File metadata and controls
91 lines (79 loc) · 2.69 KB
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
from __future__ import annotations
import warnings
from collections.abc import Sequence
from pathlib import Path
import pytest
def pytest_addoption(parser: pytest.Parser) -> None:
group = parser.getgroup("binsparse-tests")
group.addoption(
"--skips-file",
action="append",
default=None,
metavar="FILE",
help=(
"file containing test ID substrings to skip; may be repeated to merge "
"files (default: skips.txt)"
),
)
group.addoption(
"--xfails-file",
action="append",
default=None,
metavar="FILE",
help=(
"file containing test ID substrings to xfail; may be repeated to merge "
"files (default: fails.txt)"
),
)
def _load_patterns(
filenames: Sequence[str] | None,
default_name: str,
) -> dict[str, Path]:
files = [Path(filename).expanduser() for filename in filenames or ()]
if not files:
default_file = Path(__file__).parent / default_name
if default_file.exists():
files = [default_file]
patterns: dict[str, Path] = {}
for file in files:
with file.open(encoding="utf-8") as stream:
for line in stream:
pattern = line.strip()
if pattern and not pattern.startswith("#"):
patterns[pattern] = file
return patterns
def _apply_patterns(
items: list[pytest.Item],
patterns: dict[str, Path],
marker: pytest.MarkDecorator,
option: str,
) -> None:
matched = dict.fromkeys(patterns, False)
for item in items:
for pattern, source in patterns.items():
if pattern in item.nodeid:
item.add_marker(marker(reason=f"{option} ({source})"))
matched[pattern] = True
break
unmatched = [
pattern for pattern, was_matched in matched.items() if not was_matched
]
if unmatched:
entries = "\n".join(
f" {pattern} ({patterns[pattern]})" for pattern in unmatched
)
warnings.warn(
pytest.PytestWarning(
f"{len(unmatched)} pattern(s) from {option} files did not match any "
f"collected tests:\n{entries}"
),
stacklevel=1,
)
def pytest_collection_modifyitems(
config: pytest.Config,
items: list[pytest.Item],
) -> None:
skip_patterns = _load_patterns(config.getoption("--skips-file"), "skips.txt")
xfail_patterns = _load_patterns(config.getoption("--xfails-file"), "fails.txt")
_apply_patterns(items, skip_patterns, pytest.mark.skip, "--skips-file")
_apply_patterns(items, xfail_patterns, pytest.mark.xfail, "--xfails-file")