test_check_dependencies.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 distribution yields the given `distribution`."""
  32. def mock_distribution(name: str):
  33. if distribution is None:
  34. raise metadata.PackageNotFoundError
  35. else:
  36. return distribution
  37. with patch(
  38. "synapse.util.check_dependencies.metadata.distribution",
  39. mock_distribution,
  40. ):
  41. yield
  42. def test_mandatory_dependency(self) -> None:
  43. """Complain if a required package is missing or old."""
  44. with patch(
  45. "synapse.util.check_dependencies.metadata.requires",
  46. return_value=["dummypkg >= 1"],
  47. ):
  48. with self.mock_installed_package(None):
  49. self.assertRaises(DependencyException, check_requirements)
  50. with self.mock_installed_package(old):
  51. self.assertRaises(DependencyException, check_requirements)
  52. with self.mock_installed_package(new):
  53. # should not raise
  54. check_requirements()
  55. def test_version_reported_as_none(self) -> None:
  56. """Complain if importlib.metadata.version() returns None.
  57. This shouldn't normally happen, but it was seen in the wild (#12223).
  58. """
  59. with patch(
  60. "synapse.util.check_dependencies.metadata.requires",
  61. return_value=["dummypkg >= 1"],
  62. ):
  63. with self.mock_installed_package(distribution_with_no_version):
  64. self.assertRaises(DependencyException, check_requirements)
  65. def test_checks_ignore_dev_dependencies(self) -> None:
  66. """Bot generic and per-extra checks should ignore dev dependencies."""
  67. with patch(
  68. "synapse.util.check_dependencies.metadata.requires",
  69. return_value=["dummypkg >= 1; extra == 'mypy'"],
  70. ), patch("synapse.util.check_dependencies.RUNTIME_EXTRAS", {"cool-extra"}):
  71. # We're testing that none of these calls raise.
  72. with self.mock_installed_package(None):
  73. check_requirements()
  74. check_requirements("cool-extra")
  75. with self.mock_installed_package(old):
  76. check_requirements()
  77. check_requirements("cool-extra")
  78. with self.mock_installed_package(new):
  79. check_requirements()
  80. check_requirements("cool-extra")
  81. def test_generic_check_of_optional_dependency(self) -> None:
  82. """Complain if an optional package is old."""
  83. with patch(
  84. "synapse.util.check_dependencies.metadata.requires",
  85. return_value=["dummypkg >= 1; extra == 'cool-extra'"],
  86. ):
  87. with self.mock_installed_package(None):
  88. # should not raise
  89. check_requirements()
  90. with self.mock_installed_package(old):
  91. self.assertRaises(DependencyException, check_requirements)
  92. with self.mock_installed_package(new):
  93. # should not raise
  94. check_requirements()
  95. def test_check_for_extra_dependencies(self) -> None:
  96. """Complain if a package required for an extra is missing or old."""
  97. with patch(
  98. "synapse.util.check_dependencies.metadata.requires",
  99. return_value=["dummypkg >= 1; extra == 'cool-extra'"],
  100. ), patch("synapse.util.check_dependencies.RUNTIME_EXTRAS", {"cool-extra"}):
  101. with self.mock_installed_package(None):
  102. self.assertRaises(DependencyException, check_requirements, "cool-extra")
  103. with self.mock_installed_package(old):
  104. self.assertRaises(DependencyException, check_requirements, "cool-extra")
  105. with self.mock_installed_package(new):
  106. # should not raise
  107. check_requirements("cool-extra")
  108. def test_release_candidates_satisfy_dependency(self) -> None:
  109. """
  110. Tests that release candidates count as far as satisfying a dependency
  111. is concerned.
  112. (Regression test, see #12176.)
  113. """
  114. with patch(
  115. "synapse.util.check_dependencies.metadata.requires",
  116. return_value=["dummypkg >= 1"],
  117. ):
  118. with self.mock_installed_package(old_release_candidate):
  119. self.assertRaises(DependencyException, check_requirements)
  120. with self.mock_installed_package(new_release_candidate):
  121. # should not raise
  122. check_requirements()