|
14 | 14 |
|
15 | 15 | import datetime
|
16 | 16 | import os
|
| 17 | +import pkgutil |
| 18 | +import tempfile |
17 | 19 | import unittest
|
18 | 20 |
|
19 | 21 | import httplib2
|
|
31 | 33 | from system_test_utils import unique_resource_id
|
32 | 34 |
|
33 | 35 |
|
| 36 | +SPHINX_CONF = """\ |
| 37 | +extensions = [ |
| 38 | + 'sphinx.ext.autodoc', |
| 39 | + 'sphinx.ext.doctest', |
| 40 | +] |
| 41 | +""" |
| 42 | + |
| 43 | +SPHINX_SECTION_TEMPLATE = """\ |
| 44 | +Section %02d |
| 45 | +=========== |
| 46 | +
|
| 47 | +.. automodule:: google.cloud.%s |
| 48 | + :members: |
| 49 | +
|
| 50 | +""" |
| 51 | + |
| 52 | + |
34 | 53 | class Config(object):
|
35 | 54 | """Run-time configuration to be modified at set-up.
|
36 | 55 |
|
@@ -495,3 +514,59 @@ def test_failure_with_contention(self):
|
495 | 514 | # transaction.
|
496 | 515 | entity_in_txn[contention_prop_name] = u'inside'
|
497 | 516 | txn.put(entity_in_txn)
|
| 517 | + |
| 518 | + |
| 519 | +class TestDoctest(unittest.TestCase): |
| 520 | + |
| 521 | + def _submodules(self): |
| 522 | + pkg_iter = pkgutil.iter_modules(datastore.__path__) |
| 523 | + result = [] |
| 524 | + for _, mod_name, ispkg in pkg_iter: |
| 525 | + if mod_name == '_generated': |
| 526 | + self.assertTrue(ispkg) |
| 527 | + else: |
| 528 | + self.assertFalse(ispkg) |
| 529 | + result.append(mod_name) |
| 530 | + |
| 531 | + self.assertNotIn('__init__', result) |
| 532 | + return result |
| 533 | + |
| 534 | + @staticmethod |
| 535 | + def _add_section(index, mod_name, file_obj): |
| 536 | + mod_part = 'datastore' |
| 537 | + if mod_name != '__init__': |
| 538 | + mod_part += '.' + mod_name |
| 539 | + content = SPHINX_SECTION_TEMPLATE % (index, mod_part) |
| 540 | + file_obj.write(content) |
| 541 | + |
| 542 | + def _make_temp_docs(self): |
| 543 | + docs_dir = tempfile.mkdtemp(prefix='datastore-') |
| 544 | + |
| 545 | + conf_file = os.path.join(docs_dir, 'conf.py') |
| 546 | + |
| 547 | + with open(conf_file, 'w') as file_obj: |
| 548 | + file_obj.write(SPHINX_CONF) |
| 549 | + |
| 550 | + index_file = os.path.join(docs_dir, 'contents.rst') |
| 551 | + datastore_modules = self._submodules() |
| 552 | + with open(index_file, 'w') as file_obj: |
| 553 | + self._add_section(0, '__init__', file_obj) |
| 554 | + for index, datastore_module in enumerate(datastore_modules): |
| 555 | + self._add_section(index + 1, datastore_module, file_obj) |
| 556 | + |
| 557 | + return docs_dir |
| 558 | + |
| 559 | + def test_it(self): |
| 560 | + from sphinx import application |
| 561 | + |
| 562 | + docs_dir = self._make_temp_docs() |
| 563 | + outdir = os.path.join(docs_dir, 'doctest', 'out') |
| 564 | + doctreedir = os.path.join(docs_dir, 'doctest', 'doctrees') |
| 565 | + |
| 566 | + app = application.Sphinx( |
| 567 | + srcdir=docs_dir, confdir=docs_dir, |
| 568 | + outdir=outdir, doctreedir=doctreedir, |
| 569 | + buildername='doctest', warningiserror=True, parallel=1) |
| 570 | + |
| 571 | + app.build() |
| 572 | + self.assertEqual(app.statuscode, 0) |
0 commit comments