2008-06-05 08:58:43 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
Sphinx test suite utilities
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
2017-03-22 20:21:12 +09:00
|
|
|
:copyright: Copyright 2007-2017 by the Sphinx team, see AUTHORS.
|
2008-12-27 12:19:17 +01:00
|
|
|
:license: BSD, see LICENSE for details.
|
2008-06-05 08:58:43 +00:00
|
|
|
"""
|
|
|
|
|
|
2014-08-03 16:22:08 +09:00
|
|
|
import os
|
2014-09-21 17:17:02 +02:00
|
|
|
import re
|
2008-06-05 08:58:43 +00:00
|
|
|
import sys
|
2016-12-15 13:12:13 +09:00
|
|
|
import warnings
|
|
|
|
|
from xml.etree import ElementTree
|
2008-06-05 08:58:43 +00:00
|
|
|
|
2017-01-03 22:24:00 +09:00
|
|
|
from six import string_types
|
2014-04-29 11:46:47 +09:00
|
|
|
|
2017-01-03 22:24:00 +09:00
|
|
|
import pytest
|
2014-09-21 17:17:02 +02:00
|
|
|
|
2015-07-22 19:29:22 +02:00
|
|
|
from docutils import nodes
|
|
|
|
|
from docutils.parsers.rst import directives, roles
|
|
|
|
|
|
2008-12-23 20:04:45 +01:00
|
|
|
from sphinx import application
|
2014-11-24 12:13:17 +09:00
|
|
|
from sphinx.builders.latex import LaTeXBuilder
|
2009-02-17 23:55:05 +01:00
|
|
|
from sphinx.ext.autodoc import AutoDirective
|
2014-09-21 17:17:02 +02:00
|
|
|
from sphinx.pycode import ModuleAnalyzer
|
2008-06-05 08:58:43 +00:00
|
|
|
|
2017-01-05 23:43:16 +09:00
|
|
|
from path import path
|
2008-06-05 08:58:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
2017-01-03 22:24:00 +09:00
|
|
|
'rootdir', 'tempdir',
|
|
|
|
|
'skip_unless_importable', 'Struct',
|
|
|
|
|
'SphinxTestApp',
|
|
|
|
|
'path',
|
|
|
|
|
'remove_unicode_literals',
|
2008-06-05 08:58:43 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2014-09-21 17:17:02 +02:00
|
|
|
rootdir = path(os.path.dirname(__file__) or '.').abspath()
|
|
|
|
|
tempdir = path(os.environ['SPHINX_TEST_TEMPDIR']).abspath()
|
Merged revisions 65138,65145-65146,65268-65273 via svnmerge from
svn+ssh://pythondev@svn.python.org/doctools/branches/0.4.x
........
r65138 | georg.brandl | 2008-07-19 15:42:35 +0200 (Sat, 19 Jul 2008) | 2 lines
#3416: fix missing parameter.
........
r65145 | georg.brandl | 2008-07-19 20:01:25 +0200 (Sat, 19 Jul 2008) | 2 lines
Now that we don't ship Jinja anymore by default the comment can go.
........
r65146 | georg.brandl | 2008-07-19 20:01:51 +0200 (Sat, 19 Jul 2008) | 2 lines
Reread documents with globbed toctrees when files are removed/added.
........
r65268 | georg.brandl | 2008-07-29 10:21:33 +0200 (Tue, 29 Jul 2008) | 2 lines
Fix by Markus Gritsch to place correct links to headings.
........
r65269 | georg.brandl | 2008-07-29 10:21:59 +0200 (Tue, 29 Jul 2008) | 2 lines
Make the writer's settings public.
........
r65270 | georg.brandl | 2008-07-29 10:22:28 +0200 (Tue, 29 Jul 2008) | 2 lines
Export test_root.
........
r65271 | georg.brandl | 2008-07-29 10:22:47 +0200 (Tue, 29 Jul 2008) | 2 lines
Add a markup test.
........
r65272 | georg.brandl | 2008-07-29 10:27:19 +0200 (Tue, 29 Jul 2008) | 2 lines
Bump version number.
........
r65273 | georg.brandl | 2008-07-29 11:05:37 +0200 (Tue, 29 Jul 2008) | 2 lines
Correct rendering of ``samp``.
........
2008-07-29 09:07:37 +00:00
|
|
|
|
|
|
|
|
|
2014-09-21 18:54:01 +02:00
|
|
|
def assert_re_search(regex, text, flags=0):
|
|
|
|
|
if not re.search(regex, text, flags):
|
|
|
|
|
assert False, '%r did not match %r' % (regex, text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def assert_not_re_search(regex, text, flags=0):
|
|
|
|
|
if re.search(regex, text, flags):
|
|
|
|
|
assert False, '%r did match %r' % (regex, text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def assert_startswith(thing, prefix):
|
|
|
|
|
if not thing.startswith(prefix):
|
|
|
|
|
assert False, '%r does not start with %r' % (thing, prefix)
|
|
|
|
|
|
|
|
|
|
|
2016-09-18 17:14:55 +09:00
|
|
|
def assert_node(node, cls=None, xpath="", **kwargs):
|
2016-01-27 01:36:43 +09:00
|
|
|
if cls:
|
2016-09-18 17:14:55 +09:00
|
|
|
if isinstance(cls, list):
|
|
|
|
|
assert_node(node, cls[0], xpath=xpath, **kwargs)
|
|
|
|
|
if cls[1:]:
|
|
|
|
|
if isinstance(cls[1], tuple):
|
|
|
|
|
assert_node(node, cls[1], xpath=xpath, **kwargs)
|
|
|
|
|
else:
|
|
|
|
|
assert len(node) == 1, \
|
|
|
|
|
'The node%s has %d child nodes, not one' % (xpath, len(node))
|
|
|
|
|
assert_node(node[0], cls[1:], xpath=xpath + "[0]", **kwargs)
|
|
|
|
|
elif isinstance(cls, tuple):
|
|
|
|
|
assert len(node) == len(cls), \
|
|
|
|
|
'The node%s has %d child nodes, not %r' % (xpath, len(node), len(cls))
|
|
|
|
|
for i, nodecls in enumerate(cls):
|
|
|
|
|
path = xpath + "[%d]" % i
|
|
|
|
|
assert_node(node[i], nodecls, xpath=path, **kwargs)
|
|
|
|
|
elif isinstance(cls, string_types):
|
|
|
|
|
assert node == cls, 'The node %r is not %r: %r' % (xpath, cls, node)
|
|
|
|
|
else:
|
|
|
|
|
assert isinstance(node, cls), \
|
|
|
|
|
'The node%s is not subclass of %r: %r' % (xpath, cls, node)
|
2016-01-27 01:36:43 +09:00
|
|
|
|
|
|
|
|
for key, value in kwargs.items():
|
2016-09-18 17:14:55 +09:00
|
|
|
assert key in node, 'The node%s does not have %r attribute: %r' % (xpath, key, node)
|
2016-01-27 01:36:43 +09:00
|
|
|
assert node[key] == value, \
|
2016-09-18 17:14:55 +09:00
|
|
|
'The node%s[%s] is not %r: %r' % (xpath, key, value, node[key])
|
2016-01-27 01:36:43 +09:00
|
|
|
|
|
|
|
|
|
2010-08-21 23:03:06 +02:00
|
|
|
def skip_unless_importable(module, msg=None):
|
|
|
|
|
"""Decorator to skip test if module is not importable."""
|
|
|
|
|
try:
|
|
|
|
|
__import__(module)
|
|
|
|
|
except ImportError:
|
2017-01-03 22:24:00 +09:00
|
|
|
return pytest.mark.skipif(True, reason=(msg or 'conditional skip'))
|
2010-08-21 23:03:06 +02:00
|
|
|
else:
|
2017-01-03 22:24:00 +09:00
|
|
|
return pytest.mark.skipif(False, reason=(msg or 'conditional skip'))
|
2010-08-21 23:03:06 +02:00
|
|
|
|
2008-06-05 08:58:43 +00:00
|
|
|
|
2016-12-15 13:12:13 +09:00
|
|
|
def etree_parse(path):
|
|
|
|
|
with warnings.catch_warnings(record=False):
|
|
|
|
|
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
|
|
|
|
return ElementTree.parse(path)
|
|
|
|
|
|
|
|
|
|
|
2008-08-04 19:39:05 +00:00
|
|
|
class Struct(object):
|
|
|
|
|
def __init__(self, **kwds):
|
|
|
|
|
self.__dict__.update(kwds)
|
|
|
|
|
|
|
|
|
|
|
2017-01-03 22:24:00 +09:00
|
|
|
class SphinxTestApp(application.Sphinx):
|
2008-06-05 08:58:43 +00:00
|
|
|
"""
|
|
|
|
|
A subclass of :class:`Sphinx` that runs on the test root, with some
|
|
|
|
|
better default values for the initialization parameters.
|
|
|
|
|
"""
|
|
|
|
|
|
2014-09-21 17:17:02 +02:00
|
|
|
def __init__(self, buildername='html', testroot=None, srcdir=None,
|
|
|
|
|
freshenv=False, confoverrides=None, status=None, warning=None,
|
|
|
|
|
tags=None, docutilsconf=None):
|
|
|
|
|
if testroot is None:
|
|
|
|
|
defaultsrcdir = 'root'
|
|
|
|
|
testroot = rootdir / 'root'
|
|
|
|
|
else:
|
|
|
|
|
defaultsrcdir = 'test-' + testroot
|
|
|
|
|
testroot = rootdir / 'roots' / ('test-' + testroot)
|
2008-06-05 08:58:43 +00:00
|
|
|
if srcdir is None:
|
2014-09-21 17:17:02 +02:00
|
|
|
srcdir = tempdir / defaultsrcdir
|
2008-06-05 08:58:43 +00:00
|
|
|
else:
|
2014-09-21 17:17:02 +02:00
|
|
|
srcdir = tempdir / srcdir
|
|
|
|
|
|
|
|
|
|
if not srcdir.exists():
|
|
|
|
|
testroot.copytree(srcdir)
|
|
|
|
|
|
|
|
|
|
if docutilsconf is not None:
|
|
|
|
|
(srcdir / 'docutils.conf').write_text(docutilsconf)
|
|
|
|
|
|
|
|
|
|
builddir = srcdir / '_build'
|
|
|
|
|
# if confdir is None:
|
|
|
|
|
confdir = srcdir
|
|
|
|
|
# if outdir is None:
|
|
|
|
|
outdir = builddir.joinpath(buildername)
|
|
|
|
|
if not outdir.isdir():
|
|
|
|
|
outdir.makedirs()
|
|
|
|
|
# if doctreedir is None:
|
|
|
|
|
doctreedir = builddir.joinpath('doctrees')
|
|
|
|
|
if not doctreedir.isdir():
|
|
|
|
|
doctreedir.makedirs()
|
2008-06-05 08:58:43 +00:00
|
|
|
if confoverrides is None:
|
|
|
|
|
confoverrides = {}
|
2014-09-21 17:17:02 +02:00
|
|
|
# if warningiserror is None:
|
|
|
|
|
warningiserror = False
|
|
|
|
|
|
|
|
|
|
self._saved_path = sys.path[:]
|
2015-07-22 19:29:22 +02:00
|
|
|
self._saved_directives = directives._directives.copy()
|
|
|
|
|
self._saved_roles = roles._roles.copy()
|
|
|
|
|
|
|
|
|
|
self._saved_nodeclasses = set(v for v in dir(nodes.GenericNodeVisitor)
|
|
|
|
|
if v.startswith('visit_'))
|
2008-06-05 08:58:43 +00:00
|
|
|
|
2015-07-22 19:29:22 +02:00
|
|
|
try:
|
|
|
|
|
application.Sphinx.__init__(self, srcdir, confdir, outdir, doctreedir,
|
|
|
|
|
buildername, confoverrides, status, warning,
|
|
|
|
|
freshenv, warningiserror, tags)
|
|
|
|
|
except:
|
|
|
|
|
self.cleanup()
|
|
|
|
|
raise
|
2008-06-05 08:58:43 +00:00
|
|
|
|
2008-08-04 22:20:44 +00:00
|
|
|
def cleanup(self, doctrees=False):
|
2009-02-17 23:55:05 +01:00
|
|
|
AutoDirective._registry.clear()
|
2014-09-21 17:17:02 +02:00
|
|
|
ModuleAnalyzer.cache.clear()
|
2014-11-24 12:13:17 +09:00
|
|
|
LaTeXBuilder.usepackages = []
|
2014-09-21 17:17:02 +02:00
|
|
|
sys.path[:] = self._saved_path
|
|
|
|
|
sys.modules.pop('autodoc_fodder', None)
|
2015-07-22 19:29:22 +02:00
|
|
|
directives._directives = self._saved_directives
|
|
|
|
|
roles._roles = self._saved_roles
|
|
|
|
|
for method in dir(nodes.GenericNodeVisitor):
|
|
|
|
|
if method.startswith('visit_') and \
|
|
|
|
|
method not in self._saved_nodeclasses:
|
|
|
|
|
delattr(nodes.GenericNodeVisitor, 'visit_' + method[6:])
|
|
|
|
|
delattr(nodes.GenericNodeVisitor, 'depart_' + method[6:])
|
Merged revisions 65283,65303,65316-65317,65372-65375,65377,65380,65483-65485,65494 via svnmerge from
svn+ssh://pythondev@svn.python.org/doctools/branches/0.4.x
........
r65283 | georg.brandl | 2008-07-29 10:07:26 +0000 (Tue, 29 Jul 2008) | 2 lines
Update ez_setup.py.
........
r65303 | benjamin.peterson | 2008-07-30 12:35:34 +0000 (Wed, 30 Jul 2008) | 1 line
add a with_testapp decorator for test functions that passes the TestApp instance in a cleans up after it
........
r65316 | benjamin.peterson | 2008-07-30 23:12:07 +0000 (Wed, 30 Jul 2008) | 1 line
make the app for test_markup global to the module
........
r65317 | benjamin.peterson | 2008-07-30 23:31:29 +0000 (Wed, 30 Jul 2008) | 1 line
make TestApp.cleanup more aggressive
........
r65372 | georg.brandl | 2008-08-01 19:11:22 +0000 (Fri, 01 Aug 2008) | 2 lines
Add more tests, fix a few bugs in image handling.
........
r65373 | georg.brandl | 2008-08-01 19:28:33 +0000 (Fri, 01 Aug 2008) | 2 lines
Fix oversight.
........
r65374 | benjamin.peterson | 2008-08-01 19:36:32 +0000 (Fri, 01 Aug 2008) | 1 line
fix one broken test
........
r65375 | georg.brandl | 2008-08-01 19:41:11 +0000 (Fri, 01 Aug 2008) | 2 lines
Fix the handling of non-ASCII input in quickstart.
........
r65377 | georg.brandl | 2008-08-01 19:48:24 +0000 (Fri, 01 Aug 2008) | 2 lines
Allow REs in markup checks.
........
r65380 | georg.brandl | 2008-08-01 20:31:18 +0000 (Fri, 01 Aug 2008) | 2 lines
Don't rely on mtimes being different for changed files.
........
r65483 | georg.brandl | 2008-08-04 09:01:40 +0000 (Mon, 04 Aug 2008) | 4 lines
Add an "encoding" option to literalinclude.
Add tests for include directives.
........
r65484 | georg.brandl | 2008-08-04 09:11:17 +0000 (Mon, 04 Aug 2008) | 2 lines
Add changelog entry.
........
r65485 | georg.brandl | 2008-08-04 09:21:58 +0000 (Mon, 04 Aug 2008) | 2 lines
Fix markup.
........
r65494 | georg.brandl | 2008-08-04 16:34:59 +0000 (Mon, 04 Aug 2008) | 2 lines
Correctly use HTML file suffix in templates.
........
2008-08-04 17:01:15 +00:00
|
|
|
|
2014-07-12 22:14:14 +09:00
|
|
|
def __repr__(self):
|
|
|
|
|
return '<%s buildername=%r>' % (self.__class__.__name__, self.builder.name)
|
|
|
|
|
|
Merged revisions 65283,65303,65316-65317,65372-65375,65377,65380,65483-65485,65494 via svnmerge from
svn+ssh://pythondev@svn.python.org/doctools/branches/0.4.x
........
r65283 | georg.brandl | 2008-07-29 10:07:26 +0000 (Tue, 29 Jul 2008) | 2 lines
Update ez_setup.py.
........
r65303 | benjamin.peterson | 2008-07-30 12:35:34 +0000 (Wed, 30 Jul 2008) | 1 line
add a with_testapp decorator for test functions that passes the TestApp instance in a cleans up after it
........
r65316 | benjamin.peterson | 2008-07-30 23:12:07 +0000 (Wed, 30 Jul 2008) | 1 line
make the app for test_markup global to the module
........
r65317 | benjamin.peterson | 2008-07-30 23:31:29 +0000 (Wed, 30 Jul 2008) | 1 line
make TestApp.cleanup more aggressive
........
r65372 | georg.brandl | 2008-08-01 19:11:22 +0000 (Fri, 01 Aug 2008) | 2 lines
Add more tests, fix a few bugs in image handling.
........
r65373 | georg.brandl | 2008-08-01 19:28:33 +0000 (Fri, 01 Aug 2008) | 2 lines
Fix oversight.
........
r65374 | benjamin.peterson | 2008-08-01 19:36:32 +0000 (Fri, 01 Aug 2008) | 1 line
fix one broken test
........
r65375 | georg.brandl | 2008-08-01 19:41:11 +0000 (Fri, 01 Aug 2008) | 2 lines
Fix the handling of non-ASCII input in quickstart.
........
r65377 | georg.brandl | 2008-08-01 19:48:24 +0000 (Fri, 01 Aug 2008) | 2 lines
Allow REs in markup checks.
........
r65380 | georg.brandl | 2008-08-01 20:31:18 +0000 (Fri, 01 Aug 2008) | 2 lines
Don't rely on mtimes being different for changed files.
........
r65483 | georg.brandl | 2008-08-04 09:01:40 +0000 (Mon, 04 Aug 2008) | 4 lines
Add an "encoding" option to literalinclude.
Add tests for include directives.
........
r65484 | georg.brandl | 2008-08-04 09:11:17 +0000 (Mon, 04 Aug 2008) | 2 lines
Add changelog entry.
........
r65485 | georg.brandl | 2008-08-04 09:21:58 +0000 (Mon, 04 Aug 2008) | 2 lines
Fix markup.
........
r65494 | georg.brandl | 2008-08-04 16:34:59 +0000 (Mon, 04 Aug 2008) | 2 lines
Correctly use HTML file suffix in templates.
........
2008-08-04 17:01:15 +00:00
|
|
|
|
2017-01-03 22:24:00 +09:00
|
|
|
_unicode_literals_re = re.compile(r'u(".*?")|u(\'.*?\')')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_unicode_literals(s):
|
|
|
|
|
return _unicode_literals_re.sub(lambda x: x.group(1) or x.group(2), s)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_files(root, suffix=None):
|
|
|
|
|
for dirpath, dirs, files in os.walk(root, followlinks=True):
|
|
|
|
|
dirpath = path(dirpath)
|
|
|
|
|
for f in [f for f in files if not suffix or f.endswith(suffix)]:
|
|
|
|
|
fpath = dirpath / f
|
|
|
|
|
yield os.path.relpath(fpath, root)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def strip_escseq(text):
|
|
|
|
|
return re.sub('\x1b.*?m', '', text)
|