summaryrefslogtreecommitdiff
path: root/tests/test_docutilsconf.py
blob: 4aeaa56a8bda52d6d1b2c15cdd87e8fc962ba930 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# -*- coding: utf-8 -*-
"""
    test_docutilsconf
    ~~~~~~~~~~~~~~~~~

    Test docutils.conf support for several writers.

    :copyright: Copyright 2007-2014 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

import os
import re
from StringIO import StringIO
from functools import wraps

from util import test_roots, TestApp, path, SkipTest


html_warnfile = StringIO()
root = test_roots / 'test-docutilsconf'


# need cleanenv to rebuild everytime.
# docutils.conf change did not effect to rebuild.
def with_conf_app(docutilsconf='', *args, **kwargs):
    default_kw = {
        'srcdir': root,
        'cleanenv': True,
    }
    default_kw.update(kwargs)
    def generator(func):
        @wraps(func)
        def deco(*args2, **kwargs2):
            app = TestApp(*args, **default_kw)
            (app.srcdir / 'docutils.conf').write_text(docutilsconf)
            try:
                cwd = os.getcwd()
                os.chdir(app.srcdir)
                func(app, *args2, **kwargs2)
            finally:
                os.chdir(cwd)
            # don't execute cleanup if test failed
            app.cleanup()
        return deco
    return generator


def regex_count(expr, result):
    return len(re.findall(expr, result))


@with_conf_app(buildername='html')
def test_html_with_default_docutilsconf(app):
    app.builder.build(['contents'])
    result = (app.outdir / 'contents.html').text(encoding='utf-8')

    assert regex_count(r'<th class="field-name">', result) == 1
    assert regex_count(r'<th class="field-name" colspan="2">', result) == 1
    assert regex_count(r'<td class="option-group">', result) == 1
    assert regex_count(r'<td class="option-group" colspan="2">', result) == 1


@with_conf_app(buildername='html', docutilsconf=(
    '\n[html4css1 writer]'
    '\noption-limit:1'
    '\nfield-name-limit:1'
    '\n')
)
def test_html_with_docutilsconf(app):
    app.builder.build(['contents'])
    result = (app.outdir / 'contents.html').text(encoding='utf-8')

    assert regex_count(r'<th class="field-name">', result) == 0
    assert regex_count(r'<th class="field-name" colspan="2">', result) == 2
    assert regex_count(r'<td class="option-group">', result) == 0
    assert regex_count(r'<td class="option-group" colspan="2">', result) == 2


@with_conf_app(buildername='html', warning=html_warnfile)
def test_html(app):
    app.builder.build(['contents'])
    assert html_warnfile.getvalue() == ''


@with_conf_app(buildername='latex', warning=html_warnfile)
def test_latex(app):
    app.builder.build(['contents'])
    assert html_warnfile.getvalue() == ''


@with_conf_app(buildername='man', warning=html_warnfile)
def test_man(app):
    app.builder.build(['contents'])
    assert html_warnfile.getvalue() == ''


@with_conf_app(buildername='texinfo', warning=html_warnfile)
def test_texinfo(app):
    app.builder.build(['contents'])


@with_conf_app(buildername='html', srcdir='(empty)',
               docutilsconf='[general]\nsource_link=true\n')
def test_docutils_source_link(app):
    srcdir = path(app.srcdir)
    (srcdir / 'conf.py').write_text('')
    (srcdir / 'contents.rst').write_text('')
    app.builder.build_all()


@with_conf_app(buildername='html', srcdir='(empty)',
               docutilsconf='[general]\nsource_link=true\n')
def test_docutils_source_link_with_nonascii_file(app):
    srcdir = path(app.srcdir)
    mb_name = u'\u65e5\u672c\u8a9e'
    try:
        (srcdir / (mb_name + '.txt')).write_text('')
    except UnicodeEncodeError:
        from path import FILESYSTEMENCODING
        raise SkipTest(
            'nonascii filename not supported on this filesystem encoding: '
            '%s', FILESYSTEMENCODING)

    (srcdir / 'conf.py').write_text('')
    (srcdir / 'contents.rst').write_text('')

    app.builder.build_all()