| 1 |
"""Tests for ``_extras`` module.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import importlib.metadata |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from rstcheck_core import _extras |
| 10 |
|
| 11 |
|
| 12 |
class TestInstallChecker: |
| 13 |
"""Test ``is_installed_with_supported_version``.""" |
| 14 |
|
| 15 |
@staticmethod |
| 16 |
@pytest.mark.skipif(_extras.SPHINX_INSTALLED, reason="Test without sphinx extra.") |
| 17 |
def test_false_on_missing_sphinx_package() -> None: |
| 18 |
"""Test install-checker returns ``False`` when ``sphinx`` is missing.""" |
| 19 |
result = _extras.is_installed_with_supported_version("sphinx") |
| 20 |
|
| 21 |
assert result is False |
| 22 |
|
| 23 |
@staticmethod |
| 24 |
@pytest.mark.skipif(not _extras.SPHINX_INSTALLED, reason="Depends on sphinx extra.") |
| 25 |
def test_true_on_installed_sphinx_package() -> None: |
| 26 |
"""Test install-checker returns ``True`` when ``sphinx`` is installed with good version.""" |
| 27 |
result = _extras.is_installed_with_supported_version("sphinx") |
| 28 |
|
| 29 |
assert result is True |
| 30 |
|
| 31 |
@staticmethod |
| 32 |
@pytest.mark.skipif(not _extras.SPHINX_INSTALLED, reason="Depends on sphinx extra.") |
| 33 |
def test_false_on_installed_sphinx_package_too_old(monkeypatch: pytest.MonkeyPatch) -> None: |
| 34 |
"""Test install-checker returns ``False`` when ``sphinx`` is installed with bad version.""" |
| 35 |
monkeypatch.setattr(importlib.metadata, "version", lambda _: "0.0") |
| 36 |
|
| 37 |
result = _extras.is_installed_with_supported_version("sphinx") |
| 38 |
|
| 39 |
assert result is False |
| 40 |
|
| 41 |
|
| 42 |
class TestInstallGuard: |
| 43 |
"""Test ``install_guard``.""" |
| 44 |
|
| 45 |
@staticmethod |
| 46 |
@pytest.mark.skipif(_extras.SPHINX_INSTALLED, reason="Test without sphinx extra.") |
| 47 |
def test_error_on_missing_sphinx_package() -> None: |
| 48 |
"""Test install-guard raises exception when ``sphinx`` is missing.""" |
| 49 |
with pytest.raises(ModuleNotFoundError): |
| 50 |
_extras.install_guard("sphinx") |
| 51 |
|
| 52 |
@staticmethod |
| 53 |
@pytest.mark.skipif(not _extras.SPHINX_INSTALLED, reason="Depends on sphinx extra.") |
| 54 |
def test_ok_on_installed_sphinx_package() -> None: |
| 55 |
"""Test install-guard doesn't raise when ``sphinx`` is installed.""" |
| 56 |
_extras.install_guard("sphinx") |
| 57 |
|
| 58 |
|
| 59 |
class TestTomliInstallGuard: |
| 60 |
"""Test ``install_guard_tomli``.""" |
| 61 |
|
| 62 |
@staticmethod |
| 63 |
@pytest.mark.skipif(_extras.TOMLI_INSTALLED, reason="Test without tomli extra.") |
| 64 |
def test_error_tomllib_imported_is_false_and_on_missing_tomli_package() -> None: |
| 65 |
"""Test raises exception when ``tomllib_imported`` is `False` and ``tomli`` is missing.""" |
| 66 |
with pytest.raises(ModuleNotFoundError): |
| 67 |
_extras.install_guard_tomli(tomllib_imported=False) |
| 68 |
|
| 69 |
@staticmethod |
| 70 |
@pytest.mark.skipif(not _extras.TOMLI_INSTALLED, reason="Depends on tomli extra.") |
| 71 |
def test_ok_when_tomllib_imported_is_false_and_tomli_package_is_installed() -> None: |
| 72 |
"""Test doesn't raise when ``tomllib_imported`` is `False` but ``tomli`` is installed.""" |
| 73 |
_extras.install_guard_tomli(tomllib_imported=False) |
| 74 |
|
| 75 |
@staticmethod |
| 76 |
def test_ok_when_tomllib_imported_is_true() -> None: |
| 77 |
"""Test doesn't raise when ``tomllib_imported`` is `True`.""" |
| 78 |
_extras.install_guard_tomli(tomllib_imported=True) |