Merge pull request #375 from rtfd/fix-and-test-empty-toctree

Adds testing and hopefully a final fix for the empty toctree issue
This commit is contained in:
Anthony
2017-03-06 15:20:16 -08:00
committed by GitHub
16 changed files with 241 additions and 6 deletions

1
.gitignore vendored
View File

@@ -6,6 +6,7 @@
.coverage
*.DS_Store
*.sass-cache
.ropeproject/
.ruby-version
dist/
bower_components/

11
.travis.yml Normal file
View File

@@ -0,0 +1,11 @@
language: python
python:
- 2.7
- 3.4
- 3.5
- 3.6
sudo: false
install:
- pip install tox-travis
script:
- tox

View File

@@ -17,12 +17,11 @@ Read the Docs Sphinx Theme
View a working demo_ over on readthedocs.org_.
This is a mobile-friendly sphinx_ theme I made for readthedocs.org_. It's
currently in development there and includes some rtd variable checks that can be ignored
if you're just trying to use it on your project outside of that site.
This is a mobile-friendly sphinx_ theme I made for readthedocs.org_.
**This repo also exists as a submodule within the readthedocs itself**, so please make your edits to
the SASS files here, rather than the .css files on RTD.
If you'd like to update the theme,
please make your edits to the SASS files here,
rather than the .css files on checked into the repo.
.. image:: screen_mobile.png
:width: 100%

View File

@@ -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 'singlehtml' not in builder %}
{% set global_toc = toctree(maxdepth=theme_navigation_depth|int, collapse=theme_collapse_navigation, includehidden=True) %}
{% endif %}
{% if global_toc %}

0
tests/__init__.py Normal file
View File

View File

@@ -0,0 +1,2 @@
bar
===

View File

@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
master_doc = 'index'
exclude_patterns = ['_build']

View File

@@ -0,0 +1,6 @@
foo
===
.. toctree::
bar

View File

@@ -0,0 +1,12 @@
test-basic
==========
.. toctree::
foo
Heading
-------
Subheading
~~~~~~~~~~

View File

@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
master_doc = 'index'
exclude_patterns = ['_build']

View File

@@ -0,0 +1,10 @@
test-empty
==========
.. toctree::
Heading
-------
Subheading
~~~~~~~~~~

View File

@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
master_doc = 'index'
exclude_patterns = ['_build']

View File

@@ -0,0 +1,2 @@
test-missing-toctree
====================

99
tests/test_builders.py Normal file
View File

@@ -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

60
tests/util.py Normal file
View File

@@ -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

21
tox.ini Normal file
View File

@@ -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}