forked from douban/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_docs.py
More file actions
286 lines (252 loc) · 8.85 KB
/
test_docs.py
File metadata and controls
286 lines (252 loc) · 8.85 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
from vilya.models.project import CodeDoubanProject
from vilya.models.sphinx_docs import SphinxDocs
import nose
from tests.base import TestCase
base_yaml_conf_old = """
sphinx_docs:
dir: ""
"""
base_yaml_conf = """
docs:
docs:
dir: ""
builder: pickle
"""
base_index_rst = """
Unit testing sphinx docs
========================
.. toctree::
:glob:
*
"""
base_document1_rst = """
Test doc1
=========
Something here
"""
base_document2_rst = """
Test doc2
=========
Something here
"""
class TestDocsHelpers(TestCase):
html1 = '<h1>TITLE1**</h1>'
def _prj(self):
prj = CodeDoubanProject.add('test', 'owner', create_trac=False)
return prj
def _add(self, prj, fn, content):
u = self.addUser()
prj.git.commit_one_file(fn, content, 'add %s' % fn, u)
class TestDocs(TestDocsHelpers):
@nose.tools.raises(Exception)
def test_create_wrong(self):
sd = SphinxDocs('unexisting_project')
assert sd.enabled is False
def test_create_disabled(self):
prj = self._prj()
conf = """
sphinx_docs: ""
docs:
docs:
builder: pickle
"""
self._add(prj, 'code_config.yaml', conf)
sd = SphinxDocs(prj.name)
assert sd.enabled is True, "should be enabled by default"
def test_create_enabled(self):
prj = self._prj()
self._add(prj, 'code_config.yaml', base_yaml_conf)
sd = SphinxDocs(prj.name)
assert sd.enabled is True
def test_create_with_index_and_doc(self):
prj = self._prj()
self._add(prj, 'code_config.yaml', base_yaml_conf)
self._add(prj, 'index.rst', base_index_rst)
self._add(prj, 'doc1.rst', base_document1_rst)
sd = SphinxDocs(prj.name)
sd.build_all()
builder = sd.get_builder('docs')
doc = builder.template_data('', {})
assert doc['title'] == 'Unit testing sphinx docs'
def test_build_info(self):
prj = self._prj()
self._add(prj, 'code_config.yaml', base_yaml_conf)
self._add(prj, 'index.rst', base_index_rst)
self._add(prj, 'doc1.rst', base_document1_rst)
sd = SphinxDocs(prj.name)
sd.build_all()
bi = sd.last_build_info()
assert bi['status'] == 'success'
def test_need_rebuild(self):
prj = self._prj()
self._add(prj, 'code_config.yaml', base_yaml_conf)
self._add(prj, 'index.rst', base_index_rst)
sd = SphinxDocs(prj.name)
assert sd.need_rebuild()
sd.build_all()
assert not sd.need_rebuild()
self._add(prj, 'doc1.rst', base_document1_rst)
sd = SphinxDocs(prj.name) # Bad, should not have to refresh object
assert sd.need_rebuild()
sd.build_all()
assert not sd.need_rebuild()
def test_create_with_index_and_doc_and_get_again(self):
prj = self._prj()
self._add(prj, 'code_config.yaml', base_yaml_conf)
self._add(prj, 'index.rst', base_index_rst)
self._add(prj, 'doc1.rst', base_document1_rst)
sd = SphinxDocs(prj.name)
sd.build_all()
sd2 = SphinxDocs(prj.name)
builder = sd2.get_builder('docs')
assert builder.template
doc = builder.template_data('', {})
assert doc['title'] == 'Unit testing sphinx docs'
def test_create_with_index_and_doc_and_two_builders(self):
prj = self._prj()
base_yaml_conf_two_builders = """
docs:
docs:
builder: html
dir: ""
html_theme: default
html_short_title: testsub
docs2:
dir: ""
builder: pickle
"""
self._add(prj, 'code_config.yaml', base_yaml_conf_two_builders)
self._add(prj, 'index.rst', base_index_rst)
self._add(prj, 'doc1.rst', base_document1_rst)
sd = SphinxDocs(prj.name)
sd.build_all()
assert sd.builders == ['docs', 'docs2']
pickle_builder = sd.get_builder('docs2')
assert pickle_builder.template
doc = pickle_builder.template_data('', {})
assert doc['title'] == 'Unit testing sphinx docs'
html_builder = sd.get_builder('docs')
assert not html_builder.template
raw = html_builder.raw_content('index.html', {})
assert "<h1>Unit testing sphinx docs" in raw
class TestDocsPages(TestDocsHelpers):
conf = 'docs: {"pages": {"builder": "raw"}}'
builder = 'raw'
def test_pages_mode(self):
prj = self._prj()
self._add(prj, 'code_config.yaml', self.conf)
self._add(prj, 'pages/index.html', self.html1)
sd = SphinxDocs(prj.name)
assert sd.builders == ['pages']
assert sd.last_build_info() is None
sd.build_all()
assert sd.last_build_info()['status'] == 'success'
builder = sd.get_builder(sd.builders[0])
assert builder.raw_content('index.html', {}) == self.html1
def test_pages_no_docsdir(self):
prj = self._prj()
self._add(prj, 'code_config.yaml', self.conf)
self._add(prj, 'pagesNOT_THE_SAME/index.html', self.html1)
sd = SphinxDocs(prj.name)
sd.build_all()
assert sd.last_build_info()['status'] == 'no_doc_dir_found'
builder = sd.get_builder(sd.builders[0])
assert builder.raw_content('index.html', {}) is False
def test_html_and_raw_builders(self):
prj = self._prj()
base_yaml_conf_two_builders = """
docs:
docs:
builder: html
html_short_title: testsub
dir: docs
html_theme: default
pages:
builder: raw
dir: docs
"""
self._add(prj, 'code_config.yaml', base_yaml_conf_two_builders)
self._add(prj, 'docs/index.rst', base_index_rst)
self._add(prj, 'docs/index.html', self.html1)
self._add(prj, 'docs/doc1.rst', base_document1_rst)
sd = SphinxDocs(prj.name)
sd.build_all()
assert sd.builders == ['docs', 'pages']
raw_builder = sd.get_builder('pages')
doc = raw_builder.raw_content('index.html', {})
assert doc == self.html1
html_builder = sd.get_builder('docs')
assert not html_builder.template
raw = html_builder.raw_content('index.html', {})
assert "<h1>Unit testing sphinx docs" in raw
def test_html_and_raw_builders_in_different_dirs(self):
prj = self._prj()
base_yaml_conf_two_builders = """
docs:
docs:
builder: html
html_short_title: testsub
dir: html_docs
html_theme: default
pages:
builder: raw
dir: pages
"""
self._add(prj, 'code_config.yaml', base_yaml_conf_two_builders)
self._add(prj, 'html_docs/index.rst', base_index_rst)
self._add(prj, 'html_docs/doc1.rst', base_document1_rst)
self._add(prj, 'pages/index.html', self.html1)
sd = SphinxDocs(prj.name)
sd.build_all()
assert sd.builders == ['docs', 'pages']
raw_builder = sd.get_builder('pages')
doc = raw_builder.raw_content('index.html', {})
assert doc == self.html1
html_builder = sd.get_builder('docs')
assert not html_builder.template
raw = html_builder.raw_content('index.html', {})
assert "<h1>Unit testing sphinx docs" in raw
class TestDocsPagesNewConf(TestDocsHelpers):
def test_two_builders_with_other_config_fmt(self):
prj = self._prj()
base_yaml_conf_two_builders = """
docs:
docs:
builder: html
html_theme: default
html_short_title: testsub
dir: html_docs
pages:
builder: raw
"""
self._add(prj, 'code_config.yaml', base_yaml_conf_two_builders)
self._add(prj, 'html_docs/index.rst', base_index_rst)
self._add(prj, 'html_docs/doc1.rst', base_document1_rst)
self._add(prj, 'pages/index.html', self.html1)
sd = SphinxDocs(prj.name)
sd.build_all()
assert sd.builders == ['docs', 'pages'] # noqa Sorted alphabetically by default
raw_builder = sd.get_builder('pages')
doc = raw_builder.raw_content('index.html', {})
assert doc == self.html1
html_builder = sd.get_builder('docs')
assert not html_builder.template
raw = html_builder.raw_content('index.html', {})
assert "<h1>Unit testing sphinx docs" in raw
def test_sort_key(self):
prj = self._prj()
base_yaml_conf_two_builders = """
sphinx_docs:
docs:
docs:
builder: html
html_theme: default
html_short_title: testsub
sort: 2
pages:
builder: raw
sort: 1
"""
self._add(prj, 'code_config.yaml', base_yaml_conf_two_builders)
sd = SphinxDocs(prj.name)
assert sd.builders == ['pages', 'docs']