"""The configurator's region reaches the target setup as OMARCHY_REGION.""" import json import os 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 context, phases_impl # noqa: E402 def load_context(root: Path, omarchy_install: dict | None) -> context.InstallContext: config = {"kernels": ["linux-omarchy"]} if omarchy_install is not None: config["omarchy_install"] = omarchy_install config_path = root / "config.json" creds_path = root / "creds.json" config_path.write_text(json.dumps(config)) creds_path.write_text(json.dumps({"users": [{"username": "test"}]})) with mock.patch.dict(os.environ, { "OMARCHY_INSTALL_CONFIG": str(config_path), "OMARCHY_INSTALL_CREDS": str(creds_path), "OMARCHY_INSTALL_STATE_DIR": str(root / "state"), }, clear=True): return context.InstallContext.from_env() class RegionTest(unittest.TestCase): def test_context_reads_region(self): for omarchy_install, expected in [ (None, "global"), ({"mode": "full_disk"}, "global"), ({"mode": "full_disk", "region": "global"}, "global"), ({"mode": "full_disk", "region": "china"}, "china"), ]: with self.subTest(omarchy_install=omarchy_install), tempfile.TemporaryDirectory() as tmp: ctx = load_context(Path(tmp), omarchy_install) self.assertEqual(ctx.omarchy_install["region"], expected) def test_context_rejects_unknown_region(self): for region in ("China", "mars", ""): with self.subTest(region=region), tempfile.TemporaryDirectory() as tmp: with self.assertRaisesRegex(RuntimeError, "omarchy_install.region must be global or china"): load_context(Path(tmp), {"mode": "full_disk", "region": region}) def test_target_setup_receives_region(self): for region in ("global", "china"): with self.subTest(region=region), tempfile.TemporaryDirectory() as tmp: root = Path(tmp) ctx = types.SimpleNamespace( target=root / "mnt", log_path=root / "install.log", omarchy_install={"region": region}, username="test", full_name="", email="", state={"omarchy_start_time": "now", "omarchy_start_epoch": 0, "omarchy_finalizer_header_written": True}, ) with mock.patch.object(phases_impl, "_prepare_target_setup"), \ mock.patch.object(phases_impl.subprocess, "run") as run: phases_impl._run_target_setup_command(ctx, ["/usr/bin/omarchy-apply-system"]) chroot = next(call.args[0] for call in run.call_args_list if call.args[0][0] == "arch-chroot") self.assertIn(f"OMARCHY_REGION={region}", chroot) if __name__ == "__main__": unittest.main()