<s> """<STR_LIT>""" <EOL> import os <EOL> import re <EOL> import sys <EOL> import traceback <EOL> import types <EOL> import functools <EOL> from fnmatch import fnmatch <EOL> from . import case , suite , util <EOL> __unittest = True <EOL> VALID_MODULE_NAME = re . compile ( r'<STR_LIT>' , re . IGNORECASE ) <EOL> def _make_failed_import_test ( name , suiteClass ) : <EOL> message = '<STR_LIT>' % ( name , traceback . format_exc ( ) ) <EOL> return _make_failed_test ( '<STR_LIT>' , name , ImportError ( message ) , <EOL> suiteClass ) <EOL> def _make_failed_load_tests ( name , exception , suiteClass ) : <EOL> return _make_failed_test ( '<STR_LIT>' , name , exception , suiteClass ) <EOL> def _make_failed_test ( classname , methodname , exception , suiteClass ) : <EOL> def testFailure ( self ) : <EOL> raise exception <EOL> attrs = { methodname : testFailure } <EOL> TestClass = type ( classname , ( case . TestCase , ) , attrs ) <EOL> return suiteClass ( ( TestClass ( methodname ) , ) ) <EOL> def _jython_aware_splitext ( path ) : <EOL> if path . lower ( ) . endswith ( '<STR_LIT>' ) : <EOL> return path [ : - <NUM_LIT:9> ] <EOL> return os . path . splitext ( path ) [ <NUM_LIT:0> ] <EOL> class TestLoader ( object ) : <EOL> """<STR_LIT>""" <EOL> testMethodPrefix = '<STR_LIT:test>' <EOL> sortTestMethodsUsing = staticmethod ( util . three_way_cmp ) <EOL> suiteClass = suite . TestSuite <EOL> _top_level_dir = None <EOL> def loadTestsFromTestCase ( self , testCaseClass ) : <EOL> """<STR_LIT>""" <EOL> if issubclass ( testCaseClass , suite . TestSuite ) : <EOL> raise TypeError ( "<STR_LIT>" "<STR_LIT>" ) <EOL> testCaseNames = self . getTestCaseNames ( testCaseClass ) <EOL> if not testCaseNames and hasattr ( testCaseClass , '<STR_LIT>' ) : <EOL> testCaseNames = [ '<STR_LIT>' ] <EOL> loaded_suite = self . suiteClass ( map ( testCaseClass , testCaseNames ) ) <EOL> return loaded_suite <EOL> def loadTestsFromModule ( self , module , use_load_tests = True ) : <EOL> """<STR_LIT>""" <EOL> tests = [ ] <EOL> for name in dir ( module ) : <EOL> obj = getattr ( module , name ) <EOL> if isinstance ( obj , type ) and issubclass ( obj , case . TestCase ) : <EOL> tests . append ( self . loadTestsFromTestCase ( obj ) ) <EOL> load_tests = getattr ( module , '<STR_LIT>' , None ) <EOL> tests = self . suiteClass ( tests ) <EOL> if use_load_tests and load_tests is not None : <EOL> try : <EOL> return load_tests ( self , tests , None ) <EOL> except Exception as e : <EOL> return _make_failed_load_tests ( module . __name__ , e , <EOL> self . suiteClass ) <EOL> return tests <EOL> def loadTestsFromName ( self , name , module = None ) : <EOL> """<STR_LIT>""" <EOL> parts = name . split ( '<STR_LIT:.>' ) <EOL> if module is None : <EOL> parts_copy = parts [ : ] <EOL> while parts_copy : <EOL> try : <EOL> module = __import__ ( '<STR_LIT:.>' . join ( parts_copy ) ) <EOL> break <EOL> except ImportError : <EOL> del parts_copy [ - <NUM_LIT:1> ] <EOL> if not parts_copy : <EOL> raise <EOL> parts = parts [ <NUM_LIT:1> : ] <EOL> obj = module <EOL> for part in parts : <EOL> parent , obj = obj , getattr ( obj , part ) <EOL> if isinstance ( obj , types . ModuleType ) : <EOL> return self . loadTestsFromModule ( obj ) <EOL> elif isinstance ( obj , type ) and issubclass ( obj , case . TestCase ) : <EOL> return self . loadTestsFromTestCase ( obj ) <EOL> elif ( isinstance ( obj , types . FunctionType ) and <EOL> isinstance ( parent , type ) and <EOL> issubclass ( parent , case . TestCase ) ) : <EOL> name = parts [ - <NUM_LIT:1> ] <EOL> inst = parent ( name ) <EOL> if not isinstance ( getattr ( inst , name ) , types . FunctionType ) : <EOL> return self . suiteClass ( [ inst ] ) <EOL> elif isinstance ( obj , suite . TestSuite ) : <EOL> return obj <EOL> if callable ( obj ) : <EOL> test = obj ( ) <EOL> if isinstance ( test , suite . TestSuite ) : <EOL> return test <EOL> elif isinstance ( test , case . TestCase ) : <EOL> return self . suiteClass ( [ test ] ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" % <EOL> ( obj , test ) ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" % obj ) <EOL> def loadTestsFromNames ( self , names , module = None ) : <EOL> """<STR_LIT>""" <EOL> suites = [ self . loadTestsFromName ( name , module ) for name in names ] <EOL> return self . suiteClass ( suites ) <EOL> def getTestCaseNames ( self , testCaseClass ) : <EOL> """<STR_LIT>""" <EOL> def isTestMethod ( attrname , testCaseClass = testCaseClass , <EOL> prefix = self . testMethodPrefix ) : <EOL> return attrname . startswith ( prefix ) and callable ( getattr ( testCaseClass , attrname ) ) <EOL> testFnNames = list ( filter ( isTestMethod , dir ( testCaseClass ) ) ) <EOL> if self . sortTestMethodsUsing : <EOL> testFnNames . sort ( key = functools . cmp_to_key ( self . sortTestMethodsUsing ) ) <EOL> return testFnNames <EOL> def discover ( self , start_dir , pattern = '<STR_LIT>' , top_level_dir = None ) : <EOL> """<STR_LIT>""" <EOL> set_implicit_top = False <EOL> if top_level_dir is None and self . _top_level_dir is not None : <EOL> top_level_dir = self . _top_level_dir <EOL> elif top_level_dir is None : <EOL> set_implicit_top = True <EOL> top_level_dir = start_dir <EOL> top_level_dir = os . path . abspath ( top_level_dir ) <EOL> if not top_level_dir in sys . path : <EOL> sys . path . insert ( <NUM_LIT:0> , top_level_dir ) <EOL> self . _top_level_dir = top_level_dir <EOL> is_not_importable = False <EOL> if os . path . isdir ( os . path . abspath ( start_dir ) ) : <EOL> start_dir = os . path . abspath ( start_dir ) <EOL> if start_dir != top_level_dir : <EOL> is_not_importable = not os . path . isfile ( os . path . join ( start_dir , '<STR_LIT>' ) ) <EOL> else : <EOL> try : <EOL> __import__ ( start_dir ) <EOL> except ImportError : <EOL> is_not_importable = True <EOL> else : <EOL> the_module = sys . modules [ start_dir ] <EOL> top_part = start_dir . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> start_dir = os . path . abspath ( os . path . dirname ( ( the_module . __file__ ) ) ) <EOL> if set_implicit_top : <EOL> self . _top_level_dir = self . _get_directory_containing_module ( top_part ) <EOL> sys . path . remove ( top_level_dir ) <EOL> if is_not_importable : <EOL> raise ImportError ( '<STR_LIT>' % start_dir ) <EOL> tests = list ( self . _find_tests ( start_dir , pattern ) ) <EOL> return self . suiteClass ( tests ) <EOL> def _get_directory_containing_module ( self , module_name ) : <EOL> module = sys . modules [ module_name ] <EOL> full_path = os . path . abspath ( module . __file__ ) <EOL> if os . path . basename ( full_path ) . lower ( ) . startswith ( '<STR_LIT>' ) : <EOL> return os . path . dirname ( os . path . dirname ( full_path ) ) <EOL> else : <EOL> return os . path . dirname ( full_path ) <EOL> def _get_name_from_path ( self , path ) : <EOL> path = _jython_aware_splitext ( os . path . normpath ( path ) ) <EOL> _relpath = os . path . relpath ( path , self . _top_level_dir ) <EOL> assert not os . path . isabs ( _relpath ) , "<STR_LIT>" <EOL> assert not _relpath . startswith ( '<STR_LIT:..>' ) , "<STR_LIT>" <EOL> name = _relpath . replace ( os . path . sep , '<STR_LIT:.>' ) <EOL> return name <EOL> def _get_module_from_name ( self , name ) : <EOL> __import__ ( name ) <EOL> return sys . modules [ name ] <EOL> def _match_path ( self , path , full_path , pattern ) : <EOL> return fnmatch ( path , pattern ) <EOL> def _find_tests ( self , start_dir , pattern ) : <EOL> """<STR_LIT>""" <EOL> paths = os . listdir ( start_dir ) <EOL> for path in paths : <EOL> full_path = os . path . join ( start_dir , path ) <EOL> if os . path . isfile ( full_path ) : <EOL> if not VALID_MODULE_NAME . match ( path ) : <EOL> continue <EOL> if not self . _match_path ( path , full_path , pattern ) : <EOL> continue <EOL> name = self . _get_name_from_path ( full_path ) <EOL> try : <EOL> module = self . _get_module_from_name ( name ) <EOL> except : <EOL> yield _make_failed_import_test ( name , self . suiteClass ) <EOL> else : <EOL> mod_file = os . path . abspath ( getattr ( module , '<STR_LIT>' , full_path ) ) <EOL> realpath = _jython_aware_splitext ( os . path . realpath ( mod_file ) ) <EOL> fullpath_noext = _jython_aware_splitext ( os . path . realpath ( full_path ) ) <EOL> if realpath . lower ( ) != fullpath_noext . lower ( ) : <EOL> module_dir = os . path . dirname ( realpath ) <EOL> mod_name = _jython_aware_splitext ( os . path . basename ( full_path ) ) <EOL> expected_dir = os . path . dirname ( full_path ) <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise ImportError ( msg % ( mod_name , module_dir , expected_dir ) ) <EOL> yield self . loadTestsFromModule ( module ) <EOL> elif os . path . isdir ( full_path ) : <EOL> if not os . path . isfile ( os . path . join ( full_path , '<STR_LIT>' ) ) : <EOL> continue <EOL> load_tests = None <EOL> tests = None <EOL> if fnmatch ( path , pattern ) : <EOL> name = self . _get_name_from_path ( full_path ) <EOL> package = self . _get_module_from_name ( name ) <EOL> load_tests = getattr ( package , '<STR_LIT>' , None ) <EOL> tests = self . loadTestsFromModule ( package , use_load_tests = False ) <EOL> if load_tests is None : <EOL> if tests is not None : <EOL> yield tests <EOL> for test in self . _find_tests ( full_path , pattern ) : <EOL> yield test <EOL> else : <EOL> try : <EOL> yield load_tests ( self , tests , pattern ) <EOL> except Exception as e : <EOL> yield _make_failed_load_tests ( package . __name__ , e , <EOL> self . suiteClass ) <EOL> defaultTestLoader = TestLoader ( ) <EOL> def _makeLoader ( prefix , sortUsing , suiteClass = None ) : <EOL> loader = TestLoader ( ) <EOL> loader . sortTestMethodsUsing = sortUsing <EOL> loader . testMethodPrefix = prefix <EOL> if suiteClass : <EOL> loader . suiteClass = suiteClass <EOL> return loader <EOL> def getTestCaseNames ( testCaseClass , prefix , sortUsing = util . three_way_cmp ) : <EOL> return _makeLoader ( prefix , sortUsing ) . getTestCaseNames ( testCaseClass ) <EOL> def makeSuite ( testCaseClass , prefix = '<STR_LIT:test>' , sortUsing = util . three_way_cmp , <EOL> suiteClass = suite . TestSuite ) : <EOL> return _makeLoader ( prefix , sortUsing , suiteClass ) . loadTestsFromTestCase ( <EOL> testCaseClass ) <EOL> def findTestCases ( module , prefix = '<STR_LIT:test>' , sortUsing = util . three_way_cmp , <EOL> suiteClass = suite . TestSuite ) : <EOL> return _makeLoader ( prefix , sortUsing , suiteClass ) . loadTestsFromModule ( module ) </s>
