diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index b8b70711..f61f6ead 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -30,3 +30,4 @@ All four gates must pass before requesting review: - [ ] Every non-merge commit carries `Signed-off-by: Name ` 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. diff --git a/AGENTS.md b/AGENTS.md index 033868bd..b74409aa 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -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 - `, `// NOSONAR - `) 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 diff --git a/sonar-baseline.txt b/sonar-baseline.txt index 45a4fb75..7ed6ff82 100644 --- a/sonar-baseline.txt +++ b/sonar-baseline.txt @@ -1 +1 @@ -8 +5 diff --git a/tests/test_code_shape.py b/tests/test_code_shape.py index d728c863..4c69e43d 100644 --- a/tests/test_code_shape.py +++ b/tests/test_code_shape.py @@ -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 - ``. """ from __future__ import annotations import json +import re import subprocess import sys from collections import Counter @@ -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]: @@ -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 ' - '" + 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() == [] @@ -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") @@ -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 ' - '"]