test_docs.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Copyright 2009 Canonical Ltd. All rights reserved.
  2. #
  3. # This file is part of lazr.restfulclient
  4. #
  5. # lazr.restfulclient is free software: you can redistribute it and/or modify it
  6. # under the terms of the GNU Lesser General Public License as published by
  7. # the Free Software Foundation, version 3 of the License.
  8. #
  9. # lazr.restfulclient is distributed in the hope that it will be useful, but
  10. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  12. # License for more details.
  13. #
  14. # You should have received a copy of the GNU Lesser General Public License
  15. # along with lazr.restfulclient. If not, see <http://www.gnu.org/licenses/>.
  16. "Test harness for doctests."
  17. # pylint: disable-msg=E0611,W0142
  18. __metaclass__ = type
  19. __all__ = [
  20. "load_tests",
  21. ]
  22. import atexit
  23. import doctest
  24. import os
  25. import wsgi_intercept
  26. from pkg_resources import (
  27. cleanup_resources,
  28. resource_exists,
  29. resource_filename,
  30. resource_listdir,
  31. )
  32. from wsgi_intercept.httplib2_intercept import install, uninstall
  33. # We avoid importing anything from lazr.restful into the module level,
  34. # so that standalone_tests() can run without any support from
  35. # lazr.restful.
  36. DOCTEST_FLAGS = (
  37. doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE | doctest.REPORT_NDIFF
  38. )
  39. def setUp(test):
  40. from lazr.restful.example.base.tests.test_integration import WSGILayer
  41. install()
  42. wsgi_intercept.add_wsgi_intercept(
  43. "cookbooks.dev", 80, WSGILayer.make_application
  44. )
  45. def tearDown(test):
  46. from zope.component import getUtility
  47. from lazr.restful.example.base.interfaces import IFileManager
  48. uninstall()
  49. file_manager = getUtility(IFileManager)
  50. file_manager.files = {}
  51. file_manager.counter = 0
  52. def find_doctests(suffix, ignore_suffix=None):
  53. """Find doctests matching a certain suffix."""
  54. doctest_files = []
  55. # Match doctests against the suffix.
  56. if resource_exists("lazr.restfulclient", "docs"):
  57. for name in resource_listdir("lazr.restfulclient", "docs"):
  58. if ignore_suffix is not None and name.endswith(ignore_suffix):
  59. continue
  60. if name.endswith(suffix):
  61. doctest_files.append(
  62. os.path.abspath(
  63. resource_filename(
  64. "lazr.restfulclient", "docs/%s" % name
  65. )
  66. )
  67. )
  68. return doctest_files
  69. def load_tests(loader, tests, pattern):
  70. """Load all the doctests."""
  71. from lazr.restful.example.base.tests.test_integration import WSGILayer
  72. atexit.register(cleanup_resources)
  73. restful_suite = doctest.DocFileSuite(
  74. *find_doctests(".rst", ignore_suffix=".standalone.rst"),
  75. module_relative=False,
  76. optionflags=DOCTEST_FLAGS,
  77. setUp=setUp,
  78. tearDown=tearDown
  79. )
  80. restful_suite.layer = WSGILayer
  81. tests.addTest(restful_suite)
  82. tests.addTest(
  83. doctest.DocFileSuite(
  84. *find_doctests(".standalone.rst"),
  85. module_relative=False,
  86. optionflags=DOCTEST_FLAGS
  87. )
  88. )
  89. return tests