invaris 1.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- invaris/__init__.py +3 -0
- invaris/_plugins/_worktree_isolate.py +183 -0
- invaris/_plugins/invaris_gate1_plugin.py +319 -0
- invaris/adapters/__init__.py +31 -0
- invaris/adapters/base.py +232 -0
- invaris/adapters/pytest_cov.py +46 -0
- invaris/adapters/ruff.py +299 -0
- invaris/adapters/tach.py +186 -0
- invaris/adapters/typecheck.py +210 -0
- invaris/artifacts.py +287 -0
- invaris/ast_mutator.py +475 -0
- invaris/base_probe.py +201 -0
- invaris/cli.py +688 -0
- invaris/contract.py +659 -0
- invaris/coverage_sqlite.py +280 -0
- invaris/dedup.py +233 -0
- invaris/evidence/__init__.py +1 -0
- invaris/evidence/caches.py +611 -0
- invaris/evidence/gc.py +471 -0
- invaris/evidence/runs.py +456 -0
- invaris/evidence/schema.py +336 -0
- invaris/evidence/store.py +111 -0
- invaris/evidence_db.py +42 -0
- invaris/evidence_query.py +378 -0
- invaris/final_status.py +105 -0
- invaris/funnel.py +377 -0
- invaris/gate0.py +559 -0
- invaris/gate1/__init__.py +1 -0
- invaris/gate1/base_probe_exec.py +549 -0
- invaris/gate1/base_probe_plan.py +325 -0
- invaris/gate1/coverage.py +357 -0
- invaris/gate1/execution.py +532 -0
- invaris/gate1/runner.py +542 -0
- invaris/gate1/selection.py +266 -0
- invaris/gate2/__init__.py +1 -0
- invaris/gate2/execution.py +422 -0
- invaris/gate2/planning.py +359 -0
- invaris/gate2/reporting.py +319 -0
- invaris/gate2/runner.py +478 -0
- invaris/harness_sync.py +563 -0
- invaris/head_baseline.py +638 -0
- invaris/import_graph.py +287 -0
- invaris/integrity.py +678 -0
- invaris/layout.py +124 -0
- invaris/mcp_server.py +385 -0
- invaris/mutator.py +67 -0
- invaris/paths.py +105 -0
- invaris/primitives.py +145 -0
- invaris/pytest_targets.py +386 -0
- invaris/snapshot.py +407 -0
- invaris/staged_worktree.py +228 -0
- invaris/test_ast_diff.py +477 -0
- invaris/verdict.py +250 -0
- invaris/verdict_assembly.py +313 -0
- invaris-1.4.0.dist-info/METADATA +315 -0
- invaris-1.4.0.dist-info/RECORD +59 -0
- invaris-1.4.0.dist-info/WHEEL +4 -0
- invaris-1.4.0.dist-info/entry_points.txt +2 -0
- invaris-1.4.0.dist-info/licenses/LICENSE +21 -0
invaris/__init__.py
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"""Stdlib-only worktree import isolation for `sitecustomize.py` and `invaris_gate1_plugin.py`.
|
|
2
|
+
|
|
3
|
+
Prevents editable installs (`.pth` and PEP 660 `sys.meta_path` finders) from leaking candidate
|
|
4
|
+
working-tree modules into isolated `HEAD` worktree pytest runs.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import importlib
|
|
10
|
+
import os
|
|
11
|
+
import sys
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from typing import Any
|
|
14
|
+
|
|
15
|
+
_VENV_PATH_PARTS = frozenset({".venv", "venv", "site-packages", "dist-packages"})
|
|
16
|
+
_STDLIB_IMPORTER_MODS = frozenset({"_frozen_importlib", "_frozen_importlib_external"})
|
|
17
|
+
_SELF_MODULE_NAMES = frozenset({"sitecustomize", "_worktree_isolate", "invaris_gate1_plugin"})
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def is_candidate_source_path(
|
|
21
|
+
raw_path: str,
|
|
22
|
+
cand_root: Path,
|
|
23
|
+
wt_root: Path | None,
|
|
24
|
+
plugin_dir: Path | None,
|
|
25
|
+
) -> bool:
|
|
26
|
+
if not raw_path:
|
|
27
|
+
return False
|
|
28
|
+
try:
|
|
29
|
+
resolved = Path(raw_path).resolve()
|
|
30
|
+
except OSError:
|
|
31
|
+
return False
|
|
32
|
+
if wt_root is not None and resolved.is_relative_to(wt_root):
|
|
33
|
+
return False
|
|
34
|
+
if plugin_dir is not None and resolved.is_relative_to(plugin_dir):
|
|
35
|
+
return False
|
|
36
|
+
if not resolved.is_relative_to(cand_root):
|
|
37
|
+
return False
|
|
38
|
+
return not any(part in _VENV_PATH_PARTS for part in resolved.relative_to(cand_root).parts)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _iter_path_strings(obj: Any, depth: int = 0) -> list[str]:
|
|
42
|
+
if depth > 2 or obj is None:
|
|
43
|
+
return []
|
|
44
|
+
if isinstance(obj, Path):
|
|
45
|
+
return [str(obj)]
|
|
46
|
+
if isinstance(obj, str):
|
|
47
|
+
return [obj] if (os.sep in obj or (os.altsep and os.altsep in obj)) else []
|
|
48
|
+
if isinstance(obj, dict):
|
|
49
|
+
out: list[str] = []
|
|
50
|
+
for val in obj.values():
|
|
51
|
+
out.extend(_iter_path_strings(val, depth + 1))
|
|
52
|
+
return out
|
|
53
|
+
if isinstance(obj, (list, tuple, set, frozenset)):
|
|
54
|
+
out = []
|
|
55
|
+
for item in obj:
|
|
56
|
+
out.extend(_iter_path_strings(item, depth + 1))
|
|
57
|
+
return out
|
|
58
|
+
return []
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _finder_targets_candidate(
|
|
62
|
+
finder: Any,
|
|
63
|
+
cand_root: Path,
|
|
64
|
+
wt_root: Path | None,
|
|
65
|
+
plugin_dir: Path | None,
|
|
66
|
+
) -> bool:
|
|
67
|
+
mod_name = str(getattr(finder, "__module__", "") or "")
|
|
68
|
+
type_mod = str(getattr(type(finder), "__module__", "") or "")
|
|
69
|
+
if mod_name in _STDLIB_IMPORTER_MODS or type_mod in _STDLIB_IMPORTER_MODS:
|
|
70
|
+
return False
|
|
71
|
+
values: list[Any] = []
|
|
72
|
+
for owner in (finder, type(finder)):
|
|
73
|
+
try:
|
|
74
|
+
values.extend(vars(owner).values())
|
|
75
|
+
except TypeError:
|
|
76
|
+
pass
|
|
77
|
+
slots = getattr(owner, "__slots__", ())
|
|
78
|
+
if isinstance(slots, str):
|
|
79
|
+
slots = (slots,)
|
|
80
|
+
for slot in slots:
|
|
81
|
+
val = getattr(finder, slot, None)
|
|
82
|
+
if val is not None:
|
|
83
|
+
values.append(val)
|
|
84
|
+
for val in values:
|
|
85
|
+
for cand_str in _iter_path_strings(val):
|
|
86
|
+
if is_candidate_source_path(cand_str, cand_root, wt_root, plugin_dir):
|
|
87
|
+
return True
|
|
88
|
+
return False
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _isolation_roots() -> tuple[Path | None, Path | None, Path | None]:
|
|
92
|
+
cand_env = os.environ.get("INVARIS_CANDIDATE_ROOT", "")
|
|
93
|
+
if not cand_env:
|
|
94
|
+
return None, None, None
|
|
95
|
+
wt_env = os.environ.get("INVARIS_WORKTREE_ROOT", "")
|
|
96
|
+
plugin_env = os.environ.get("INVARIS_PLUGIN_DIR", "")
|
|
97
|
+
try:
|
|
98
|
+
cand_root = Path(cand_env).resolve()
|
|
99
|
+
wt_root = Path(wt_env).resolve() if wt_env else None
|
|
100
|
+
plugin_dir = Path(plugin_env).resolve() if plugin_env else None
|
|
101
|
+
except OSError:
|
|
102
|
+
return None, None, None
|
|
103
|
+
return cand_root, wt_root, plugin_dir
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _module_leak_path(
|
|
107
|
+
mod: Any,
|
|
108
|
+
cand_root: Path,
|
|
109
|
+
wt_root: Path | None,
|
|
110
|
+
plugin_dir: Path | None,
|
|
111
|
+
) -> str | None:
|
|
112
|
+
mfile = getattr(mod, "__file__", None)
|
|
113
|
+
if isinstance(mfile, str) and is_candidate_source_path(mfile, cand_root, wt_root, plugin_dir):
|
|
114
|
+
return mfile
|
|
115
|
+
mpath = getattr(mod, "__path__", None)
|
|
116
|
+
if mpath is not None:
|
|
117
|
+
try:
|
|
118
|
+
paths = list(mpath)
|
|
119
|
+
except TypeError:
|
|
120
|
+
paths = []
|
|
121
|
+
for p in paths:
|
|
122
|
+
if isinstance(p, str) and is_candidate_source_path(p, cand_root, wt_root, plugin_dir):
|
|
123
|
+
return p
|
|
124
|
+
return None
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def enforce_worktree_isolation() -> None:
|
|
128
|
+
"""Prune candidate-root `sys.path` entries and PEP 660 `sys.meta_path` editable finders."""
|
|
129
|
+
cand_root, wt_root, plugin_dir = _isolation_roots()
|
|
130
|
+
if cand_root is None:
|
|
131
|
+
return
|
|
132
|
+
|
|
133
|
+
pruned = [
|
|
134
|
+
entry
|
|
135
|
+
for entry in sys.path
|
|
136
|
+
if not (entry and is_candidate_source_path(entry, cand_root, wt_root, plugin_dir))
|
|
137
|
+
]
|
|
138
|
+
front: list[str] = []
|
|
139
|
+
if wt_root is not None:
|
|
140
|
+
if (wt_root / "src").is_dir():
|
|
141
|
+
front.append(str(wt_root / "src"))
|
|
142
|
+
front.append(str(wt_root))
|
|
143
|
+
|
|
144
|
+
final_path: list[str] = []
|
|
145
|
+
seen: set[str] = set()
|
|
146
|
+
for entry in (*front, *pruned):
|
|
147
|
+
if entry not in seen:
|
|
148
|
+
seen.add(entry)
|
|
149
|
+
final_path.append(entry)
|
|
150
|
+
sys.path[:] = final_path
|
|
151
|
+
|
|
152
|
+
sys.meta_path[:] = [
|
|
153
|
+
finder
|
|
154
|
+
for finder in sys.meta_path
|
|
155
|
+
if not _finder_targets_candidate(finder, cand_root, wt_root, plugin_dir)
|
|
156
|
+
]
|
|
157
|
+
|
|
158
|
+
for mod_name, mod in list(sys.modules.items()):
|
|
159
|
+
if mod is None or mod_name in _SELF_MODULE_NAMES:
|
|
160
|
+
continue
|
|
161
|
+
if _module_leak_path(mod, cand_root, wt_root, plugin_dir) is not None:
|
|
162
|
+
sys.modules.pop(mod_name, None)
|
|
163
|
+
|
|
164
|
+
importlib.invalidate_caches()
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def detect_isolation_leaks() -> list[str]:
|
|
168
|
+
"""Return sorted `module:path` entries for any loaded module resolving under candidate root."""
|
|
169
|
+
cand_root, wt_root, plugin_dir = _isolation_roots()
|
|
170
|
+
if cand_root is None:
|
|
171
|
+
return []
|
|
172
|
+
leaks: list[str] = []
|
|
173
|
+
for mod_name, mod in list(sys.modules.items()):
|
|
174
|
+
if mod is None or mod_name in _SELF_MODULE_NAMES:
|
|
175
|
+
continue
|
|
176
|
+
leak = _module_leak_path(mod, cand_root, wt_root, plugin_dir)
|
|
177
|
+
if leak is not None:
|
|
178
|
+
leaks.append(f"{mod_name}:{leak}")
|
|
179
|
+
return sorted(leaks)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
if __name__ == "sitecustomize":
|
|
183
|
+
enforce_worktree_isolation()
|
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
"""Self-contained pytest plugin injected via PYTHONPATH + `-p invaris_gate1_plugin`.
|
|
2
|
+
|
|
3
|
+
Zero imports from `invaris` so it executes inside ANY target repository's Python environment.
|
|
4
|
+
Controlled by environment variables:
|
|
5
|
+
- INVARIS_PYTEST_REPORT: path to write structured JSON session results.
|
|
6
|
+
- INVARIS_COVERAGE_CONTEXT: when "1", switches coverage.py context to `item.nodeid` per test.
|
|
7
|
+
- INVARIS_REPO_ROOT: repo root path for normalizing frame paths to repo-relative POSIX paths.
|
|
8
|
+
- INVARIS_NODEIDS_FILE: optional path to newline-delimited selectors (file paths, bare
|
|
9
|
+
class/function nodeids, or exact parametrized nodeids) filtered during collection.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import importlib.util
|
|
15
|
+
import json
|
|
16
|
+
import os
|
|
17
|
+
from collections.abc import Generator
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
from typing import Any
|
|
20
|
+
|
|
21
|
+
_STATE: dict[str, Any] = {
|
|
22
|
+
"collected": [],
|
|
23
|
+
"deselected_count": 0,
|
|
24
|
+
"passed": [],
|
|
25
|
+
"skipped": [],
|
|
26
|
+
"failures": [],
|
|
27
|
+
"exitstatus": None,
|
|
28
|
+
"isolation_leaks": [],
|
|
29
|
+
}
|
|
30
|
+
_EXC_BY_KEY: dict[tuple[str, str], dict[str, Any]] = {}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _load_isolate_module() -> Any:
|
|
34
|
+
path = Path(__file__).resolve().with_name("_worktree_isolate.py")
|
|
35
|
+
spec = importlib.util.spec_from_file_location("_worktree_isolate", path)
|
|
36
|
+
if spec is None or spec.loader is None:
|
|
37
|
+
return None
|
|
38
|
+
mod = importlib.util.module_from_spec(spec)
|
|
39
|
+
spec.loader.exec_module(mod)
|
|
40
|
+
return mod
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
_ISOLATE_MOD = _load_isolate_module()
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def enforce_worktree_isolation() -> None:
|
|
47
|
+
if _ISOLATE_MOD is not None:
|
|
48
|
+
_ISOLATE_MOD.enforce_worktree_isolation()
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def detect_isolation_leaks() -> list[str]:
|
|
52
|
+
if _ISOLATE_MOD is None:
|
|
53
|
+
return []
|
|
54
|
+
return list(_ISOLATE_MOD.detect_isolation_leaks())
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
enforce_worktree_isolation()
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _rel_path(path_str: str) -> str:
|
|
61
|
+
if not path_str:
|
|
62
|
+
return ""
|
|
63
|
+
repo_root = os.environ.get("INVARIS_REPO_ROOT", "")
|
|
64
|
+
p = Path(path_str)
|
|
65
|
+
if repo_root:
|
|
66
|
+
try:
|
|
67
|
+
root = Path(repo_root).resolve()
|
|
68
|
+
resolved = p.resolve() if p.is_absolute() else (root / p).resolve()
|
|
69
|
+
return resolved.relative_to(root).as_posix()
|
|
70
|
+
except (OSError, ValueError):
|
|
71
|
+
pass
|
|
72
|
+
return path_str.replace("\\", "/")
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _switch_coverage_context(ctx: str) -> None:
|
|
76
|
+
if os.environ.get("INVARIS_COVERAGE_CONTEXT") != "1":
|
|
77
|
+
return
|
|
78
|
+
try:
|
|
79
|
+
import coverage # noqa: PLC0415
|
|
80
|
+
|
|
81
|
+
cov = coverage.Coverage.current()
|
|
82
|
+
if cov is not None:
|
|
83
|
+
cov.switch_context(ctx)
|
|
84
|
+
except (ImportError, AttributeError, RuntimeError):
|
|
85
|
+
pass
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _extract_from_excinfo(excinfo: Any) -> dict[str, Any]:
|
|
89
|
+
exc_type = getattr(getattr(excinfo, "type", None), "__name__", "Exception")
|
|
90
|
+
try:
|
|
91
|
+
exc_msg = str(excinfo.value).splitlines()[0][:240] if str(excinfo.value) else ""
|
|
92
|
+
except Exception: # noqa: BLE001
|
|
93
|
+
exc_msg = ""
|
|
94
|
+
frames: list[dict[str, Any]] = []
|
|
95
|
+
tb = getattr(excinfo, "traceback", None)
|
|
96
|
+
if tb is not None:
|
|
97
|
+
for entry in tb:
|
|
98
|
+
raw_path = str(getattr(entry, "path", ""))
|
|
99
|
+
# pytest TracebackEntry.lineno is 0-based
|
|
100
|
+
raw_ln = getattr(entry, "lineno", None)
|
|
101
|
+
line_no = int(raw_ln) + 1 if isinstance(raw_ln, int) else None
|
|
102
|
+
func_name = str(getattr(entry, "name", "") or "")
|
|
103
|
+
frames.append(
|
|
104
|
+
{
|
|
105
|
+
"file": _rel_path(raw_path),
|
|
106
|
+
"raw_path": raw_path,
|
|
107
|
+
"line": line_no,
|
|
108
|
+
"func": func_name,
|
|
109
|
+
}
|
|
110
|
+
)
|
|
111
|
+
return {"exc_type": exc_type, "exc_message": exc_msg, "frames": frames}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _extract_from_longrepr(longrepr: Any) -> dict[str, Any]:
|
|
115
|
+
exc_type = "Error"
|
|
116
|
+
exc_msg = ""
|
|
117
|
+
frames: list[dict[str, Any]] = []
|
|
118
|
+
crash = getattr(longrepr, "reprcrash", None)
|
|
119
|
+
if crash is not None:
|
|
120
|
+
msg = str(getattr(crash, "message", "") or "")
|
|
121
|
+
if ":" in msg:
|
|
122
|
+
head, _, tail = msg.partition(":")
|
|
123
|
+
if head.strip().isidentifier():
|
|
124
|
+
exc_type = head.strip()
|
|
125
|
+
exc_msg = tail.strip()[:240]
|
|
126
|
+
else:
|
|
127
|
+
exc_msg = msg.strip()[:240]
|
|
128
|
+
else:
|
|
129
|
+
exc_msg = msg.strip()[:240]
|
|
130
|
+
cpath = str(getattr(crash, "path", "") or "")
|
|
131
|
+
cln = getattr(crash, "lineno", None)
|
|
132
|
+
if cpath:
|
|
133
|
+
frames.append(
|
|
134
|
+
{
|
|
135
|
+
"file": _rel_path(cpath),
|
|
136
|
+
"raw_path": cpath,
|
|
137
|
+
"line": int(cln) if isinstance(cln, int) else None,
|
|
138
|
+
"func": "",
|
|
139
|
+
}
|
|
140
|
+
)
|
|
141
|
+
tb = getattr(longrepr, "reprtraceback", None)
|
|
142
|
+
entries = getattr(tb, "reprentries", None) if tb is not None else None
|
|
143
|
+
if entries:
|
|
144
|
+
tb_frames: list[dict[str, Any]] = []
|
|
145
|
+
for entry in entries:
|
|
146
|
+
floc = getattr(entry, "reprfileloc", None)
|
|
147
|
+
if floc is None:
|
|
148
|
+
continue
|
|
149
|
+
fpath = str(getattr(floc, "path", "") or "")
|
|
150
|
+
fln = getattr(floc, "lineno", None)
|
|
151
|
+
tb_frames.append(
|
|
152
|
+
{
|
|
153
|
+
"file": _rel_path(fpath),
|
|
154
|
+
"raw_path": fpath,
|
|
155
|
+
"line": int(fln) if isinstance(fln, int) else None,
|
|
156
|
+
"func": "",
|
|
157
|
+
}
|
|
158
|
+
)
|
|
159
|
+
if tb_frames:
|
|
160
|
+
frames = tb_frames
|
|
161
|
+
if not exc_msg:
|
|
162
|
+
text = longrepr if isinstance(longrepr, str) else str(longrepr or "")
|
|
163
|
+
lines = [ln.strip() for ln in text.splitlines() if ln.strip()]
|
|
164
|
+
for raw_ln in reversed(lines):
|
|
165
|
+
cand = raw_ln[1:].strip() if raw_ln.startswith("E ") else raw_ln
|
|
166
|
+
if ":" in cand:
|
|
167
|
+
head, _, tail = cand.partition(":")
|
|
168
|
+
if head.strip().isidentifier():
|
|
169
|
+
exc_type = head.strip()
|
|
170
|
+
exc_msg = tail.strip()[:240]
|
|
171
|
+
break
|
|
172
|
+
if not exc_msg and lines:
|
|
173
|
+
last = lines[-1]
|
|
174
|
+
cand = last[1:].strip() if last.startswith("E ") else last
|
|
175
|
+
exc_msg = cand[:240]
|
|
176
|
+
return {"exc_type": exc_type, "exc_message": exc_msg, "frames": frames}
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def _load_target_selectors(path_str: str) -> tuple[set[str], set[str], set[str]]:
|
|
180
|
+
"""Parse `INVARIS_NODEIDS_FILE` into `(whole_files, exact_nodeids, bare_prefixes)`."""
|
|
181
|
+
whole_files: set[str] = set()
|
|
182
|
+
exact_nodeids: set[str] = set()
|
|
183
|
+
bare_prefixes: set[str] = set()
|
|
184
|
+
p = Path(path_str)
|
|
185
|
+
if not p.is_file():
|
|
186
|
+
return whole_files, exact_nodeids, bare_prefixes
|
|
187
|
+
for raw_line in p.read_text(encoding="utf-8", errors="replace").splitlines():
|
|
188
|
+
sel = raw_line.strip().replace("\\", "/")
|
|
189
|
+
if sel.startswith("./"):
|
|
190
|
+
sel = sel[2:]
|
|
191
|
+
if not sel:
|
|
192
|
+
continue
|
|
193
|
+
if "::" not in sel:
|
|
194
|
+
whole_files.add(sel)
|
|
195
|
+
else:
|
|
196
|
+
exact_nodeids.add(sel)
|
|
197
|
+
if "[" not in sel:
|
|
198
|
+
bare_prefixes.add(sel)
|
|
199
|
+
return whole_files, exact_nodeids, bare_prefixes
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _matches_selector(
|
|
203
|
+
nodeid: str,
|
|
204
|
+
whole_files: set[str],
|
|
205
|
+
exact_nodeids: set[str],
|
|
206
|
+
bare_prefixes: set[str],
|
|
207
|
+
) -> bool:
|
|
208
|
+
nid = nodeid.replace("\\", "/")
|
|
209
|
+
if nid.startswith("./"):
|
|
210
|
+
nid = nid[2:]
|
|
211
|
+
if nid in exact_nodeids:
|
|
212
|
+
return True
|
|
213
|
+
fpath = nid.split("::", 1)[0]
|
|
214
|
+
if fpath in whole_files:
|
|
215
|
+
return True
|
|
216
|
+
if not bare_prefixes:
|
|
217
|
+
return False
|
|
218
|
+
base_no_param = nid.split("[", 1)[0]
|
|
219
|
+
if base_no_param in bare_prefixes:
|
|
220
|
+
return True
|
|
221
|
+
parts = base_no_param.split("::")
|
|
222
|
+
prefix = parts[0]
|
|
223
|
+
for part in parts[1:-1]:
|
|
224
|
+
prefix = f"{prefix}::{part}"
|
|
225
|
+
if prefix in bare_prefixes:
|
|
226
|
+
return True
|
|
227
|
+
return False
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def pytest_collection_modifyitems(config: Any, items: list[Any]) -> None:
|
|
231
|
+
targets_file = os.environ.get("INVARIS_NODEIDS_FILE", "")
|
|
232
|
+
if targets_file:
|
|
233
|
+
whole_files, exact_nodeids, bare_prefixes = _load_target_selectors(targets_file)
|
|
234
|
+
if whole_files or exact_nodeids or bare_prefixes:
|
|
235
|
+
selected: list[Any] = []
|
|
236
|
+
deselected: list[Any] = []
|
|
237
|
+
for item in items:
|
|
238
|
+
if _matches_selector(str(item.nodeid), whole_files, exact_nodeids, bare_prefixes):
|
|
239
|
+
selected.append(item)
|
|
240
|
+
else:
|
|
241
|
+
deselected.append(item)
|
|
242
|
+
if deselected:
|
|
243
|
+
items[:] = selected
|
|
244
|
+
config.hook.pytest_deselected(items=deselected)
|
|
245
|
+
_STATE["deselected_count"] = len(deselected)
|
|
246
|
+
_STATE["collected"] = [str(item.nodeid) for item in items]
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def pytest_runtest_setup(item: Any) -> None:
|
|
250
|
+
_switch_coverage_context(str(item.nodeid))
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def pytest_runtest_logfinish(nodeid: str, location: Any) -> None:
|
|
254
|
+
_switch_coverage_context("")
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
try:
|
|
258
|
+
import pytest as _pytest
|
|
259
|
+
|
|
260
|
+
@_pytest.hookimpl(tryfirst=True)
|
|
261
|
+
def pytest_load_initial_conftests(early_config: Any, parser: Any, args: Any) -> None:
|
|
262
|
+
enforce_worktree_isolation()
|
|
263
|
+
|
|
264
|
+
@_pytest.hookimpl(hookwrapper=True)
|
|
265
|
+
def pytest_runtest_makereport(item: Any, call: Any) -> Generator[None, Any, None]:
|
|
266
|
+
outcome = yield
|
|
267
|
+
rep = outcome.get_result()
|
|
268
|
+
if call.excinfo is not None and rep.failed:
|
|
269
|
+
_EXC_BY_KEY[(str(rep.nodeid), str(rep.when))] = _extract_from_excinfo(call.excinfo)
|
|
270
|
+
except ImportError:
|
|
271
|
+
pass
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def pytest_collectreport(report: Any) -> None:
|
|
275
|
+
if report.failed:
|
|
276
|
+
info = _extract_from_longrepr(report.longrepr)
|
|
277
|
+
nodeid = str(getattr(report, "nodeid", "") or "")
|
|
278
|
+
_STATE["failures"].append(
|
|
279
|
+
{
|
|
280
|
+
"nodeid": nodeid,
|
|
281
|
+
"phase": "collect",
|
|
282
|
+
"exc_type": info["exc_type"],
|
|
283
|
+
"exc_message": info["exc_message"],
|
|
284
|
+
"frames": info["frames"],
|
|
285
|
+
"longrepr": str(getattr(report, "longreprtext", "") or report.longrepr)[:8000],
|
|
286
|
+
}
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def pytest_runtest_logreport(report: Any) -> None:
|
|
291
|
+
nodeid = str(report.nodeid)
|
|
292
|
+
when = str(report.when)
|
|
293
|
+
if report.failed:
|
|
294
|
+
info = _EXC_BY_KEY.pop((nodeid, when), None) or _extract_from_longrepr(report.longrepr)
|
|
295
|
+
_STATE["failures"].append(
|
|
296
|
+
{
|
|
297
|
+
"nodeid": nodeid,
|
|
298
|
+
"phase": when,
|
|
299
|
+
"exc_type": info["exc_type"],
|
|
300
|
+
"exc_message": info["exc_message"],
|
|
301
|
+
"frames": info["frames"],
|
|
302
|
+
"longrepr": str(getattr(report, "longreprtext", "") or report.longrepr)[:8000],
|
|
303
|
+
}
|
|
304
|
+
)
|
|
305
|
+
elif report.skipped:
|
|
306
|
+
if nodeid not in _STATE["skipped"]:
|
|
307
|
+
_STATE["skipped"].append(nodeid)
|
|
308
|
+
elif report.passed and when == "call":
|
|
309
|
+
if nodeid not in _STATE["passed"]:
|
|
310
|
+
_STATE["passed"].append(nodeid)
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def pytest_sessionfinish(session: Any, exitstatus: int) -> None:
|
|
314
|
+
_switch_coverage_context("")
|
|
315
|
+
_STATE["exitstatus"] = int(exitstatus)
|
|
316
|
+
_STATE["isolation_leaks"] = detect_isolation_leaks()
|
|
317
|
+
report_path = os.environ.get("INVARIS_PYTEST_REPORT")
|
|
318
|
+
if report_path:
|
|
319
|
+
Path(report_path).write_text(json.dumps(_STATE, indent=1) + "\n", encoding="utf-8")
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""Structured tool adapters for Invaris v1."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from invaris.adapters.base import CommandResult, get_tool_version, resolve_command, run_command
|
|
8
|
+
from invaris.adapters.ruff import run_ruff_format, run_ruff_lint
|
|
9
|
+
from invaris.adapters.tach import run_tach
|
|
10
|
+
from invaris.adapters.typecheck import run_typecheck
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"CommandResult",
|
|
14
|
+
"Gate1Result",
|
|
15
|
+
"get_tool_version",
|
|
16
|
+
"resolve_command",
|
|
17
|
+
"run_command",
|
|
18
|
+
"run_gate1",
|
|
19
|
+
"run_ruff_format",
|
|
20
|
+
"run_ruff_lint",
|
|
21
|
+
"run_tach",
|
|
22
|
+
"run_typecheck",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def __getattr__(name: str) -> Any:
|
|
27
|
+
if name in ("Gate1Result", "run_gate1"):
|
|
28
|
+
from invaris.adapters import pytest_cov
|
|
29
|
+
|
|
30
|
+
return getattr(pytest_cov, name)
|
|
31
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|