import sys
from importlib.metadata import Distribution, PackageNotFoundError
from pathlib import Path
import pytest
from warden import _find_warden
from warden._find_warden import WardenNotFound, find_warden_bin
EXE_NAME = "warden" + (".exe" if sys.platform == "win32" else "")
def _write_record(site_packages: Path, entries: list[str]) -> Path:
dist_info = site_packages / "warden_cli-0.1.0.dist-info"
dist_info.mkdir(parents=True, exist_ok=True)
dist_info.joinpath("RECORD").write_text("".join(f"{e},,\n" for e in entries))
return dist_info
def _patch_lookup(monkeypatch: pytest.MonkeyPatch, dist_info: Path | None) -> None:
def lookup(name: str) -> Distribution:
if dist_info is None:
raise PackageNotFoundError(name)
return Distribution.at(dist_info)
monkeypatch.setattr(_find_warden, "distribution", lookup)
def _touch(path: Path) -> Path:
path.parent.mkdir(parents=True, exist_ok=True)
path.touch()
return path
def test_venv_install_resolves_relative_record_entry(tmp_path, monkeypatch):
venv = tmp_path / "venv"
binary = _touch(venv / "bin" / EXE_NAME)
dist_info = _write_record(
venv / "lib" / "python3.12" / "site-packages",
[f"../../../bin/{EXE_NAME}"],
)
_patch_lookup(monkeypatch, dist_info)
assert find_warden_bin() == str(binary)
def test_target_install_resolves_sibling_bin_entry(tmp_path, monkeypatch):
target = tmp_path / "X"
binary = _touch(target / "bin" / EXE_NAME)
_touch(target / "bin" / "other-tool")
dist_info = _write_record(target, ["bin/other-tool", f"bin/{EXE_NAME}"])
_patch_lookup(monkeypatch, dist_info)
assert find_warden_bin() == str(binary)
def test_no_distribution_raises(monkeypatch):
_patch_lookup(monkeypatch, None)
with pytest.raises(WardenNotFound, match="No installed `warden-cli` distribution"):
find_warden_bin()
def test_no_record_raises(tmp_path, monkeypatch):
dist_info = tmp_path / "warden_cli-0.1.0.dist-info"
dist_info.mkdir()
_patch_lookup(monkeypatch, dist_info)
with pytest.raises(WardenNotFound, match="no RECORD"):
find_warden_bin()
def test_no_matching_entry_raises(tmp_path, monkeypatch):
site_packages = tmp_path / "site-packages"
_touch(site_packages / "warden" / "__init__.py")
dist_info = _write_record(site_packages, ["warden/__init__.py", f"bin/{EXE_NAME}"])
_patch_lookup(monkeypatch, dist_info)
with pytest.raises(WardenNotFound, match="No .* script entry"):
find_warden_bin()
def test_binary_not_named_by_record_is_never_returned(tmp_path, monkeypatch):
_touch(tmp_path / "somewhere-else" / "bin" / EXE_NAME)
dist_info = _write_record(tmp_path / "X", [])
_patch_lookup(monkeypatch, dist_info)
with pytest.raises(WardenNotFound):
find_warden_bin()
def test_package_and_dist_info_entries_are_excluded(tmp_path, monkeypatch):
site_packages = tmp_path / "site-packages"
_touch(site_packages / "warden" / EXE_NAME)
_touch(site_packages / "warden_cli-0.1.0.dist-info" / EXE_NAME)
binary = _touch(site_packages / "bin" / EXE_NAME)
dist_info = _write_record(
site_packages,
[
f"warden/{EXE_NAME}",
f"warden_cli-0.1.0.dist-info/{EXE_NAME}",
f"bin/{EXE_NAME}",
],
)
_patch_lookup(monkeypatch, dist_info)
assert find_warden_bin() == str(binary)