2008-06-05 08:58:43 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
Sphinx test suite utilities
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
2016-01-14 22:54:04 +01:00
|
|
|
:copyright: Copyright 2007-2016 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
|
|
|
|
|
import tempfile
|
2013-12-15 14:16:53 +09:00
|
|
|
from functools import wraps
|
2008-06-05 08:58:43 +00:00
|
|
|
|
2016-09-18 17:14:55 +09:00
|
|
|
from six import StringIO, string_types
|
2014-04-29 11:46:47 +09:00
|
|
|
|
2014-09-21 17:17:02 +02:00
|
|
|
from nose import tools, SkipTest
|
|
|
|
|
|
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
|
2012-11-04 09:30:19 +09:00
|
|
|
from sphinx.theming import Theme
|
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
|
|
|
|
2016-06-12 00:00:52 +09:00
|
|
|
from path import path, repr_as # NOQA
|
2008-06-05 08:58:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
2014-09-21 17:17:02 +02:00
|
|
|
'rootdir', 'tempdir', 'raises', 'raises_msg',
|
2010-08-21 23:03:06 +02:00
|
|
|
'skip_if', 'skip_unless', 'skip_unless_importable', 'Struct',
|
2009-02-19 22:12:47 +01:00
|
|
|
'ListOutput', 'TestApp', 'with_app', 'gen_with_app',
|
2014-04-28 19:58:26 +09:00
|
|
|
'path', 'with_tempdir',
|
2010-06-20 22:39:38 +02:00
|
|
|
'sprint', '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()
|
2008-07-29 09:07:37 +00:00
|
|
|
|
|
|
|
|
|
2008-06-05 08:58:43 +00:00
|
|
|
def _excstr(exc):
|
|
|
|
|
if type(exc) is tuple:
|
|
|
|
|
return str(tuple(map(_excstr, exc)))
|
|
|
|
|
return exc.__name__
|
|
|
|
|
|
2014-09-21 17:17:02 +02:00
|
|
|
|
2008-06-05 08:58:43 +00:00
|
|
|
def raises(exc, func, *args, **kwds):
|
2014-09-21 17:17:02 +02:00
|
|
|
"""Raise AssertionError if ``func(*args, **kwds)`` does not raise *exc*."""
|
2008-06-05 08:58:43 +00:00
|
|
|
try:
|
|
|
|
|
func(*args, **kwds)
|
|
|
|
|
except exc:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
raise AssertionError('%s did not raise %s' %
|
|
|
|
|
(func.__name__, _excstr(exc)))
|
|
|
|
|
|
2014-09-21 17:17:02 +02:00
|
|
|
|
2008-06-05 08:58:43 +00:00
|
|
|
def raises_msg(exc, msg, func, *args, **kwds):
|
2014-09-21 17:17:02 +02:00
|
|
|
"""Raise AssertionError if ``func(*args, **kwds)`` does not raise *exc*,
|
|
|
|
|
and check if the message contains *msg*.
|
2008-06-05 08:58:43 +00:00
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
func(*args, **kwds)
|
2014-01-19 14:17:10 +04:00
|
|
|
except exc as err:
|
2008-08-23 15:04:45 +00:00
|
|
|
assert msg in str(err), "\"%s\" not in \"%s\"" % (msg, err)
|
2008-06-05 08:58:43 +00:00
|
|
|
else:
|
|
|
|
|
raise AssertionError('%s did not raise %s' %
|
|
|
|
|
(func.__name__, _excstr(exc)))
|
|
|
|
|
|
2014-09-21 17:17:02 +02: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
|
|
|
|
|
|
|
|
|
2015-09-11 09:35:46 +02:00
|
|
|
try:
|
|
|
|
|
from nose.tools import assert_in, assert_not_in
|
|
|
|
|
except ImportError:
|
|
|
|
|
def assert_in(x, thing, msg=''):
|
|
|
|
|
if x not in thing:
|
2016-05-14 12:28:40 +02:00
|
|
|
assert False, msg or '%r is not in %r' % (x, thing)
|
2016-06-12 00:00:52 +09:00
|
|
|
|
2015-09-11 09:35:46 +02:00
|
|
|
def assert_not_in(x, thing, msg=''):
|
|
|
|
|
if x in thing:
|
2016-05-14 12:28:40 +02:00
|
|
|
assert False, msg or '%r is in %r' % (x, thing)
|
2014-09-21 18:54:01 +02:00
|
|
|
|
|
|
|
|
|
2010-08-21 22:47:18 +02:00
|
|
|
def skip_if(condition, msg=None):
|
|
|
|
|
"""Decorator to skip test if condition is true."""
|
|
|
|
|
def deco(test):
|
|
|
|
|
@tools.make_decorator(test)
|
|
|
|
|
def skipper(*args, **kwds):
|
|
|
|
|
if condition:
|
|
|
|
|
raise SkipTest(msg or 'conditional skip')
|
|
|
|
|
return test(*args, **kwds)
|
|
|
|
|
return skipper
|
|
|
|
|
return deco
|
|
|
|
|
|
2014-09-21 17:17:02 +02:00
|
|
|
|
2010-08-21 22:47:18 +02:00
|
|
|
def skip_unless(condition, msg=None):
|
|
|
|
|
"""Decorator to skip test if condition is false."""
|
|
|
|
|
return skip_if(not condition, msg)
|
|
|
|
|
|
2014-09-21 17:17:02 +02: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:
|
|
|
|
|
return skip_if(True, msg)
|
|
|
|
|
else:
|
|
|
|
|
return skip_if(False, msg)
|
|
|
|
|
|
2008-06-05 08:58:43 +00:00
|
|
|
|
2008-08-04 19:39:05 +00:00
|
|
|
class Struct(object):
|
|
|
|
|
def __init__(self, **kwds):
|
|
|
|
|
self.__dict__.update(kwds)
|
|
|
|
|
|
|
|
|
|
|
2008-08-04 17:01:15 +00:00
|
|
|
class ListOutput(object):
|
2008-06-05 08:58:43 +00:00
|
|
|
"""
|
2008-08-04 17:01:15 +00:00
|
|
|
File-like object that collects written text in a list.
|
2008-06-05 08:58:43 +00:00
|
|
|
"""
|
|
|
|
|
def __init__(self, name):
|
|
|
|
|
self.name = name
|
2008-08-04 17:01:15 +00:00
|
|
|
self.content = []
|
|
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
|
del self.content[:]
|
2008-06-05 08:58:43 +00:00
|
|
|
|
|
|
|
|
def write(self, text):
|
2008-08-04 17:01:15 +00:00
|
|
|
self.content.append(text)
|
2008-06-05 08:58:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestApp(application.Sphinx):
|
|
|
|
|
"""
|
|
|
|
|
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 = {}
|
|
|
|
|
if status is None:
|
2014-04-30 21:30:46 +09:00
|
|
|
status = StringIO()
|
2008-06-05 08:58:43 +00:00
|
|
|
if warning is None:
|
2008-08-04 17:01:15 +00:00
|
|
|
warning = ListOutput('stderr')
|
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):
|
2012-11-04 09:30:19 +09:00
|
|
|
Theme.themes.clear()
|
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:])
|
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)
|
|
|
|
|
|
2008-08-04 17:01:15 +00:00
|
|
|
|
2008-08-23 15:04:45 +00:00
|
|
|
def with_app(*args, **kwargs):
|
2008-08-04 17:01:15 +00:00
|
|
|
"""
|
|
|
|
|
Make a TestApp with args and kwargs, pass it to the test and clean up
|
|
|
|
|
properly.
|
|
|
|
|
"""
|
|
|
|
|
def generator(func):
|
|
|
|
|
@wraps(func)
|
|
|
|
|
def deco(*args2, **kwargs2):
|
2014-09-21 17:17:02 +02:00
|
|
|
status, warning = StringIO(), StringIO()
|
|
|
|
|
kwargs['status'] = status
|
|
|
|
|
kwargs['warning'] = warning
|
2008-08-04 17:01:15 +00:00
|
|
|
app = TestApp(*args, **kwargs)
|
2014-09-21 17:17:02 +02:00
|
|
|
try:
|
|
|
|
|
func(app, status, warning, *args2, **kwargs2)
|
|
|
|
|
finally:
|
|
|
|
|
app.cleanup()
|
2009-02-19 22:12:47 +01:00
|
|
|
return deco
|
|
|
|
|
return generator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def gen_with_app(*args, **kwargs):
|
|
|
|
|
"""
|
2011-05-15 11:15:20 +02:00
|
|
|
Decorate a test generator to pass a TestApp as the first argument to the
|
|
|
|
|
test generator when it's executed.
|
2009-02-19 22:12:47 +01:00
|
|
|
"""
|
|
|
|
|
def generator(func):
|
|
|
|
|
@wraps(func)
|
|
|
|
|
def deco(*args2, **kwargs2):
|
2014-09-21 17:17:02 +02:00
|
|
|
status, warning = StringIO(), StringIO()
|
|
|
|
|
kwargs['status'] = status
|
|
|
|
|
kwargs['warning'] = warning
|
2009-02-19 22:12:47 +01:00
|
|
|
app = TestApp(*args, **kwargs)
|
2014-09-21 17:17:02 +02:00
|
|
|
try:
|
|
|
|
|
for item in func(app, status, warning, *args2, **kwargs2):
|
|
|
|
|
yield item
|
|
|
|
|
finally:
|
|
|
|
|
app.cleanup()
|
2008-08-04 17:01:15 +00:00
|
|
|
return deco
|
|
|
|
|
return generator
|
|
|
|
|
|
2008-06-05 08:58:43 +00:00
|
|
|
|
|
|
|
|
def with_tempdir(func):
|
2009-09-09 16:39:38 +02:00
|
|
|
def new_func(*args, **kwds):
|
2014-09-21 17:17:02 +02:00
|
|
|
new_tempdir = path(tempfile.mkdtemp(dir=tempdir))
|
|
|
|
|
func(new_tempdir, *args, **kwds)
|
2008-06-05 08:58:43 +00:00
|
|
|
new_func.__name__ = func.__name__
|
|
|
|
|
return new_func
|
|
|
|
|
|
|
|
|
|
|
2008-08-04 17:01:15 +00:00
|
|
|
def sprint(*args):
|
|
|
|
|
sys.stderr.write(' '.join(map(str, args)) + '\n')
|
2010-06-20 22:39:38 +02:00
|
|
|
|
2014-09-21 17:17:02 +02:00
|
|
|
|
2010-07-28 19:15:04 +02:00
|
|
|
_unicode_literals_re = re.compile(r'u(".*?")|u(\'.*?\')')
|
2014-09-21 17:17:02 +02:00
|
|
|
|
|
|
|
|
|
2010-06-20 22:39:38 +02:00
|
|
|
def remove_unicode_literals(s):
|
|
|
|
|
return _unicode_literals_re.sub(lambda x: x.group(1) or x.group(2), s)
|
2014-08-03 16:22:08 +09:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
2016-05-30 19:53:59 +09:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def strip_escseq(text):
|
|
|
|
|
return re.sub('\x1b.*?m', '', text)
|