miscell cosmetic + pass the substrate object to TestBonding so it can compute an...
[tests.git] / qaapi / qa / QAAPI.py
index 5310895..39ee19e 100644 (file)
@@ -1,45 +1,36 @@
 import sys, os
 import traceback
-import qa.modules
-from qa.Config import Config
-from qa.logger import log      
+import tests
+from Config import Config
+from tests.Test import Test
+from logger import log 
 
 class QAAPI:
    
-    modules_path = os.path.realpath(qa.modules.__path__[0])
+    tests_path = os.path.realpath(tests.__path__[0])
     methods = []
                
        
-    def __init__(self, globals = globals(), config = None, logging=None):
+    def __init__(self, globals, config = None, logging=None, verbose=None):
        if config is None: self.config = Config()
        else: self.config = Config(config)
 
-       # Load methods
-       real_files = lambda name: not name.startswith('__init__') \
-                                 and  name.endswith('.py')
-       remove_ext = lambda name: name.split(".py")[0]  
-       iterator = os.walk(self.modules_path)
-       (root, basenames, files) = iterator.next()
-       method_base = ""
-       self.methods.extend([method_base+file for file in map(remove_ext, filter(real_files, files))])  
-       for (root, dirs, files) in iterator:
-           parts = root.split(os.sep)  
-           for basename in basenames:
-               if basename in parts:
-                   method_base = ".".join(parts[parts.index(basename):])+"."
-           files = filter(real_files, files)
-           files = map(remove_ext, files)      
-           self.methods.extend([method_base+file for file in  files]) 
-
-       # Add methods to self and global environment 
-        for method in self.methods:
-           callable = self.callable(method)(self.config)
-           if logging: callable = log(callable, method)
+       test_files = self.test_files(self.tests_path)
+       callables = set()
+        # determine what is callable
+       for file in test_files:
+           tests = self.callables(file)
+           tests = filter(lambda t: t.test_name not in ['Test'], tests)
+           callables.update(tests)
+               
+       # Add methods to self and global environemt     
+        for method in callables:
+           if logging: method = log(method, method.test_name)
            elif hasattr(self.config, 'log') and self.config.log:
-                callable = log(callable, method)
-           
+               method = log(method, method.test_name)
            class Dummy: pass
-            paths = method.split(".")
+            paths = method.test_name.split(".")
+           
             if len(paths) > 1:
                 first = paths.pop(0)
 
@@ -55,35 +46,65 @@ class QAAPI:
                 for path in paths:
                     if not hasattr(obj, path):
                         if path == paths[-1]:
-                            setattr(obj, path, callable)
-                           globals[method]=obj  
+                            setattr(obj, path, method)
+                           globals[method.test_name]=obj  
                         else:
                             setattr(obj, path, Dummy())
                     obj = getattr(obj, path)
            else:
-               if not hasattr(self, method):
-                   setattr(self, method, callable)
                if globals is not None:
-                   globals[method] = callable          
-               
+                   globals[method.test_name] = method          
 
-    def callable(self, method):
+    def test_files(self, tests_dir):
+       """
+       Build a list of files   
+       """     
+       
+       # Load files from tests direcotry
+        real_files = lambda name: not name.startswith('__init__') \
+                                  and  name.endswith('.py')
+        remove_ext = lambda name: name.split(".py")[0]
+        iterator = os.walk(tests_dir)
+        (root, basenames, files) = iterator.next()
+        test_base = ""
+        test_files = []
+        test_files.extend([test_base+file for file in map(remove_ext, filter(real_files, files))])
+        
+       # recurse through directory             
+        #for (root, dirs, files) in iterator:
+        #    parts = root.split(os.sep)
+        #    for basename in basenames:
+        #        if basename in parts:
+        #            test_base = ".".join(parts[parts.index(basename):])+"."
+        #    files = filter(real_files, files)
+        #    files = map(remove_ext, files)
+        #    test_files.extend([test_base+file for file in  files])
+       return list(set(test_files)) 
+
+    def callables(self, test_file):
        """
        Return a new instance of the specified method. 
        """      
         
-       # Look up test  
-       if method not in self.methods:
-           raise Exception, "Invalid method: %s" % method
-
        # Get new instance of method
+       parts = test_file.split(".")
+       # add every part except for the last to name (filename)
+       tests_dir =  "tests."
+       test_basename = ".".join(parts[:-1])
+       if test_basename: test_basename += '.'
+       test_path = tests_dir + test_file
        try:
-           #classname = method.split(".")[-1]
-           module_name = "qa.modules."+method
-           module = __import__(module_name, globals(), locals(), module_name)
-           components = module_name.split('.')
-           module = getattr(module, components[-1:][0])        
-           return module
+           test = __import__(test_path, globals(), locals(), test_path)
+           callables = []
+           for attribute in dir(test):
+               attr = getattr(test, attribute)
+               if callable(attr) and hasattr(attr, 'status'):
+                   setattr(attr, 'test_name', test_basename+attribute)
+                   try:
+                       test_instance = attr(self.config)
+                       callables.append(test_instance)
+                   except: pass
+           return callables 
        except ImportError, AttributeError:
            raise