sphinx/tests/util.py
Georg Brandl ae2570c269 Merged revisions 64808,65013,65076,65100-65101,65119,65121-65123 via svnmerge from
svn+ssh://pythondev@svn.python.org/doctools/branches/0.4.x

........
  r64808 | georg.brandl | 2008-07-08 21:39:33 +0200 (Tue, 08 Jul 2008) | 2 lines

  Allow relocation of source and doctree dir.
........
  r65013 | georg.brandl | 2008-07-16 15:25:30 +0200 (Wed, 16 Jul 2008) | 2 lines

  Remove curious quote.
........
  r65076 | georg.brandl | 2008-07-17 22:43:01 +0200 (Thu, 17 Jul 2008) | 2 lines

  Add a test for sphinx.quickstart.
........
  r65100 | georg.brandl | 2008-07-18 14:41:54 +0200 (Fri, 18 Jul 2008) | 2 lines

  Fix phony targets.
........
  r65101 | georg.brandl | 2008-07-18 14:55:03 +0200 (Fri, 18 Jul 2008) | 2 lines

  Fix problems in "make check".
........
  r65119 | georg.brandl | 2008-07-18 23:06:42 +0200 (Fri, 18 Jul 2008) | 2 lines

  Emit a more precise error message in autodoc.
........
  r65121 | georg.brandl | 2008-07-18 23:41:35 +0200 (Fri, 18 Jul 2008) | 2 lines

  Warn if a toctree-included document doesn't contain a title.
........
  r65122 | georg.brandl | 2008-07-18 23:51:28 +0200 (Fri, 18 Jul 2008) | 2 lines

  Don't use \samp{} for code with whitespaces, only for :samp:`code`.
........
  r65123 | georg.brandl | 2008-07-19 00:49:46 +0200 (Sat, 19 Jul 2008) | 2 lines

  Put inheritance info always on its own line.
........
2008-07-18 22:55:36 +00:00

118 lines
3.0 KiB
Python

# -*- coding: utf-8 -*-
"""
Sphinx test suite utilities
~~~~~~~~~~~~~~~~~~~~~~~~~~~
:copyright: 2008 by Georg Brandl.
:license: BSD.
"""
import sys
import StringIO
import tempfile
from sphinx import application, builder
from path import path
__all__ = [
'raises', 'raises_msg',
'ErrorOutput', 'TestApp',
'path', 'with_tempdir', 'write_file',
]
def _excstr(exc):
if type(exc) is tuple:
return str(tuple(map(_excstr, exc)))
return exc.__name__
def raises(exc, func, *args, **kwds):
"""
Raise :exc:`AssertionError` if ``func(*args, **kwds)`` does not
raise *exc*.
"""
try:
func(*args, **kwds)
except exc:
pass
else:
raise AssertionError('%s did not raise %s' %
(func.__name__, _excstr(exc)))
def raises_msg(exc, msg, func, *args, **kwds):
"""
Raise :exc:`AssertionError` if ``func(*args, **kwds)`` does not
raise *exc*, and check if the message contains *msg*.
"""
try:
func(*args, **kwds)
except exc, err:
assert msg in str(err)
else:
raise AssertionError('%s did not raise %s' %
(func.__name__, _excstr(exc)))
class ErrorOutput(object):
"""
File-like object that raises :exc:`AssertionError` on ``write()``.
"""
def __init__(self, name):
self.name = name
def write(self, text):
assert False, 'tried to write %r to %s' % (text, self.name)
class TestApp(application.Sphinx):
"""
A subclass of :class:`Sphinx` that runs on the test root, with some
better default values for the initialization parameters.
"""
def __init__(self, srcdir=None, confdir=None, outdir=None, doctreedir=None,
buildername='html', confoverrides=None, status=None, warning=None,
freshenv=None, confname='conf.py'):
application.CONFIG_FILENAME = confname
if srcdir is None:
srcdir = path(__file__).parent.joinpath('root').abspath()
else:
srcdir = path(srcdir)
if confdir is None:
confdir = srcdir
if outdir is None:
outdir = srcdir.joinpath('_build', buildername)
if doctreedir is None:
doctreedir = srcdir.joinpath(srcdir, '_build', 'doctrees')
if confoverrides is None:
confoverrides = {}
if status is None:
status = StringIO.StringIO()
if warning is None:
warning = ErrorOutput('stderr')
if freshenv is None:
freshenv = True
application.Sphinx.__init__(self, srcdir, confdir, outdir, doctreedir,
buildername, confoverrides, status, warning,
freshenv)
def with_tempdir(func):
def new_func():
tempdir = path(tempfile.mkdtemp())
func(tempdir)
tempdir.rmtree()
new_func.__name__ = func.__name__
return new_func
def write_file(name, contents):
f = open(str(name), 'wb')
f.write(contents)
f.close()