blob: 3cc06c16fb509d1f923ee21e57300e5a47f64dc7 (
plain)
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
|
# coding: utf-8
"""
Exposes a get_doctests() function for the project's test harness.
"""
import os
import pkgutil
import doctest
import traceback
from pystache.tests.common import PACKAGE_DIR, TEXT_DOCTEST_PATHS
# This module follows the guidance documented here:
#
# http://docs.python.org/library/doctest.html#unittest-api
#
def get_doctests(text_file_dir):
"""
Return a list of TestSuite instances for all doctests in the project.
Arguments:
text_file_dir: the directory in which to search for all text files
(i.e. non-module files) containing doctests.
"""
suites = []
# Since module_relative is False in our calls to DocFileSuite below,
# paths should be OS-specific. See the following for more info--
#
# http://docs.python.org/library/doctest.html#doctest.DocFileSuite
#
paths = [os.path.normpath(os.path.join(text_file_dir, path)) for path in TEXT_DOCTEST_PATHS]
for path in paths:
suite = doctest.DocFileSuite(path, module_relative=False)
suites.append(suite)
modules = _get_module_doctests(PACKAGE_DIR)
for module in modules:
suite = doctest.DocTestSuite(module)
suites.append(suite)
return suites
def _get_module_doctests(package_dir):
modules = []
for pkg in pkgutil.walk_packages([package_dir]):
# The importer is a pkgutil.ImpImporter instance:
#
# http://docs.python.org/library/pkgutil.html#pkgutil.ImpImporter
#
importer, module_name, is_package = pkg
if is_package:
# Otherwise, we will get the following error when adding tests:
#
# ValueError: (<module 'tests' from '.../pystache/tests/__init__.pyc'>, 'has no tests')
#
continue
# The loader is a pkgutil.ImpLoader instance.
loader = importer.find_module(module_name)
try:
module = loader.load_module(module_name)
except ImportError, e:
# In some situations, the test harness was swallowing and/or
# suppressing the display of the stack trace when errors
# occurred here. The following code makes errors occurring here
# easier to troubleshoot.
details = "".join(traceback.format_exception(*sys.exc_info()))
raise ImportError(details)
modules.append(module)
return modules
|