test_docs.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright 2009-2018 Canonical Ltd. All rights reserved.
  2. #
  3. # This file is part of wadllib
  4. #
  5. # wadllib 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. # wadllib 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 wadllib. If not, see <http://www.gnu.org/licenses/>.
  16. """Test harness."""
  17. from __future__ import absolute_import, print_function
  18. __metaclass__ = type
  19. __all__ = [
  20. 'load_tests',
  21. ]
  22. import __future__
  23. import atexit
  24. import doctest
  25. import os
  26. import pkg_resources
  27. DOCTEST_FLAGS = (
  28. doctest.ELLIPSIS |
  29. doctest.NORMALIZE_WHITESPACE |
  30. doctest.REPORT_NDIFF)
  31. def load_tests(loader, tests, pattern):
  32. doctest_files = []
  33. if pkg_resources.resource_exists('wadllib', 'docs'):
  34. for name in pkg_resources.resource_listdir('wadllib', 'docs'):
  35. if name.endswith('.rst'):
  36. doctest_files.append(
  37. os.path.abspath(
  38. pkg_resources.resource_filename(
  39. 'wadllib', 'docs/%s' % name)))
  40. globs = {
  41. future_item: getattr(__future__, future_item)
  42. for future_item in ('absolute_import', 'print_function')}
  43. kwargs = dict(
  44. module_relative=False, globs=globs, optionflags=DOCTEST_FLAGS)
  45. atexit.register(pkg_resources.cleanup_resources)
  46. tests.addTest(doctest.DocFileSuite(*doctest_files, **kwargs))
  47. return tests