|
| 1 | +from contextlib import contextmanager |
| 2 | +from typing import ContextManager, Optional |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +from synapse.util.check_dependencies import ( |
| 6 | + DependencyException, |
| 7 | + check_requirements, |
| 8 | + metadata, |
| 9 | +) |
| 10 | + |
| 11 | +from tests.unittest import TestCase |
| 12 | + |
| 13 | + |
| 14 | +class DummyDistribution(metadata.Distribution): |
| 15 | + def __init__(self, version: str): |
| 16 | + self._version = version |
| 17 | + |
| 18 | + @property |
| 19 | + def version(self): |
| 20 | + return self._version |
| 21 | + |
| 22 | + |
| 23 | +old = DummyDistribution("0.1.2") |
| 24 | +new = DummyDistribution("1.2.3") |
| 25 | + |
| 26 | +# could probably use stdlib TestCase --- no need for twisted here |
| 27 | + |
| 28 | + |
| 29 | +class TestDependencyChecker(TestCase): |
| 30 | + @contextmanager |
| 31 | + def mock_installed_package( |
| 32 | + self, distribution: Optional[DummyDistribution] |
| 33 | + ) -> ContextManager[None]: |
| 34 | + """Pretend that looking up any distribution yields the given `distribution`.""" |
| 35 | + |
| 36 | + def mock_distribution(name: str): |
| 37 | + if distribution is None: |
| 38 | + raise metadata.PackageNotFoundError |
| 39 | + else: |
| 40 | + return distribution |
| 41 | + |
| 42 | + with patch( |
| 43 | + "synapse.util.check_dependencies.metadata.distribution", |
| 44 | + mock_distribution, |
| 45 | + ): |
| 46 | + yield |
| 47 | + |
| 48 | + def test_mandatory_dependency(self) -> None: |
| 49 | + """Complain if a required package is missing or old.""" |
| 50 | + with patch( |
| 51 | + "synapse.util.check_dependencies.metadata.requires", |
| 52 | + return_value=["dummypkg >= 1"], |
| 53 | + ): |
| 54 | + with self.mock_installed_package(None): |
| 55 | + self.assertRaises(DependencyException, check_requirements) |
| 56 | + with self.mock_installed_package(old): |
| 57 | + self.assertRaises(DependencyException, check_requirements) |
| 58 | + with self.mock_installed_package(new): |
| 59 | + # should not raise |
| 60 | + check_requirements() |
| 61 | + |
| 62 | + def test_generic_check_of_optional_dependency(self) -> None: |
| 63 | + """Complain if an optional package is old.""" |
| 64 | + with patch( |
| 65 | + "synapse.util.check_dependencies.metadata.requires", |
| 66 | + return_value=["dummypkg >= 1; extra == 'cool-extra'"], |
| 67 | + ): |
| 68 | + with self.mock_installed_package(None): |
| 69 | + # should not raise |
| 70 | + check_requirements() |
| 71 | + with self.mock_installed_package(old): |
| 72 | + self.assertRaises(DependencyException, check_requirements) |
| 73 | + with self.mock_installed_package(new): |
| 74 | + # should not raise |
| 75 | + check_requirements() |
| 76 | + |
| 77 | + def test_check_for_extra_dependencies(self) -> None: |
| 78 | + """Complain if a package required for an extra is missing or old.""" |
| 79 | + with patch( |
| 80 | + "synapse.util.check_dependencies.metadata.requires", |
| 81 | + return_value=["dummypkg >= 1; extra == 'cool-extra'"], |
| 82 | + ), patch("synapse.util.check_dependencies.EXTRAS", {"cool-extra"}): |
| 83 | + with self.mock_installed_package(None): |
| 84 | + self.assertRaises(DependencyException, check_requirements, "cool-extra") |
| 85 | + with self.mock_installed_package(old): |
| 86 | + self.assertRaises(DependencyException, check_requirements, "cool-extra") |
| 87 | + with self.mock_installed_package(new): |
| 88 | + # should not raise |
| 89 | + check_requirements() |
0 commit comments