summaryrefslogtreecommitdiff
path: root/sphinx/testing/util.py
diff options
context:
space:
mode:
Diffstat (limited to 'sphinx/testing/util.py')
-rw-r--r--sphinx/testing/util.py74
1 files changed, 38 insertions, 36 deletions
diff --git a/sphinx/testing/util.py b/sphinx/testing/util.py
index bd6f5948f..6ad254b35 100644
--- a/sphinx/testing/util.py
+++ b/sphinx/testing/util.py
@@ -1,4 +1,3 @@
-# -*- coding: utf-8 -*-
"""
sphinx.testing.util
~~~~~~~~~~~~~~~~~~~
@@ -16,11 +15,10 @@ from xml.etree import ElementTree
from docutils import nodes
from docutils.parsers.rst import directives, roles
-from six import string_types
from sphinx import application, locale
from sphinx.builders.latex import LaTeXBuilder
-from sphinx.ext.autodoc import AutoDirective
+from sphinx.deprecation import RemovedInSphinx40Warning
from sphinx.pycode import ModuleAnalyzer
from sphinx.testing.path import path
from sphinx.util.osutil import relpath
@@ -39,25 +37,25 @@ __all__ = [
def assert_re_search(regex, text, flags=0):
- # type: (Pattern, unicode, int) -> None
+ # type: (Pattern, str, int) -> None
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):
- # type: (Pattern, unicode, int) -> None
+ # type: (Pattern, str, int) -> None
if re.search(regex, text, flags):
assert False, '%r did match %r' % (regex, text)
def assert_startswith(thing, prefix):
- # type: (unicode, unicode) -> None
+ # type: (str, str) -> None
if not thing.startswith(prefix):
assert False, '%r does not start with %r' % (thing, prefix)
def assert_node(node, cls=None, xpath="", **kwargs):
- # type: (nodes.Node, Any, unicode, Any) -> None
+ # type: (nodes.Node, Any, str, Any) -> None
if cls:
if isinstance(cls, list):
assert_node(node, cls[0], xpath=xpath, **kwargs)
@@ -65,35 +63,44 @@ def assert_node(node, cls=None, xpath="", **kwargs):
if isinstance(cls[1], tuple):
assert_node(node, cls[1], xpath=xpath, **kwargs)
else:
+ assert isinstance(node, nodes.Element), \
+ 'The node%s does not have any children' % xpath
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 isinstance(node, nodes.Element), \
+ 'The node%s does not have any items' % xpath
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):
+ elif isinstance(cls, str):
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)
- for key, value in kwargs.items():
- assert key in node, 'The node%s does not have %r attribute: %r' % (xpath, key, node)
- assert node[key] == value, \
- 'The node%s[%s] is not %r: %r' % (xpath, key, value, node[key])
+ if kwargs:
+ assert isinstance(node, nodes.Element), \
+ 'The node%s does not have any attributes' % xpath
+
+ for key, value in kwargs.items():
+ assert key in node, \
+ 'The node%s does not have %r attribute: %r' % (xpath, key, node)
+ assert node[key] == value, \
+ 'The node%s[%s] is not %r: %r' % (xpath, key, value, node[key])
def etree_parse(path):
- # type: (unicode) -> Any
+ # type: (str) -> Any
with warnings.catch_warnings(record=False):
warnings.filterwarnings("ignore", category=DeprecationWarning)
- return ElementTree.parse(path) # type: ignore
+ return ElementTree.parse(path)
-class Struct(object):
+class Struct:
def __init__(self, **kwds):
# type: (Any) -> None
self.__dict__.update(kwds)
@@ -108,52 +115,45 @@ class SphinxTestApp(application.Sphinx):
def __init__(self, buildername='html', srcdir=None,
freshenv=False, confoverrides=None, status=None, warning=None,
tags=None, docutilsconf=None):
- # type: (unicode, path, bool, Dict, IO, IO, unicode, unicode) -> None
+ # type: (str, path, bool, Dict, IO, IO, List[str], str) -> None
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:
+ outdir.makedirs(exist_ok=True)
doctreedir = builddir.joinpath('doctrees')
- if not doctreedir.isdir():
- doctreedir.makedirs()
+ doctreedir.makedirs(exist_ok=True)
if confoverrides is None:
confoverrides = {}
-# if warningiserror is None:
warningiserror = False
self._saved_path = sys.path[:]
- self._saved_directives = directives._directives.copy()
- self._saved_roles = roles._roles.copy()
+ self._saved_directives = directives._directives.copy() # type: ignore
+ self._saved_roles = roles._roles.copy() # type: ignore
self._saved_nodeclasses = set(v for v in dir(nodes.GenericNodeVisitor)
if v.startswith('visit_'))
try:
- application.Sphinx.__init__(self, srcdir, confdir, outdir, doctreedir, # type: ignore # NOQA
- buildername, confoverrides, status, warning,
- freshenv, warningiserror, tags)
+ super().__init__(srcdir, confdir, outdir, doctreedir,
+ buildername, confoverrides, status, warning,
+ freshenv, warningiserror, tags)
except Exception:
self.cleanup()
raise
def cleanup(self, doctrees=False):
# type: (bool) -> None
- AutoDirective._registry.clear()
ModuleAnalyzer.cache.clear()
LaTeXBuilder.usepackages = []
locale.translators.clear()
sys.path[:] = self._saved_path
sys.modules.pop('autodoc_fodder', None)
- directives._directives = self._saved_directives
- roles._roles = self._saved_roles
+ directives._directives = self._saved_directives # type: ignore
+ roles._roles = self._saved_roles # type: ignore
for method in dir(nodes.GenericNodeVisitor):
if method.startswith('visit_') and \
method not in self._saved_nodeclasses:
@@ -165,7 +165,7 @@ class SphinxTestApp(application.Sphinx):
return '<%s buildername=%r>' % (self.__class__.__name__, self.builder.name)
-class SphinxTestAppWrapperForSkipBuilding(object):
+class SphinxTestAppWrapperForSkipBuilding:
"""
This class is a wrapper for SphinxTestApp to speed up the test by skipping
`app.build` process if it is already built and there is even one output
@@ -192,12 +192,14 @@ _unicode_literals_re = re.compile(r'u(".*?")|u(\'.*?\')')
def remove_unicode_literals(s):
- # type: (unicode) -> unicode
+ # type: (str) -> str
+ warnings.warn('remove_unicode_literals() is deprecated.',
+ RemovedInSphinx40Warning)
return _unicode_literals_re.sub(lambda x: x.group(1) or x.group(2), s)
def find_files(root, suffix=None):
- # type: (unicode, bool) -> Generator
+ # type: (str, bool) -> Generator
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)]: # type: ignore
@@ -206,5 +208,5 @@ def find_files(root, suffix=None):
def strip_escseq(text):
- # type: (unicode) -> unicode
+ # type: (str) -> str
return re.sub('\x1b.*?m', '', text)