55 lines
2.1 KiB
Python
55 lines
2.1 KiB
Python
import importlib.util
|
|
from pathlib import Path
|
|
import subprocess
|
|
|
|
import pytest
|
|
|
|
|
|
SCRIPT = Path(__file__).parents[1] / "scripts/install_shared_generated_guard.py"
|
|
spec = importlib.util.spec_from_file_location("shared_guard", SCRIPT)
|
|
guard = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(guard)
|
|
|
|
|
|
def repository(tmp_path):
|
|
repo = tmp_path / "repo"
|
|
repo.mkdir()
|
|
subprocess.run(["git", "init", str(repo)], check=True, capture_output=True)
|
|
hooks = tmp_path / "shared-hooks"
|
|
hooks.mkdir()
|
|
subprocess.run(["git", "-C", str(repo), "config", "core.hooksPath", str(hooks)], check=True)
|
|
return repo, hooks
|
|
|
|
|
|
def test_real_shared_hook_rejects_generated_only_and_retains_other_hooks(tmp_path):
|
|
repo, hooks = repository(tmp_path)
|
|
other = hooks / "prepare-commit-msg"
|
|
other.write_text("# existing owner hook\n")
|
|
before = other.read_bytes()
|
|
assert guard.install(repo)["status"] == "missing"
|
|
assert not (hooks / "pre-commit").exists()
|
|
assert guard.install(repo, True)["changed"]
|
|
assert not guard.install(repo, True)["changed"]
|
|
(repo / "WORK-RECORDS.md").write_text("derived\n")
|
|
subprocess.run(["git", "-C", str(repo), "add", "WORK-RECORDS.md"], check=True)
|
|
assert subprocess.run([str(hooks / "pre-commit")], cwd=repo, capture_output=True).returncode == 1
|
|
(repo / "implementation.py").write_text("print('source')\n")
|
|
subprocess.run(["git", "-C", str(repo), "add", "implementation.py"], check=True)
|
|
assert subprocess.run([str(hooks / "pre-commit")], cwd=repo, capture_output=True).returncode == 0
|
|
assert other.read_bytes() == before
|
|
|
|
|
|
def test_foreign_or_redirected_guard_is_never_replaced(tmp_path):
|
|
repo, hooks = repository(tmp_path)
|
|
hook = hooks / "pre-commit"
|
|
hook.write_text("owner implementation\n")
|
|
with pytest.raises(ValueError):
|
|
guard.install(repo, True)
|
|
assert hook.read_text() == "owner implementation\n"
|
|
hook.unlink()
|
|
private = tmp_path / "unrelated"
|
|
private.write_text("retained\n")
|
|
hook.symlink_to(private)
|
|
with pytest.raises(ValueError):
|
|
guard.install(repo, True)
|
|
assert private.read_text() == "retained\n"
|