2022-12-02 12:58:56 -05:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 06:26:48 -05:00
|
|
|
# Copyright 2022 The Matrix.org Foundation C.I.C.
|
2023-11-21 15:29:58 -05:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2022-12-02 12:58:56 -05:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2022-03-01 12:44:41 -05:00
|
|
|
from contextlib import contextmanager
|
2022-12-02 12:58:56 -05:00
|
|
|
from os import PathLike
|
2024-07-16 07:08:06 -04:00
|
|
|
from pathlib import Path
|
2022-12-02 12:58:56 -05:00
|
|
|
from typing import Generator, Optional, Union
|
2022-03-01 12:44:41 -05:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
from synapse.util.check_dependencies import (
|
|
|
|
DependencyException,
|
|
|
|
check_requirements,
|
|
|
|
metadata,
|
|
|
|
)
|
|
|
|
|
|
|
|
from tests.unittest import TestCase
|
|
|
|
|
|
|
|
|
|
|
|
class DummyDistribution(metadata.Distribution):
|
2022-12-02 12:58:56 -05:00
|
|
|
def __init__(self, version: str):
|
2022-03-01 12:44:41 -05:00
|
|
|
self._version = version
|
|
|
|
|
|
|
|
@property
|
2022-12-02 12:58:56 -05:00
|
|
|
def version(self) -> str:
|
2022-03-01 12:44:41 -05:00
|
|
|
return self._version
|
|
|
|
|
2024-07-16 07:08:06 -04:00
|
|
|
def locate_file(self, path: Union[str, PathLike]) -> Path:
|
2022-03-01 12:44:41 -05:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
2022-12-02 12:58:56 -05:00
|
|
|
def read_text(self, filename: str) -> None:
|
2022-03-01 12:44:41 -05:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
|
|
old = DummyDistribution("0.1.2")
|
2022-03-08 05:47:28 -05:00
|
|
|
old_release_candidate = DummyDistribution("0.1.2rc3")
|
2022-03-01 12:44:41 -05:00
|
|
|
new = DummyDistribution("1.2.3")
|
2022-03-08 05:47:28 -05:00
|
|
|
new_release_candidate = DummyDistribution("1.2.3rc4")
|
2022-12-02 12:58:56 -05:00
|
|
|
distribution_with_no_version = DummyDistribution(None) # type: ignore[arg-type]
|
2022-03-01 12:44:41 -05:00
|
|
|
|
|
|
|
# could probably use stdlib TestCase --- no need for twisted here
|
|
|
|
|
|
|
|
|
|
|
|
class TestDependencyChecker(TestCase):
|
|
|
|
@contextmanager
|
|
|
|
def mock_installed_package(
|
|
|
|
self, distribution: Optional[DummyDistribution]
|
|
|
|
) -> Generator[None, None, None]:
|
2022-09-29 16:16:08 -04:00
|
|
|
"""Pretend that looking up any package yields the given `distribution`.
|
|
|
|
|
|
|
|
If `distribution = None`, we pretend that the package is not installed.
|
|
|
|
"""
|
2022-03-01 12:44:41 -05:00
|
|
|
|
2022-12-02 12:58:56 -05:00
|
|
|
def mock_distribution(name: str) -> DummyDistribution:
|
2022-03-01 12:44:41 -05:00
|
|
|
if distribution is None:
|
|
|
|
raise metadata.PackageNotFoundError
|
|
|
|
else:
|
|
|
|
return distribution
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"synapse.util.check_dependencies.metadata.distribution",
|
|
|
|
mock_distribution,
|
|
|
|
):
|
|
|
|
yield
|
|
|
|
|
|
|
|
def test_mandatory_dependency(self) -> None:
|
|
|
|
"""Complain if a required package is missing or old."""
|
|
|
|
with patch(
|
|
|
|
"synapse.util.check_dependencies.metadata.requires",
|
|
|
|
return_value=["dummypkg >= 1"],
|
|
|
|
):
|
|
|
|
with self.mock_installed_package(None):
|
|
|
|
self.assertRaises(DependencyException, check_requirements)
|
|
|
|
with self.mock_installed_package(old):
|
|
|
|
self.assertRaises(DependencyException, check_requirements)
|
|
|
|
with self.mock_installed_package(new):
|
|
|
|
# should not raise
|
|
|
|
check_requirements()
|
|
|
|
|
2022-03-18 15:03:46 -04:00
|
|
|
def test_version_reported_as_none(self) -> None:
|
|
|
|
"""Complain if importlib.metadata.version() returns None.
|
|
|
|
|
2023-11-15 08:02:11 -05:00
|
|
|
This shouldn't normally happen, but it was seen in the wild
|
|
|
|
(https://github.com/matrix-org/synapse/issues/12223).
|
2022-03-18 15:03:46 -04:00
|
|
|
"""
|
|
|
|
with patch(
|
|
|
|
"synapse.util.check_dependencies.metadata.requires",
|
|
|
|
return_value=["dummypkg >= 1"],
|
|
|
|
):
|
|
|
|
with self.mock_installed_package(distribution_with_no_version):
|
|
|
|
self.assertRaises(DependencyException, check_requirements)
|
|
|
|
|
2022-03-03 07:47:55 -05:00
|
|
|
def test_checks_ignore_dev_dependencies(self) -> None:
|
2022-09-29 16:16:08 -04:00
|
|
|
"""Both generic and per-extra checks should ignore dev dependencies."""
|
2022-03-03 07:47:55 -05:00
|
|
|
with patch(
|
|
|
|
"synapse.util.check_dependencies.metadata.requires",
|
|
|
|
return_value=["dummypkg >= 1; extra == 'mypy'"],
|
|
|
|
), patch("synapse.util.check_dependencies.RUNTIME_EXTRAS", {"cool-extra"}):
|
|
|
|
# We're testing that none of these calls raise.
|
|
|
|
with self.mock_installed_package(None):
|
|
|
|
check_requirements()
|
|
|
|
check_requirements("cool-extra")
|
|
|
|
with self.mock_installed_package(old):
|
|
|
|
check_requirements()
|
|
|
|
check_requirements("cool-extra")
|
|
|
|
with self.mock_installed_package(new):
|
|
|
|
check_requirements()
|
|
|
|
check_requirements("cool-extra")
|
|
|
|
|
2022-03-01 12:44:41 -05:00
|
|
|
def test_generic_check_of_optional_dependency(self) -> None:
|
|
|
|
"""Complain if an optional package is old."""
|
|
|
|
with patch(
|
|
|
|
"synapse.util.check_dependencies.metadata.requires",
|
|
|
|
return_value=["dummypkg >= 1; extra == 'cool-extra'"],
|
|
|
|
):
|
|
|
|
with self.mock_installed_package(None):
|
|
|
|
# should not raise
|
|
|
|
check_requirements()
|
|
|
|
with self.mock_installed_package(old):
|
|
|
|
self.assertRaises(DependencyException, check_requirements)
|
|
|
|
with self.mock_installed_package(new):
|
|
|
|
# should not raise
|
|
|
|
check_requirements()
|
|
|
|
|
|
|
|
def test_check_for_extra_dependencies(self) -> None:
|
|
|
|
"""Complain if a package required for an extra is missing or old."""
|
|
|
|
with patch(
|
|
|
|
"synapse.util.check_dependencies.metadata.requires",
|
|
|
|
return_value=["dummypkg >= 1; extra == 'cool-extra'"],
|
2022-03-03 07:47:55 -05:00
|
|
|
), patch("synapse.util.check_dependencies.RUNTIME_EXTRAS", {"cool-extra"}):
|
2022-03-01 12:44:41 -05:00
|
|
|
with self.mock_installed_package(None):
|
|
|
|
self.assertRaises(DependencyException, check_requirements, "cool-extra")
|
|
|
|
with self.mock_installed_package(old):
|
|
|
|
self.assertRaises(DependencyException, check_requirements, "cool-extra")
|
|
|
|
with self.mock_installed_package(new):
|
|
|
|
# should not raise
|
2022-03-03 07:47:55 -05:00
|
|
|
check_requirements("cool-extra")
|
2022-03-08 05:47:28 -05:00
|
|
|
|
|
|
|
def test_release_candidates_satisfy_dependency(self) -> None:
|
|
|
|
"""
|
|
|
|
Tests that release candidates count as far as satisfying a dependency
|
|
|
|
is concerned.
|
2023-11-15 08:02:11 -05:00
|
|
|
(Regression test, see https://github.com/matrix-org/synapse/issues/12176.)
|
2022-03-08 05:47:28 -05:00
|
|
|
"""
|
|
|
|
with patch(
|
|
|
|
"synapse.util.check_dependencies.metadata.requires",
|
|
|
|
return_value=["dummypkg >= 1"],
|
|
|
|
):
|
|
|
|
with self.mock_installed_package(old_release_candidate):
|
|
|
|
self.assertRaises(DependencyException, check_requirements)
|
|
|
|
|
|
|
|
with self.mock_installed_package(new_release_candidate):
|
|
|
|
# should not raise
|
|
|
|
check_requirements()
|
2022-09-29 16:16:08 -04:00
|
|
|
|
|
|
|
def test_setuptools_rust_ignored(self) -> None:
|
2023-11-15 08:02:11 -05:00
|
|
|
"""
|
|
|
|
Test a workaround for a `poetry build` problem. Reproduces
|
|
|
|
https://github.com/matrix-org/synapse/issues/13926.
|
|
|
|
"""
|
2022-09-29 16:16:08 -04:00
|
|
|
with patch(
|
|
|
|
"synapse.util.check_dependencies.metadata.requires",
|
|
|
|
return_value=["setuptools_rust >= 1.3"],
|
|
|
|
):
|
|
|
|
with self.mock_installed_package(None):
|
|
|
|
# should not raise, even if setuptools_rust is not installed
|
|
|
|
check_requirements()
|
|
|
|
with self.mock_installed_package(old):
|
|
|
|
# We also ignore old versions of setuptools_rust
|
|
|
|
check_requirements()
|