forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_contributing_test_counts_are_digit_free.py
More file actions
193 lines (160 loc) · 8.02 KB
/
Copy pathtest_contributing_test_counts_are_digit_free.py
File metadata and controls
193 lines (160 loc) · 8.02 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
r"""Repo-wide shape invariant: CONTRIBUTING.md must not carry hardcoded test counts.
When contributing to Soup, over 50% of pull requests add or modify unit tests.
Maintaining hardcoded test suite counts (e.g. `370 files, 18151 tests`) in
`CONTRIBUTING.md` creates a continuous drift treadmill, off-by-one guesswork,
and unnecessary merge conflicts across concurrent contributions.
Per Issue #465, the resolution is subtraction over redundant maintenance:
eliminating the exact count metrics makes drift structurally impossible, while
preserving developer ergonomics.
This test acts as a permanent anti-regression shape fence: it guarantees that
the `tests/` directory entry and the `### Test Files` section header remain
present in `CONTRIBUTING.md` (anti-rot), while asserting that neither line carries
hardcoded file or test counts, while safely admitting version tags (v0.69.0),
Python version ranges (Python 3.10-3.12), and tool requirements (pytest 8.x).
"""
from __future__ import annotations
import pathlib
import re
from dataclasses import dataclass
ROOT = pathlib.Path(__file__).resolve().parents[1]
CONTRIBUTING_PATH = ROOT / "CONTRIBUTING.md"
# Matches count metrics: e.g. "370 files", "18151 tests", "18,151 tests", "(370)"
_COUNT_PATTERN = re.compile(
r"\b\d+[\d,]*\+?\s*(?:files?|tests?)\b|\(\s*\d+\s*(?:files?)?\s*\)",
re.IGNORECASE,
)
@dataclass(frozen=True)
class ContributingShapeVerdict:
"""The result of auditing CONTRIBUTING.md for hardcoded test counts."""
is_valid: bool
violations: tuple[tuple[int, str, str], ...]
missing_sections: tuple[str, ...]
def format_diagnostic(self) -> str:
lines = []
if self.missing_sections:
lines.append("Missing required documentation sections (anti-rot violation):")
for s in self.missing_sections:
lines.append(f" * Missing: {s}")
if self.violations:
lines.append("Hardcoded test or file counts found in CONTRIBUTING.md:")
for lineno, line_text, section in self.violations:
lines.append(f" * Line {lineno} ({section}): {line_text!r}")
return "\n".join(lines)
def audit_contributing_test_count_shape(
file_path: pathlib.Path = CONTRIBUTING_PATH,
) -> ContributingShapeVerdict:
"""Inspect CONTRIBUTING.md test suite lines for hardcoded count metrics."""
if not file_path.exists():
return ContributingShapeVerdict(False, (), (f"File not found: {file_path}",))
lines = file_path.read_text(encoding="utf-8").splitlines()
violations = []
missing_sections = []
# 1. Inspect the `tests/` directory overview line
test_dir_match = None
for lineno, line in enumerate(lines, 1):
if re.match(r"^\s*tests/\s+-\s+Test suite", line):
test_dir_match = (lineno, line)
break
if test_dir_match is None:
missing_sections.append("Directory overview line: 'tests/ - Test suite ...'")
elif _COUNT_PATTERN.search(test_dir_match[1]):
violations.append((test_dir_match[0], test_dir_match[1], "tests/ overview line"))
# 2. Inspect the `### Test Files` section header
test_files_match = None
for lineno, line in enumerate(lines, 1):
if re.match(r"^###\s+Test Files", line):
test_files_match = (lineno, line)
break
if test_files_match is None:
missing_sections.append("Section header: '### Test Files ...'")
elif _COUNT_PATTERN.search(test_files_match[1]):
violations.append((test_files_match[0], test_files_match[1], "### Test Files header"))
return ContributingShapeVerdict(
is_valid=(len(violations) == 0 and len(missing_sections) == 0),
violations=tuple(violations),
missing_sections=tuple(missing_sections),
)
class TestContributingTestCountsAreNotHardcoded:
"""CONTRIBUTING.md must describe the test suite without drifting hardcoded numbers."""
def test_contributing_test_suite_lines_contain_no_hardcoded_counts(self) -> None:
verdict = audit_contributing_test_count_shape(CONTRIBUTING_PATH)
assert verdict.is_valid, (
f"CONTRIBUTING.md failed test count shape invariant:\n"
f"{verdict.format_diagnostic()}\n"
f"Remove hardcoded file/test count numbers to prevent drift (#465)."
)
def test_contributing_target_sections_are_present(self) -> None:
"""Control: ensure the target sections are not silently deleted."""
verdict = audit_contributing_test_count_shape(CONTRIBUTING_PATH)
assert verdict.missing_sections == (), (
f"Required sections missing from CONTRIBUTING.md: {verdict.missing_sections}"
)
class TestTheGuardHasTeeth:
"""CONTROL. Prove the auditor catches hardcoded counts while admitting version tags."""
def test_hardcoded_counts_in_test_suite_line_fails(self, tmp_path: pathlib.Path) -> None:
doc = tmp_path / "CONTRIBUTING.md"
doc.write_text(
"tests/ - Test suite (370 files, 18151 tests)\n### Test Files\n",
encoding="utf-8",
)
verdict = audit_contributing_test_count_shape(doc)
assert not verdict.is_valid
assert len(verdict.violations) == 1
assert verdict.violations[0][0] == 1
def test_hardcoded_counts_in_test_files_header_fails(self, tmp_path: pathlib.Path) -> None:
doc = tmp_path / "CONTRIBUTING.md"
doc.write_text("tests/ - Test suite\n### Test Files (370 files)\n", encoding="utf-8")
verdict = audit_contributing_test_count_shape(doc)
assert not verdict.is_valid
assert len(verdict.violations) == 1
assert verdict.violations[0][0] == 2
def test_bare_digits_in_test_files_header_fails(self, tmp_path: pathlib.Path) -> None:
doc = tmp_path / "CONTRIBUTING.md"
doc.write_text("tests/ - Test suite\n### Test Files (370)\n", encoding="utf-8")
verdict = audit_contributing_test_count_shape(doc)
assert not verdict.is_valid
assert len(verdict.violations) == 1
assert verdict.violations[0][0] == 2
def test_version_tags_and_python_versions_are_accepted(
self, tmp_path: pathlib.Path
) -> None:
"""CONTROL: Legitimate version tags (v0.69.0), pytest, and Python ranges must pass."""
doc = tmp_path / "CONTRIBUTING.md"
doc.write_text(
"tests/ - Test suite (v0.69.0, pytest 8.x, Python 3.10-3.12)\n"
"### Test Files\n",
encoding="utf-8",
)
verdict = audit_contributing_test_count_shape(doc)
assert verdict.is_valid
assert verdict.violations == ()
def test_missing_test_suite_line_fails_anti_rot_check(self, tmp_path: pathlib.Path) -> None:
doc = tmp_path / "CONTRIBUTING.md"
doc.write_text("### Test Files\n", encoding="utf-8")
verdict = audit_contributing_test_count_shape(doc)
assert not verdict.is_valid
assert len(verdict.missing_sections) == 1
def test_missing_test_files_header_fails_anti_rot_check(self, tmp_path: pathlib.Path) -> None:
doc = tmp_path / "CONTRIBUTING.md"
doc.write_text("tests/ - Test suite\n", encoding="utf-8")
verdict = audit_contributing_test_count_shape(doc)
assert not verdict.is_valid
assert len(verdict.missing_sections) == 1
def test_nonexistent_file_fails(self, tmp_path: pathlib.Path) -> None:
nonexistent = tmp_path / "DOES_NOT_EXIST.md"
verdict = audit_contributing_test_count_shape(nonexistent)
assert not verdict.is_valid
def test_valid_unrelated_digits_in_contributing_are_accepted(
self, tmp_path: pathlib.Path
) -> None:
"""CONTROL: Numbers in other sections (recipe count, Python version) must pass."""
doc = tmp_path / "CONTRIBUTING.md"
doc.write_text(
"Python 3.11 is required.\n"
"recipes/ - Ready-made models (146 recipes)\n"
"tests/ - Test suite covering all modules\n"
"### Test Files\n",
encoding="utf-8",
)
verdict = audit_contributing_test_count_shape(doc)
assert verdict.is_valid