sphinx-autoapi/tests/test_integration.py

173 lines
5.5 KiB
Python
Raw Normal View History

2017-11-05 22:34:49 +00:00
import io
import json
import os
2015-08-03 22:18:49 +00:00
import sys
import shutil
from contextlib import contextmanager
2021-04-04 02:01:27 +00:00
from unittest.mock import patch
import pytest
import sphinx
2015-06-23 02:22:27 +00:00
from sphinx.application import Sphinx
from sphinx.errors import ExtensionError
2015-06-06 20:20:11 +00:00
@contextmanager
def sphinx_build(test_dir, confoverrides=None):
2019-01-27 05:20:45 +00:00
os.chdir("tests/{0}".format(test_dir))
try:
app = Sphinx(
2019-01-27 05:20:45 +00:00
srcdir=".",
confdir=".",
outdir="_build/text",
doctreedir="_build/.doctrees",
buildername="text",
confoverrides=confoverrides,
)
app.build(force_all=True)
yield
finally:
2020-04-06 03:03:05 +00:00
if os.path.exists("_build"):
shutil.rmtree("_build")
2019-01-27 05:20:45 +00:00
os.chdir("../..")
2021-04-04 02:01:27 +00:00
class LanguageIntegrationTests:
def _run_test(self, test_dir, test_file, test_string):
with sphinx_build(test_dir):
2019-01-27 05:20:45 +00:00
with io.open(test_file, encoding="utf8") as fin:
text = fin.read().strip()
2021-04-04 02:01:27 +00:00
assert test_string in text
2021-04-04 02:01:27 +00:00
class TestJavaScript(LanguageIntegrationTests):
def _js_read(self, path):
2019-01-27 05:20:45 +00:00
return json.load(open("../fixtures/javascript.json"))
2019-01-27 05:20:45 +00:00
@patch("autoapi.mappers.javascript.JavaScriptSphinxMapper.read_file", _js_read)
def test_integration(self):
self._run_test(
2019-01-27 05:20:45 +00:00
"jsexample",
"_build/text/autoapi/Circle/index.txt",
"Creates an instance of Circle",
)
2020-04-06 03:03:05 +00:00
@pytest.mark.skipif(
sphinx.version_info >= (3,),
reason="golangdomain extension does not support sphinx >=3",
)
2021-04-04 02:01:27 +00:00
class TestGo(LanguageIntegrationTests):
def _go_read(self, path, **kwargs):
2019-01-27 05:20:45 +00:00
return json.load(open("../fixtures/go.json"))
2019-01-27 05:20:45 +00:00
@patch("autoapi.mappers.go.GoSphinxMapper.read_file", _go_read)
def test_integration(self):
self._run_test(
2019-01-27 05:20:45 +00:00
"goexample",
"_build/text/autoapi/main/index.txt",
"CopyFuncs produces a json-annotated array of Func objects",
)
2020-04-06 03:03:05 +00:00
@pytest.mark.skipif(
sphinx.version_info >= (3,),
reason="dotnetdomain extension does not support sphinx >=3",
)
2021-04-04 02:01:27 +00:00
class TestDotNet(LanguageIntegrationTests):
def _dotnet_read(self, path):
2019-01-27 05:20:45 +00:00
return json.load(open("../fixtures/dotnet.json"))
# Mock this because it's slow otherwise
def _dotnet_load(self, patterns, dirs, ignore=()):
2019-01-27 05:20:45 +00:00
data = self.read_file(path="inmem")
self.paths["inmem"] = data
2015-08-03 18:49:18 +00:00
@staticmethod
def _dotnet_finished(app, exception):
pass
2019-01-27 05:20:45 +00:00
@patch("autoapi.mappers.dotnet.DotNetSphinxMapper.load", _dotnet_load)
@patch("autoapi.mappers.dotnet.DotNetSphinxMapper.read_file", _dotnet_read)
@patch("autoapi.mappers.dotnet.DotNetSphinxMapper.build_finished", _dotnet_finished)
def test_integration(self):
self._run_test(
2019-01-27 05:20:45 +00:00
"dotnetexample",
"_build/text/autoapi/Microsoft/AspNet/Identity/IUserStore-TUser/index.txt",
"Provides an abstraction for a store which manages user accounts.",
)
2021-04-04 02:01:27 +00:00
class TestIntegration(LanguageIntegrationTests):
def test_template_overrides(self):
self._run_test(
2019-01-27 05:20:45 +00:00
"templateexample",
"_build/text/autoapi/example/index.txt",
2022-07-25 18:34:17 +00:00
"This is a function template override",
)
2021-04-04 02:01:27 +00:00
class TestTOCTree(LanguageIntegrationTests):
def test_toctree_overrides(self):
2019-04-06 18:15:18 +00:00
self._run_test("toctreeexample", "_build/text/index.txt", "API Reference")
2016-11-04 02:52:54 +00:00
def test_toctree_domain_insertion(self):
"""
Test that the example_function gets added to the TOC Tree
"""
self._run_test(
"toctreeexample", "_build/text/index.txt", '* "example_function()"'
)
class TestExtensionErrors:
@pytest.fixture(autouse=True)
def unload_go_and_dotnet_libraries(self):
# unload dotnet and golang domain libraries, because they may be imported before
for mod_name in ("sphinxcontrib.dotnetdomain", "sphinxcontrib.golangdomain"):
try:
del sys.modules[mod_name]
except KeyError:
pass
@pytest.mark.parametrize(
"proj_name, override_conf, err_msg",
[
(
"toctreeexample",
{"autoapi_type": "INVALID VALUE"},
(
2023-01-17 05:14:38 +00:00
"Invalid autoapi_type setting, following values are "
'allowed: "dotnet", "go", "javascript", "python"'
),
),
(
"goexample",
{"autoapi_type": "go", "extensions": ["autoapi.extension"]},
(
"AutoAPI of type `go` requires following "
"packages to be installed and included in extensions list: "
"sphinxcontrib.golangdomain (available as "
'"sphinxcontrib-golangdomain" on PyPI)'
),
),
(
"dotnetexample",
{"autoapi_type": "dotnet", "extensions": ["autoapi.extension"]},
(
"AutoAPI of type `dotnet` requires following "
"packages to be installed and included in extensions list: "
"sphinxcontrib.dotnetdomain (available as "
'"sphinxcontrib-dotnetdomain" on PyPI)'
),
),
],
)
def test_extension_setup_errors(self, proj_name, override_conf, err_msg):
with pytest.raises(ExtensionError) as err_info:
with sphinx_build(proj_name, override_conf):
pass
assert str(err_info.value) == err_msg