2017-11-05 22:34:49 +00:00
|
|
|
import io
|
2015-06-23 03:22:51 +00:00
|
|
|
import json
|
2015-05-29 21:29:56 +00:00
|
|
|
import os
|
2015-08-03 22:18:49 +00:00
|
|
|
import sys
|
2015-05-29 21:29:56 +00:00
|
|
|
import shutil
|
|
|
|
import unittest
|
2016-11-02 23:29:28 +00:00
|
|
|
from contextlib import contextmanager
|
2015-05-29 21:29:56 +00:00
|
|
|
|
2015-06-23 03:22:51 +00:00
|
|
|
from mock import patch
|
2017-11-10 20:58:34 +00:00
|
|
|
import pytest
|
2015-06-23 03:22:51 +00:00
|
|
|
|
2017-11-10 20:58:34 +00:00
|
|
|
import sphinx
|
2015-06-23 02:22:27 +00:00
|
|
|
from sphinx.application import Sphinx
|
2015-06-06 20:20:11 +00:00
|
|
|
|
2015-05-29 21:29:56 +00:00
|
|
|
|
2016-11-02 23:29:28 +00:00
|
|
|
@contextmanager
|
2017-11-10 20:58:34 +00:00
|
|
|
def sphinx_build(test_dir, confoverrides=None):
|
2016-11-02 23:29:28 +00:00
|
|
|
os.chdir('tests/{0}'.format(test_dir))
|
|
|
|
try:
|
|
|
|
app = Sphinx(
|
|
|
|
srcdir='.',
|
|
|
|
confdir='.',
|
|
|
|
outdir='_build/text',
|
|
|
|
doctreedir='_build/.doctrees',
|
|
|
|
buildername='text',
|
2017-11-10 20:58:34 +00:00
|
|
|
confoverrides=confoverrides,
|
2016-11-02 23:29:28 +00:00
|
|
|
)
|
|
|
|
app.build(force_all=True)
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
shutil.rmtree('_build')
|
|
|
|
os.chdir('../..')
|
|
|
|
|
|
|
|
|
2015-06-10 18:48:15 +00:00
|
|
|
class LanguageIntegrationTests(unittest.TestCase):
|
2015-05-29 21:29:56 +00:00
|
|
|
|
2015-06-23 03:22:51 +00:00
|
|
|
def _run_test(self, test_dir, test_file, test_string):
|
2016-11-02 23:29:28 +00:00
|
|
|
with sphinx_build(test_dir):
|
2017-11-05 22:34:49 +00:00
|
|
|
with io.open(test_file, encoding='utf8') as fin:
|
2015-06-23 03:22:51 +00:00
|
|
|
text = fin.read().strip()
|
|
|
|
self.assertIn(test_string, text)
|
|
|
|
|
|
|
|
|
|
|
|
class JavaScriptTests(LanguageIntegrationTests):
|
|
|
|
|
|
|
|
def _js_read(self, path):
|
|
|
|
return json.load(open('../fixtures/javascript.json'))
|
|
|
|
|
|
|
|
@patch('autoapi.mappers.javascript.JavaScriptSphinxMapper.read_file', _js_read)
|
|
|
|
def test_integration(self):
|
|
|
|
self._run_test(
|
2015-06-23 02:22:27 +00:00
|
|
|
'jsexample',
|
|
|
|
'_build/text/autoapi/Circle/index.txt',
|
|
|
|
'Creates an instance of Circle'
|
2015-06-23 03:22:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class GoTests(LanguageIntegrationTests):
|
|
|
|
|
|
|
|
def _go_read(self, path):
|
|
|
|
return json.load(open('../fixtures/go.json'))
|
|
|
|
|
|
|
|
@patch('autoapi.mappers.go.GoSphinxMapper.read_file', _go_read)
|
|
|
|
def test_integration(self):
|
|
|
|
self._run_test(
|
2015-06-23 02:22:27 +00:00
|
|
|
'goexample',
|
|
|
|
'_build/text/autoapi/main/index.txt',
|
|
|
|
'CopyFuncs produces a json-annotated array of Func objects'
|
2015-06-23 03:22:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class PythonTests(LanguageIntegrationTests):
|
|
|
|
|
|
|
|
def test_integration(self):
|
2016-11-02 23:29:28 +00:00
|
|
|
with sphinx_build('pyexample'):
|
2017-11-05 22:34:49 +00:00
|
|
|
example_path = '_build/text/autoapi/example/index.txt'
|
|
|
|
with io.open(example_path, encoding='utf8') as example_handle:
|
|
|
|
example_file = example_handle.read()
|
2016-11-02 23:29:28 +00:00
|
|
|
self.assertIn(
|
|
|
|
'class example.Foo',
|
|
|
|
example_file
|
|
|
|
)
|
|
|
|
self.assertIn(
|
2016-11-04 22:47:57 +00:00
|
|
|
'method_okay(foo=None, bar=None)',
|
2016-11-02 23:29:28 +00:00
|
|
|
example_file
|
|
|
|
)
|
|
|
|
self.assertIn(
|
2016-11-04 22:47:57 +00:00
|
|
|
'method_multiline(foo=None, bar=None, baz=None)',
|
2016-11-02 23:29:28 +00:00
|
|
|
example_file
|
|
|
|
)
|
|
|
|
self.assertIn(
|
2016-11-04 22:47:57 +00:00
|
|
|
'method_tricky(foo=None, bar=dict)',
|
2016-11-02 23:29:28 +00:00
|
|
|
example_file
|
|
|
|
)
|
|
|
|
self.assertFalse(
|
|
|
|
os.path.exists('_build/text/autoapi/method_multiline')
|
|
|
|
)
|
2017-11-05 22:34:49 +00:00
|
|
|
index_path = '_build/text/index.txt'
|
|
|
|
with io.open(index_path, encoding='utf8') as index_handle:
|
|
|
|
index_file = index_handle.read()
|
2017-08-28 21:01:14 +00:00
|
|
|
self.assertIn(
|
|
|
|
'Sphinx AutoAPI Index',
|
|
|
|
index_file
|
|
|
|
)
|
|
|
|
self.assertIn(
|
|
|
|
'Foo',
|
|
|
|
index_file
|
|
|
|
)
|
2015-06-23 03:22:51 +00:00
|
|
|
|
2017-11-10 20:58:34 +00:00
|
|
|
@pytest.mark.skipif(sphinx.version_info < (1, 4),
|
|
|
|
reason="Cannot override extensions in Sphinx 1.3")
|
|
|
|
def test_napoleon_integration(self):
|
|
|
|
with sphinx_build('pyexample'):
|
|
|
|
example_path = '_build/text/autoapi/example/index.txt'
|
|
|
|
with io.open(example_path, encoding='utf8') as example_handle:
|
|
|
|
example_file = example_handle.read()
|
|
|
|
|
|
|
|
# Check that docstrings are not transformed without napoleon loaded
|
|
|
|
self.assertIn(
|
|
|
|
'Args',
|
|
|
|
example_file
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertIn(
|
|
|
|
'Returns',
|
|
|
|
example_file
|
|
|
|
)
|
|
|
|
|
|
|
|
confoverrides={
|
|
|
|
'extensions': [
|
|
|
|
'autoapi.extension',
|
|
|
|
'sphinx.ext.autodoc',
|
|
|
|
'sphinx.ext.napoleon',
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
with sphinx_build('pyexample', confoverrides=confoverrides):
|
|
|
|
example_path = '_build/text/autoapi/example/index.txt'
|
|
|
|
with io.open(example_path, encoding='utf8') as example_handle:
|
|
|
|
example_file = example_handle.read()
|
|
|
|
|
|
|
|
self.assertIn(
|
|
|
|
'Parameters',
|
|
|
|
example_file
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertIn(
|
|
|
|
'Return type',
|
|
|
|
example_file
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertNotIn(
|
|
|
|
'Args',
|
|
|
|
example_file
|
|
|
|
)
|
|
|
|
|
2015-06-23 03:22:51 +00:00
|
|
|
|
|
|
|
class DotNetTests(LanguageIntegrationTests):
|
|
|
|
|
|
|
|
def _dotnet_read(self, path):
|
|
|
|
return json.load(open('../fixtures/dotnet.json'))
|
|
|
|
|
|
|
|
# Mock this because it's slow otherwise
|
2015-09-23 23:00:43 +00:00
|
|
|
def _dotnet_load(self, patterns, dirs, ignore=[]):
|
2015-06-23 03:22:51 +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
|
|
|
|
|
2015-06-23 03:22:51 +00:00
|
|
|
@patch('autoapi.mappers.dotnet.DotNetSphinxMapper.load', _dotnet_load)
|
|
|
|
@patch('autoapi.mappers.dotnet.DotNetSphinxMapper.read_file', _dotnet_read)
|
2015-08-03 18:49:18 +00:00
|
|
|
@patch('autoapi.mappers.dotnet.DotNetSphinxMapper.build_finished', _dotnet_finished)
|
2015-06-23 03:22:51 +00:00
|
|
|
def test_integration(self):
|
|
|
|
self._run_test(
|
2015-06-23 02:22:27 +00:00
|
|
|
'dotnetexample',
|
2016-03-26 01:17:05 +00:00
|
|
|
'_build/text/autoapi/Microsoft/AspNet/Identity/IUserStore-TUser/index.txt',
|
|
|
|
'Provides an abstraction for a store which manages user accounts.'
|
2015-06-23 03:22:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class IntegrationTests(LanguageIntegrationTests):
|
|
|
|
|
|
|
|
def test_template_overrides(self):
|
|
|
|
self._run_test(
|
2015-06-23 02:22:27 +00:00
|
|
|
'templateexample',
|
|
|
|
'_build/text/autoapi/example/index.txt',
|
|
|
|
'This is a fuction template override'
|
2015-06-23 03:22:51 +00:00
|
|
|
)
|
2015-07-07 22:43:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TOCTreeTests(LanguageIntegrationTests):
|
|
|
|
|
|
|
|
def test_toctree_overrides(self):
|
|
|
|
self._run_test(
|
|
|
|
'toctreeexample',
|
|
|
|
'_build/text/index.txt',
|
|
|
|
'AutoAPI Index'
|
|
|
|
)
|
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'
|
|
|
|
)
|