Files
roboco/tests/unit/services/test_workspace_read_clone.py
T

95 lines
3.7 KiB
Python
Raw Normal View History

"""WorkspaceService._sync_read_clone — the conventions read-clone refresh.
The read clone is hard-reset to the default branch on every refresh. The bug it
fixes: the old refresh used a token-less fetch, so a private repo's clone stayed
frozen at clone-time and never saw commits merged afterwards. This proves the
refresh actually advances the clone to a post-clone commit.
"""
from __future__ import annotations
import subprocess
from typing import TYPE_CHECKING
from roboco.services.workspace import WorkspaceService
if TYPE_CHECKING:
from pathlib import Path
def _git(cwd: Path, *args: str) -> subprocess.CompletedProcess[str]:
return subprocess.run(
["git", *args], cwd=str(cwd), capture_output=True, text=True, check=False
)
def _commit(repo: Path, message: str) -> str:
_git(repo, "add", "-A")
_git(repo, "-c", "user.email=t@t", "-c", "user.name=t", "commit", "-qm", message)
return _git(repo, "rev-parse", "HEAD").stdout.strip()
def test_sync_read_clone_advances_to_a_post_clone_commit(tmp_path: Path) -> None:
origin = tmp_path / "origin"
origin.mkdir()
_git(origin, "init", "-q", "-b", "master")
(origin / "README.md").write_text("v1\n")
_commit(origin, "first")
clone = tmp_path / "clone"
_git(tmp_path, "clone", "-q", str(origin), str(clone))
first = _git(clone, "rev-parse", "HEAD").stdout.strip()
# A new commit lands on origin AFTER the clone — the frozen-clone scenario.
(origin / "NEW.txt").write_text("added later\n")
second = _commit(origin, "second")
assert first != second
# token=None: a file:// fetch needs no auth (mirrors a public-repo refresh);
# for a private https repo the token would be injected into the fetch URL.
WorkspaceService._sync_read_clone(clone, f"file://{origin}", "master", None)
assert _git(clone, "rev-parse", "HEAD").stdout.strip() == second
assert (clone / "NEW.txt").is_file()
def test_sync_read_clone_fetches_tags(tmp_path: Path) -> None:
"""The read clone must carry tags: the release manager derives "commits
since last release" from the newest tag, and a tagless clone makes
``git describe`` fail so it walks the entire history (the 729-commit bug)."""
origin = tmp_path / "origin"
origin.mkdir()
_git(origin, "init", "-q", "-b", "master")
(origin / "README.md").write_text("v1\n")
_commit(origin, "first")
# The clone itself is tagless (--no-tags, as agent clones are) — the tag
# lands on origin and the read-clone refresh must pull it in.
clone = tmp_path / "clone"
_git(tmp_path, "clone", "-q", "--no-tags", str(origin), str(clone))
assert _git(clone, "tag").stdout.strip() == ""
# -c tag.gpgsign=false: create a lightweight tag regardless of a global
# signing config that would otherwise force an annotated (signed) tag.
_git(origin, "-c", "tag.gpgsign=false", "tag", "v0.17.0")
WorkspaceService._sync_read_clone(clone, f"file://{origin}", "master", None)
assert "v0.17.0" in _git(clone, "tag").stdout.split()
assert _git(clone, "describe", "--tags", "--abbrev=0").stdout.strip() == "v0.17.0"
def test_sync_read_clone_is_best_effort_on_unreachable_origin(tmp_path: Path) -> None:
origin = tmp_path / "origin"
origin.mkdir()
_git(origin, "init", "-q", "-b", "master")
(origin / "README.md").write_text("v1\n")
_commit(origin, "first")
clone = tmp_path / "clone"
_git(tmp_path, "clone", "-q", str(origin), str(clone))
head = _git(clone, "rev-parse", "HEAD").stdout.strip()
# A bogus origin must not raise — the refresh logs and leaves the clone as-is.
WorkspaceService._sync_read_clone(clone, "file:///nonexistent/repo", "master", None)
assert _git(clone, "rev-parse", "HEAD").stdout.strip() == head