test_check_dependencies.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. from contextlib import contextmanager
  2. from typing import Generator, Optional
  3. from unittest.mock import patch
  4. from synapse.util.check_dependencies import (
  5. DependencyException,
  6. check_requirements,
  7. metadata,
  8. )
  9. from tests.unittest import TestCase
  10. class DummyDistribution(metadata.Distribution):
  11. def __init__(self, version: object):
  12. self._version = version
  13. @property
  14. def version(self):
  15. return self._version
  16. def locate_file(self, path):
  17. raise NotImplementedError()
  18. def read_text(self, filename):
  19. raise NotImplementedError()
  20. old = DummyDistribution("0.1.2")
  21. old_release_candidate = DummyDistribution("0.1.2rc3")
  22. new = DummyDistribution("1.2.3")
  23. new_release_candidate = DummyDistribution("1.2.3rc4")
  24. distribution_with_no_version = DummyDistribution(None)
  25. # could probably use stdlib TestCase --- no need for twisted here
  26. class TestDependencyChecker(TestCase):
  27. @contextmanager
  28. def mock_installed_package(
  29. self, distribution: Optional[DummyDistribution]
  30. ) -> Generator[None, None, None]:
  31. """Pretend that looking up any package yields the given `distribution`.
  32. If `distribution = None`, we pretend that the package is not installed.
  33. """
  34. def mock_distribution(name: str):
  35. if distribution is None:
  36. raise metadata.PackageNotFoundError
  37. else:
  38. return distribution
  39. with patch(
  40. "synapse.util.check_dependencies.metadata.distribution",
  41. mock_distribution,
  42. ):
  43. yield
  44. def test_mandatory_dependency(self) -> None:
  45. """Complain if a required package is missing or old."""
  46. with patch(
  47. "synapse.util.check_dependencies.metadata.requires",
  48. return_value=["dummypkg >= 1"],
  49. ):
  50. with self.mock_installed_package(None):
  51. self.assertRaises(DependencyException, check_requirements)
  52. with self.mock_installed_package(old):
  53. self.assertRaises(DependencyException, check_requirements)
  54. with self.mock_installed_package(new):
  55. # should not raise
  56. check_requirements()
  57. def test_version_reported_as_none(self) -> None:
  58. """Complain if importlib.metadata.version() returns None.
  59. This shouldn't normally happen, but it was seen in the wild (#12223).
  60. """
  61. with patch(
  62. "synapse.util.check_dependencies.metadata.requires",
  63. return_value=["dummypkg >= 1"],
  64. ):
  65. with self.mock_installed_package(distribution_with_no_version):
  66. self.assertRaises(DependencyException, check_requirements)
  67. def test_checks_ignore_dev_dependencies(self) -> None:
  68. """Both generic and per-extra checks should ignore dev dependencies."""
  69. with patch(
  70. "synapse.util.check_dependencies.metadata.requires",
  71. return_value=["dummypkg >= 1; extra == 'mypy'"],
  72. ), patch("synapse.util.check_dependencies.RUNTIME_EXTRAS", {"cool-extra"}):
  73. # We're testing that none of these calls raise.
  74. with self.mock_installed_package(None):
  75. check_requirements()
  76. check_requirements("cool-extra")
  77. with self.mock_installed_package(old):
  78. check_requirements()
  79. check_requirements("cool-extra")
  80. with self.mock_installed_package(new):
  81. check_requirements()
  82. check_requirements("cool-extra")
  83. def test_generic_check_of_optional_dependency(self) -> None:
  84. """Complain if an optional package is old."""
  85. with patch(
  86. "synapse.util.check_dependencies.metadata.requires",
  87. return_value=["dummypkg >= 1; extra == 'cool-extra'"],
  88. ):
  89. with self.mock_installed_package(None):
  90. # should not raise
  91. check_requirements()
  92. with self.mock_installed_package(old):
  93. self.assertRaises(DependencyException, check_requirements)
  94. with self.mock_installed_package(new):
  95. # should not raise
  96. check_requirements()
  97. def test_check_for_extra_dependencies(self) -> None:
  98. """Complain if a package required for an extra is missing or old."""
  99. with patch(
  100. "synapse.util.check_dependencies.metadata.requires",
  101. return_value=["dummypkg >= 1; extra == 'cool-extra'"],
  102. ), patch("synapse.util.check_dependencies.RUNTIME_EXTRAS", {"cool-extra"}):
  103. with self.mock_installed_package(None):
  104. self.assertRaises(DependencyException, check_requirements, "cool-extra")
  105. with self.mock_installed_package(old):
  106. self.assertRaises(DependencyException, check_requirements, "cool-extra")
  107. with self.mock_installed_package(new):
  108. # should not raise
  109. check_requirements("cool-extra")
  110. def test_release_candidates_satisfy_dependency(self) -> None:
  111. """
  112. Tests that release candidates count as far as satisfying a dependency
  113. is concerned.
  114. (Regression test, see #12176.)
  115. """
  116. with patch(
  117. "synapse.util.check_dependencies.metadata.requires",
  118. return_value=["dummypkg >= 1"],
  119. ):
  120. with self.mock_installed_package(old_release_candidate):
  121. self.assertRaises(DependencyException, check_requirements)
  122. with self.mock_installed_package(new_release_candidate):
  123. # should not raise
  124. check_requirements()
  125. def test_setuptools_rust_ignored(self) -> None:
  126. """Test a workaround for a `poetry build` problem. Reproduces #13926."""
  127. with patch(
  128. "synapse.util.check_dependencies.metadata.requires",
  129. return_value=["setuptools_rust >= 1.3"],
  130. ):
  131. with self.mock_installed_package(None):
  132. # should not raise, even if setuptools_rust is not installed
  133. check_requirements()
  134. with self.mock_installed_package(old):
  135. # We also ignore old versions of setuptools_rust
  136. check_requirements()