git/orchard/main
tests/test_changes.py
81 lines3 KB last change raw · history
"""Version-to-commit resolution against a real git history.

These run against a temporary repository built for the test, so the pickaxe
search and the history walk are exercised rather than stubbed.
"""

from __future__ import annotations

import subprocess
import sys
import tempfile
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

from updater import changes  # noqa: E402


def _repo(tmp: Path) -> Path:
    def run(*args: str) -> None:
        subprocess.run(
            ["git", "-C", str(tmp), "-c", "commit.gpgsign=false", *args],
            check=True,
            capture_output=True,
        )

    run("init", "-q")
    run("config", "user.email", "t@example.invalid")
    run("config", "user.name", "t")
    return tmp


def _commit(repo: Path, src: str, message: str) -> str:
    (repo / "PKGBUILD").write_text(src)
    subprocess.run(
        ["git", "-C", str(repo), "-c", "commit.gpgsign=false", "add", "PKGBUILD"],
        check=True,
        capture_output=True,
    )
    subprocess.run(
        ["git", "-C", str(repo), "-c", "commit.gpgsign=false", "commit", "-qm", message],
        check=True,
        capture_output=True,
    )
    return subprocess.run(
        ["git", "-C", str(repo), "rev-parse", "HEAD"], check=True, capture_output=True, text=True
    ).stdout.strip()


def test_the_version_match_includes_pkgrel_and_epoch():
    """`pkgver` alone is not the version.

    A maintainer bumps `pkgrel` for a dependency rebuild without touching
    `pkgver`, so matching on pkgver alone resolves both `1.0-1` and `1.0-2` to
    the commit that first declared `1.0` -- and the diff is then taken against a
    revision the machine never had, which makes everything the packager changed
    in between look like a fresh finding.
    """
    assert changes.declared_version("pkgname=x\npkgver=1.0\npkgrel=2\n") == "1.0-2"
    assert changes.declared_version("pkgname=x\npkgver=1.0\npkgrel=1\n") == "1.0-1"
    assert changes.declared_version("pkgname=x\nepoch=2\npkgver=1.0\npkgrel=3\n") == "2:1.0-3"
    assert changes.declared_version("pkgname=x\n") == ""


def test_a_pkgrel_bump_resolves_to_its_own_commit():
    with tempfile.TemporaryDirectory() as td:
        repo = _repo(Path(td))
        first = _commit(repo, "pkgname=x\npkgver=1.0\npkgrel=1\n", "1.0-1")
        second = _commit(repo, "pkgname=x\npkgver=1.0\npkgrel=2\n", "1.0-2")

        assert changes.commit_for_version(repo, "1.0-2") == second
        assert changes.commit_for_version(repo, "1.0-1") == first


def test_an_unknown_version_resolves_to_nothing():
    with tempfile.TemporaryDirectory() as td:
        repo = _repo(Path(td))
        _commit(repo, "pkgname=x\npkgver=1.0\npkgrel=1\n", "1.0-1")
        assert changes.commit_for_version(repo, "9.9-9") == ""
        assert changes.commit_for_version(repo, "") == ""