Commit d9e84960 by Chris Jerdonek

Started refactoring pystache/tests/main.py and stubbed out _PystacheTestProgram.

parent 5a47ea6d
...@@ -10,6 +10,7 @@ This module is for our test console script. ...@@ -10,6 +10,7 @@ This module is for our test console script.
import os import os
import sys import sys
import unittest import unittest
from unittest import TestProgram
UNITTEST_FILE_PREFIX = "test_" UNITTEST_FILE_PREFIX = "test_"
...@@ -31,62 +32,79 @@ UNITTEST_FILE_PREFIX = "test_" ...@@ -31,62 +32,79 @@ UNITTEST_FILE_PREFIX = "test_"
# TestCase or TestSuite instances (e.g. doctests and spec tests), and then # TestCase or TestSuite instances (e.g. doctests and spec tests), and then
# call the base class's runTests(). # call the base class's runTests().
class Tester(object): def _find_unittest_files(package_dir):
"""
Return a list of paths to all unit-test files in the given package directory.
""" """
Discovers and runs unit tests. paths = [] # Return value.
def is_unittest(file_name):
return file_name.startswith(UNITTEST_FILE_PREFIX) and file_name.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(package_dir):
file_names = filter(is_unittest, file_names)
for file_name in file_names:
path = os.path.join(dir_path, file_name)
paths.append(path)
return paths
def _get_module_names(package_dir, paths):
""" """
Return a list of fully-qualified test module names given a list of module paths.
def _find_unittest_files(self, package_dir): """
""" package_dir = os.path.abspath(package_dir)
Return a list of paths to all unit-test files in the given package directory. package_name = os.path.split(package_dir)[1]
""" prefix_length = len(package_dir)
unittest_paths = [] # Return value.
def is_unittest(file_name): module_names = []
return file_name.startswith(UNITTEST_FILE_PREFIX) and file_name.endswith('.py') for path in paths:
path = os.path.abspath(path) # for example <path_to_package>/subpackage/module.py
rel_path = path[prefix_length:] # for example /subpackage/module.py
rel_path = os.path.splitext(rel_path)[0] # for example /subpackage/module
# os.walk() is new in Python 2.3 parts = []
# http://docs.python.org/library/os.html#os.walk while True:
for dir_path, dir_names, file_names in os.walk(package_dir): (rel_path, tail) = os.path.split(rel_path)
file_names = filter(is_unittest, file_names) if not tail:
break
parts.insert(0, tail)
# We now have, for example, ['subpackage', 'module'].
parts.insert(0, package_name)
module = ".".join(parts)
module_names.append(module)
for file_name in file_names: return module_names
unittest_path = os.path.join(dir_path, file_name)
unittest_paths.append(unittest_path)
return unittest_paths
def _modules_from_paths(self, package_dir, paths): def _get_test_module_names(package_dir):
""" """
Return a list of fully-qualified module names given paths. Return a list of fully-qualified module names given a list of module paths.
""" """
package_dir = os.path.abspath(package_dir) paths = _find_unittest_files(package_dir)
package_name = os.path.split(package_dir)[1] modules = _get_module_names(package_dir, paths)
prefix_length = len(package_dir) return modules
module_names = []
for path in paths: class _PystacheTestProgram(TestProgram):
path = os.path.abspath(path) # for example <path_to_package>/subpackage/module.py pass
rel_path = path[prefix_length:] # for example /subpackage/module.py
rel_path = os.path.splitext(rel_path)[0] # for example /subpackage/module
class Tester(object):
parts = []
while True: """
(rel_path, tail) = os.path.split(rel_path) Discovers and runs unit tests.
if not tail:
break """
parts.insert(0, tail)
# We now have, for example, ['subpackage', 'module'].
parts.insert(0, package_name)
module = ".".join(parts)
module_names.append(module)
return module_names
# TODO: consider replacing the package argument with a package_dir argument. # TODO: consider replacing the package argument with a package_dir argument.
def run_tests(self, package, sys_argv): def run_tests(self, package, sys_argv):
...@@ -105,13 +123,10 @@ class Tester(object): ...@@ -105,13 +123,10 @@ class Tester(object):
# the unittest module is equipped to handle. # the unittest module is equipped to handle.
unittest.main(argv=sys_argv, module=None) unittest.main(argv=sys_argv, module=None)
# No need to return since unitttest.main() exits. # No need to return since unitttest.main() exits.
# Otherwise, auto-detect all unit tests. # Otherwise, auto-detect all unit tests.
package_dir = os.path.dirname(package.__file__) package_dir = os.path.dirname(package.__file__)
unittest_paths = self._find_unittest_files(package_dir) modules = _get_test_module_names(package_dir)
modules = self._modules_from_paths(package_dir, unittest_paths)
modules.sort() modules.sort()
# This is a sanity check to ensure that the unit-test discovery # This is a sanity check to ensure that the unit-test discovery
......
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