utils/devstats/tests/test_refs.py
Krzysztof kuhy Rudnicki f370d313b5
Some checks are pending
file length / file-length (push) Waiting to run
kuhy_credit drift / drift (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
palette drift / palette (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
shared gate tests / gate unit tests (push) Waiting to run
devstats: run.sh, HTML report, origin refs
devstats/run.sh installs python3/git via pacman when missing, refuses
python < 3.11, runs `compare` (capped when capped.sh exists), prints the
tables, writes ~/data/devstats/report.html (bar chart above the tables)
and opens it unless --no-open.

The local default branch is now logged together with its origin/
tracking ref (union, each SHA once), so the 16 "behind its upstream
tracking ref" warnings are gone without pulling any checkout.

Bug-class-check: devstats/tests/test_refs.py
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 13:32:40 +01:00

64 lines
2.6 KiB
Python

"""Default-branch resolution: the local branch plus its origin/ tracking ref."""
from __future__ import annotations
from devstats.refs import Ref, list_refs, resolve, tag_times
from .conftest import DAY, NOW, lines, run
def ref(ts: int, sha: str = "s", symref: str = "") -> Ref:
return Ref(sha=sha, ts=ts, symref=symref, created=ts)
def test_origin_head_names_the_branch_and_both_tips_are_logged() -> None:
refs = {
"refs/remotes/origin/HEAD": ref(0, symref="refs/remotes/origin/trunk"),
"refs/remotes/origin/trunk": ref(NOW, sha="o"),
"refs/heads/trunk": ref(NOW - 2 * DAY - DAY // 2, sha="l"),
"refs/heads/main": ref(NOW),
}
got = resolve(refs)
assert got.branch == "trunk"
assert got.log_refs == ("refs/heads/trunk", "refs/remotes/origin/trunk")
def test_falls_back_to_main_then_master_without_origin() -> None:
assert resolve({"refs/heads/main": ref(1)}).log_refs == ("refs/heads/main",)
got = resolve({"refs/heads/master": ref(1), "refs/heads/dev": ref(2)})
assert (got.branch, got.log_refs) == ("master", ("refs/heads/master",))
def test_origin_only_branch_is_not_counted() -> None:
got = resolve({"refs/remotes/origin/HEAD": ref(0, symref="refs/remotes/origin/main"),
"refs/remotes/origin/main": ref(1)})
assert (got.branch, got.log_refs) == (None, ())
def test_no_default_branch() -> None:
got = resolve({"refs/heads/dev": ref(1)})
assert got.branch is None
assert got.log_refs == ()
def test_equal_tips_log_once_any_difference_logs_both() -> None:
same = {"refs/heads/main": ref(5, sha="a"), "refs/remotes/origin/main": ref(5, sha="a")}
ahead = {"refs/heads/main": ref(9, sha="a"), "refs/remotes/origin/main": ref(5, sha="b")}
assert resolve(same).log_refs == ("refs/heads/main",)
assert resolve(ahead).log_refs == ("refs/heads/main", "refs/remotes/origin/main")
def test_list_refs_reads_branches_remotes_and_tags(make_repo) -> None:
repo = make_repo("r")
repo.commit({"a.py": lines(1)}, NOW - 10 * DAY)
repo.tag("v1", NOW - 9 * DAY, annotated=True)
repo.tag("light", NOW - 8 * DAY)
run(repo.path, "update-ref", "refs/remotes/origin/main", "HEAD")
run(repo.path, "symbolic-ref", "refs/remotes/origin/HEAD", "refs/remotes/origin/main")
refs = list_refs(repo.path)
assert refs["refs/remotes/origin/HEAD"].symref == "refs/remotes/origin/main"
assert refs["refs/heads/main"].ts == NOW - 10 * DAY
# annotated: tagger date; lightweight: the commit's date
assert sorted(ts for _, ts in tag_times(refs)) == [NOW - 10 * DAY, NOW - 9 * DAY]
assert refs["refs/tags/light"].sha in dict(tag_times(refs))