"""GitSource.stable_versions 模板方法单测(github/gitcode 共享的 tag 拉取逻辑)。
跑法(仓库根):uv run pytest tests/test_git_source.py -q
零网络:git() 与 ensure_clone 打桩,只验 fetch 参数、重试、解析与 tag 过滤。"""
import subprocess
from pathlib import Path
import pytest
from src.sources import base
class _FakeSource(base.GitSource):
CORPUS_NAME = "fake"
CLONES = Path("/nonexistent")
FETCH_ARGS = ("-c", "x=1")
def repos(self) -> list[tuple[str, str | None]]:
return [("r", None)]
def clone_url(self, repo: str) -> str:
return f"https://x/{repo}.git"
def is_stable_tag(self, name: str) -> bool:
return not name.endswith("rc1")
def build_docs(self, repo: str, docs: Path, out: Path) -> dict[str, int]:
return {}
def ensure_clone(self, repo: str, branch: str | None) -> Path:
return Path("/clone/r")
def test_stable_versions_parses_and_filters(monkeypatch):
calls: list[tuple[str, ...]] = []
def fake_git(clone: Path, *args: str) -> str:
calls.append(args)
if "fetch" in args:
return ""
return "v1.0.0 1700000000\nv1.1.0rc1 1700000001\nv0.9.0 1699999999"
monkeypatch.setattr(base, "git", fake_git)
got = _FakeSource().stable_versions("r", None)
assert [v["tagName"] for v in got] == ["v1.0.0", "v0.9.0"]
assert got[0]["publishedAt"] == "2023-11-14T22:13:20Z"
assert calls[0] == ("-c", "x=1", "fetch", "--tags", "--depth", "1", "origin")
def test_stable_versions_fetch_retries_then_raises(monkeypatch):
calls: list[tuple[str, ...]] = []
def fake_git(clone: Path, *args: str) -> str:
calls.append(args)
if "fetch" in args:
raise subprocess.CalledProcessError(128, "git")
return ""
monkeypatch.setattr(base, "git", fake_git)
monkeypatch.setattr(base.time, "sleep", lambda s: None)
with pytest.raises(subprocess.CalledProcessError):
_FakeSource().stable_versions("r", None)
assert sum("fetch" in c for c in calls) == 3