1
0

test_check_dependencies.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # Copyright 2022 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from contextlib import contextmanager
  15. from os import PathLike
  16. from typing import Generator, Optional, Union
  17. from unittest.mock import patch
  18. from synapse.util.check_dependencies import (
  19. DependencyException,
  20. check_requirements,
  21. metadata,
  22. )
  23. from tests.unittest import TestCase
  24. class DummyDistribution(metadata.Distribution):
  25. def __init__(self, version: str):
  26. self._version = version
  27. @property
  28. def version(self) -> str:
  29. return self._version
  30. def locate_file(self, path: Union[str, PathLike]) -> PathLike:
  31. raise NotImplementedError()
  32. def read_text(self, filename: str) -> None:
  33. raise NotImplementedError()
  34. old = DummyDistribution("0.1.2")
  35. old_release_candidate = DummyDistribution("0.1.2rc3")
  36. new = DummyDistribution("1.2.3")
  37. new_release_candidate = DummyDistribution("1.2.3rc4")
  38. distribution_with_no_version = DummyDistribution(None) # type: ignore[arg-type]
  39. # could probably use stdlib TestCase --- no need for twisted here
  40. class TestDependencyChecker(TestCase):
  41. @contextmanager
  42. def mock_installed_package(
  43. self, distribution: Optional[DummyDistribution]
  44. ) -> Generator[None, None, None]:
  45. """Pretend that looking up any package yields the given `distribution`.
  46. If `distribution = None`, we pretend that the package is not installed.
  47. """
  48. def mock_distribution(name: str) -> DummyDistribution:
  49. if distribution is None:
  50. raise metadata.PackageNotFoundError
  51. else:
  52. return distribution
  53. with patch(
  54. "synapse.util.check_dependencies.metadata.distribution",
  55. mock_distribution,
  56. ):
  57. yield
  58. def test_mandatory_dependency(self) -> None:
  59. """Complain if a required package is missing or old."""
  60. with patch(
  61. "synapse.util.check_dependencies.metadata.requires",
  62. return_value=["dummypkg >= 1"],
  63. ):
  64. with self.mock_installed_package(None):
  65. self.assertRaises(DependencyException, check_requirements)
  66. with self.mock_installed_package(old):
  67. self.assertRaises(DependencyException, check_requirements)
  68. with self.mock_installed_package(new):
  69. # should not raise
  70. check_requirements()
  71. def test_version_reported_as_none(self) -> None:
  72. """Complain if importlib.metadata.version() returns None.
  73. This shouldn't normally happen, but it was seen in the wild (#12223).
  74. """
  75. with patch(
  76. "synapse.util.check_dependencies.metadata.requires",
  77. return_value=["dummypkg >= 1"],
  78. ):
  79. with self.mock_installed_package(distribution_with_no_version):
  80. self.assertRaises(DependencyException, check_requirements)
  81. def test_checks_ignore_dev_dependencies(self) -> None:
  82. """Both generic and per-extra checks should ignore dev dependencies."""
  83. with patch(
  84. "synapse.util.check_dependencies.metadata.requires",
  85. return_value=["dummypkg >= 1; extra == 'mypy'"],
  86. ), patch("synapse.util.check_dependencies.RUNTIME_EXTRAS", {"cool-extra"}):
  87. # We're testing that none of these calls raise.
  88. with self.mock_installed_package(None):
  89. check_requirements()
  90. check_requirements("cool-extra")
  91. with self.mock_installed_package(old):
  92. check_requirements()
  93. check_requirements("cool-extra")
  94. with self.mock_installed_package(new):
  95. check_requirements()
  96. check_requirements("cool-extra")
  97. def test_generic_check_of_optional_dependency(self) -> None:
  98. """Complain if an optional package is old."""
  99. with patch(
  100. "synapse.util.check_dependencies.metadata.requires",
  101. return_value=["dummypkg >= 1; extra == 'cool-extra'"],
  102. ):
  103. with self.mock_installed_package(None):
  104. # should not raise
  105. check_requirements()
  106. with self.mock_installed_package(old):
  107. self.assertRaises(DependencyException, check_requirements)
  108. with self.mock_installed_package(new):
  109. # should not raise
  110. check_requirements()
  111. def test_check_for_extra_dependencies(self) -> None:
  112. """Complain if a package required for an extra is missing or old."""
  113. with patch(
  114. "synapse.util.check_dependencies.metadata.requires",
  115. return_value=["dummypkg >= 1; extra == 'cool-extra'"],
  116. ), patch("synapse.util.check_dependencies.RUNTIME_EXTRAS", {"cool-extra"}):
  117. with self.mock_installed_package(None):
  118. self.assertRaises(DependencyException, check_requirements, "cool-extra")
  119. with self.mock_installed_package(old):
  120. self.assertRaises(DependencyException, check_requirements, "cool-extra")
  121. with self.mock_installed_package(new):
  122. # should not raise
  123. check_requirements("cool-extra")
  124. def test_release_candidates_satisfy_dependency(self) -> None:
  125. """
  126. Tests that release candidates count as far as satisfying a dependency
  127. is concerned.
  128. (Regression test, see #12176.)
  129. """
  130. with patch(
  131. "synapse.util.check_dependencies.metadata.requires",
  132. return_value=["dummypkg >= 1"],
  133. ):
  134. with self.mock_installed_package(old_release_candidate):
  135. self.assertRaises(DependencyException, check_requirements)
  136. with self.mock_installed_package(new_release_candidate):
  137. # should not raise
  138. check_requirements()
  139. def test_setuptools_rust_ignored(self) -> None:
  140. """Test a workaround for a `poetry build` problem. Reproduces #13926."""
  141. with patch(
  142. "synapse.util.check_dependencies.metadata.requires",
  143. return_value=["setuptools_rust >= 1.3"],
  144. ):
  145. with self.mock_installed_package(None):
  146. # should not raise, even if setuptools_rust is not installed
  147. check_requirements()
  148. with self.mock_installed_package(old):
  149. # We also ignore old versions of setuptools_rust
  150. check_requirements()