Files
sphinx/tests/test_build.py
T

166 lines
5.5 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""
test_build
~~~~~~~~~~
2014-09-21 17:17:02 +02:00
Test all builders.
2016-01-14 22:54:04 +01:00
:copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
2014-09-21 17:17:02 +02:00
from six import BytesIO
2016-01-27 01:27:05 +09:00
import pickle
from docutils import nodes
import mock
2012-05-01 15:13:06 +09:00
from textwrap import dedent
from sphinx.errors import SphinxError
2016-06-12 00:00:52 +09:00
import sphinx.builders.linkcheck
2016-11-24 00:26:36 +09:00
from util import with_app, with_tempdir, rootdir, tempdir, SkipTest, TestApp, path
2014-09-21 17:17:02 +02:00
2014-07-12 22:14:14 +09:00
try:
from docutils.writers.manpage import Writer as ManWriter
except ImportError:
ManWriter = None
def request_session_head(url, **kwargs):
response = mock.Mock()
response.status_code = 200
response.url = url
return response
2009-12-28 17:09:09 +01:00
2010-01-17 16:48:02 +01:00
2014-09-21 17:17:02 +02:00
def verify_build(buildername, srcdir):
if buildername == 'man' and ManWriter is None:
2014-07-12 22:14:14 +09:00
raise SkipTest('man writer is not available')
2014-09-21 17:17:02 +02:00
app = TestApp(buildername=buildername, srcdir=srcdir)
try:
2014-09-21 17:17:02 +02:00
app.builder.build_all()
finally:
app.cleanup()
2014-09-21 17:17:02 +02:00
def test_build_all():
# If supported, build in a non-ASCII source dir
test_name = u'\u65e5\u672c\u8a9e'
try:
srcdir = tempdir / test_name
(rootdir / 'root').copytree(tempdir / test_name)
except UnicodeEncodeError:
srcdir = tempdir / 'all'
else:
# add a doc with a non-ASCII file name to the source dir
(srcdir / (test_name + '.txt')).write_text(dedent("""
nonascii file name page
=======================
"""))
master_doc = srcdir / 'contents.txt'
master_doc.write_text(master_doc.text() + dedent(u"""
2014-09-21 17:17:02 +02:00
.. toctree::
%(test_name)s/%(test_name)s
""" % {'test_name': test_name})
)
2014-09-21 17:17:02 +02:00
with mock.patch('sphinx.builders.linkcheck.requests') as requests:
requests.head = request_session_head
# note: no 'html' - if it's ok with dirhtml it's ok with html
for buildername in ['dirhtml', 'singlehtml', 'latex', 'texinfo', 'pickle',
'json', 'text', 'htmlhelp', 'qthelp', 'epub2', 'epub',
'applehelp', 'changes', 'xml', 'pseudoxml', 'man',
'linkcheck']:
yield verify_build, buildername, srcdir
2014-09-21 17:17:02 +02:00
2016-07-14 01:10:57 +09:00
@with_tempdir
def test_master_doc_not_found(tmpdir):
(tmpdir / 'conf.py').write_text('master_doc = "index"')
assert tmpdir.listdir() == ['conf.py']
try:
2016-07-14 01:10:57 +09:00
app = TestApp(buildername='dummy', srcdir=tmpdir)
app.builder.build_all()
assert False # SphinxError not raised
except Exception as exc:
assert isinstance(exc, SphinxError)
2016-01-03 20:52:39 +09:00
finally:
2016-07-14 01:10:57 +09:00
app.cleanup()
2014-09-21 17:17:02 +02:00
@with_app(buildername='text', testroot='circular')
def test_circular_toctree(app, status, warning):
app.builder.build_all()
2014-09-21 17:17:02 +02:00
warnings = warning.getvalue()
2014-10-09 23:53:33 +09:00
assert (
'circular toctree references detected, ignoring: '
'sub <- contents <- sub') in warnings
assert (
'circular toctree references detected, ignoring: '
'contents <- sub <- contents') in warnings
2014-09-21 17:17:02 +02:00
@with_app(buildername='text', testroot='numbered-circular')
def test_numbered_circular_toctree(app, status, warning):
app.builder.build_all()
2014-09-21 17:17:02 +02:00
warnings = warning.getvalue()
2014-10-09 23:53:33 +09:00
assert (
'circular toctree references detected, ignoring: '
'sub <- contents <- sub') in warnings
assert (
'circular toctree references detected, ignoring: '
'contents <- sub <- contents') in warnings
2016-01-27 01:27:05 +09:00
@with_app(buildername='dummy', testroot='image-glob')
2016-01-27 01:27:05 +09:00
def test_image_glob(app, status, warning):
app.builder.build_all()
# index.rst
doctree = pickle.loads((app.doctreedir / 'index.doctree').bytes())
assert isinstance(doctree[0][1], nodes.image)
assert doctree[0][1]['candidates'] == {'*': 'rimg.png'}
assert doctree[0][1]['uri'] == 'rimg.png'
assert isinstance(doctree[0][2], nodes.figure)
assert isinstance(doctree[0][2][0], nodes.image)
assert doctree[0][2][0]['candidates'] == {'*': 'rimg.png'}
assert doctree[0][2][0]['uri'] == 'rimg.png'
assert isinstance(doctree[0][3], nodes.image)
assert doctree[0][3]['candidates'] == {'application/pdf': 'img.pdf',
'image/gif': 'img.gif',
'image/png': 'img.png'}
assert doctree[0][3]['uri'] == 'img.*'
assert isinstance(doctree[0][4], nodes.figure)
assert isinstance(doctree[0][4][0], nodes.image)
assert doctree[0][4][0]['candidates'] == {'application/pdf': 'img.pdf',
'image/gif': 'img.gif',
'image/png': 'img.png'}
assert doctree[0][4][0]['uri'] == 'img.*'
# subdir/index.rst
doctree = pickle.loads((app.doctreedir / 'subdir/index.doctree').bytes())
assert isinstance(doctree[0][1], nodes.image)
2016-11-24 00:26:36 +09:00
sub = path('subdir')
assert doctree[0][1]['candidates'] == {'*': sub / 'rimg.png'}
assert doctree[0][1]['uri'] == sub / 'rimg.png'
assert isinstance(doctree[0][2], nodes.image)
assert doctree[0][2]['candidates'] == {'application/pdf': 'subdir/svgimg.pdf',
2016-01-27 01:27:05 +09:00
'image/svg+xml': 'subdir/svgimg.svg'}
2016-11-24 00:26:36 +09:00
assert doctree[0][2]['uri'] == sub / 'svgimg.*'
2016-01-27 01:27:05 +09:00
assert isinstance(doctree[0][3], nodes.figure)
assert isinstance(doctree[0][3][0], nodes.image)
assert doctree[0][3][0]['candidates'] == {'application/pdf': 'subdir/svgimg.pdf',
2016-01-27 01:27:05 +09:00
'image/svg+xml': 'subdir/svgimg.svg'}
2016-11-24 00:26:36 +09:00
assert doctree[0][3][0]['uri'] == sub / 'svgimg.*'