You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

180 lines
7.0 KiB

  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
  74. (https://github.com/matrix-org/synapse/issues/12223).
  75. """
  76. with patch(
  77. "synapse.util.check_dependencies.metadata.requires",
  78. return_value=["dummypkg >= 1"],
  79. ):
  80. with self.mock_installed_package(distribution_with_no_version):
  81. self.assertRaises(DependencyException, check_requirements)
  82. def test_checks_ignore_dev_dependencies(self) -> None:
  83. """Both generic and per-extra checks should ignore dev dependencies."""
  84. with patch(
  85. "synapse.util.check_dependencies.metadata.requires",
  86. return_value=["dummypkg >= 1; extra == 'mypy'"],
  87. ), patch("synapse.util.check_dependencies.RUNTIME_EXTRAS", {"cool-extra"}):
  88. # We're testing that none of these calls raise.
  89. with self.mock_installed_package(None):
  90. check_requirements()
  91. check_requirements("cool-extra")
  92. with self.mock_installed_package(old):
  93. check_requirements()
  94. check_requirements("cool-extra")
  95. with self.mock_installed_package(new):
  96. check_requirements()
  97. check_requirements("cool-extra")
  98. def test_generic_check_of_optional_dependency(self) -> None:
  99. """Complain if an optional package is old."""
  100. with patch(
  101. "synapse.util.check_dependencies.metadata.requires",
  102. return_value=["dummypkg >= 1; extra == 'cool-extra'"],
  103. ):
  104. with self.mock_installed_package(None):
  105. # should not raise
  106. check_requirements()
  107. with self.mock_installed_package(old):
  108. self.assertRaises(DependencyException, check_requirements)
  109. with self.mock_installed_package(new):
  110. # should not raise
  111. check_requirements()
  112. def test_check_for_extra_dependencies(self) -> None:
  113. """Complain if a package required for an extra is missing or old."""
  114. with patch(
  115. "synapse.util.check_dependencies.metadata.requires",
  116. return_value=["dummypkg >= 1; extra == 'cool-extra'"],
  117. ), patch("synapse.util.check_dependencies.RUNTIME_EXTRAS", {"cool-extra"}):
  118. with self.mock_installed_package(None):
  119. self.assertRaises(DependencyException, check_requirements, "cool-extra")
  120. with self.mock_installed_package(old):
  121. self.assertRaises(DependencyException, check_requirements, "cool-extra")
  122. with self.mock_installed_package(new):
  123. # should not raise
  124. check_requirements("cool-extra")
  125. def test_release_candidates_satisfy_dependency(self) -> None:
  126. """
  127. Tests that release candidates count as far as satisfying a dependency
  128. is concerned.
  129. (Regression test, see https://github.com/matrix-org/synapse/issues/12176.)
  130. """
  131. with patch(
  132. "synapse.util.check_dependencies.metadata.requires",
  133. return_value=["dummypkg >= 1"],
  134. ):
  135. with self.mock_installed_package(old_release_candidate):
  136. self.assertRaises(DependencyException, check_requirements)
  137. with self.mock_installed_package(new_release_candidate):
  138. # should not raise
  139. check_requirements()
  140. def test_setuptools_rust_ignored(self) -> None:
  141. """
  142. Test a workaround for a `poetry build` problem. Reproduces
  143. https://github.com/matrix-org/synapse/issues/13926.
  144. """
  145. with patch(
  146. "synapse.util.check_dependencies.metadata.requires",
  147. return_value=["setuptools_rust >= 1.3"],
  148. ):
  149. with self.mock_installed_package(None):
  150. # should not raise, even if setuptools_rust is not installed
  151. check_requirements()
  152. with self.mock_installed_package(old):
  153. # We also ignore old versions of setuptools_rust
  154. check_requirements()