forked from douban/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sphinx_docs.py
More file actions
583 lines (516 loc) · 18.7 KB
/
test_sphinx_docs.py
File metadata and controls
583 lines (516 loc) · 18.7 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import os
from vilya.libs.permdir import get_tmpdir
from vilya.models.project import CodeDoubanProject
from vilya.models.sphinx_docs import (
SphinxDocs, guess_builder_from_path)
import nose
from tests.base import TestCase
base_yaml_conf = """
docs:
pickle:
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.get_by_name('test')
prj.delete()
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)
def setUp(self):
self._nb_dirs_in_tempdir = len(os.listdir(get_tmpdir()))
TestCase.setUp(self)
def tearDown(self):
new_nb_dirs_in_tmpdir = len(os.listdir(get_tmpdir()))
assert self._nb_dirs_in_tempdir == new_nb_dirs_in_tmpdir, (
"Builder process should not leave dirs in tmpdir")
TestCase.tearDown(self)
class TestDocsWithoutConf(TestDocsHelpers):
def test_create_enabled(self):
prj = self._prj()
sd = SphinxDocs(prj.name)
assert sd.enabled is True
def test_create_with_index_and_doc(self):
prj = self._prj()
self._add(prj, 'docs/index.rst', base_index_rst)
self._add(prj, 'docs/doc1.rst', base_document1_rst)
sd = SphinxDocs(prj.name)
sd.build_all()
builder = sd.get_builder()
doc = builder.template_data('', {})
assert doc['title'] == 'Unit testing sphinx docs'
def test_build_info(self):
prj = self._prj()
self._add(prj, 'docs/index.rst', base_index_rst)
self._add(prj, 'docs/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, 'docs/index.rst', base_index_rst)
sd = SphinxDocs(prj.name)
assert sd.need_rebuild()
sd.build_all()
assert not sd.need_rebuild()
self._add(prj, 'docs/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, 'docs/index.rst', base_index_rst)
self._add(prj, 'docs/doc1.rst', base_document1_rst)
sd = SphinxDocs(prj.name)
sd.build_all()
sd2 = SphinxDocs(prj.name)
builder = sd2.get_builder()
assert builder.template
doc = builder.template_data('', {})
assert doc['title'] == 'Unit testing sphinx docs'
def test_create_with_index_and_doc_and_conf_py(self):
conf_content = """rst_epilog = 'Ahhhhhhhhhhhhhhhh la fin' """
prj = self._prj()
self._add(prj, 'docs/index.rst', base_index_rst)
self._add(prj, 'docs/doc1.rst', base_document1_rst)
self._add(prj, 'docs/conf.py', conf_content)
sd = SphinxDocs(prj.name)
sd.build_all()
builder = sd.get_builder()
doc = builder.template_data('', {})
assert 'Ahhhhhhhhhhhhhhhh' in doc['body']
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()
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()
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()
assert builder.template
doc = builder.template_data('', {})
assert doc['title'] == 'Unit testing sphinx docs'
def test_create_with_index_and_doc_and_conf_py(self):
conf_content = """rst_epilog = 'Ahhhhhhhhhhhhhhhh la fin' """
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)
self._add(prj, 'conf.py', conf_content)
sd = SphinxDocs(prj.name)
sd.build_all()
builder = sd.get_builder()
doc = builder.template_data('', {})
assert 'Ahhhhhhhhhhhhhhhh' in doc['body']
def test_create_with_index_and_doc_and_two_builders(self):
prj = self._prj()
base_yaml_conf_two_builders = """
docs:
html:
dir: ''
builder: html
pickle:
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 == ['html', 'pickle']
pickle_builder = sd.get_builder('pickle')
assert pickle_builder.template
doc = pickle_builder.template_data('', {})
assert doc['title'] == 'Unit testing sphinx docs'
html_builder = sd.get_builder('html')
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:
raw:
dir: 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 == [self.builder]
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:
html:
dir: docs
builder: html
raw:
dir: docs
builder: raw
"""
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 == ['html', 'raw']
raw_builder = sd.get_builder('raw')
doc = raw_builder.raw_content('index.html', {})
assert doc == self.html1
html_builder = sd.get_builder('html')
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:
html:
dir: html_docs
builder: html
raw:
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 == ['html', 'raw']
raw_builder = sd.get_builder('raw')
doc = raw_builder.raw_content('index.html', {})
assert doc == self.html1
html_builder = sd.get_builder('html')
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 = """
#sphinx_docs:
# dir: "docs"
# builders:
# - html:
# html_theme: default
# html_short_title: testsub
# dir: html_docs
# - raw:
# dir: pages
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 = """
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']
def test_guess_builder_from_path_found(self):
bs = ['build1', 'build2']
pr = 'testprj'
path = '/testprj/docs/build1/doc1'
builder, found = guess_builder_from_path(bs, pr, path)
assert builder == 'build1'
assert found is True
def test_guess_builder_from_path_found_second_one(self):
bs = ['build1', 'build2']
pr = 'testprj'
path = '/testprj/docs/build2/doc1'
builder, found = guess_builder_from_path(bs, pr, path)
assert builder == 'build2'
assert found is True
def test_guess_builder_from_path_not_found(self):
bs = ['build1', 'build2']
pr = 'testprj'
path = '/testprj/docs/build__NOTFOUND/doc1'
builder, found = guess_builder_from_path(bs, pr, path)
assert builder == 'build1'
assert found is False
class TestDocsPagesFromPath(TestDocsHelpers):
def test_get_url_from_path_sphinx_doc_builder_default(self):
prj = self._prj()
base_yaml_conf_two_builders = """
docs:
mypages:
builder: raw
dir: mypagesdir
name: Pages
sort: 1
mariodocs:
builder: html
dir: htmldocs/1
name: HTMLDocs
sort: 2
"""
self._add(prj, 'code_config.yaml', base_yaml_conf_two_builders)
sd = SphinxDocs(prj.name)
path = 'htmldocs/1/index.rst'
url = sd.get_url_from_path(path)
assert url == 'docs/mariodocs/index.html'
def test_get_url_from_path_sphinx_doc_builder_pickle_no_subdir(self):
prj = self._prj()
base_yaml_conf_two_builders = """
docs:
mydocs:
dir: mydocsdir/6
pages:
builder: raw
"""
self._add(prj, 'code_config.yaml', base_yaml_conf_two_builders)
sd = SphinxDocs(prj.name)
path = 'mydocsdir/6/index.rst'
url = sd.get_url_from_path(path)
assert url == 'docs/mydocs/index/'
def test_get_url_from_path_sphinx_doc_builder_pickle_subdir(self):
prj = self._prj()
base_yaml_conf_two_builders = """
docs:
mydocs:
dir: mydocsdir
pages:
builder: raw
"""
self._add(prj, 'code_config.yaml', base_yaml_conf_two_builders)
sd = SphinxDocs(prj.name)
path = 'mydocsdir/subdir/doc1.rst'
url = sd.get_url_from_path(path)
assert url == 'docs/mydocs/subdir/doc1/'
def test_get_url_from_path_sphinx_doc_raw_builder_no_subdir(self):
prj = self._prj()
base_yaml_conf_two_builders = """
docs:
mydocs:
dir: mydocsdir/3
builder: raw
"""
self._add(prj, 'code_config.yaml', base_yaml_conf_two_builders)
sd = SphinxDocs(prj.name)
path = 'mydocsdir/3/foo.bar'
url = sd.get_url_from_path(path)
assert url == 'docs/mydocs/foo.bar'
def test_get_url_from_path_no_dir(self):
prj = self._prj()
base_yaml_conf_two_builders = """
docs:
mydocs:
dir: mydocsdir
mypages:
builder: raw
"""
self._add(prj, 'code_config.yaml', base_yaml_conf_two_builders)
sd = SphinxDocs(prj.name)
path = 'mypages/subdir/doc1.html'
url = sd.get_url_from_path(path)
assert url == 'docs/mypages/subdir/doc1.html'
def test_get_url_from_path_doc_builder_raw(self):
prj = self._prj()
base_yaml_conf_two_builders = """
docs:
mydocs:
dir: mydocsdir
mypages:
builder: raw
dir: pages/1
"""
self._add(prj, 'code_config.yaml', base_yaml_conf_two_builders)
sd = SphinxDocs(prj.name)
path = 'pages/1/subdir/doc1.html'
url = sd.get_url_from_path(path)
assert url == 'docs/mypages/subdir/doc1.html'
def test_get_url_from_path_old_conf(self):
prj = self._prj()
sd = SphinxDocs(prj.name)
path = 'docs/index.rst'
url = sd.get_url_from_path(path)
assert url == 'docs/rstdocs/index/'
def test_get_url_from_path_complex_src_dir(self):
prj = self._prj()
base_yaml_conf_two_builders = """
docs:
mypages:
builder: raw
dir: blog/output
name: Blog
sort: 1
"""
self._add(prj, 'code_config.yaml', base_yaml_conf_two_builders)
sd = SphinxDocs(prj.name)
path = 'blog/output/doc1.html'
url = sd.get_url_from_path(path)
assert url == 'docs/mypages/doc1.html'
def test_get_url_from_path_percent_sign_path(self):
prj = self._prj()
sd = SphinxDocs(prj.name)
path = 'docs/doubanlib%2Fsqlstore.rst'
url = sd.get_url_from_path(path)
assert url == 'docs/rstdocs/doubanlib%252Fsqlstore/'
path = 'docs/%E6%80%A7%E8%83%BD%E6%B5%8B%E8%AF%95.rst'
url = sd.get_url_from_path(path)
assert url == 'docs/rstdocs/%25E6%2580%25A7%25E8%2583%25BD%25E6%25B5%258B%25E8%25AF%2595/' # noqa
class TestAutodoc(TestDocsHelpers):
conf_py = """
import sys, os
sys.path.insert(0, os.path.abspath('..'))
extensions = ['sphinx.ext.autodoc']
"""
index_rst = """
Test
====
.. automodule:: testapi
:members:
"""
api_py = """
class C(object):
'''Test C docstring'''
"""
code_config_yaml = """
docs:
mydocs:
checkout_root: True
link: http://code.dapps.douban.com//
builder: pickle
dir: docs
"""
def test_use_autodoc_not_configured(self):
prj = self._prj()
self._add(prj, 'docs/conf.py', self.conf_py)
self._add(prj, 'docs/index.rst', self.index_rst)
self._add(prj, 'testapi.py', self.api_py)
sd = SphinxDocs(prj.name)
sd.build_all()
builder = sd.get_builder()
doc = builder.template_data('', {})
assert "Test C docstring" not in doc['body']
def test_use_autodoc_configured(self):
prj = self._prj()
self._add(prj, 'code_config.yaml', self.code_config_yaml)
self._add(prj, 'docs/conf.py', self.conf_py)
self._add(prj, 'docs/index.rst', self.index_rst)
self._add(prj, 'testapi.py', self.api_py)
sd = SphinxDocs(prj.name)
sd.build_all()
builder = sd.get_builder()
doc = builder.template_data('', {})
assert "Test C docstring" in doc['body']