Changeset 118

Show
Ignore:
Timestamp:
Tue Nov 14 17:38:29 2006
Author:
jpellerin
Message:

Work in progress on new loader scratch pad

Files:

Legend:

Unmodified
Added
Removed
Modified
  • branches/new_loader/work.py

    r117 r118  
    1 1 import os  
    2 2 import re  
      3 from imp import load_source  
    3 4 from nose.selector import Selector  
    4 5 from nose.config import Config  
      6 from nose.importer import _import  
    5 7 from nose.util import split_test_name  
    6 8  
     
    16 18  
    17 19  
      20 def ispackageinit(module):  
      21     filename = module.__file__  
      22     base, ext = os.path.splitext(os.path.basename(filename))  
      23     return base == '__init__' and ext.startswith('.py')  
      24  
      25  
      26 def module_name(filename, package=None):  
      27     base, junk = os.path.splitext(filename)  
      28     if package is None:  
      29         return base  
      30     return "%s.%s" % (package, base)  
      31  
      32  
    18 33 class TestAddress:  
    19 34     def __init__(self, name, working_dir):  
     
    72 87              
    73 88  
      89 class ModuleSuite:  
      90     def __init__(self, name, path, loader, working_dir, wanted):  
      91         self.name = name  
      92         self.path = path  
      93         self.module = None  
      94         self.loader = loader  
      95         self.working_dir = working_dir  
      96         self.wanted = wanted  
      97         self._tests = []  
      98  
      99     def __nonzero__(self):  
      100         self.collectTests()  
      101         return bool(self._tests)  
      102  
      103     def __len__(self):  
      104         self.collectTests()  
      105         return len(self._tests)  
      106  
      107     def __iter__(self):  
      108         self.collectTests()  
      109         return iter(self._tests)  
      110  
      111     def __str__(self):  
      112         return "ModuleSuite(%s, %s)" % (self.name, self.path)  
      113  
      114     def addTest(self, test):  
      115         # depth-first?  
      116         if test:  
      117             self._tests.append(test)  
      118  
      119     def collectTests(self):  
      120         if self._tests:  
      121             return  
      122         self._tests = []  
      123         if self.module is None:  
      124             # We know the exact source of each module so why not?  
      125             # We still need to add the module's parent dir (up to the top  
      126             # if it's a package) to sys.path first, though  
      127             self.module = load_source(self.name, self.path)  
      128         for test in self.loader.loadTestsFromModule(self.module, self.wanted):  
      129             self.addTest(test)  
      130          
      131     def run(self, result):  
      132         self.collectTests()  
      133         if not self._tests:  
      134             return  
      135         # FIXME this needs to be a real run() with exc handling  
      136         self.setUp()  
      137         for test in self._tests:  
      138             test(result)  
      139         self.tearDown()  
      140  
      141     def setUp(self):  
      142         print "SETUP %s" % self  
      143  
      144     def tearDown(self):  
      145         print "TEARDOWN %s" % self  
    74 146  
      147          
      148          
    75 149 class TestLoader:  
    76 150  
     
    78 152         if not os.path.isabs(working_dir):  
    79 153             working_dir = os.path.join(os.getcwd(), working_dir)  
    80    
    81 154         tests = None  
    82 155         if names:  
    83               tests = map(lambda n: TestAddress(n, working_dir), names)  
    84    
    85           # print working_dir  
    86           def abs_(filename):  
    87               return os.path.join(working_dir, filename)  
    88           ltests = None  
      156             tests = [ TestAddress(n, working_dir) for n in names ]  
    89 157         if tests is not None:  
    90 158             def intests(filename):  
     
    94 162             def intests(filename):  
    95 163                 return True  
    96                
      164         return self.findTests(working_dir, wanted=intests)  
      165      
      166     def findTests(self, working_dir, wanted=None, package=None):  
    97 167         for dirpath, dirnames, filenames in os.walk(working_dir):  
    98 168  
     
    100 170              
    101 171             to_remove = set()  
      172             packages = []  
    102 173             for dirname in dirnames:  
    103 174  
     
    107 178                 remove = True  
    108 179                 ldir = os.path.join(dirpath, dirname)  
    109                   package = ispackage(ldir)  
    110                   if ((package or conf.testMatch.search(dirname))  
    111                       and intests(ldir)):  
    112                       if package:  
      180                 pack = ispackage(ldir)  
      181                 if ((pack or conf.testMatch.search(dirname))  
      182                     and wanted(ldir)):  
      183                     if pack:  
    113 184                         remove = True  
    114                           print "**",ldir  
      185                         # print "**",ldir  
    114 185                         # FIXME track this, we'll yield a ModuleSuite later  
      186                         packages.append((dirname,  
      187                                          os.path.join(ldir, '__init__.py')))  
    115 188                     else:  
    116 189                         remove = False  
    117                           print "Continue into", dirname  
    118 190                 if remove:  
    119 191                     to_remove.add(dirname)  
     
    132 204                 if (filename.endswith('.py')  
    133 205                     and conf.testMatch.search(filename)  
    134                       and intests(lname)):  
    135                       print "**", lname  
      206                     and wanted(lname)):  
      207                     # print "**", lname  
      208                     yield ModuleSuite(  
      209                         name=module_name(filename, package=package),  
      210                         path=lname, loader=self,  
      211                         working_dir=working_dir,  
      212                         wanted=wanted)  
    136 213                     # FIXME yield a ModuleSuite if it's a python module  
    137 214                     # FIXME yield a FileSuite if it's not  
    138 215             # FIXME yield ModuleSuites for all module dirs  
      216             for name, path in packages:  
      217                 yield ModuleSuite(  
      218                     name=module_name(name, package=package),  
      219                     path=path, loader=self,  
      220                     working_dir=working_dir,  
      221                     wanted=wanted)  
      222             # At this point we're diving into directories that aren't packages  
      223             # so if we think we are in a package, we have to forget the  
      224             # package name, lest modules in the directory think their names  
      225             # are package.foo when they are really just foo  
      226             package = None  
      227                  
      228     def loadTestsFromModule(self, module, wanted=None):  
      229         """Construct a TestSuite containing all of the tests in  
      230         the module, including tests in the package if it is a package  
      231         """  
      232         if ispackageinit(module):  
      233             path = os.path.dirname(module.__file__)  
      234             for test in self.findTests(path, wanted, package=module.__name__):  
      235                 # FIXME  
      236                 print "  ", test  
      237         return []  
    139 238  
    140 239 if __name__ == '__main__':  
    141 240     import sys  
    142 241     l = TestLoader()  
    143       l.collectTests('unit_tests/support', sys.argv[1:])  
    144    
      242     for test in l.collectTests('unit_tests/support', sys.argv[1:]):  
      243         print test  
      244         test.run('whatever')  
      245     #mods = sys.modules.keys()[:]  
      246     #mods.sort()  
      247     #print mods  
      248      
    145 249 # note these testable possibilities  
    146 250 # need a test for each of these