Skip to content

Commit 006df2a

Browse files
committed
Add testguard/diff_checks.py
1 parent c0164ed commit 006df2a

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

‎testguard/diff_checks.py‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"""Checks for ``git diff`` output.
2+
3+
The most common way an agent (or a rushed human) fakes a green suite is
4+
not in the test file itself — it is in the *change*: delete the failing
5+
test, remove the assertion, add a skip, add a sleep. Scanning the diff
6+
catches the weakening at review time, before it merges.
7+
"""
8+
9+
from __future__ import annotations
10+
11+
import re
12+
13+
from .findings import Finding
14+
15+
_HUNK_RE = re.compile(r"^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@")
16+
_TEST_DEF_RE = re.compile(r"^\s*def\s+(test\w*)\s*\(|^\s*(test|it)\s*\(")
17+
_ASSERT_RE = re.compile(r"\bassert\b|\bexpect\s*\(")
18+
_SKIP_RE = re.compile(r"@pytest\.mark\.skip|pytest\.skip|unittest\.skip"
19+
r"|\.skip\s*\(|\bxit\s*\(|\bxdescribe\s*\(")
20+
_SLEEP_RE = re.compile(r"\b(time\.sleep|asyncio\.sleep|setTimeout|sleep)\s*\(")
21+
_FAKE_PASS_RE = re.compile(r"assert\s+True\b|expect\s*\(\s*true\s*\)\s*\.\s*toBe\s*\(\s*true\s*\)")
22+
23+
24+
def analyze_diff(diff_text: str, label: str = "<diff>") -> list[Finding]:
25+
findings: list[Finding] = []
26+
current_file = label
27+
new_lineno = 0
28+
hunk_removed_asserts: list[tuple[int, str]] = []
29+
hunk_has_added_assert = False
30+
31+
def flush_hunk() -> None:
32+
if hunk_removed_asserts and not hunk_has_added_assert:
33+
for line_no, content in hunk_removed_asserts:
34+
findings.append(Finding(
35+
current_file, line_no, "removed-assert", "high",
36+
"Assertion removed from a test with no replacement "
37+
"assertion in the same change.", content[:120]))
38+
39+
for raw in diff_text.splitlines():
40+
line = raw.rstrip("\n")
41+
if line.startswith("+++ "):
42+
flush_hunk()
43+
hunk_removed_asserts, hunk_has_added_assert = [], False
44+
name = line[4:].strip()
45+
if name.startswith("b/"):
46+
name = name[2:]
47+
current_file = name if name != "/dev/null" else current_file
48+
continue
49+
if line.startswith("diff --git"):
50+
flush_hunk()
51+
hunk_removed_asserts, hunk_has_added_assert = [], False
52+
continue
53+
match = _HUNK_RE.match(line)
54+
if match:
55+
flush_hunk()
56+
hunk_removed_asserts, hunk_has_added_assert = [], False
57+
new_lineno = int(match.group(1))
58+
continue
59+
if line.startswith("--- ") or line.startswith("index ") \
60+
or line.startswith("new file") or line.startswith("deleted file"):
61+
continue
62+
if line.startswith("+") and not line.startswith("+++"):
63+
content = line[1:]
64+
if _ASSERT_RE.search(content):
65+
hunk_has_added_assert = True
66+
if _SKIP_RE.search(content):
67+
findings.append(Finding(current_file, new_lineno, "added-skip",
68+
"medium", "Change adds a skip to a test.",
69+
content.strip()[:120]))
70+
if _SLEEP_RE.search(content):
71+
findings.append(Finding(current_file, new_lineno, "added-sleep",
72+
"medium", "Change adds a hardcoded sleep to a test.",
73+
content.strip()[:120]))
74+
if _FAKE_PASS_RE.search(content):
75+
findings.append(Finding(current_file, new_lineno, "added-fake-pass",
76+
"high",
77+
"Change adds an assertion that can never fail.",
78+
content.strip()[:120]))
79+
new_lineno += 1
80+
elif line.startswith("-") and not line.startswith("---"):
81+
content = line[1:]
82+
test_match = _TEST_DEF_RE.search(content)
83+
if test_match:
84+
findings.append(Finding(current_file, new_lineno, "deleted-test",
85+
"high",
86+
"Change deletes (or renames) a test — "
87+
"check that the coverage is replaced, "
88+
"not just dropped.",
89+
content.strip()[:120]))
90+
elif _ASSERT_RE.search(content):
91+
hunk_removed_asserts.append((new_lineno, content.strip()))
92+
else:
93+
# Context line: advances the new-file line number.
94+
if line.startswith(" ") or line == "":
95+
new_lineno += 1
96+
97+
flush_hunk()
98+
return findings

0 commit comments

Comments
 (0)