Commit 1806f853 by Chris Jerdonek

Test script now works with Python 2.4; tox now works with Python 2.4 to 3.2.

Doctest discovery no longer depends on pkgutil.walk_packages().
parent 71a97ed2
......@@ -51,7 +51,7 @@ def get_data_path(file_name):
def _find_files(root_dir, should_include):
"""
Return a list of paths to all files in the given directory.
Return a list of paths to all modules below the given directory.
Arguments:
......@@ -60,10 +60,13 @@ def _find_files(root_dir, should_include):
"""
paths = [] # Return value.
is_module = lambda path: path.endswith(".py")
# os.walk() is new in Python 2.3
# http://docs.python.org/library/os.html#os.walk
for dir_path, dir_names, file_names in os.walk(root_dir):
new_paths = [os.path.join(dir_path, file_name) for file_name in file_names]
new_paths = filter(is_module, new_paths)
new_paths = filter(should_include, new_paths)
paths.extend(new_paths)
......@@ -100,13 +103,20 @@ def _make_module_names(package_dir, paths):
return module_names
def get_module_names(package_dir, should_include):
def get_module_names(package_dir=None, should_include=None):
"""
Return a list of fully-qualified module names in the given package.
"""
if package_dir is None:
package_dir = PACKAGE_DIR
if should_include is None:
should_include = lambda path: True
paths = _find_files(package_dir, should_include)
names = _make_module_names(package_dir, paths)
names.sort()
return names
......
......@@ -17,7 +17,8 @@ if sys.version_info >= (3,):
from lib2to3.main import main as lib2to3main # new in Python 2.6?
from shutil import copyfile
from pystache.tests.common import PACKAGE_DIR, TEXT_DOCTEST_PATHS
from pystache.tests.common import TEXT_DOCTEST_PATHS
from pystache.tests.common import get_module_names
# This module follows the guidance documented here:
......@@ -51,7 +52,7 @@ def get_doctests(text_file_dir):
suite = doctest.DocFileSuite(path, module_relative=False)
suites.append(suite)
modules = _get_module_doctests(PACKAGE_DIR)
modules = get_module_names()
for module in modules:
suite = doctest.DocTestSuite(module)
suites.append(suite)
......@@ -87,34 +88,3 @@ def _convert_paths(paths):
new_paths.append(new_path)
return new_paths
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
......@@ -66,10 +66,9 @@ def _discover_test_modules(package_dir):
"""
def is_unittest_module(path):
file_name = os.path.basename(path)
return file_name.startswith(UNITTEST_FILE_PREFIX) and file_name.endswith('.py')
return file_name.startswith(UNITTEST_FILE_PREFIX)
names = get_module_names(package_dir, is_unittest_module)
names.sort()
names = get_module_names(package_dir=package_dir, should_include=is_unittest_module)
# This is a sanity check to ensure that the unit-test discovery
# methods are working.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment