Permalink
Browse files

Adds testing and hopefully a final fix for the empty toctree issue

This adds some more hacky fixes to our layout to avoid singlehtml builders.
Instead of trying to guess if this is working, I added testing!
  • Loading branch information...
agjohnson committed Mar 6, 2017
1 parent 4dc19dc commit 79c72ef53db147be011c0db8465aaad65f00251d
View
@@ -6,6 +6,7 @@
.coverage
*.DS_Store
*.sass-cache
.ropeproject/
.ruby-version
dist/
bower_components/
View
@@ -0,0 +1,11 @@
language: python
python:
- 2.7
- 3.4
- 3.5
- 3.6
sudo: false
install:
- pip install tox-travis
script:
- tox
@@ -129,7 +129,7 @@
The singlehtml builder doesn't handle this toctree call when the
toctree is empty. Skip building this for now.
#}
{% if builder != 'singlehtml' %}
{% if builder not in ['singlehtml', 'readthedocssinglehtml', 'readthedocssinglehtmllocalmedia'] %}
{% set global_toc = toctree(maxdepth=theme_navigation_depth|int, collapse=theme_collapse_navigation, includehidden=True) %}
{% endif %}
{% if global_toc %}
View
No changes.
@@ -0,0 +1,2 @@
bar
===
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
master_doc = 'index'
exclude_patterns = ['_build']
@@ -0,0 +1,6 @@
foo
===
.. toctree::
bar
@@ -0,0 +1,12 @@
test-basic
==========
.. toctree::
foo
Heading
-------
Subheading
~~~~~~~~~~
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
master_doc = 'index'
exclude_patterns = ['_build']
@@ -0,0 +1,10 @@
test-empty
==========
.. toctree::
Heading
-------
Subheading
~~~~~~~~~~
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
master_doc = 'index'
exclude_patterns = ['_build']
@@ -0,0 +1,2 @@
test-missing-toctree
====================
View
@@ -0,0 +1,99 @@
import os
import pytest
import sphinx
from sphinx import addnodes
from sphinx.builders.html import SingleFileHTMLBuilder, DirectoryHTMLBuilder
from .util import build_all
def test_basic():
for (app, status, warning) in build_all('test-basic'):
assert app.env.get_doctree('index').traverse(addnodes.toctree)
content = open(os.path.join(app.outdir, 'index.html')).read()
if isinstance(app.builder, DirectoryHTMLBuilder):
search = (
'<div class="toctree-wrapper compound">\n'
'<ul>\n'
'<li class="toctree-l1">'
'<a class="reference internal" href="foo/">foo</a>'
'<ul>\n'
'<li class="toctree-l2">'
'<a class="reference internal" href="bar/">bar</a></li>\n'
'</ul>\n'
'</li>\n'
'</ul>\n'
'</div>'
)
assert search in content
elif isinstance(app.builder, SingleFileHTMLBuilder):
search = (
'<div class="local-toc"><ul>\n'
'<li class="toctree-l1">'
'<a class="reference internal" href="index.html#document-foo">foo</a>'
'<ul>\n'
'<li class="toctree-l2">'
'<a class="reference internal" href="index.html#document-bar">bar</a>'
'</li>\n'
'</ul>'
)
assert search in content
else:
search = (
'<div class="toctree-wrapper compound">\n'
'<ul>\n'
'<li class="toctree-l1">'
'<a class="reference internal" href="foo.html">foo</a>'
'<ul>\n'
'<li class="toctree-l2">'
'<a class="reference internal" href="bar.html">bar</a></li>\n'
'</ul>\n'
'</li>\n'
'</ul>\n'
'</div>'
)
assert search in content, ('Missing search with builder {0}'
.format(app.builder.name))
def test_empty():
"""Local TOC is showing, as toctree was empty"""
for (app, status, warning) in build_all('test-empty'):
assert app.env.get_doctree('index').traverse(addnodes.toctree)
content = open(os.path.join(app.outdir, 'index.html')).read()
if sphinx.version_info < (1, 4):
if isinstance(app.builder, SingleFileHTMLBuilder):
assert '<div class="toctree-wrapper compound">\n</div>' in content
assert '<div class="local-toc">' in content
else:
global_toc = (
'<div class="toctree-wrapper compound">\n'
'<ul class="simple">\n</ul>\n'
'</div>'
)
local_toc = (
'<div class="local-toc"><ul class="simple">'
'</ul>\n</div>'
)
assert global_toc in content
assert local_toc not in content
else:
global_toc = '<div class="toctree-wrapper compound">\n</div>'
local_toc = (
'<div class="local-toc"><ul>\n'
'<li><a class="reference internal" href="#">test-empty</a></li>'
'</ul>\n</div>'
)
assert global_toc in content
assert local_toc not in content
def test_missing_toctree():
"""Local TOC is showing, as toctree was missing"""
for (app, status, warning) in build_all('test-missing-toctree'):
assert app.env.get_doctree('index').traverse(addnodes.toctree) == []
content = open(os.path.join(app.outdir, 'index.html')).read()
assert '<div class="toctree' not in content
assert '<div class="local-toc">' in content
View
@@ -0,0 +1,60 @@
from __future__ import print_function
import os
import tempfile
import shutil
from contextlib import contextmanager
import pytest
from sphinx.application import Sphinx
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
@contextmanager
def build(root, builder='html', **kwargs):
tmpdir = tempfile.mkdtemp()
srcdir = os.path.join(os.path.dirname(__file__), 'roots', root)
destdir = os.path.join(tmpdir, builder)
doctreedir = os.path.join(tmpdir, 'doctree/')
status = StringIO()
warning = StringIO()
kwargs.update({
'status': status,
'warning': warning,
})
confoverrides = kwargs.pop('confoverrides', {})
confoverrides['html_theme'] = 'sphinx_rtd_theme'
extensions = confoverrides.get('extensions', [])
extensions.append('readthedocs_ext.readthedocs')
confoverrides['extensions'] = extensions
kwargs['confoverrides'] = confoverrides
try:
app = Sphinx(srcdir, srcdir, destdir, doctreedir, builder, **kwargs)
app.builder.build_all()
yield (app, status.getvalue(), warning.getvalue())
except Exception as e:
print('# root:', root)
print('# builder:', builder)
print('# source:', srcdir)
print('# destination:', destdir)
print('# status:', '\n' + status.getvalue())
print('# warning:', '\n' + warning.getvalue())
raise
finally:
shutil.rmtree(tmpdir)
def build_all(root, **kwargs):
for builder in ['html', 'singlehtml', 'readthedocs', 'readthedocsdirhtml',
'readthedocssinglehtml', 'readthedocssinglehtmllocalmedia']:
with build(root, builder, **kwargs) as ret:
yield ret
View
21 tox.ini
@@ -0,0 +1,21 @@
[tox]
envlist = py{27,34,35,36}-sphinx{13,14,15}
[tox:travis]
2.7 = py27-sphinx{13,14,15}
3.4 = py34-sphinx{13,14,15}
3.5 = py35-sphinx{13,14,15}
3.6 = py36-sphinx{13,14,15}
[testenv]
setev =
LANG=C
deps =
.
readthedocs-sphinx-ext
pytest
sphinx13: Sphinx < 1.4
sphinx14: Sphinx < 1.5
sphinx15: Sphinx < 1.6
commands =
py.test {posargs}

0 comments on commit 79c72ef

Please sign in to comment.