| 1 |
"""Tests for ``_sphinx`` module.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import typing as t |
| 6 |
|
| 7 |
import docutils.parsers.rst.directives as docutils_directives |
| 8 |
import docutils.parsers.rst.roles as docutils_roles |
| 9 |
import pytest |
| 10 |
|
| 11 |
from rstcheck_core import _extras, _sphinx |
| 12 |
|
| 13 |
if _extras.SPHINX_INSTALLED: |
| 14 |
import sphinx.application |
| 15 |
|
| 16 |
|
| 17 |
@pytest.mark.skipif(not _extras.SPHINX_INSTALLED, reason="Depends on sphinx extra.") |
| 18 |
def test_dummy_app_creator() -> None: |
| 19 |
"""Test creation of dummy sphinx app.""" |
| 20 |
result = _sphinx.create_dummy_sphinx_app() |
| 21 |
|
| 22 |
assert isinstance(result, sphinx.application.Sphinx) |
| 23 |
|
| 24 |
|
| 25 |
class TestContextManager: |
| 26 |
"""Test ``load_sphinx_if_available`` context manager.""" |
| 27 |
|
| 28 |
@staticmethod |
| 29 |
@pytest.mark.skipif(_extras.SPHINX_INSTALLED, reason="Test without sphinx extra.") |
| 30 |
@pytest.mark.usefixtures("patch_docutils_directives_and_roles_dict") |
| 31 |
def test_yield_nothing_with_sphinx_missing() -> None: |
| 32 |
"""Test for ``None`` yield and no action when sphinx is missing.""" |
| 33 |
with _sphinx.load_sphinx_if_available() as ctx_manager: |
| 34 |
assert ctx_manager is None |
| 35 |
assert not docutils_directives._directives |
| 36 |
assert not docutils_roles._roles |
| 37 |
|
| 38 |
@staticmethod |
| 39 |
@pytest.mark.skipif(not _extras.SPHINX_INSTALLED, reason="Depends on sphinx extra.") |
| 40 |
@pytest.mark.usefixtures("patch_docutils_directives_and_roles_dict") |
| 41 |
def test_yield_nothing_with_sphinx_installed() -> None: |
| 42 |
"""Test for ``None`` yield but action when sphinx is installed.""" |
| 43 |
with _sphinx.load_sphinx_if_available() as ctx_manager: |
| 44 |
assert ctx_manager is None |
| 45 |
assert docutils_directives._directives |
| 46 |
assert docutils_roles._roles |
| 47 |
assert "sphinx.addnodes" not in sphinx.application.builtin_extensions |
| 48 |
|
| 49 |
|
| 50 |
class TestSphinxDirectiveAndRoleGetter: |
| 51 |
"""Test ``get_sphinx_directives_and_roles`` function.""" |
| 52 |
|
| 53 |
@staticmethod |
| 54 |
@pytest.mark.skipif(_extras.SPHINX_INSTALLED, reason="Test without sphinx extra.") |
| 55 |
def test_exception_on_missing_sphinx() -> None: |
| 56 |
"""Test that the install guard triggers.""" |
| 57 |
with pytest.raises(ModuleNotFoundError): |
| 58 |
_sphinx.get_sphinx_directives_and_roles() |
| 59 |
|
| 60 |
@staticmethod |
| 61 |
@pytest.mark.skipif(not _extras.SPHINX_INSTALLED, reason="Depends on sphinx extra.") |
| 62 |
@pytest.mark.usefixtures("patch_docutils_directives_and_roles_dict") |
| 63 |
def test_c_domain_is_loaded() -> None: |
| 64 |
"""Test C domain is loaded.""" |
| 65 |
(result_directives, result_roles) = _sphinx.get_sphinx_directives_and_roles() |
| 66 |
|
| 67 |
assert "function" in result_directives |
| 68 |
assert "c:function" in result_directives |
| 69 |
assert "member" in result_roles |
| 70 |
assert "c:member" in result_roles |
| 71 |
|
| 72 |
@staticmethod |
| 73 |
@pytest.mark.skipif(not _extras.SPHINX_INSTALLED, reason="Depends on sphinx extra.") |
| 74 |
@pytest.mark.usefixtures("patch_docutils_directives_and_roles_dict") |
| 75 |
def test_cpp_domain_is_loaded() -> None: |
| 76 |
"""Test C++ domain is loaded.""" |
| 77 |
(result_directives, result_roles) = _sphinx.get_sphinx_directives_and_roles() |
| 78 |
|
| 79 |
assert "function" in result_directives |
| 80 |
assert "cpp:function" in result_directives |
| 81 |
assert "member" in result_roles |
| 82 |
assert "cpp:member" in result_roles |
| 83 |
|
| 84 |
@staticmethod |
| 85 |
@pytest.mark.skipif(not _extras.SPHINX_INSTALLED, reason="Depends on sphinx extra.") |
| 86 |
@pytest.mark.usefixtures("patch_docutils_directives_and_roles_dict") |
| 87 |
def test_javascript_domain_is_loaded() -> None: |
| 88 |
"""Test JavaScript domain is loaded.""" |
| 89 |
(result_directives, result_roles) = _sphinx.get_sphinx_directives_and_roles() |
| 90 |
|
| 91 |
assert "function" in result_directives |
| 92 |
assert "js:function" in result_directives |
| 93 |
assert "func" in result_roles |
| 94 |
assert "js:func" in result_roles |
| 95 |
|
| 96 |
@staticmethod |
| 97 |
@pytest.mark.skipif(not _extras.SPHINX_INSTALLED, reason="Depends on sphinx extra.") |
| 98 |
@pytest.mark.usefixtures("patch_docutils_directives_and_roles_dict") |
| 99 |
def test_python_domain_is_loaded() -> None: |
| 100 |
"""Test Python domain is loaded.""" |
| 101 |
(result_directives, result_roles) = _sphinx.get_sphinx_directives_and_roles() |
| 102 |
|
| 103 |
assert "function" in result_directives |
| 104 |
assert "py:function" in result_directives |
| 105 |
assert "func" in result_roles |
| 106 |
assert "py:func" in result_roles |
| 107 |
|
| 108 |
@staticmethod |
| 109 |
@pytest.mark.skipif(not _extras.SPHINX_INSTALLED, reason="Depends on sphinx extra.") |
| 110 |
def test_docutils_state_dict_is_loaded(monkeypatch: pytest.MonkeyPatch) -> None: |
| 111 |
"""Test docutils' state is loaded.""" |
| 112 |
test_dict_directives: dict[str, t.Any] = {"test-directive": "test-directive"} |
| 113 |
monkeypatch.setattr("sphinx.util.docutils.directives._directives", test_dict_directives) |
| 114 |
test_dict_roles: dict[str, t.Any] = {"test-role": "test-role"} |
| 115 |
monkeypatch.setattr("sphinx.util.docutils.roles._roles", test_dict_roles) |
| 116 |
|
| 117 |
(result_directives, result_roles) = _sphinx.get_sphinx_directives_and_roles() |
| 118 |
|
| 119 |
assert "test-directive" in result_directives |
| 120 |
assert "test-role" in result_roles |
| 121 |
|
| 122 |
|
| 123 |
class TestDirectiveAndRoleFilter: |
| 124 |
"""Test ``filter_whitelisted_directives_and_roles`` function.""" |
| 125 |
|
| 126 |
@staticmethod |
| 127 |
@pytest.mark.skipif(not _extras.SPHINX_INSTALLED, reason="Depends on sphinx extra.") |
| 128 |
def test_directives_are_filtered(monkeypatch: pytest.MonkeyPatch) -> None: |
| 129 |
"""Test directives are filtered.""" |
| 130 |
monkeypatch.setattr(_sphinx, "_DIRECTIVE_WHITELIST", ["test-directive"]) |
| 131 |
unfiltered_directives = ["test-directive", "test-directive2"] |
| 132 |
|
| 133 |
(result_directives, _) = _sphinx.filter_whitelisted_directives_and_roles( |
| 134 |
unfiltered_directives, [] |
| 135 |
) |
| 136 |
|
| 137 |
assert "test-directive" not in result_directives |
| 138 |
assert "test-directive2" in result_directives |
| 139 |
|
| 140 |
@staticmethod |
| 141 |
@pytest.mark.skipif(not _extras.SPHINX_INSTALLED, reason="Depends on sphinx extra.") |
| 142 |
def test_code_directives_are_filtered() -> None: |
| 143 |
"""Test code directives are filtered.""" |
| 144 |
(unfiltered_directives, _) = _sphinx.get_sphinx_directives_and_roles() |
| 145 |
|
| 146 |
(result_directives, _) = _sphinx.filter_whitelisted_directives_and_roles( |
| 147 |
unfiltered_directives, [] |
| 148 |
) |
| 149 |
|
| 150 |
assert "code" not in result_directives |
| 151 |
assert "code-block" not in result_directives |
| 152 |
assert "sourcecode" not in result_directives |
| 153 |
|
| 154 |
@staticmethod |
| 155 |
@pytest.mark.skipif(not _extras.SPHINX_INSTALLED, reason="Depends on sphinx extra.") |
| 156 |
def test_roles_are_filtered(monkeypatch: pytest.MonkeyPatch) -> None: |
| 157 |
"""Test roles are filtered.""" |
| 158 |
monkeypatch.setattr(_sphinx, "_ROLE_WHITELIST", ["test-role"]) |
| 159 |
unfiltered_roles = ["test-role", "test-role2"] |
| 160 |
|
| 161 |
(_, result_roles) = _sphinx.filter_whitelisted_directives_and_roles( |
| 162 |
[], unfiltered_roles |
| 163 |
) |
| 164 |
|
| 165 |
assert "test-role" not in result_roles |
| 166 |
assert "test-role2" in result_roles |