<s> import unittest <EOL> import sys <EOL> import os <EOL> import optparse <EOL> __all__ = [ ] <EOL> def FilterSuite ( suite , predicate ) : <EOL> new_suite = suite . __class__ ( ) <EOL> for x in suite : <EOL> if isinstance ( x , unittest . TestSuite ) : <EOL> subsuite = FilterSuite ( x , predicate ) <EOL> if subsuite . countTestCases ( ) == <NUM_LIT:0> : <EOL> continue <EOL> new_suite . addTest ( subsuite ) <EOL> continue <EOL> assert isinstance ( x , unittest . TestCase ) <EOL> if predicate ( x ) : <EOL> new_suite . addTest ( x ) <EOL> return new_suite <EOL> class _TestLoader ( unittest . TestLoader ) : <EOL> def __init__ ( self , * args ) : <EOL> super ( _TestLoader , self ) . __init__ ( * args ) <EOL> self . discover_calls = [ ] <EOL> def loadTestsFromModule ( self , module , use_load_tests = True ) : <EOL> if module . __file__ != __file__ : <EOL> return super ( _TestLoader , self ) . loadTestsFromModule ( <EOL> module , use_load_tests ) <EOL> suite = unittest . TestSuite ( ) <EOL> for discover_args in self . discover_calls : <EOL> subsuite = self . discover ( * discover_args ) <EOL> suite . addTest ( subsuite ) <EOL> return suite <EOL> class _RunnerImpl ( unittest . TextTestRunner ) : <EOL> def __init__ ( self , filters ) : <EOL> super ( _RunnerImpl , self ) . __init__ ( verbosity = <NUM_LIT:2> ) <EOL> self . filters = filters <EOL> def ShouldTestRun ( self , test ) : <EOL> return not self . filters or any ( name in test . id ( ) for name in self . filters ) <EOL> def run ( self , suite ) : <EOL> filtered_test = FilterSuite ( suite , self . ShouldTestRun ) <EOL> return super ( _RunnerImpl , self ) . run ( filtered_test ) <EOL> class TestRunner ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _loader = _TestLoader ( ) <EOL> def AddDirectory ( self , dir_path , test_file_pattern = "<STR_LIT>" ) : <EOL> assert os . path . isdir ( dir_path ) <EOL> self . _loader . discover_calls . append ( ( dir_path , test_file_pattern , dir_path ) ) <EOL> def Main ( self , argv = None ) : <EOL> if argv is None : <EOL> argv = sys . argv <EOL> parser = optparse . OptionParser ( ) <EOL> options , args = parser . parse_args ( argv [ <NUM_LIT:1> : ] ) <EOL> runner = _RunnerImpl ( filters = args ) <EOL> return unittest . main ( module = __name__ , argv = [ sys . argv [ <NUM_LIT:0> ] ] , <EOL> testLoader = self . _loader , <EOL> testRunner = runner ) </s>
