[Zope-Checkins] CVS: Zope3/lib/python/Zope/Testing - Builder.py:1.1.4.3 __init__.py:1.3.38.1
Tres Seaver
tseaver@zope.com
Wed, 21 Nov 2001 17:19:23 -0500
Update of /cvs-repository/Zope3/lib/python/Zope/Testing
In directory cvs.zope.org:/tmp/cvs-serv5192/lib/python/Zope/Testing
Modified Files:
Tag: Zope-3x-branch
Builder.py __init__.py
Log Message:
- Further unit test cleanups.
=== Zope3/lib/python/Zope/Testing/Builder.py 1.1.4.2 => 1.1.4.3 ===
import unittest
-import sys, os, string, re
+import sys, os, string
from fnmatch import fnmatch
-import imp, traceback
-
-def getSuiteFromFile( filepath ):
- """
- Extract and return the test suite from filepath.
- """
- if not os.path.isfile(filepath):
- raise ValueError, '%s is not a file' % filepath
-
- path, filename = os.path.split(filepath)
- name, ext = os.path.splitext(filename)
- file, pathname, desc = imp.find_module( name, [ path ] )
- saved_syspath = sys.path[:]
-
- try:
- module=imp.load_module( name, file, pathname, desc )
- finally:
- file.close()
- sys.path[:] = saved_syspath
-
- function=getattr( module, 'test_suite', None )
-
- if function is None:
- return None
-
- return function()
-
-TEST_FILE_NAME = re.compile( r'^test([A-Za-z0-9_]+).py$' )
-TEST_SUITE_DEF = re.compile( r'^def test_suite\(' )
-
-def smellsLikeATest( filepath
- ):
- """
- Does 'filepath' match our criteria for unit test modules?
-
- - filename matches 'test*.py';
-
- - defines a 'test_suite' function at module scope.
- """
- path, name = os.path.split( filepath )
- fname, ext = os.path.splitext( name )
-
- match = TEST_FILE_NAME.match( name )
- if match and match.group(0) != 'runner':
- return len( filter( TEST_SUITE_DEF.match
- , open( filepath, 'r' ).readlines() ) ) > 0
- return 0
+import traceback
def listTestableNames( pathname ):
"""
@@ -75,60 +29,17 @@
return names
-def extractSuite( pathname ):
- """
- Extract and return the appropriate test suite, along with
- a list of filed imports.
- """
- suite = None
- import_failures = []
-
- if os.path.isdir( pathname ):
-
- suite = unittest.TestSuite()
-
- for name in listTestableNames( pathname ):
-
- fullpath = os.path.join( pathname, name )
- sub_suite, sub_failures = extractSuite( fullpath )
- if sub_suite:
- suite.addTest( sub_suite )
- import_failures.extend( sub_failures )
-
- if not suite.countTestCases(): # ignore empty suites
- suite = None
-
- elif smellsLikeATest( pathname ):
-
- working_dir = os.getcwd()
- try:
- dirname, name = os.path.split( pathname )
-
- if dirname:
- os.chdir( dirname )
-
- try:
- suite = getSuiteFromFile( name )
- except:
- import_failures.append( pathname )
- finally:
- os.chdir( working_dir )
-
- return suite, import_failures
-
-
class TestFinder( unittest.TestLoader ):
"""
- Handle crawling the filesystem, looking for tests.
+ Crawl the filesystem, looking for tests.
"""
+ def __init__( self, root_dir=None ):
- def __init__( self, sw_home=None ):
+ if root_dir is None:
+ root_dir = self._guessSoftwareHome()
- if sw_home is None:
- sw_home = self._guessSoftwareHome()
-
- self._sw_home = sw_home
- self._sw_home_len = len( sw_home.split( os.sep ) )
+ self._root_dir = root_dir
+ self._root_dir_len = len( root_dir.split( os.sep ) )
self._candiates = []
self._cant_load = []
@@ -138,14 +49,14 @@
"""
from Zope import Testing
zope_pkg, rest = os.path.split( Testing.__path__[0] )
- sw_home, rest = os.path.split( zope_pkg )
- return sw_home
+ root_dir, rest = os.path.split( zope_pkg )
+ return root_dir
def _splitPath( self, path ):
"""
- Return a list of path elements, relative to sw_home.
+ Return a list of path elements, relative to root_dir.
"""
- return path.split( os.sep )[ self._sw_home_len : ]
+ return path.split( os.sep )[ self._root_dir_len : ]
def _visit( self, arg, dirname, names ):
"""
@@ -199,20 +110,20 @@
self._recordLoadFailure( candidate, msg )
return suite
- def loadTestsFromPath( self, path=None ):
+ def _loadTestsFromPath( self, path=None ):
self._candidates = []
if path is None:
- path = self._sw_home
+ path = self._root_dir
os.path.walk( path, self._visit, None )
suite = self._buildSuite()
suite.warnings = self._cant_load
suite.loaded = self._loaded
return suite
+ __call__ = _loadTestsFromPath
+
def allZopeTests():
- #suite, failed_imports = extractSuite( sw_home )
- #return suite
import Zope
- return TestFinder().loadTestsFromPath( Zope.__path__[0] )
+ return TestFinder()( Zope.__path__[0] )
=== Zope3/lib/python/Zope/Testing/__init__.py 1.3 => 1.3.38.1 ===
os.environ['ZEO_CLIENT'] = '1'
+from Builder import TestFinder
+def allZopeTests():
+ import Zope
+ return TestFinder()( Zope.__path__[0] )