tests / unittest (push) Failing after 13s
Stage 1 flags the living documents a pull request makes stale (no model, nothing sent). Stage 2, on repositories that opt in with drafter: claude, has Claude Code draft the edits in a sandboxed copy of the tree and opens them as a pull request into the feature branch. A monthly pass re-reads the top-level documents against the whole repository. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
189 lines
6.9 KiB
Python
189 lines
6.9 KiB
Python
"""Shared test helpers. Later tasks add make_repo and FakeGitea."""
|
|
|
|
import tempfile
|
|
from collections.abc import Iterable
|
|
from pathlib import Path
|
|
|
|
|
|
def make_tree(root: Path, files: Iterable[str]) -> None:
|
|
"""Create each repository-relative file under root, with empty content."""
|
|
for rel in files:
|
|
path = root / rel
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text("", encoding="utf-8")
|
|
|
|
|
|
def tmp_tree(files: Iterable[str]) -> tempfile.TemporaryDirectory:
|
|
"""A temporary directory holding the given files; use as a context manager."""
|
|
tmp = tempfile.TemporaryDirectory()
|
|
make_tree(Path(tmp.name), files)
|
|
return tmp
|
|
|
|
|
|
import subprocess
|
|
|
|
|
|
def _git(cwd: Path, *args: str) -> None:
|
|
subprocess.run(
|
|
["git", *args], cwd=cwd, check=True, capture_output=True, text=True
|
|
)
|
|
|
|
|
|
def make_repo(tmp: Path) -> Path:
|
|
"""Initialize a git repo under tmp with two commits.
|
|
|
|
Commit 1 adds a.txt; commit 2 modifies a.txt and adds b.txt. Local user config
|
|
is set and gpg signing is off.
|
|
"""
|
|
repo = Path(tmp)
|
|
repo.mkdir(parents=True, exist_ok=True)
|
|
_git(repo, "init", "-q", "-b", "main")
|
|
_git(repo, "config", "user.name", "Test User")
|
|
_git(repo, "config", "user.email", "test@example.org")
|
|
_git(repo, "config", "commit.gpgsign", "false")
|
|
(repo / "a.txt").write_text("one\n", encoding="utf-8")
|
|
_git(repo, "add", "a.txt")
|
|
_git(repo, "commit", "-q", "-m", "first")
|
|
(repo / "a.txt").write_text("one\ntwo\n", encoding="utf-8")
|
|
(repo / "b.txt").write_text("b\n", encoding="utf-8")
|
|
_git(repo, "add", "a.txt", "b.txt")
|
|
_git(repo, "commit", "-q", "-m", "second")
|
|
return repo
|
|
|
|
|
|
class FakeGitea:
|
|
"""In-memory stand-in for docs_review.forge.Gitea; records calls in .calls."""
|
|
|
|
def __init__(self, login="docs-review-bot", email="docs-review-bot@example.org",
|
|
repo="o/r"):
|
|
self.repo = repo
|
|
self.login = login
|
|
self.email = email
|
|
self.comments: dict[int, list[dict]] = {}
|
|
self.prs: list[dict] = []
|
|
self.issues: list[dict] = []
|
|
self.calls: list[tuple] = []
|
|
self._next = 100
|
|
|
|
def _id(self) -> int:
|
|
self._next += 1
|
|
return self._next
|
|
|
|
def identity(self) -> tuple[str, str]:
|
|
self.calls.append(("identity",))
|
|
return self.login, self.email
|
|
|
|
def find_comment(self, issue: int, marker: str) -> dict | None:
|
|
self.calls.append(("find_comment", issue, marker))
|
|
for c in reversed(self.comments.get(issue, [])):
|
|
if c["user"]["login"] == self.login and marker in c["body"]:
|
|
return c
|
|
return None
|
|
|
|
def upsert_comment(self, issue: int, marker: str, body: str) -> None:
|
|
self.calls.append(("upsert_comment", issue, marker, body))
|
|
found = self.find_comment(issue, marker)
|
|
self.calls.pop() # drop the inner find_comment record
|
|
if found:
|
|
found["body"] = body
|
|
else:
|
|
self.comments.setdefault(issue, []).append(
|
|
{"id": self._id(), "body": body, "user": {"login": self.login}}
|
|
)
|
|
|
|
def _own(self, pr: dict) -> bool:
|
|
return (pr["user"]["login"] == self.login
|
|
and pr["head"]["repo"]["full_name"] == self.repo)
|
|
|
|
def add_foreign_pr(self, head: str, body: str, *, login="mallory", repo=None,
|
|
state="open") -> int:
|
|
"""A PR on branch head by someone else (or from another repo), as Gitea lists it."""
|
|
number = self._id()
|
|
self.prs.append(
|
|
{"number": number, "user": {"login": login},
|
|
"head": {"ref": head, "repo": {"full_name": repo or self.repo}},
|
|
"base": {"ref": "main"}, "title": "foreign", "body": body,
|
|
"state": state, "merged": False})
|
|
return number
|
|
|
|
def find_open_pr_prefix(self, head_prefix: str) -> dict | None:
|
|
self.calls.append(("find_open_pr_prefix", head_prefix))
|
|
for pr in self.prs:
|
|
if self._own(pr) and pr["state"] == "open" and pr["head"]["ref"].startswith(head_prefix):
|
|
return pr
|
|
return None
|
|
|
|
def find_open_pr(self, head: str) -> dict | None:
|
|
self.calls.append(("find_open_pr", head))
|
|
return self._newest(head, "open")
|
|
|
|
def find_pr(self, head: str, state: str = "all") -> dict | None:
|
|
self.calls.append(("find_pr", head, state))
|
|
return self._newest(head, state)
|
|
|
|
def _newest(self, head: str, state: str) -> dict | None:
|
|
match = [p for p in self.prs
|
|
if self._own(p) and p["head"]["ref"] == head and state in ("all", p["state"])]
|
|
return max(match, key=lambda p: p["number"], default=None)
|
|
|
|
def merge_pr(self, number: int) -> None:
|
|
self.calls.append(("merge_pr", number))
|
|
pr = self._pr(number)
|
|
pr["state"], pr["merged"] = "closed", True
|
|
|
|
def open_pr(self, head: str, base: str, title: str, body: str) -> int:
|
|
self.calls.append(("open_pr", head, base, title, body))
|
|
number = self._id()
|
|
self.prs.append(
|
|
{"number": number, "user": {"login": self.login},
|
|
"head": {"ref": head, "repo": {"full_name": self.repo}}, "base": {"ref": base},
|
|
"title": title,
|
|
"body": body, "state": "open", "merged": False}
|
|
)
|
|
return number
|
|
|
|
def _pr(self, number: int) -> dict:
|
|
return next(p for p in self.prs if p["number"] == number)
|
|
|
|
def edit_pr(self, number: int, *, title=None, body=None) -> None:
|
|
self.calls.append(("edit_pr", number, title, body))
|
|
pr = self._pr(number)
|
|
if title is not None:
|
|
pr["title"] = title
|
|
if body is not None:
|
|
pr["body"] = body
|
|
|
|
def close_pr(self, number: int, comment: str) -> None:
|
|
self.calls.append(("close_pr", number, comment))
|
|
self.comments.setdefault(number, []).append(
|
|
{"id": self._id(), "body": comment, "user": {"login": self.login}}
|
|
)
|
|
self._pr(number)["state"] = "closed"
|
|
|
|
def find_open_issue(self, marker: str) -> dict | None:
|
|
self.calls.append(("find_open_issue", marker))
|
|
for i in self.issues:
|
|
if i["state"] == "open" and marker in i["body"]:
|
|
return i
|
|
return None
|
|
|
|
def open_issue(self, title: str, body: str) -> int:
|
|
self.calls.append(("open_issue", title, body))
|
|
number = self._id()
|
|
self.issues.append(
|
|
{"number": number, "title": title, "body": body, "state": "open"}
|
|
)
|
|
return number
|
|
|
|
def edit_issue(self, number: int, *, title=None, body=None) -> None:
|
|
self.calls.append(("edit_issue", number, title, body))
|
|
issue = next(i for i in self.issues if i["number"] == number)
|
|
if title is not None:
|
|
issue["title"] = title
|
|
if body is not None:
|
|
issue["body"] = body
|
|
|
|
def close_issue(self, number: int) -> None:
|
|
self.calls.append(("close_issue", number))
|
|
next(i for i in self.issues if i["number"] == number)["state"] = "closed"
|