Back to index

plone3  3.1.7
utils.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights
00004 # Reserved.
00005 #
00006 # This software is subject to the provisions of the Zope Public License,
00007 # Version 2.0 (ZPL).  A copy of the ZPL should accompany this
00008 # distribution.
00009 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00010 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00011 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00012 # FOR A PARTICULAR PURPOSE
00013 #
00014 ##############################################################################
00015 import os
00016 import unittest
00017 
00018 from Globals import package_home
00019 
00020 try:
00021     from zope.interface import directlyProvides
00022 except ImportError:
00023     def directlyProvides(obj, *interfaces):
00024         obj.__implements__ = ( getattr( obj.__class__, '__implements__', () ) +
00025                                tuple( interfaces )
00026                              )
00027 
00028 product_dir = package_home( globals() )
00029 product_prefix = os.path.join( os.path.split(product_dir)[:-1] )
00030 
00031 _wwwdir = os.path.join( product_dir, 'www' )
00032 
00033 #
00034 #   Most of this module is shamelessly ripped off from Zope3.test
00035 #
00036 def remove_stale_bytecode( arg, dirname, names ):
00037     """
00038         Troll product, removing compiled turds whose source is now gone.
00039     """
00040     names = map( os.path.normcase, names )
00041 
00042     for name in names:
00043 
00044         if name.endswith( ".pyc" ) or name.endswith( ".pyo" ):
00045 
00046             srcname = name[:-1]
00047 
00048             if srcname not in names:
00049 
00050                 fullname = os.path.join( dirname, name )
00051 
00052                 if __debug__:
00053                     print "Removing stale bytecode file", fullname
00054 
00055                 os.unlink( fullname )
00056 
00057 class TestFileFinder:
00058 
00059     def __init__( self ):
00060         self.files = []
00061 
00062     def visit( self, prefix, dir, files ):
00063         """
00064             Visitor for os.path.walk:  accumulates filenamse of unittests.
00065         """
00066         #if dir[-5:] != "tests":
00067         #    return
00068 
00069         # ignore tests that aren't in packages
00070         if not "__init__.py" in files:
00071 
00072             if not files or files == ['CVS']:
00073                 return
00074 
00075             if 0 and __debug__: # XXX: don't care!
00076                 print "not a package", dir
00077 
00078             return
00079 
00080         for file in files:
00081 
00082             if file.startswith( prefix ) and file.endswith( ".py" ):
00083                 path = os.path.join(dir, file)
00084                 self.files.append(path)
00085 
00086 def find_unit_test_files( from_dir=product_dir, test_prefix='test' ):
00087     """
00088         Walk the product, return a list of all unittest files.
00089     """
00090     finder = TestFileFinder()
00091     os.path.walk( from_dir, finder.visit, test_prefix )
00092     return finder.files
00093 
00094 def module_name_from_path( path ):
00095     """
00096         Return the dotted module name matching the filesystem path.
00097     """
00098     assert path.endswith( '.py' )
00099     path = path[:-3]
00100     path = path[ len(product_prefix) + 1: ] # strip extraneous crap
00101     dirs = []
00102     while path:
00103         path, end = os.path.split( path )
00104         dirs.insert( 0, end )
00105     return ".".join( dirs )
00106 
00107 def get_suite( file ):
00108     """
00109         Retrieve a TestSuite from 'file'.
00110     """
00111     module_name = module_name_from_path( file )
00112     loader = unittest.defaultTestLoader
00113     try:
00114         suite = loader.loadTestsFromName( '%s.test_suite' % module_name )
00115     except AttributeError:
00116 
00117         try:
00118             suite = loader.loadTestsFromName(  module_name )
00119         except ImportError, err:
00120             print "Error importing %s\n%s" % (module_name, err)
00121             raise
00122     return suite
00123 
00124 def allTests( from_dir=product_dir, test_prefix='test' ):
00125     """
00126         Walk the product and build a unittest.TestSuite aggregating tests.
00127     """
00128     os.path.walk( from_dir, remove_stale_bytecode, None )
00129     test_files = find_unit_test_files( from_dir, test_prefix )
00130     test_files.sort()
00131 
00132     suite = unittest.TestSuite()
00133 
00134     for test_file in test_files:
00135 
00136         s = get_suite( test_file )
00137         if s is not None:
00138             suite.addTest( s )
00139 
00140     return suite