-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.py
More file actions
120 lines (104 loc) · 3.37 KB
/
Copy pathrunner.py
File metadata and controls
120 lines (104 loc) · 3.37 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
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
"""Drive pytest as a subprocess with a fixed test order; parse per-test outcomes."""
from __future__ import annotations
import os
import re
import subprocess
import sys
from dataclasses import dataclass
from pathlib import Path
# pytest -v line format: "<nodeid> PASSED [ 12%]"
_OUTCOME_RE = re.compile(
r"^(?P<nodeid>\S+?)\s+(?P<outcome>PASSED|FAILED|ERROR|SKIPPED|XFAIL|XPASS)\b"
)
# `pytest --collect-only -q` emits summary lines we want to ignore.
_COLLECT_SUMMARY_RE = re.compile(r"^\d+\s+tests?\s+collected", re.IGNORECASE)
@dataclass
class RunResult:
outcomes: dict[str, str]
exit_code: int
stdout: str
stderr: str
def _plugin_pythonpath() -> str:
"""Path to the directory containing the `flake_bisect` package."""
return str(Path(__file__).resolve().parent.parent)
def _env_with_plugin() -> dict[str, str]:
env = dict(os.environ)
extra = _plugin_pythonpath()
existing = env.get("PYTHONPATH", "")
env["PYTHONPATH"] = extra + (os.pathsep + existing if existing else "")
return env
def collect_tests(rootdir: str, testpaths: list[str]) -> list[str]:
"""Return all pytest nodeids under the given paths, in collection order."""
cmd = [
sys.executable,
"-m",
"pytest",
f"--rootdir={rootdir}",
"--collect-only",
"-q",
"--no-header",
"-p",
"no:cacheprovider",
*testpaths,
]
proc = subprocess.run(
cmd, cwd=rootdir, capture_output=True, text=True, env=_env_with_plugin()
)
if proc.returncode not in (0, 5):
raise RuntimeError(
f"pytest --collect-only failed (exit {proc.returncode}):\n"
f"STDOUT:\n{proc.stdout}\nSTDERR:\n{proc.stderr}"
)
nodeids: list[str] = []
for raw in proc.stdout.splitlines():
line = raw.strip()
if not line:
continue
if _COLLECT_SUMMARY_RE.match(line):
continue
if line.startswith(("=", "_", "warnings", "no tests")):
continue
# nodeids always contain a path with .py and usually `::`
if "::" not in line and not line.endswith(".py"):
continue
nodeids.append(line)
return nodeids
def run_ordered(
rootdir: str, ordered_ids: list[str], extra_args: list[str] | None = None
) -> RunResult:
"""Run pytest forcing the given order; return per-test outcomes."""
env = _env_with_plugin()
env["FLAKE_BISECT_ORDER"] = "\n".join(ordered_ids)
cmd = [
sys.executable,
"-m",
"pytest",
f"--rootdir={rootdir}",
"-p",
"flake_bisect._order_plugin",
"-p",
"no:cacheprovider",
"-v",
"--tb=no",
"--no-header",
"-rN",
*(extra_args or []),
*ordered_ids,
]
proc = subprocess.run(
cmd, cwd=rootdir, capture_output=True, text=True, env=env
)
outcomes: dict[str, str] = {}
for raw in proc.stdout.splitlines():
m = _OUTCOME_RE.match(raw.strip())
if m:
outcomes[m.group("nodeid")] = m.group("outcome")
return RunResult(
outcomes=outcomes,
exit_code=proc.returncode,
stdout=proc.stdout,
stderr=proc.stderr,
)
def target_failed(result: RunResult, target: str) -> bool:
"""True iff `target` was reported as FAILED or ERROR in the run."""
return result.outcomes.get(target) in ("FAILED", "ERROR")