-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_tests.py
More file actions
executable file
·86 lines (69 loc) · 2.26 KB
/
run_tests.py
File metadata and controls
executable file
·86 lines (69 loc) · 2.26 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
#!/usr/bin/env python
from __future__ import annotations
import datetime as dt
from logging import getLogger
from subprocess import CalledProcessError, check_call
from tomllib import TOMLDecodeError, loads
from typing import TYPE_CHECKING
from utilities.logging import basic_config
from utilities.os import temp_environ
from utilities.pathlib import get_repo_root
from utilities.re import extract_group
from utilities.time import sleep
if TYPE_CHECKING:
from pathlib import Path
_LOGGER = getLogger(__name__)
def main() -> None:
basic_config(obj=_LOGGER)
path = get_repo_root().joinpath("src", "tests")
for path_i in sorted(path.glob("test_*.py")):
_run_test(path_i)
def _run_test(path: Path, /) -> None:
group = _get_group(path)
marker = _get_marker(group)
if marker.exists():
return
_LOGGER.info("Testing %r...", str(path))
while True:
if _run_command(path):
marker.touch()
return
sleep(1)
def _get_group(path: Path, /) -> str:
return extract_group(r"^test_(\w+)$", path.stem).replace("_", "-")
def _get_marker(group: str, /) -> Path:
hour = dt.datetime.now(dt.UTC).replace(minute=0, second=0, microsecond=0)
return get_repo_root().joinpath(".pytest_cache", f"{hour:%Y%m%dT%H}-{group}")
def _run_command(path: Path, /) -> bool:
cmd: list[str] = [
"uv",
"run",
"--only-group=core",
"--only-group=hypothesis",
"--only-group=pytest",
"--isolated",
"--managed-python",
]
text = get_repo_root().joinpath("pyproject.toml").read_text()
try:
loaded = loads(text)
except TOMLDecodeError:
_LOGGER.exception("Invalid TOML document")
return False
groups: list[str] = loaded["dependency-groups"]
if (group := _get_group(path)) in groups:
cmd.append(f"--only-group={group}")
if (test := f"{group}-test") in groups:
cmd.append(f"--only-group={test}")
cmd.extend(["pytest", "-nauto", str(path)])
with temp_environ(PYTEST_ADDOPTS=None):
try:
code = check_call(cmd)
except CalledProcessError:
return False
if code == 0:
return True
_LOGGER.error("pytest failed")
return False
if __name__ == "__main__":
main()