from __future__ import annotations import json import os import subprocess import sys import types from pathlib import Path from types import SimpleNamespace import pytest def test_cache_dir_defaults_to_home_cache(monkeypatch, tmp_path: Path): # Ensure default path uses ~/.cache when XDG_CACHE_HOME is unset. from enroll.cache import enroll_cache_dir monkeypatch.delenv("XDG_CACHE_HOME", raising=False) monkeypatch.setattr(Path, "home", lambda: tmp_path) p = enroll_cache_dir() assert str(p).startswith(str(tmp_path)) assert p.name == "enroll" def test_harvest_cache_state_json_property(tmp_path: Path): from enroll.cache import HarvestCache hc = HarvestCache(tmp_path / "h1") assert hc.state_json == hc.dir / "state.json" def test_cache_dir_security_rejects_symlink(tmp_path: Path): from enroll.cache import _ensure_dir_secure real = tmp_path / "real" real.mkdir() link = tmp_path / "link" link.symlink_to(real, target_is_directory=True) with pytest.raises(RuntimeError, match="Refusing to use symlink"): _ensure_dir_secure(link) def test_cache_dir_chmod_failures_are_ignored(monkeypatch, tmp_path: Path): from enroll import cache # Make the cache base path deterministic and writable. monkeypatch.setattr(cache, "enroll_cache_dir", lambda: tmp_path) # Force os.chmod to fail to cover the "except OSError: pass" paths. monkeypatch.setattr( os, "chmod", lambda *a, **k: (_ for _ in ()).throw(OSError("nope")) ) hc = cache.new_harvest_cache_dir() assert hc.dir.exists() assert hc.dir.is_dir() def test_stat_triplet_falls_back_to_numeric_ids(monkeypatch, tmp_path: Path): from enroll.fsutil import stat_triplet import pwd import grp p = tmp_path / "x" p.write_text("x", encoding="utf-8") # Force username/group resolution failures. monkeypatch.setattr( pwd, "getpwuid", lambda _uid: (_ for _ in ()).throw(KeyError("no user")) ) monkeypatch.setattr( grp, "getgrgid", lambda _gid: (_ for _ in ()).throw(KeyError("no group")) ) owner, group, mode = stat_triplet(str(p)) assert owner.isdigit() assert group.isdigit() assert len(mode) == 4 def test_ignore_policy_iter_effective_lines_removes_block_comments(): from enroll.ignore import IgnorePolicy pol = IgnorePolicy() data = b"""keep1 /* drop me */ keep2 """ assert list(pol.iter_effective_lines(data)) == [b"keep1", b"keep2"] def test_ignore_policy_deny_reason_dir_variants(tmp_path: Path): from enroll.ignore import IgnorePolicy pol = IgnorePolicy() # denied by glob assert pol.deny_reason_dir("/etc/shadow") == "denied_path" # symlink rejected d = tmp_path / "d" d.mkdir() link = tmp_path / "l" link.symlink_to(d, target_is_directory=True) assert pol.deny_reason_dir(str(link)) == "symlink" # not a directory f = tmp_path / "f" f.write_text("x", encoding="utf-8") assert pol.deny_reason_dir(str(f)) == "not_directory" # ok assert pol.deny_reason_dir(str(d)) is None def test_run_jinjaturtle_parses_outputs(monkeypatch, tmp_path: Path): # Fully unit-test enroll.jinjaturtle.run_jinjaturtle by stubbing subprocess.run. from enroll.jinjaturtle import run_jinjaturtle def fake_run(cmd, **kwargs): # noqa: ARG001 # cmd includes "-d -t