"""Unit: bot_bottle.resources — build-resource resolution for both a source checkout and an installed wheel. The checkout path is what the whole test suite already runs under; the wheel path is exercised here by faking an installed layout (a package dir with a bundled ``_resources/`` and no sibling Dockerfiles) and asserting that ``build_root()`` stages a repo-root-shaped context. See ``test_wheel_install.py`` for the end-to-end build+install check. """ from __future__ import annotations import unittest from pathlib import Path from unittest.mock import patch from bot_bottle import resources from tests.unit import use_bottle_root class TestCheckoutMode(unittest.TestCase): """The environment the suite runs in: a real source checkout.""" def test_is_source_checkout(self): self.assertTrue(resources.is_source_checkout()) def test_build_root_is_repo_root(self): root = resources.build_root() self.assertTrue((root / "bot_bottle").is_dir()) self.assertTrue((root / "pyproject.toml").is_file()) self.assertTrue((root / "Dockerfile.gateway").is_file()) def test_resource_helpers_resolve(self): self.assertTrue(resources.dockerfile("Dockerfile.gateway").is_file()) self.assertTrue(resources.nix_netpool_module().is_file()) self.assertTrue(resources.netpool_script().is_file()) def test_bundled_resources_all_exist_at_root(self): # Drift guard: every path setup.py bundles must exist in the checkout. root = resources.build_root() for rel in resources.BUNDLED_RESOURCES: self.assertTrue((root / rel).is_file(), f"missing bundled resource: {rel}") class TestDistVersion(unittest.TestCase): """The version key for the staged build root, from importlib.metadata.""" def test_returns_installed_version(self): import importlib.metadata as md with patch.object(md, "version", return_value="9.9.9"): self.assertEqual("9.9.9", resources._dist_version()) # pylint: disable=protected-access def test_falls_back_when_not_installed(self): import importlib.metadata as md with patch.object(md, "version", side_effect=md.PackageNotFoundError()): self.assertEqual("dev", resources._dist_version()) # pylint: disable=protected-access class TestWheelMode(unittest.TestCase): """Fake an installed wheel: a package dir with _resources/ and no checkout Dockerfiles beside it.""" def _fake_install(self, tmp: Path) -> Path: pkg = tmp / "site-packages" / "bot_bottle" (pkg / "cli").mkdir(parents=True) (pkg / "__init__.py").write_text("") (pkg / "cli" / "__init__.py").write_text("# module\n") bundled = pkg / "_resources" for rel in resources.BUNDLED_RESOURCES: dst = bundled / rel dst.parent.mkdir(parents=True, exist_ok=True) dst.write_text(f"# fake {rel}\n") return pkg def test_stage_and_resolve(self): import tempfile with tempfile.TemporaryDirectory() as tmpname: tmp = Path(tmpname) pkg = self._fake_install(tmp) appdata = tmp / "appdata" restore = use_bottle_root(appdata) self.addCleanup(restore) with patch.object(resources, "_PKG", pkg), \ patch.object(resources, "_BUNDLED", pkg / "_resources"), \ patch.object(resources, "_CHECKOUT_ROOT", tmp / "no-checkout"): self.assertFalse(resources.is_source_checkout()) root = resources.build_root() # Staged context looks like a repo root. self.assertTrue((root / "bot_bottle" / "__init__.py").is_file()) self.assertTrue((root / "bot_bottle" / "cli" / "__init__.py").is_file()) self.assertTrue((root / "pyproject.toml").is_file()) self.assertTrue((root / "Dockerfile.gateway").is_file()) self.assertTrue((root / "nix" / "firecracker-netpool.nix").is_file()) self.assertTrue((root / "scripts" / "firecracker-netpool.sh").is_file()) # The bundled-resource copies are NOT re-nested under the staged # package (keeps it byte-identical to a checkout package). self.assertFalse((root / "bot_bottle" / "_resources").exists()) # Helpers resolve off the staged root. self.assertEqual(root / "Dockerfile.gateway", resources.dockerfile("Dockerfile.gateway")) # Idempotent: second call returns the same completed dir. self.assertEqual(root, resources.build_root()) def test_missing_bundle_raises(self): import tempfile with tempfile.TemporaryDirectory() as tmpname: tmp = Path(tmpname) pkg = tmp / "bot_bottle" pkg.mkdir() restore = use_bottle_root(tmp / "appdata") self.addCleanup(restore) with patch.object(resources, "_PKG", pkg), \ patch.object(resources, "_BUNDLED", pkg / "_resources"), \ patch.object(resources, "_CHECKOUT_ROOT", tmp / "no-checkout"): with self.assertRaises(resources.ResourceError): resources.build_root() if __name__ == "__main__": unittest.main()