utils/md_naming/tests/test_stale.py
Krzysztof kuhy Rudnicki a10d190b65
Some checks failed
file length / file-length (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
palette drift / palette (push) Waiting to run
design_system tests / test (push) Has been cancelled
gatelock pre-commit / pre-commit (push) Has been cancelled
gatelock tests / test (3.10) (push) Has been cancelled
gatelock tests / test (3.11) (push) Has been cancelled
gatelock tests / test (3.12) (push) Has been cancelled
web_ui tests / test (push) Has been cancelled
Add the shared markdown-naming gate and migrate utils' own docs
Markdown across every repo had accumulated a dozen ad-hoc names
(refactor_claude_todo.md x23, DESIGN_AUDIT_TODO.md, NEXT_SESSION,
HANDOFF, PLAN, SPEC) with nothing distinguishing a live task from a
finished record. An audit found docs asserting work was pending that
shipped months earlier, and one asserting completion while the work
was open.

Four namespaces now: README.md, CLAUDE*.md, DOCS*.md, TODO*.md. Task
files carry the literal "REMOVE ME AFTER FINISH" and are deleted when
the work lands.

Enforced bidirectionally -- a TODO without the marker fails, AND any
file carrying the marker that is not named TODO*.md fails. Without the
reverse rule a task can be renamed into the permanent namespace and
quietly live forever, which is the failure being fixed.

md_naming/ mirrors the file_length topology and imports its tables
(EXCLUDED_DIRS, THIRD_PARTY_REPOS, VENDORED_ANYWHERE, GENERATED_PATTERN)
rather than restating them, so the two gates cannot disagree about the
same file. 71 tests, 100% branch coverage.

stale.py warns on TODOs past 90 days but never fails: no machine can
prove a task is finished, so the honest mechanism is a nudge.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01A47aX5DJwmA3QFn3pkEWi7
2026-08-24 15:53:12 +02:00

106 lines
3.2 KiB
Python

"""The staleness reporter: warns, never fails."""
from __future__ import annotations
import subprocess
from pathlib import Path
from md_naming import stale
from md_naming.tests.conftest import write
NOW = 1_800_000_000
def commit(repo: Path, when: int) -> None:
"""Commit everything in `repo` with both dates pinned to `when`."""
stamp = f"{when} +0000"
env = {"GIT_AUTHOR_DATE": stamp, "GIT_COMMITTER_DATE": stamp}
subprocess.run(["git", "-C", str(repo), "add", "-A"], check=True)
subprocess.run(
[
"git",
"-C",
str(repo),
"-c",
"user.email=t@t",
"-c",
"user.name=t",
"commit",
"-qm",
"x",
],
check=True,
env={**dict(__import__("os").environ), **env},
)
def test_old_todo_is_reported(repo: Path) -> None:
write(repo, "TODO-a.md", "REMOVE ME AFTER FINISH\n")
commit(repo, NOW - 100 * 86_400)
found = stale.stale_todos(repo, limit=90, now=NOW)
assert [p.name for p, _ in found] == ["TODO-a.md"]
assert found[0][1] == 100
def test_fresh_todo_is_not_reported(repo: Path) -> None:
write(repo, "TODO-a.md", "REMOVE ME AFTER FINISH\n")
commit(repo, NOW - 5 * 86_400)
assert stale.stale_todos(repo, limit=90, now=NOW) == []
def test_non_todo_ignored(repo: Path) -> None:
write(repo, "DOCS-a.md")
commit(repo, NOW - 500 * 86_400)
assert stale.stale_todos(repo, limit=90, now=NOW) == []
def test_uncommitted_todo_has_unknown_age(repo: Path) -> None:
"""A file git has never seen yields no date, so it is not reported."""
write(repo, "TODO-a.md", "REMOVE ME AFTER FINISH\n")
assert stale.stale_todos(repo, limit=0, now=NOW) == []
def test_age_days_outside_repo(tmp_path: Path) -> None:
assert stale.age_days(tmp_path / "x.md", tmp_path, NOW) is None
def test_main_always_exits_zero(repo: Path, monkeypatch, capsys) -> None:
write(repo, "TODO-a.md", "REMOVE ME AFTER FINISH\n")
commit(repo, NOW - 999 * 86_400)
monkeypatch.chdir(repo)
monkeypatch.setattr("sys.argv", ["stale", "--days", "1"])
assert stale.main() == 0
assert "::warning" in capsys.readouterr().out
def test_last_commit_epoch_when_git_missing(repo: Path, monkeypatch) -> None:
"""No git binary means unknown age, not a crash."""
def boom(*args, **kwargs):
raise OSError("no git")
monkeypatch.setattr(stale.subprocess, "run", boom)
assert stale.last_commit_epoch(repo / "TODO-a.md", repo) is None
def test_last_commit_epoch_non_numeric(repo: Path, monkeypatch) -> None:
"""Garbage on stdout is treated as unknown rather than parsed."""
class Result:
returncode = 0
stdout = "not-a-number\n"
monkeypatch.setattr(stale.subprocess, "run", lambda *a, **k: Result())
assert stale.last_commit_epoch(repo / "TODO-a.md", repo) is None
def test_main_reports_absolute_path_outside_root(
repo: Path, monkeypatch, capsys
) -> None:
"""A TODO outside the walk root still prints, using its full path."""
write(repo, "TODO-a.md", "REMOVE ME AFTER FINISH\n")
commit(repo, NOW - 999 * 86_400)
monkeypatch.chdir(repo)
monkeypatch.setattr("sys.argv", ["stale"])
assert stale.main() == 0