Reworked test.py to handle deeper directory structure
authorW. Trevor King <wking@drexel.edu>
Wed, 9 Dec 2009 01:02:34 +0000 (20:02 -0500)
committerW. Trevor King <wking@drexel.edu>
Wed, 9 Dec 2009 01:02:34 +0000 (20:02 -0500)
libbe/bug.py
libbe/bugdir.py
libbe/comment.py
libbe/util/plugin.py [moved from libbe/plugin.py with 98% similarity]
test.py

index 84eb1ed03657abc62de166a22f21dd7bcea6cd4f..5755b6395ce8763aadc8f55e384073c502b32aa5 100644 (file)
@@ -34,7 +34,7 @@ import xml.sax.saxutils
 
 import libbe
 import libbe.util.id
-from libbe.storage.util.properties import Property, doc_property,
+from libbe.storage.util.properties import Property, doc_property, \
     local_property, defaulting_property, checked_property, cached_property, \
     primed_property, change_hook_property, settings_property
 import libbe.storage.settings_object as settings_object
index 44356243beeca8378ec413e628c1f1e8b29acd41..02ff96f03915630d8588a8358a72dd2884b8d1e6 100644 (file)
@@ -32,7 +32,7 @@ import time
 import libbe
 import libbe.util.encoding as encoding
 import libbe.storage as storage
-from libbe.storage.util.properties import Property, doc_property,
+from libbe.storage.util.properties import Property, doc_property, \
     local_property, defaulting_property, checked_property, \
     fn_checked_property, cached_property, primed_property, \
     change_hook_property, settings_property
index 987e39c658a82f2d83a5d6288dec6f11d6b90c28..f5a630989d11a46b8ee5eccdcced071172e9f129 100644 (file)
@@ -34,7 +34,7 @@ import xml.sax.saxutils
 
 import libbe
 import libbe.util.id
-from libbe.storage.util.properties import Property, doc_property,
+from libbe.storage.util.properties import Property, doc_property, \
     local_property, defaulting_property, checked_property, cached_property, \
     primed_property, change_hook_property, settings_property
 import libbe.storage.settings_object as settings_object
similarity index 98%
rename from libbe/plugin.py
rename to libbe/util/plugin.py
index 03f68fc9320bcf681010e23e1d07692028b4c05b..edb492223e99d800771ac34c3126614d58d69d6f 100644 (file)
@@ -30,7 +30,7 @@ import libbe
 if libbe.TESTING == True:
     import doctest
 
-def my_import(mod_name):
+def import_by_name(mod_name):
     module = __import__(mod_name)
     components = mod_name.split('.')
     for comp in components[1:]:
diff --git a/test.py b/test.py
index 4f20808b326ad1c078e5dd28c1d49fd8524e9033..2b93bcfa78dd8e5664fcb6093cf281d1b2a2ed3d 100644 (file)
--- a/test.py
+++ b/test.py
@@ -1,54 +1,79 @@
 """Usage: python test.py [module(s) ...]
 
-When called without optional module names, run the doctests from *all*
-modules.  This may raise lots of errors if you haven't installed one
-of the versioning control systems.
+When called without optional module names, run the test suites for
+*all* modules.  This may raise lots of errors if you haven't installed
+one of the versioning control systems.
 
-When called with module name arguments, only run the doctests from
-those modules.
+When called with module name arguments, only run the test suites from
+those modules and their submodules.  For example:
+  python test.py libbe.bugdir libbe.storage
 """
 
-import libbe
-libbe.TESTING = True
-from libbe import plugin, vcs
-import unittest
 import doctest
+import os
+import os.path
 import sys
+import unittest
 
-suite = unittest.TestSuite()
+import libbe
+libbe.TESTING = True
+from libbe.util.tree import Tree
+from libbe.util.plugin import import_by_name
 
-if len(sys.argv) > 1:
-    for submodname in sys.argv[1:]:
-        match = False
-        mod = plugin.get_plugin("libbe", submodname)
-        if mod is not None:
-            if hasattr(mod, "suite"):
-                suite.addTest(mod.suite)
-                match = True
-            else:
-                print "Module \"%s\" has no test suite" % submodname
-        mod = plugin.get_plugin("becommands", submodname)
-        if mod is not None:
-            if hasattr(mod, "suite"):
-                suite.addTest(mod.suite)
-            else:
-                suite.addTest(doctest.DocTestSuite(mod))
-            match = True
-        if not match:
-            print "No modules match \"%s\"" % submodname
-            sys.exit(1)
-else:
-    failed = False
-    for modname,module in plugin.iter_plugins("libbe"):
-        if not hasattr(module, "suite"):
+def python_tree(root_path='libbe', root_modname='libbe'):
+    tree = Tree()
+    tree.path = root_path
+    tree.parent = None
+    stack = [tree]
+    while len(stack) > 0:
+        f = stack.pop(0)
+        if f.path.endswith('.py'):
+            f.name = os.path.basename(f.path)[:-len('.py')]
+        elif os.path.isdir(f.path) \
+                and os.path.exists(os.path.join(f.path, '__init__.py')):
+            f.name = os.path.basename(f.path)
+            f.is_module = True
+            for child in os.listdir(f.path):
+                if child == '__init__.py':
+                    continue
+                c = Tree()
+                c.path = os.path.join(f.path, child)
+                c.parent = f
+                stack.append(c)
+        else:
             continue
-        suite.addTest(module.suite)
-    for modname,module in plugin.iter_plugins("becommands"):
-        suite.addTest(doctest.DocTestSuite(module))
+        if f.parent == None:
+            f.modname = root_modname
+        else:
+            f.modname = f.parent.modname + '.' + f.name
+            f.parent.append(f)
+    return tree
+
+def add_module_tests(suite, module_name):
+    mod = import_by_name(module_name)
+    if mod == None:
+        raise KeyError, 'module "%s" not found' % module_name
+    if hasattr(mod, 'suite'):
+        s = mod.suite
+    else:
+        s = unittest.TestLoader().loadTestsFromModule(mod)
+    suite.addTest(s)
 
-_vcs = vcs.installed_vcs()
-vcs.set_preferred_vcs(_vcs.name)
-print 'Using %s as the testing VCS' % _vcs.name
+suite = unittest.TestSuite()
+tree = python_tree()
+if len(sys.argv) <= 1:
+    for node in tree.traverse():
+        add_module_tests(suite, node.modname)
+else:
+    added = []
+    for modname in sys.argv[1:]:
+        for node in tree.traverse():
+            if node.modname == modname:
+                for n in node.traverse():
+                    if n.modname not in added:
+                        add_module_tests(suite, n.modname)
+                        added.append(n.modname)
+                break
 
 result = unittest.TextTestRunner(verbosity=2).run(suite)