summaryrefslogtreecommitdiffstats
path: root/tests/util.py
blob: c9fdcc11e8534b10d7f96a35e8f6d0d08f458579 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from __future__ import print_function

import os
import tempfile
import shutil
from contextlib import contextmanager

import pytest
from sphinx.application import Sphinx

try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO


@contextmanager
def build(root, builder='html', **kwargs):
    tmpdir = tempfile.mkdtemp()

    srcdir = os.path.join(os.path.dirname(__file__), 'roots', root)
    destdir = os.path.join(tmpdir, builder)
    doctreedir = os.path.join(tmpdir, 'doctree/')

    status = StringIO()
    warning = StringIO()

    kwargs.update({
        'status': status,
        'warning': warning,
    })

    confoverrides = kwargs.pop('confoverrides', {})
    confoverrides['html_theme'] = 'sphinx_rtd_theme'
    extensions = confoverrides.get('extensions', [])
    extensions.append('sphinx_rtd_theme')
    extensions.append('readthedocs_ext.readthedocs')
    confoverrides['extensions'] = extensions
    kwargs['confoverrides'] = confoverrides

    try:
        app = Sphinx(srcdir, srcdir, destdir, doctreedir, builder, **kwargs)
        app.builder.build_all()
        yield (app, status.getvalue(), warning.getvalue())
    except Exception as e:
        print('# root:', root)
        print('# builder:', builder)
        print('# source:', srcdir)
        print('# destination:', destdir)
        print('# status:', '\n' + status.getvalue())
        print('# warning:', '\n' + warning.getvalue())
        raise
    finally:
        shutil.rmtree(tmpdir)


def build_all(root, **kwargs):
    for builder in ['html', 'singlehtml', 'readthedocs', 'readthedocsdirhtml',
                    'readthedocssinglehtml', 'readthedocssinglehtmllocalmedia']:
        with build(root, builder, **kwargs) as ret:
            yield ret