|
| 1 | +import os.path as op |
| 2 | +import shutil |
| 3 | + |
| 4 | +import pytest |
| 5 | +from sphinx.application import Sphinx |
| 6 | +from sphinx.util.docutils import docutils_namespace |
| 7 | + |
| 8 | + |
| 9 | +# Test framework adapted from sphinx-gallery (BSD 3-clause) |
| 10 | +@pytest.fixture(scope='module') |
| 11 | +def sphinx_app(tmpdir_factory): |
| 12 | + temp_dir = (tmpdir_factory.getbasetemp() / 'root').strpath |
| 13 | + src_dir = op.join(op.dirname(__file__), 'tinybuild') |
| 14 | + |
| 15 | + def ignore(src, names): |
| 16 | + return ('_build', 'generated') |
| 17 | + |
| 18 | + shutil.copytree(src_dir, temp_dir, ignore=ignore) |
| 19 | + # For testing iteration, you can get similar behavior just doing `make` |
| 20 | + # inside the tinybuild directory |
| 21 | + src_dir = temp_dir |
| 22 | + conf_dir = temp_dir |
| 23 | + out_dir = op.join(temp_dir, '_build', 'html') |
| 24 | + toctrees_dir = op.join(temp_dir, '_build', 'toctrees') |
| 25 | + # Avoid warnings about re-registration, see: |
| 26 | + # https://github.com/sphinx-doc/sphinx/issues/5038 |
| 27 | + with docutils_namespace(): |
| 28 | + app = Sphinx(src_dir, conf_dir, out_dir, toctrees_dir, |
| 29 | + buildername='html') |
| 30 | + # need to build within the context manager |
| 31 | + # for automodule and backrefs to work |
| 32 | + app.build(False, []) |
| 33 | + return app |
| 34 | + |
| 35 | + |
| 36 | +def test_MyClass(sphinx_app): |
| 37 | + """Test that class documentation is reasonable.""" |
| 38 | + src_dir, out_dir = sphinx_app.srcdir, sphinx_app.outdir |
| 39 | + class_rst = op.join(src_dir, 'generated', |
| 40 | + 'numpydoc_test_module.MyClass.rst') |
| 41 | + with open(class_rst, 'r') as fid: |
| 42 | + rst = fid.read() |
| 43 | + assert r'numpydoc\_test\_module' in rst # properly escaped |
| 44 | + class_html = op.join(out_dir, 'generated', |
| 45 | + 'numpydoc_test_module.MyClass.html') |
| 46 | + with open(class_html, 'r') as fid: |
| 47 | + html = fid.read() |
| 48 | + # escaped * chars should no longer be preceded by \'s, |
| 49 | + # if we see a \* in the output we know it's incorrect: |
| 50 | + assert r'\*' in html # XXX should be "not in", bug! |
| 51 | + # "self" should not be in the parameter list for the class: |
| 52 | + assert 'self,' in html # XXX should be "not in", bug! |
| 53 | + # check xref was embedded properly (dict should link using xref): |
| 54 | + assert 'stdtypes.html#dict' in html |
| 55 | + |
| 56 | + |
| 57 | +def test_my_function(sphinx_app): |
| 58 | + """Test that a timings page is created.""" |
| 59 | + out_dir = sphinx_app.outdir |
| 60 | + function_html = op.join(out_dir, 'generated', |
| 61 | + 'numpydoc_test_module.my_function.html') |
| 62 | + with open(function_html, 'r') as fid: |
| 63 | + html = fid.read() |
| 64 | + assert r'\*args' not in html |
| 65 | + assert '*args' in html |
| 66 | + # check xref (iterable should link using xref): |
| 67 | + assert 'glossary.html#term-iterable' in html |
0 commit comments