test_docs.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright 2009 Canonical Ltd. All rights reserved.
  2. #
  3. # This file is part of lazr.uri
  4. #
  5. # lazr.uri 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.uri is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. # 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.uri. If not, see <http://www.gnu.org/licenses/>.
  16. """Test harness for doctests."""
  17. from __future__ import print_function
  18. __metaclass__ = type
  19. __all__ = [
  20. 'load_tests',
  21. ]
  22. import atexit
  23. import doctest
  24. import os
  25. from pkg_resources import (
  26. cleanup_resources,
  27. resource_exists,
  28. resource_filename,
  29. resource_listdir,
  30. )
  31. DOCTEST_FLAGS = (
  32. doctest.ELLIPSIS |
  33. doctest.NORMALIZE_WHITESPACE |
  34. doctest.REPORT_NDIFF)
  35. def find_doctests(suffix):
  36. """Find doctests matching a certain suffix."""
  37. doctest_files = []
  38. # Match doctests against the suffix.
  39. if resource_exists('lazr.uri', 'docs'):
  40. for name in resource_listdir('lazr.uri', 'docs'):
  41. if name.endswith(suffix):
  42. doctest_files.append(
  43. os.path.abspath(
  44. resource_filename('lazr.uri', 'docs/%s' % name)))
  45. return doctest_files
  46. def load_tests(loader, tests, pattern):
  47. """Load all the doctests."""
  48. atexit.register(cleanup_resources)
  49. tests.addTest(doctest.DocFileSuite(
  50. *find_doctests('.rst'),
  51. module_relative=False, optionflags=DOCTEST_FLAGS,
  52. globs={"print_function": print_function}))
  53. return tests