Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ All four gates must pass before requesting review:
- [ ] Every non-merge commit carries `Signed-off-by: Name <email>` matching the commit author (`git commit -s`).
- [ ] No AI attribution: no model attribution trailers, co-author trailers, or machine generation lines in commits or the PR description.
- [ ] Changes adhere to the repository working rules in `AGENTS.md`.
- [ ] Each new `NOSONAR` is listed below with the refactor that was tried and why it was rejected (`AGENTS.md`, architecture rule 7), or there is none.
7 changes: 7 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ to loosen. Change a rule only in that test and here, in the same PR, with the re
never above `sonar-baseline.txt`, and every pull request fails while `main` sits below it, so the PR after a fixing
merge lowers it (`scripts/ci/sonar_ratchet.py`). A change that fixes one lowers the pinned number in the same commit;
nothing is ever raised.
7. **A code-scan suppression is the exception; the refactor comes first.** An issue is suppressed
(`# NOSONAR - <reason>`, `// NOSONAR - <reason>`) only when the refactor it asks for would change behaviour or
break a contract (an ASGI coroutine, a snapshot copy another thread needs, a pattern whose rewrite widens what it
matches) or would touch a safety path or an overhead budget without the evidence those need. The reason names why
the refactor was rejected; `tests/test_code_shape.py` fails a suppression without one. Each pull request that adds
one lists it, with the refactor that was tried, in its body. Issues are never accepted or marked false positive in
the code-scan service: a suppression lives in the code, where review sees it.

### Writing tests that prove something

Expand Down
2 changes: 1 addition & 1 deletion sonar-baseline.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8
5
26 changes: 26 additions & 0 deletions tests/test_code_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
1. No module under ``src/`` passes ``MAX_LINES``.
2. Complexity: ruff C901 (McCabe > 10) findings per file are pinned in ``tests/complexity_baseline.json``. A file may
not gain one, and a file that loses one must lower its entry in the same change, so the count can only fall.
3. Every Sonar suppression comment states its reason: ``NOSONAR - <why the refactor was rejected>``.
"""

from __future__ import annotations

import json
import re
import subprocess
import sys
from collections import Counter
Expand All @@ -18,6 +20,10 @@
ROOT = Path(__file__).resolve().parents[1]
MAX_LINES = 1000
BASELINE = ROOT / "tests" / "complexity_baseline.json"
SUPPRESSION = "NO" + "SONAR" # split, so this file holds no suppression of its own
EXPLAINED = re.compile(SUPPRESSION + r" - \S")
SCANNED = ("src/sharpit", "tests", "scripts", "ui/src")
SOURCE_SUFFIXES = {".py", ".ts", ".js", ".svelte"}


def oversized(root: Path = ROOT / "src", limit: int = MAX_LINES) -> list[str]:
Expand Down Expand Up @@ -51,6 +57,17 @@ def ratchet_violations(found: Counter[str], pinned: dict[str, int]) -> list[str]
return problems


def unexplained_suppressions(root: Path = ROOT, dirs: tuple[str, ...] = SCANNED) -> list[str]:
return [
f"{path.relative_to(root).as_posix()}:{n}: {SUPPRESSION} needs ' - <reason>'"
for top in dirs
for path in sorted((root / top).rglob("*"))
if path.suffix in SOURCE_SUFFIXES and path.is_file()
for n, line in enumerate(path.read_text(encoding="utf-8", errors="replace").splitlines(), 1)
if SUPPRESSION in line and not EXPLAINED.search(line)
]


def test_no_module_is_oversized():
assert oversized() == []

Expand All @@ -59,6 +76,10 @@ def test_complexity_only_goes_down():
assert ratchet_violations(complexity_counts(), json.loads(BASELINE.read_text(encoding="utf-8"))) == []


def test_every_suppression_states_its_reason():
assert unexplained_suppressions() == []


def test_the_ratchets_catch_a_violation(tmp_path: Path) -> None:
"""Each ratchet fails on planted input, so a green run means the limits hold, not that nothing was checked."""
(tmp_path / "big.py").write_text("x = 1\n" * 3, encoding="utf-8")
Expand All @@ -72,3 +93,8 @@ def test_the_ratchets_catch_a_violation(tmp_path: Path) -> None:
"c.py",
]
assert "lower its entry" in ratchet_violations(Counter({"a.py": 1, "b.py": 1}), pinned)[0]

(tmp_path / "src").mkdir()
(tmp_path / "src" / "ok.py").write_text(f"x = 1 # {SUPPRESSION} - a reason\n", encoding="utf-8")
(tmp_path / "src" / "bare.ts").write_text(f"let x = 1; // {SUPPRESSION}\n", encoding="utf-8")
assert unexplained_suppressions(tmp_path, ("src",)) == [f"src/bare.ts:1: {SUPPRESSION} needs ' - <reason>'"]