File: test_sphinx.py

package info (click to toggle)
recommonmark 0.7.1%2Bds-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 624 kB
  • sloc: python: 1,033; makefile: 237; sh: 20
file content (229 lines) | stat: -rw-r--r-- 6,674 bytes parent folder | download
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import os
import io
import sys
import shutil
import unittest
from contextlib import contextmanager

from sphinx.application import Sphinx


@contextmanager
def sphinx_built_file(test_dir, test_file):
    os.chdir('tests/{0}'.format(test_dir))
    try:
        app = Sphinx(
            srcdir='.',
            confdir='.',
            outdir='_build/text',
            doctreedir='_build/.doctrees',
            buildername='html',
            verbosity=1,
        )
        app.build(force_all=True)
        with io.open(test_file, encoding='utf-8') as fin:
            yield fin.read().strip()
    finally:
        shutil.rmtree('_build')
        os.chdir('../..')


def run_test(test_dir, test_file, test_string):
    with sphinx_built_file(test_dir, test_file) as output:
        assert test_string in output


class SphinxIntegrationTests(unittest.TestCase):

    build_path = None

    def setUp(self):
        if self.build_path is not None:
            self.app = Sphinx(
                srcdir=self.build_path,
                confdir=self.build_path,
                outdir=os.path.join(self.build_path, '_build', 'text'),
                doctreedir=os.path.join(self.build_path, '_build', '.doctrees'),
                buildername='html',
                verbosity=1,
            )
            self.app.build(force_all=True)

    def tearDown(self):
        if self.build_path is not None:
            shutil.rmtree(os.path.join(self.build_path, '_build'))

    def read_file(self, path):
        full_path = os.path.join(self.build_path, '_build', 'text', path)
        with io.open(full_path, encoding='utf-8') as h:
            return h.read().strip()


class GenericTests(SphinxIntegrationTests):

    build_path = 'tests/sphinx_generic'

    def test_headings(self):
        output = self.read_file('index.html')
        self.assertIn(
            ('<h1>Heading 1'
             '<a class="headerlink" '
             'href="#heading-1" '
             'title="Link to this heading">¶</a>'
             '</h1>'),
            output,
        )
        self.assertIn(
            ('<h2>Heading 2'
             '<a class="headerlink" '
             'href="#heading-2" '
             'title="Link to this heading">¶</a>'
             '</h2>'),
            output,
        )
        self.assertIn(
            ('<h3>Heading 3'
             '<a class="headerlink" '
             'href="#heading-3" '
             'title="Link to this heading">¶</a>'
             '</h3>'),
            output,
        )
        self.assertIn(
            ('<h4>Heading 4'
             '<a class="headerlink" '
             'href="#heading-4" '
             'title="Link to this heading">¶</a>'
             '</h4>'),
            output,
        )

    def test_links(self):
        output = self.read_file('index.html')
        self.assertIn(
            ('This is a '
             '<a class="reference external" '
             'href="http://example.com">link</a>'),
            output
        )
        self.assertIn(
            ('This is a '
             '<a class="reference external" '
             'href="http://example.com/foobar">ref link</a>'),
            output
        )
        self.assertIn(
            ('This is a '
             '<a class="reference external" '
             'href="/example">relative link</a>'),
            output
        )
        self.assertIn(
            ('This is a '
             '<a class="reference internal" href="#">'
             '<span class="doc">pending ref</span>'
             '</a>'),
            output
        )
        self.assertIn(
            ('External link to Markdown file: '
             '<a class="reference external" '
             'href="https://github.com/readthedocs/recommonmark/blob/master/README.md">Readme</a>.'),
            output
        )

    def test_image(self):
        output = self.read_file('index.html')
        self.assertIn(
            '<p><img alt="foo &quot;handle quotes&quot;" src="image.png" /></p>',
            output
        )

    def test_paragraph(self):
        output = self.read_file('index.html')
        self.assertIn('<p>Foo</p>', output)
        self.assertIn('<p>Bar</p>', output)

    def test_lists(self):
        output = self.read_file('index.html')
        self.assertIn(
            ('<ul class="simple">\n'
             '<li><p>Item A</p></li>\n'
             '<li><p>Item B</p></li>\n'
             '<li><p>Item C</p></li>\n'
             '</ul>'),
            output
        )
        self.assertIn(
            ('<ol class="simple">\n'
             '<li><p>Item 1</p></li>\n'
             '<li><p>Item 2</p></li>\n'
             '<li><p>Item 3</p></li>\n'
             '</ol>'),
            output
        )

    def test_code(self):
        output = self.read_file('index.html')
        self.assertIn(
            ('<pre><span></span>'
             '<span class="ch">#!/bin/sh</span>\n'
             '<span class="n">python</span>\n'
             '</pre>'),
            output
        )

    def test_thematic_break(self):
        output = self.read_file('index.html')
        self.assertIn(
            '<p>Foo</p>\n<hr class="docutils" />\n<p>Bar</p>',
            output
        )


class CodeBlockTests(unittest.TestCase):

    def test_integration(self):
        with sphinx_built_file('sphinx_code_block', '_build/text/index.html') as output:
            self.assertIn('<div class="highlight">', output)


class IndentedCodeTests(unittest.TestCase):

    def test_integration(self):
        run_test(
            'sphinx_indented_code',
            '_build/text/index.html',
            '<div class="highlight">'
        )

class NestedHeaderBlock(unittest.TestCase):

    def test_integration(self):
        run_test(
            'sphinx_nested_header_block',
            '_build/text/index.html',
            '<h1>'
        )

class CustomExtensionTests(SphinxIntegrationTests):

    build_path = 'tests/sphinx_custom_md'

    def test_integration(self):
        output = self.read_file('index.html')
        self.assertIn('<table ', output)
        self.assertIn('<th class="head"><p>abc</p></th>', output)
        self.assertIn('<th class="head"><p>data</p></th>', output)
        self.assertIn('</table>', output)


        self.assertIn(
            ('<nav class="contents" id="contents">\n'
             '<p class="topic-title">Contents</p>\n'
             '<ul class="simple">\n'
             '<li><p><a class="reference internal" href="#header" id="id1">Header</a></p>\n<ul>\n'
             '<li><p><a class="reference internal" href="#header-2" id="id2">Header 2</a></p></li>\n'
             '</ul>\n</li>\n</ul>'),
            output
            )