145 lines
6.1 KiB
Python
145 lines
6.1 KiB
Python
from __future__ import annotations
|
|
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
|
|
META_ROOT = Path(__file__).resolve().parents[1]
|
|
SCRIPT = META_ROOT / "tools" / "checks" / "check_dependency_boundaries.py"
|
|
|
|
|
|
def load_boundary_module():
|
|
spec = importlib.util.spec_from_file_location("check_dependency_boundaries", SCRIPT)
|
|
if spec is None or spec.loader is None:
|
|
raise RuntimeError(f"Could not load {SCRIPT}")
|
|
module = importlib.util.module_from_spec(spec)
|
|
sys.modules[spec.name] = module
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
class DependencyBoundaryDiscoveryTests(unittest.TestCase):
|
|
def test_editable_install_metadata_is_not_treated_as_a_source_package(self) -> None:
|
|
boundary = load_boundary_module()
|
|
with tempfile.TemporaryDirectory(prefix="govoplan-boundary-discovery-") as directory:
|
|
root = Path(directory)
|
|
package = root / "govoplan-example" / "src" / "govoplan_example"
|
|
package.mkdir(parents=True)
|
|
(package / "__init__.py").write_text("", encoding="utf-8")
|
|
(package.parent / "govoplan_example.egg-info").mkdir()
|
|
|
|
repos, prefixes, errors = boundary._discover_python_repos(root)
|
|
|
|
self.assertEqual(errors, ())
|
|
self.assertEqual(repos, {"govoplan-example": package})
|
|
self.assertEqual(prefixes, {"govoplan-example": "govoplan_example"})
|
|
|
|
def test_ambiguous_or_missing_source_packages_fail_discovery(self) -> None:
|
|
boundary = load_boundary_module()
|
|
with tempfile.TemporaryDirectory(prefix="govoplan-boundary-discovery-") as directory:
|
|
root = Path(directory)
|
|
missing_source = root / "govoplan-missing" / "src"
|
|
missing_source.mkdir(parents=True)
|
|
(missing_source / "govoplan_missing.egg-info").mkdir()
|
|
|
|
ambiguous_source = root / "govoplan-ambiguous" / "src"
|
|
for name in ("govoplan_alpha", "govoplan_beta"):
|
|
package = ambiguous_source / name
|
|
package.mkdir(parents=True)
|
|
(package / "__init__.py").write_text("", encoding="utf-8")
|
|
|
|
repos, prefixes, errors = boundary._discover_python_repos(root)
|
|
|
|
self.assertEqual(repos, {})
|
|
self.assertEqual(prefixes, {})
|
|
self.assertEqual(len(errors), 2)
|
|
self.assertTrue(any("govoplan-missing" in error and "found 0" in error for error in errors))
|
|
self.assertTrue(any("govoplan-ambiguous" in error and "found 2" in error for error in errors))
|
|
|
|
def test_duplicate_python_package_ownership_fails_discovery(self) -> None:
|
|
boundary = load_boundary_module()
|
|
with tempfile.TemporaryDirectory(prefix="govoplan-boundary-discovery-") as directory:
|
|
root = Path(directory)
|
|
for owner in ("govoplan-first", "govoplan-second"):
|
|
package = root / owner / "src" / "govoplan_shared"
|
|
package.mkdir(parents=True)
|
|
(package / "__init__.py").write_text("", encoding="utf-8")
|
|
|
|
repos, prefixes, errors = boundary._discover_python_repos(root)
|
|
|
|
self.assertEqual(set(repos), {"govoplan-first"})
|
|
self.assertEqual(prefixes, {"govoplan-first": "govoplan_shared"})
|
|
self.assertEqual(len(errors), 1)
|
|
self.assertIn("already owned", errors[0])
|
|
|
|
def test_workspace_discovery_covers_changed_capability_repositories(self) -> None:
|
|
boundary = load_boundary_module()
|
|
|
|
self.assertEqual(boundary.PYTHON_DISCOVERY_ERRORS, ())
|
|
self.assertGreaterEqual(len(boundary.REPOS), 40)
|
|
for owner in (
|
|
"govoplan-core",
|
|
"govoplan-identity",
|
|
"govoplan-idm",
|
|
"govoplan-calendar",
|
|
"govoplan-poll",
|
|
"govoplan-scheduling",
|
|
):
|
|
self.assertIn(owner, boundary.REPOS)
|
|
|
|
def test_webui_discovery_is_fail_closed(self) -> None:
|
|
boundary = load_boundary_module()
|
|
with tempfile.TemporaryDirectory(prefix="govoplan-boundary-webui-") as directory:
|
|
root = Path(directory)
|
|
good = root / "govoplan-good" / "webui"
|
|
good.mkdir(parents=True)
|
|
(good / "package.json").write_text(
|
|
json.dumps({"name": "@govoplan/good-webui"}),
|
|
encoding="utf-8",
|
|
)
|
|
missing = root / "govoplan-missing" / "webui"
|
|
missing.mkdir(parents=True)
|
|
invalid = root / "govoplan-invalid" / "webui"
|
|
invalid.mkdir(parents=True)
|
|
(invalid / "package.json").write_text("{", encoding="utf-8")
|
|
unnamed = root / "govoplan-unnamed" / "webui"
|
|
unnamed.mkdir(parents=True)
|
|
(unnamed / "package.json").write_text("{}", encoding="utf-8")
|
|
duplicate = root / "govoplan-zduplicate" / "webui"
|
|
duplicate.mkdir(parents=True)
|
|
(duplicate / "package.json").write_text(
|
|
json.dumps({"name": "@govoplan/good-webui"}),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
repos, packages, errors = boundary._discover_webui_repos(root)
|
|
|
|
self.assertEqual(repos, {"govoplan-good": good})
|
|
self.assertEqual(packages, {"govoplan-good": "@govoplan/good-webui"})
|
|
self.assertEqual(len(errors), 4)
|
|
self.assertTrue(any("govoplan-missing" in error and "no package.json" in error for error in errors))
|
|
self.assertTrue(any("govoplan-invalid" in error and "invalid" in error for error in errors))
|
|
self.assertTrue(any("govoplan-unnamed" in error and "package name" in error for error in errors))
|
|
self.assertTrue(any("govoplan-zduplicate" in error and "already owned" in error for error in errors))
|
|
|
|
def test_workspace_webui_discovery_covers_composition_repositories(self) -> None:
|
|
boundary = load_boundary_module()
|
|
|
|
self.assertEqual(boundary.WEBUI_DISCOVERY_ERRORS, ())
|
|
self.assertGreaterEqual(len(boundary.WEBUI_REPOS), 17)
|
|
for owner in (
|
|
"govoplan-core",
|
|
"govoplan-calendar",
|
|
"govoplan-scheduling",
|
|
"govoplan-notifications",
|
|
):
|
|
self.assertIn(owner, boundary.WEBUI_REPOS)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|