"""Local-source installs pin the packages the ISO built from the checkouts.""" import subprocess import sys import tempfile import types import unittest from pathlib import Path from unittest import mock ROOT = Path(__file__).resolve().parents[2] sys.path.insert(0, str(ROOT / "configs/airootfs/usr/share/omarchy-iso")) sys.modules.setdefault( "orchestrator.archinstall_adapter", types.ModuleType("orchestrator.archinstall_adapter"), ) from orchestrator import phases_impl # noqa: E402 LINE = "IgnorePkg = omarchy-dev omarchy-settings-dev" CONF = "[options]\nHoldPkg = pacman glibc\n\n[core]\nInclude = /etc/pacman.d/mirrorlist\n" class PinLocalPackagesTest(unittest.TestCase): def run_pin(self, root: Path, *, pinned: bool, defer: bool = False): (root / "etc").mkdir() (root / "etc/pacman.conf").write_text(CONF) pinned_file = root / "pinned-packages" if pinned: pinned_file.write_text("omarchy-dev\nomarchy-settings-dev\n") ctx = types.SimpleNamespace(target=root, username="test", defer_provisioning=defer) with mock.patch.object(phases_impl, "PINNED_PACKAGES", pinned_file), \ mock.patch.object(phases_impl.subprocess, "run") as run: phases_impl.pin_local_packages(ctx) phases_impl.pin_local_packages(ctx) return run def test_pins_once_and_installs_refresh_hook(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) run = self.run_pin(root, pinned=True) self.assertEqual((root / "etc/pacman.conf").read_text(), CONF.replace("[options]\n", f"[options]\n{LINE}\n")) hook = root / "home/test/.config/omarchy/hooks/pre-refresh-pacman.d/pin-local-packages" self.assertIn(f'line="{LINE}"', hook.read_text()) self.assertTrue(hook.stat().st_mode & 0o111) guard = (root / "etc/pacman.d/hooks/00-omarchy-pin-local-builds.hook").read_text() for package in ("omarchy-dev", "omarchy-settings-dev"): self.assertIn(f"Target = {package}\n", guard) self.assertIn("AbortOnFail", guard) skel = root / "etc/skel/.config/omarchy/hooks/pre-refresh-pacman.d/pin-local-packages" self.assertEqual(skel.read_text(), hook.read_text()) run.assert_called_with( ["arch-chroot", str(root), "chown", "-R", "test:test", "/home/test/.config/omarchy/hooks"], check=True, ) def test_hook_restores_the_pin_after_a_refresh(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) self.run_pin(root, pinned=True) hook = root / "home/test/.config/omarchy/hooks/pre-refresh-pacman.d/pin-local-packages" conf = root / "pacman.conf" conf.write_text(CONF) script = hook.read_text().replace("/etc/pacman.conf", str(conf)).replace("sudo ", "") for _ in range(2): subprocess.run(["bash", "-c", script], check=True) self.assertEqual(conf.read_text(), CONF.replace("[options]\n", f"[options]\n{LINE}\n")) def test_deferred_install_stages_the_hook_for_the_first_owner(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) run = self.run_pin(root, pinned=True, defer=True) self.assertIn(LINE, (root / "etc/pacman.conf").read_text()) self.assertTrue((root / "etc/pacman.d/hooks/00-omarchy-pin-local-builds.hook").exists()) self.assertTrue((root / "etc/skel/.config/omarchy/hooks/pre-refresh-pacman.d/pin-local-packages").exists()) self.assertFalse((root / "home").exists()) run.assert_not_called() def test_published_builds_stay_unpinned(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) self.run_pin(root, pinned=False) self.assertEqual((root / "etc/pacman.conf").read_text(), CONF) self.assertFalse((root / "etc/pacman.d").exists()) if __name__ == "__main__": unittest.main()