-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcheck_test_evidence.py
More file actions
309 lines (262 loc) · 8.5 KB
/
Copy pathcheck_test_evidence.py
File metadata and controls
309 lines (262 loc) · 8.5 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env python3
"""Require test evidence for changed SDK source files."""
from __future__ import annotations
import argparse
from dataclasses import dataclass
from pathlib import PurePosixPath
import subprocess
import sys
from typing import Iterable, Sequence
SDK_ROOT = PurePosixPath("agentrun")
UNITTEST_ROOT = PurePosixPath("tests/unittests")
E2E_ROOT = PurePosixPath("tests/e2e")
DEFAULT_BASE_REF = "origin/main"
@dataclass(frozen=True)
class ChangedFile:
status: str
path: str
@dataclass(frozen=True)
class MissingTestEvidence:
source_path: str
evidence_type: str
expected_patterns: tuple[str, ...]
def is_sdk_source(path: str) -> bool:
posix_path = PurePosixPath(path)
return (
path.endswith(".py")
and posix_path.is_relative_to(SDK_ROOT)
and "__pycache__" not in posix_path.parts
)
def is_unit_test_evidence(path: str) -> bool:
return is_test_evidence(path, UNITTEST_ROOT)
def is_e2e_test_evidence(path: str) -> bool:
return is_test_evidence(path, E2E_ROOT)
def is_test_evidence(path: str, test_root: PurePosixPath) -> bool:
posix_path = PurePosixPath(path)
return path.endswith(".py") and posix_path.is_relative_to(test_root)
def _module_parts(source_path: str) -> tuple[str, ...]:
relative = PurePosixPath(source_path).relative_to(SDK_ROOT)
if relative.name == "__init__.py":
return relative.parent.parts
return relative.with_suffix("").parts
def expected_test_patterns(
source_path: str, test_root: PurePosixPath
) -> tuple[str, ...]:
module_parts = _module_parts(source_path)
if not module_parts:
return (f"{test_root}/test_agentrun.py",)
top_level = module_parts[0]
leaf = module_parts[-1]
nearest_package = module_parts[-2] if len(module_parts) > 1 else top_level
patterns = [
f"{test_root}/{top_level}/",
f"{test_root}/test_{top_level}.py",
f"{test_root}/**/test_{top_level}.py",
f"{test_root}/**/test_{nearest_package}.py",
f"{test_root}/**/test_{leaf}.py",
]
return tuple(dict.fromkeys(patterns))
def has_matching_test_evidence(
source_path: str,
changed_test_paths: Iterable[str],
test_root: PurePosixPath,
) -> bool:
module_parts = _module_parts(source_path)
if not module_parts:
return any(
is_test_evidence(path, test_root) for path in changed_test_paths
)
top_level = module_parts[0]
leaf = module_parts[-1]
nearest_package = module_parts[-2] if len(module_parts) > 1 else top_level
acceptable_names = {
f"test_{top_level}.py",
f"test_{nearest_package}.py",
f"test_{leaf}.py",
}
for path in changed_test_paths:
posix_path = PurePosixPath(path)
if not is_test_evidence(path, test_root):
continue
if posix_path.name in acceptable_names:
return True
if posix_path.is_relative_to(test_root / top_level):
return True
return False
def find_missing_test_evidence(
changed_files: Sequence[ChangedFile],
) -> list[MissingTestEvidence]:
changed_sources = [
changed.path
for changed in changed_files
if changed.status != "D" and is_sdk_source(changed.path)
]
changed_tests = [
changed.path
for changed in changed_files
if changed.status != "D"
and (
is_unit_test_evidence(changed.path)
or is_e2e_test_evidence(changed.path)
)
]
missing: list[MissingTestEvidence] = []
required_evidence = (
("unit", UNITTEST_ROOT),
("e2e", E2E_ROOT),
)
for source_path in changed_sources:
for evidence_type, test_root in required_evidence:
if has_matching_test_evidence(
source_path, changed_tests, test_root
):
continue
missing.append(
MissingTestEvidence(
source_path=source_path,
evidence_type=evidence_type,
expected_patterns=expected_test_patterns(
source_path, test_root
),
)
)
return missing
def parse_changed_file_specs(specs: Sequence[str]) -> list[ChangedFile]:
changed_files: list[ChangedFile] = []
for spec in specs:
if not spec:
continue
if ":" in spec and spec.split(":", 1)[0] in {
"A",
"C",
"D",
"M",
"R",
"T",
}:
status, path = spec.split(":", 1)
else:
status, path = "M", spec
changed_files.append(ChangedFile(status=status[0], path=path.strip()))
return changed_files
def _run_git(args: Sequence[str]) -> str:
result = subprocess.run(
["git", *args],
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
if result.returncode != 0:
raise RuntimeError(result.stderr.strip() or "git command failed")
return result.stdout
def _parse_git_name_status(output: str) -> list[ChangedFile]:
changed_files: list[ChangedFile] = []
for line in output.splitlines():
if not line.strip():
continue
fields = line.split("\t")
status = fields[0][0]
path = fields[-1]
changed_files.append(ChangedFile(status=status, path=path))
return changed_files
def git_ref_exists(ref: str) -> bool:
result = subprocess.run(
["git", "rev-parse", "--verify", "--quiet", ref],
check=False,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
return result.returncode == 0
def get_changed_files(base_ref: str) -> list[ChangedFile]:
changed: list[ChangedFile] = []
if not git_ref_exists(base_ref):
raise RuntimeError(
f"Base ref {base_ref!r} was not found. Fetch the PR base ref or "
"pass explicit --changed-file entries."
)
base = _run_git(["merge-base", base_ref, "HEAD"]).strip()
changed.extend(
_parse_git_name_status(
_run_git([
"diff",
"--name-status",
"--diff-filter=ACMRT",
base,
"HEAD",
])
)
)
changed.extend(
_parse_git_name_status(
_run_git(
["diff", "--name-status", "--diff-filter=ACMRT", "HEAD", "--"]
)
)
)
deduped: dict[str, ChangedFile] = {}
for changed_file in changed:
deduped[changed_file.path] = changed_file
return list(deduped.values())
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description=(
"Fail when changed agentrun source files do not have "
"matching unit and e2e test evidence in the same change."
)
)
parser.add_argument(
"--base-ref",
default=DEFAULT_BASE_REF,
help=(
"Git base ref for committed PR changes. Default:"
f" {DEFAULT_BASE_REF}"
),
)
parser.add_argument(
"--changed-file",
action="append",
default=[],
metavar="[STATUS:]PATH",
help=(
"Explicit changed file for tests or custom CI integrations. "
"Status defaults to M."
),
)
return parser
def main(argv: Sequence[str] | None = None) -> int:
args = build_parser().parse_args(argv)
try:
changed_files = (
parse_changed_file_specs(args.changed_file)
if args.changed_file
else get_changed_files(args.base_ref)
)
except RuntimeError as exc:
print(
f"SDK test evidence gate could not inspect git diff: {exc}",
file=sys.stderr,
)
return 2
missing = find_missing_test_evidence(changed_files)
if not missing:
print("SDK test evidence gate passed.")
return 0
print("SDK test evidence gate failed.", file=sys.stderr)
print(
"Every changed agentrun/**/*.py file must include matching "
"tests/unittests and tests/e2e evidence.",
file=sys.stderr,
)
for item in missing:
print(
f"\nMissing {item.evidence_type} test evidence for:"
f" {item.source_path}",
file=sys.stderr,
)
print("Expected one of:", file=sys.stderr)
for pattern in item.expected_patterns:
print(f" - {pattern}", file=sys.stderr)
return 1
if __name__ == "__main__":
raise SystemExit(main())