792beca2c5
Sanitized open-source starter: renamed to daedalus, Norse-free of internal references, AI attribution stripped, skill merged into AGENTS.md, CI converted to Gitea Actions. No registered scripts yet.
91 lines
3.4 KiB
Python
91 lines
3.4 KiB
Python
"""Tests for ci.sanity.
|
|
|
|
Base ref selection and changed-file listing with git stubbed.
|
|
"""
|
|
import subprocess
|
|
|
|
import pytest
|
|
|
|
from ci import checks
|
|
from ci import sanity
|
|
|
|
|
|
def fake_git(existing_refs: set[str], outputs: dict[str, str]):
|
|
"""Build a git stub: rev-parse succeeds for existing_refs, other commands return outputs."""
|
|
def run(*args: str) -> subprocess.CompletedProcess:
|
|
if args[0] == "rev-parse":
|
|
ref = args[-1].removesuffix("^{commit}")
|
|
return subprocess.CompletedProcess(args, 0 if ref in existing_refs else 1, "", "")
|
|
return subprocess.CompletedProcess(args, 0, outputs.get(" ".join(args), ""), "")
|
|
return run
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def clear_ci_env(monkeypatch):
|
|
"""Tests start without CI merge request variables."""
|
|
for name in ("SANITY_DIFF_BASE", "SANITY_TARGET_BRANCH", "GITHUB_BASE_REF"):
|
|
monkeypatch.delenv(name, raising=False)
|
|
|
|
|
|
def test_base_ref_prefers_merge_request_base(monkeypatch):
|
|
"""The PR diff base SHA wins over branch names."""
|
|
monkeypatch.setenv("SANITY_DIFF_BASE", "abc123")
|
|
monkeypatch.setattr(sanity, "git", fake_git({"abc123", "origin/main"}, {}))
|
|
assert sanity.base_ref() == "abc123"
|
|
|
|
|
|
def test_base_ref_uses_target_branch(monkeypatch):
|
|
"""The PR target branch is used when there is no base SHA."""
|
|
monkeypatch.setenv("GITHUB_BASE_REF", "develop")
|
|
monkeypatch.setattr(sanity, "git", fake_git({"origin/develop", "origin/main"}, {}))
|
|
assert sanity.base_ref() == "origin/develop"
|
|
|
|
|
|
def test_base_ref_falls_back_to_master(monkeypatch):
|
|
"""origin/master is used when origin/main is missing."""
|
|
monkeypatch.setattr(sanity, "git", fake_git({"origin/master"}, {}))
|
|
assert sanity.base_ref() == "origin/master"
|
|
|
|
|
|
def test_base_ref_none_without_remote_branch(monkeypatch):
|
|
"""No base ref exists in a fresh repository."""
|
|
monkeypatch.setattr(sanity, "git", fake_git(set(), {}))
|
|
assert sanity.base_ref() is None
|
|
|
|
|
|
def test_changed_files_diffs_against_base(tmp_path, monkeypatch):
|
|
"""Diffed and untracked files are listed; deleted paths are dropped."""
|
|
for name in ("changed.py", "new.sh"):
|
|
(tmp_path / name).write_text("", encoding="utf-8")
|
|
monkeypatch.setattr(checks, "REPO_ROOT", tmp_path)
|
|
monkeypatch.setattr(sanity, "git", fake_git({"origin/main"}, {
|
|
"diff --name-only --diff-filter=ACMR origin/main": "changed.py\ngone.py\n",
|
|
"ls-files --others --exclude-standard": "new.sh\n",
|
|
}))
|
|
assert sanity.changed_files() == [tmp_path / "changed.py", tmp_path / "new.sh"]
|
|
|
|
|
|
def test_changed_files_lists_everything_without_base(tmp_path, monkeypatch):
|
|
"""Every tracked and untracked file is checked when no base exists."""
|
|
(tmp_path / "a.md").write_text("", encoding="utf-8")
|
|
monkeypatch.setattr(checks, "REPO_ROOT", tmp_path)
|
|
monkeypatch.setattr(sanity, "git", fake_git(set(), {
|
|
"ls-files --cached --others --exclude-standard": "a.md\n",
|
|
}))
|
|
assert sanity.changed_files() == [tmp_path / "a.md"]
|
|
|
|
|
|
def test_main_counts_errors(tmp_path, monkeypatch):
|
|
"""main sums the errors of every check over every changed file."""
|
|
good = tmp_path / "good.txt"
|
|
good.write_bytes(b"ok\n")
|
|
bad = tmp_path / "bad.txt"
|
|
bad.write_bytes(b"crlf\r\n")
|
|
monkeypatch.setattr(sanity, "changed_files", lambda: [good, bad])
|
|
assert sanity.main() == 1
|
|
|
|
|
|
def test_git_runs_in_repo_root():
|
|
"""The real git helper returns a completed process."""
|
|
assert sanity.git("--version").returncode == 0
|