<s> from __future__ import print_function <EOL> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import sys <EOL> import os <EOL> from pycopia import scheduler <EOL> from pycopia import timelib <EOL> from pycopia import debugger <EOL> from pycopia import UserFile <EOL> from pycopia import dictlib <EOL> from pycopia import module <EOL> from pycopia import methods <EOL> from pycopia . QA import constants <EOL> from pycopia . db . config import ConfigError <EOL> class TestError ( AssertionError ) : <EOL> """<STR_LIT>""" <EOL> class TestFailError ( TestError ) : <EOL> """<STR_LIT>""" <EOL> class TestIncompleteError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class TestSuiteAbort ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class TestPrerequisiteError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class TestResult ( object ) : <EOL> def __init__ ( self , value ) : <EOL> self . _value = value <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % self . _value <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . _value ) <EOL> def __nonzero__ ( self ) : <EOL> return self . _value == constants . PASSED <EOL> def __int__ ( self ) : <EOL> return int ( self . _value ) <EOL> def is_passed ( self ) : <EOL> return self . _value == constants . PASSED <EOL> def not_passed ( self ) : <EOL> return self . _value in ( constants . FAILED , constants . EXPECTED_FAIL , <EOL> constants . INCOMPLETE , constants . ABORT ) <EOL> def is_failed ( self ) : <EOL> return self . _value == constants . FAILED <EOL> def is_incomplete ( self ) : <EOL> return self . _value == constants . INCOMPLETE <EOL> class TestOptions ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , initdict ) : <EOL> self . OPTIONS = dictlib . AttrDictDefault ( initdict , default = ( ) ) <EOL> def __get__ ( self , instance , owner ) : <EOL> return self . OPTIONS <EOL> def __set__ ( self , instance , value ) : <EOL> pass <EOL> def insert_options ( klass , ** kwargs ) : <EOL> if type ( klass ) is type and issubclass ( klass , Test ) : <EOL> if not klass . __dict__ . has_key ( "<STR_LIT>" ) : <EOL> klass . OPTIONS = TestOptions ( kwargs ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> class Test ( object ) : <EOL> """<STR_LIT>""" <EOL> OPTIONS = TestOptions ( { } ) <EOL> PREREQUISITES = [ ] <EOL> def __init__ ( self , config ) : <EOL> cl = self . __class__ <EOL> self . test_name = "<STR_LIT>" % ( cl . __module__ , cl . __name__ ) <EOL> self . config = config <EOL> self . _report = config . report <EOL> self . _debug = config . flags . DEBUG <EOL> self . _verbose = config . flags . VERBOSE <EOL> @ classmethod <EOL> def set_test_options ( cls ) : <EOL> insert_options ( cls ) <EOL> opts = cls . OPTIONS <EOL> pl = [ ] <EOL> for prereq in cls . PREREQUISITES : <EOL> if isinstance ( prereq , basestring ) : <EOL> pl . append ( PreReq ( prereq ) ) <EOL> elif type ( prereq ) is tuple : <EOL> pl . append ( PreReq ( * prereq ) ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> opts . prerequisites = pl <EOL> opts . bugid = None <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . config . register_testcase ( self . test_name ) <EOL> self . _report . add_heading ( self . test_name , <NUM_LIT:2> ) <EOL> if args or kwargs : <EOL> self . _report . add_message ( "<STR_LIT>" , repr_args ( args , kwargs ) , <NUM_LIT:2> ) <EOL> self . starttime = timelib . now ( ) <EOL> self . _initialize ( ) <EOL> teststarttime = timelib . now ( ) <EOL> try : <EOL> rv = self . execute ( * args , ** kwargs ) <EOL> except KeyboardInterrupt : <EOL> if self . _debug : <EOL> ex , val , tb = sys . exc_info ( ) <EOL> debugger . post_mortem ( tb , ex , val ) <EOL> rv = self . incomplete ( "<STR_LIT>" % self . test_name ) <EOL> self . _finalize ( rv ) <EOL> raise <EOL> except TestFailError , errval : <EOL> rv = self . failed ( "<STR_LIT>" % ( errval , ) ) <EOL> except TestIncompleteError , errval : <EOL> rv = self . incomplete ( "<STR_LIT>" % ( errval , ) ) <EOL> except AssertionError as errval : <EOL> rv = self . failed ( "<STR_LIT>" % ( errval , ) ) <EOL> except TestSuiteAbort : <EOL> self . config . register_testcase ( None ) <EOL> raise <EOL> except debugger . DebuggerQuit : <EOL> rv = self . incomplete ( "<STR_LIT>" % ( self . test_name , ) ) <EOL> except ConfigError as cerr : <EOL> rv = self . incomplete ( "<STR_LIT>" . format ( cerr ) ) <EOL> self . config . register_testcase ( None ) <EOL> if self . _debug : <EOL> ex , val , tb = sys . exc_info ( ) <EOL> debugger . post_mortem ( tb , ex , val ) <EOL> tb = None <EOL> raise TestSuiteAbort ( cerr ) <EOL> except : <EOL> ex , val , tb = sys . exc_info ( ) <EOL> if self . _debug : <EOL> debugger . post_mortem ( tb , ex , val ) <EOL> tb = None <EOL> rv = self . incomplete ( "<STR_LIT>" % ( self . test_name , ex , val ) ) <EOL> endtime = timelib . now ( ) <EOL> self . config . register_testcase ( None ) <EOL> self . _report . add_message ( "<STR_LIT>" , teststarttime , <NUM_LIT:2> ) <EOL> self . _report . add_message ( "<STR_LIT>" , endtime , <NUM_LIT:2> ) <EOL> minutes , seconds = divmod ( endtime - teststarttime , <NUM_LIT> ) <EOL> hours , minutes = divmod ( minutes , <NUM_LIT> ) <EOL> self . info ( "<STR_LIT>" % ( hours , minutes , seconds ) ) <EOL> return self . _finalize ( rv ) <EOL> def _initialize ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . initialize ( ) <EOL> except : <EOL> ex , val , tb = sys . exc_info ( ) <EOL> self . diagnostic ( "<STR_LIT>" % ( ex , val ) ) <EOL> if self . _debug : <EOL> debugger . post_mortem ( tb , ex , val ) <EOL> self . abort ( "<STR_LIT>" ) <EOL> def _finalize ( self , rv ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . finalize ( rv ) <EOL> except : <EOL> ex , val , tb = sys . exc_info ( ) <EOL> self . diagnostic ( "<STR_LIT>" % ( ex , val ) ) <EOL> if self . _debug : <EOL> debugger . post_mortem ( tb , ex , val ) <EOL> self . abort ( "<STR_LIT>" ) <EOL> return rv <EOL> def get_start_timestamp ( self ) : <EOL> return timelib . strftime ( "<STR_LIT>" , timelib . localtime ( self . starttime ) ) <EOL> def get_filename ( self , basename = None , ext = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> filename = "<STR_LIT>" % ( basename or self . test_name . replace ( "<STR_LIT:.>" , "<STR_LIT:_>" ) , <EOL> self . get_start_timestamp ( ) , ext ) <EOL> return os . path . join ( self . config . resultsdir , filename ) <EOL> def get_file ( self , basename = None , ext = "<STR_LIT>" , mode = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> fname = self . get_filename ( basename , ext ) <EOL> return UserFile . UserFile ( fname , mode ) <EOL> def sleep ( self , Nsecs ) : <EOL> """<STR_LIT>""" <EOL> return scheduler . sleep ( Nsecs ) <EOL> def schedule ( self , delay , cb ) : <EOL> """<STR_LIT>""" <EOL> return scheduler . add ( delay , callback = cb ) <EOL> def timed ( self , function , args = ( ) , kwargs = { } , timeout = <NUM_LIT:30> ) : <EOL> """<STR_LIT>""" <EOL> sched = scheduler . get_scheduler ( ) <EOL> return sched . timeout ( function , args , kwargs , timeout ) <EOL> def timedio ( self , function , args = ( ) , kwargs = { } , timeout = <NUM_LIT:30> ) : <EOL> """<STR_LIT>""" <EOL> sched = scheduler . get_scheduler ( ) <EOL> return sched . iotimeout ( function , args , kwargs , timeout ) <EOL> def run_subtest ( self , _testclass , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> orig = self . config . get_report ( ) <EOL> if not self . _verbose : <EOL> from pycopia import reports <EOL> nr = reports . get_report ( ( "<STR_LIT>" , ) ) <EOL> self . config . set_report ( nr ) <EOL> inst = _testclass ( self . config ) <EOL> try : <EOL> return inst ( * args , ** kwargs ) <EOL> finally : <EOL> self . config . set_report ( orig ) <EOL> def run_command ( self , cmdline , env = None , timeout = None , logfile = None ) : <EOL> """<STR_LIT>""" <EOL> from pycopia import proctools <EOL> p = proctools . spawnpipe ( cmdline , logfile = logfile , env = env ) <EOL> try : <EOL> if timeout : <EOL> sched = scheduler . get_scheduler ( ) <EOL> text = sched . iotimeout ( p . read , timeout = timeout ) <EOL> else : <EOL> text = p . read ( ) <EOL> finally : <EOL> p . wait ( ) <EOL> p . close ( ) <EOL> return p . exitstatus , text <EOL> def debug ( self ) : <EOL> """<STR_LIT>""" <EOL> debugger . set_trace ( start = <NUM_LIT:2> ) <EOL> def set_debug ( self , onoff = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> ov = self . _debug <EOL> self . _debug = self . config . flags . DEBUG = onoff <EOL> return ov <EOL> def set_verbose ( self , level = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> ov = self . _verbose <EOL> self . _verbose = self . config . flags . VERBOSE = level <EOL> return ov <EOL> verbose = property ( lambda s : s . _verbose , set_verbose ) <EOL> def _get_prerequisites ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . OPTIONS . prerequisites <EOL> prerequisites = property ( _get_prerequisites ) <EOL> def initialize ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def finalize ( self , result ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def execute ( self , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> return self . incomplete ( <EOL> '<STR_LIT>' ) <EOL> def passed ( self , msg = constants . NO_MESSAGE ) : <EOL> """<STR_LIT>""" <EOL> self . _report . passed ( msg , <NUM_LIT:2> ) <EOL> return TestResult ( constants . PASSED ) <EOL> def failed ( self , msg = constants . NO_MESSAGE ) : <EOL> """<STR_LIT>""" <EOL> if self . OPTIONS . bugid : <EOL> self . _report . diagnostic ( <EOL> "<STR_LIT>" % ( self . OPTIONS . bugid , ) , <NUM_LIT:2> ) <EOL> self . _report . expectedfail ( msg , <NUM_LIT:2> ) <EOL> return TestResult ( constants . EXPECTED_FAIL ) <EOL> else : <EOL> self . _report . failed ( msg , <NUM_LIT:2> ) <EOL> return TestResult ( constants . FAILED ) <EOL> def expectedfail ( self , msg = constants . NO_MESSAGE ) : <EOL> """<STR_LIT>""" <EOL> self . _report . expectedfail ( msg , <NUM_LIT:2> ) <EOL> return TestResult ( constants . EXPECTED_FAIL ) <EOL> def incomplete ( self , msg = constants . NO_MESSAGE ) : <EOL> """<STR_LIT>""" <EOL> self . _report . incomplete ( msg , <NUM_LIT:2> ) <EOL> return TestResult ( constants . INCOMPLETE ) <EOL> def abort ( self , msg = constants . NO_MESSAGE ) : <EOL> """<STR_LIT>""" <EOL> self . _report . abort ( msg , <NUM_LIT:2> ) <EOL> raise TestSuiteAbort ( msg ) <EOL> def info ( self , msg , level = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if level <= self . _verbose : <EOL> self . _report . info ( msg , <NUM_LIT:2> ) <EOL> def diagnostic ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> self . _report . diagnostic ( msg , <NUM_LIT:2> ) <EOL> def manual ( self ) : <EOL> """<STR_LIT>""" <EOL> UI = self . config . UI <EOL> UI . Print ( self . test_name ) <EOL> UI . write ( self . __class__ . __doc__ ) <EOL> UI . Print ( "<STR_LIT>" ) <EOL> completed = UI . yes_no ( "<STR_LIT>" ) <EOL> if completed : <EOL> passed = UI . yes_no ( "<STR_LIT>" ) <EOL> msg = UI . user_input ( "<STR_LIT>" if passed else "<STR_LIT>" ) <EOL> if passed : <EOL> return self . passed ( "<STR_LIT>" + msg ) <EOL> else : <EOL> if msg : <EOL> self . diagnostic ( msg ) <EOL> return self . failed ( "<STR_LIT>" ) <EOL> else : <EOL> msg = UI . user_input ( "<STR_LIT>" ) <EOL> return self . incomplete ( "<STR_LIT>" + msg ) <EOL> def report_build ( self , buildstring ) : <EOL> """<STR_LIT>""" <EOL> self . _report . add_message ( "<STR_LIT>" , buildstring , <NUM_LIT:1> ) <EOL> def assertPassed ( self , arg , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if int ( arg ) != constants . PASSED : <EOL> raise TestFailError , msg or "<STR_LIT>" <EOL> def assertFailed ( self , arg , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if int ( arg ) not in ( constants . FAILED , constants . EXPECTED_FAIL ) : <EOL> raise TestFailError , msg or "<STR_LIT>" <EOL> def assertEqual ( self , arg1 , arg2 , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if arg1 != arg2 : <EOL> raise TestFailError , msg or "<STR_LIT>" % ( arg1 , arg2 ) <EOL> def assertNotEqual ( self , arg1 , arg2 , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if arg1 == arg2 : <EOL> raise TestFailError , msg or "<STR_LIT>" % ( arg1 , arg2 ) <EOL> def assertGreaterThan ( self , arg1 , arg2 , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if not ( arg1 > arg2 ) : <EOL> raise TestFailError , msg or "<STR_LIT>" % ( arg1 , arg2 ) <EOL> def assertGreaterThanOrEqual ( self , arg1 , arg2 , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if not ( arg1 >= arg2 ) : <EOL> raise TestFailError , msg or "<STR_LIT>" % ( arg1 , arg2 ) <EOL> def assertLessThan ( self , arg1 , arg2 , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if not ( arg1 < arg2 ) : <EOL> raise TestFailError , msg or "<STR_LIT>" % ( arg1 , arg2 ) <EOL> def assertLessThanOrEqual ( self , arg1 , arg2 , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if not ( arg1 <= arg2 ) : <EOL> raise TestFailError , msg or "<STR_LIT>" % ( arg1 , arg2 ) <EOL> def assertTrue ( self , arg , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if not arg : <EOL> raise TestFailError , msg or "<STR_LIT>" % ( arg , ) <EOL> failUnless = assertTrue <EOL> def assertFalse ( self , arg , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if arg : <EOL> raise TestFailError , msg or "<STR_LIT>" % ( arg , ) <EOL> def assertApproximatelyEqual ( self , arg1 , arg2 , fudge = None , msg = None ) : <EOL> """<STR_LIT>""" <EOL> if fudge is None : <EOL> fudge = arg1 * <NUM_LIT> <EOL> if abs ( arg1 - arg2 ) > fudge : <EOL> raise TestFailError , msg or "<STR_LIT>" % ( arg1 , arg2 , fudge ) <EOL> def assertRaises ( self , exception , method , args = None , kwargs = None , msg = None ) : <EOL> """<STR_LIT>""" <EOL> args = args or ( ) <EOL> kwargs = kwargs or { } <EOL> try : <EOL> rv = method ( * args , ** kwargs ) <EOL> except exception : <EOL> return <EOL> raise TestFailError , msg or "<STR_LIT>" % ( method , exception ) <EOL> failIfEqual = assertNotEqual <EOL> failIfNotEqual = assertEqual <EOL> assertNotTrue = assertFalse <EOL> assertNotFalse = assertTrue <EOL> failUnlessRaises = assertRaises <EOL> def save_text ( self , text , filename = None ) : <EOL> """<STR_LIT>""" <EOL> if filename is None : <EOL> filename = self . get_filename ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> fo = UserFile . UserFile ( filename , "<STR_LIT:a>" ) <EOL> try : <EOL> fo . write ( str ( text ) ) <EOL> finally : <EOL> fo . close ( ) <EOL> @ classmethod <EOL> def open_data_file ( cls , fname ) : <EOL> """<STR_LIT>""" <EOL> fullname = os . path . join ( <EOL> os . path . dirname ( sys . modules [ cls . __module__ ] . __file__ ) , fname ) <EOL> return UserFile . UserFile ( fullname ) <EOL> def save_data ( self , data , note = None ) : <EOL> """<STR_LIT>""" <EOL> self . _report . add_data ( data , note ) <EOL> class PreReq ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , implementation , args = None , kwargs = None ) : <EOL> self . implementation = str ( implementation ) <EOL> self . args = args or ( ) <EOL> self . kwargs = kwargs or { } <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . implementation , <EOL> self . args , self . kwargs ) <EOL> def __str__ ( self ) : <EOL> return repr_test ( self . implementation , self . args , self . kwargs ) <EOL> class TestEntry ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , inst , args = None , kwargs = None , autoadded = False ) : <EOL> self . inst = inst <EOL> self . args = args or ( ) <EOL> self . kwargs = kwargs or { } <EOL> self . _result = TestResult ( constants . INCOMPLETE ) <EOL> self . autoadded = autoadded <EOL> def run ( self , config = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _result = self . inst ( * self . args , ** self . kwargs ) <EOL> except KeyboardInterrupt : <EOL> self . _result = TestResult ( constants . ABORT ) <EOL> raise <EOL> return self . _result <EOL> def __eq__ ( self , other ) : <EOL> return self . inst == other . inst <EOL> def _setResult ( self , val ) : <EOL> self . _result = val <EOL> result = property ( lambda s : s . _result , _setResult , <EOL> doc = "<STR_LIT>" ) <EOL> def match_test ( self , name , args , kwargs ) : <EOL> """<STR_LIT>""" <EOL> return ( name , args , kwargs ) == ( self . inst . test_name , self . args , self . kwargs ) <EOL> def match_prerequisite ( self , prereq ) : <EOL> """<STR_LIT>""" <EOL> return ( self . inst . test_name , self . args , self . kwargs ) == ( prereq . implementation , prereq . args , prereq . kwargs ) <EOL> def _get_prerequisites ( self ) : <EOL> return self . inst . prerequisites <EOL> prerequisites = property ( _get_prerequisites ) <EOL> def get_signature ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _signature <EOL> except AttributeError : <EOL> arg_sig = repr ( ( self . args , self . kwargs ) ) <EOL> self . _signature = ( id ( self . inst . __class__ ) , arg_sig ) <EOL> return self . _signature <EOL> signature = property ( get_signature , doc = "<STR_LIT>" ) <EOL> def abort ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _result = self . inst . abort ( "<STR_LIT>" ) <EOL> return self . _result <EOL> test_name = property ( lambda s : s . inst . test_name ) <EOL> def __repr__ ( self ) : <EOL> return repr_test ( self . inst . test_name , self . args , self . kwargs ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __repr__ ( ) , self . _result ) <EOL> class SuiteEntry ( TestEntry ) : <EOL> """<STR_LIT>""" <EOL> def _get_result ( self ) : <EOL> self . _results = self . inst . results <EOL> for res in self . _results : <EOL> if res . not_passed ( ) : <EOL> self . _result = res <EOL> return res <EOL> self . _result = TestResult ( constants . PASSED ) <EOL> return TestResult ( constants . PASSED ) <EOL> def _setResult ( self , val ) : <EOL> self . _result = val <EOL> result = property ( lambda s : s . _get_result ( ) , <EOL> _setResult , None , <EOL> """<STR_LIT>""" ) <EOL> results = property ( lambda s : s . _results , None , None , <EOL> """<STR_LIT>""" ) <EOL> def PruneEnd ( n , l ) : <EOL> return l [ : n ] <EOL> class TestEntrySeries ( TestEntry ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , testinstance , N , chooser , filter , args , kwargs ) : <EOL> from pycopia import combinatorics <EOL> self . inst = testinstance <EOL> self . args = args or ( ) <EOL> self . kwargs = kwargs or { } <EOL> self . _sig = methods . MethodSignature ( testinstance . execute ) <EOL> self . result = TestResult ( constants . INCOMPLETE ) <EOL> chooser = chooser or PruneEnd <EOL> arglist = [ ] <EOL> if args : <EOL> arglist . extend ( args ) <EOL> if kwargs : <EOL> for name , default in self . _sig . kwarguments : <EOL> try : <EOL> val = kwargs [ name ] <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> arglist . append ( val ) <EOL> self . _counter = combinatorics . ListCounter ( combinatorics . prune ( N , arglist , chooser ) ) <EOL> if filter : <EOL> assert callable ( filter ) <EOL> self . _filter = filter <EOL> else : <EOL> self . _filter = lambda * args , ** kwargs : True <EOL> test_name = property ( lambda s : s . inst . test_name ) <EOL> def match_prerequisite ( self , prereq ) : <EOL> """<STR_LIT>""" <EOL> return self . inst . test_name == prereq . implementation <EOL> def run ( self , config = None ) : <EOL> resultset = { constants . PASSED : <NUM_LIT:0> , constants . FAILED : <NUM_LIT:0> , <EOL> constants . EXPECTED_FAIL : <NUM_LIT:0> , constants . INCOMPLETE : <NUM_LIT:0> } <EOL> for argset in self . _counter : <EOL> kwargs = self . _sig . get_keyword_arguments ( argset ) <EOL> if self . _filter ( ** kwargs ) : <EOL> entry = TestEntry ( self . inst , ( ) , kwargs ) <EOL> entryresult = entry . run ( ) <EOL> resultset [ int ( entryresult ) ] += <NUM_LIT:1> <EOL> if resultset [ constants . FAILED ] > <NUM_LIT:0> : <EOL> self . result = TestResult ( constants . FAILED ) <EOL> elif resultset [ constants . INCOMPLETE ] > <NUM_LIT:0> : <EOL> self . result = TestResult ( constants . INCOMPLETE ) <EOL> elif resultset [ constants . PASSED ] > <NUM_LIT:0> : <EOL> self . result = TestResult ( constants . PASSED ) <EOL> return self . result <EOL> def repr_test ( name , args , kwargs ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % ( name , repr_args ( args , kwargs ) ) <EOL> def repr_args ( args , kwargs ) : <EOL> """<STR_LIT>""" <EOL> args_s = ( ( "<STR_LIT>" if kwargs else "<STR_LIT:%s>" ) % "<STR_LIT:U+002CU+0020>" . join ( map ( repr , args ) ) ) if args else "<STR_LIT>" <EOL> kws = "<STR_LIT:U+002CU+0020>" . join ( map ( lambda it : "<STR_LIT>" % ( it [ <NUM_LIT:0> ] , it [ <NUM_LIT:1> ] ) , kwargs . items ( ) ) ) <EOL> return "<STR_LIT>" % ( args_s , kws ) <EOL> def parse_args ( arguments ) : <EOL> """<STR_LIT>""" <EOL> ANY = None <EOL> def _ArgGetter ( * args , ** kwargs ) : <EOL> return args , kwargs <EOL> funcstr = "<STR_LIT>" % arguments <EOL> exec funcstr in locals ( ) <EOL> return args , kwargs <EOL> def timestamp ( t ) : <EOL> """<STR_LIT>""" <EOL> return timelib . strftime ( "<STR_LIT>" , timelib . localtime ( t ) ) <EOL> class TestSuite ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cf , nested = <NUM_LIT:0> , name = None ) : <EOL> self . config = cf <EOL> self . report = cf . report <EOL> self . _debug = cf . flags . DEBUG <EOL> self . _tests = [ ] <EOL> self . _testset = set ( ) <EOL> self . _multitestset = set ( ) <EOL> self . _nested = nested <EOL> cl = self . __class__ <EOL> self . test_name = name or "<STR_LIT>" % ( cl . __module__ , cl . __name__ ) <EOL> self . result = None <EOL> def __iter__ ( self ) : <EOL> return iter ( self . _tests ) <EOL> def _get_results ( self ) : <EOL> return map ( lambda t : t . result , self . _tests ) <EOL> results = property ( _get_results ) <EOL> def _add_with_prereq ( self , entry , _auto = False ) : <EOL> """<STR_LIT>""" <EOL> if self . _debug < <NUM_LIT:3> : <EOL> for prereq in entry . inst . OPTIONS . prerequisites : <EOL> impl = prereq . implementation <EOL> if "<STR_LIT:.>" not in impl : <EOL> impl = sys . modules [ entry . inst . __class__ . __module__ ] . __name__ + "<STR_LIT:.>" + impl <EOL> prereq . implementation = impl <EOL> pretestclass = module . get_object ( impl ) <EOL> pretestclass . set_test_options ( ) <EOL> preentry = TestEntry ( pretestclass ( self . config ) , prereq . args , prereq . kwargs , True ) <EOL> presig , argsig = preentry . get_signature ( ) <EOL> if presig not in self . _multitestset : <EOL> self . _add_with_prereq ( preentry , True ) <EOL> testcaseid = entry . get_signature ( ) <EOL> if not _auto : <EOL> self . _tests . append ( entry ) <EOL> elif testcaseid not in self . _testset : <EOL> self . _tests . append ( entry ) <EOL> self . _testset . add ( testcaseid ) <EOL> def add_test ( self , _testclass , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( _testclass , str ) : <EOL> _testclass = module . get_class ( _testclass ) <EOL> _testclass . set_test_options ( ) <EOL> testinstance = _testclass ( self . config ) <EOL> entry = TestEntry ( testinstance , args , kwargs , False ) <EOL> self . _add_with_prereq ( entry ) <EOL> def add_tests ( self , _testclasslist , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( _testclasslist , list ) <EOL> for testclass in _testclasslist : <EOL> if type ( testclass ) is tuple : <EOL> self . add_test ( * testclass ) <EOL> else : <EOL> self . add_test ( testclass , * args , ** kwargs ) <EOL> def add_test_from_result ( self , dbtestresult ) : <EOL> """<STR_LIT>""" <EOL> testclass = module . get_class ( dbtestresult . testimplementation ) <EOL> testclass . set_test_options ( ) <EOL> args , kwargs = parse_args ( dbtestresult . arguments ) <EOL> testinstance = testclass ( self . config ) <EOL> entry = TestEntry ( testinstance , args , kwargs , False ) <EOL> self . _add_with_prereq ( entry ) <EOL> def add_test_series ( self , _testclass , N = <NUM_LIT:100> , chooser = None , filter = None , <EOL> args = None , kwargs = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( _testclass , str ) : <EOL> _testclass = module . get_class ( _testclass ) <EOL> _testclass . set_test_options ( ) <EOL> testinstance = _testclass ( self . config ) <EOL> try : <EOL> entry = TestEntrySeries ( testinstance , N , chooser , filter , args , kwargs ) <EOL> except ValueError , err : <EOL> self . info ( "<STR_LIT>" % ( <EOL> err , _testclass . __name__ ) ) <EOL> else : <EOL> mysig , myargsig = entry . get_signature ( ) <EOL> self . _multitestset . add ( mysig ) <EOL> self . _add_with_prereq ( entry ) <EOL> def add_suite ( self , suite , test_name = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( suite , str ) : <EOL> suite = module . get_class ( suite ) <EOL> if type ( suite ) is type ( Test ) : <EOL> suite = suite ( self . config , <NUM_LIT:1> ) <EOL> else : <EOL> suite . config = self . config <EOL> suite . _nested = <NUM_LIT:1> <EOL> self . _tests . append ( SuiteEntry ( suite ) ) <EOL> if test_name : <EOL> suite . test_name = test_name <EOL> else : <EOL> suite . test_name = "<STR_LIT>" % ( suite . test_name , len ( self . _tests ) - <NUM_LIT:1> ) <EOL> return suite <EOL> def add ( self , klass , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if type ( klass ) is type : <EOL> if issubclass ( klass , Test ) : <EOL> self . addTest ( klass , * args , ** kwargs ) <EOL> elif issubclass ( klass , TestSuite ) : <EOL> self . add_suite ( klass , * args , ** kwargs ) <EOL> else : <EOL> raise ValueError , "<STR_LIT>" <EOL> else : <EOL> raise ValueError , "<STR_LIT>" <EOL> def get_test_entries ( self , name , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> for entry in self . _tests : <EOL> if entry . matches ( name , args , kwargs ) : <EOL> yield entry <EOL> def add_arguments ( self , name , args , kwargs ) : <EOL> """<STR_LIT>""" <EOL> for entry in self . get_test_entries ( name ) : <EOL> entry . add_arguments ( args , kwargs ) <EOL> def info ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> self . report . info ( msg , <NUM_LIT:1> ) <EOL> def report_build ( self , buildstring ) : <EOL> """<STR_LIT>""" <EOL> self . report . add_message ( "<STR_LIT>" , buildstring , <NUM_LIT:1> ) <EOL> def _get_prerequisites ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( ) <EOL> prerequisites = property ( _get_prerequisites ) <EOL> def run ( self , config = None ) : <EOL> """<STR_LIT>""" <EOL> if config is not None : <EOL> self . config = config <EOL> self . report = config . report <EOL> self . _debug = config . flags . DEBUG <EOL> return self . __call__ ( ) <EOL> def get_start_timestamp ( self ) : <EOL> return timelib . strftime ( "<STR_LIT>" , timelib . localtime ( self . starttime ) ) <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . config . register_testsuite ( self . test_name ) <EOL> self . starttime = timelib . now ( ) <EOL> try : <EOL> self . _initialize ( * args , ** kwargs ) <EOL> except TestSuiteAbort : <EOL> self . _finalize ( ) <EOL> rv = constants . INCOMPLETE <EOL> else : <EOL> self . _run_tests ( ) <EOL> rv = self . _finalize ( ) <EOL> endtime = timelib . now ( ) <EOL> self . config . register_testsuite ( None ) <EOL> self . report . add_message ( "<STR_LIT>" , self . starttime , <NUM_LIT:1> ) <EOL> self . report . add_message ( "<STR_LIT>" , endtime , <NUM_LIT:1> ) <EOL> return rv <EOL> def _initialize ( self , * args , ** kwargs ) : <EOL> self . report . add_heading ( self . test_name , <NUM_LIT:1> ) <EOL> if self . config . flags . VERBOSE : <EOL> s = [ "<STR_LIT>" ] <EOL> for i , entry in enumerate ( self . _tests ) : <EOL> s . append ( "<STR_LIT>" % ( i + <NUM_LIT:1> , entry ) ) <EOL> self . report . info ( "<STR_LIT:\n>" . join ( s ) , <NUM_LIT:1> ) <EOL> del s <EOL> try : <EOL> self . initialize ( * args , ** kwargs ) <EOL> except KeyboardInterrupt : <EOL> self . info ( "<STR_LIT>" ) <EOL> raise TestSuiteAbort ( "<STR_LIT>" ) <EOL> except : <EOL> ex , val , tb = sys . exc_info ( ) <EOL> if self . _debug : <EOL> ex , val , tb = sys . exc_info ( ) <EOL> debugger . post_mortem ( tb , ex , val ) <EOL> self . info ( "<STR_LIT>" % ( ex , val ) ) <EOL> raise TestSuiteAbort ( val ) <EOL> def check_prerequisites ( self , currententry , upto ) : <EOL> """<STR_LIT>""" <EOL> for prereq in currententry . prerequisites : <EOL> for entry in self . _tests [ : upto ] : <EOL> if entry . match_prerequisite ( prereq ) : <EOL> if entry . result . is_passed ( ) : <EOL> continue <EOL> else : <EOL> self . report . add_heading ( currententry . inst . test_name , <NUM_LIT:2> ) <EOL> self . report . diagnostic ( "<STR_LIT>" % ( prereq , ) , <NUM_LIT:2> ) <EOL> self . report . incomplete ( "<STR_LIT>" , <NUM_LIT:2> ) <EOL> currententry . result = TestResult ( constants . INCOMPLETE ) <EOL> return False <EOL> return True <EOL> def _run_tests ( self ) : <EOL> for i , entry in enumerate ( self . _tests ) : <EOL> if self . _debug < <NUM_LIT:2> and not self . check_prerequisites ( entry , i ) : <EOL> continue <EOL> if self . config . flags . VERBOSE : <EOL> self . config . logfile . note ( "<STR_LIT>" % ( timelib . localtimestamp ( ) , entry ) ) <EOL> try : <EOL> rv = entry . run ( ) <EOL> except KeyboardInterrupt : <EOL> if self . _nested : <EOL> raise TestSuiteAbort ( "<STR_LIT>" ) <EOL> else : <EOL> if self . config . UI . yes_no ( "<STR_LIT>" ) : <EOL> self . info ( "<STR_LIT>" ) <EOL> break <EOL> except TestSuiteAbort , err : <EOL> self . info ( "<STR_LIT>" % ( entry . test_name , err ) ) <EOL> entry . result = TestResult ( constants . INCOMPLETE ) <EOL> rv = constants . ABORT <EOL> break <EOL> if rv is None : <EOL> self . report . diagnostic ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( entry . test_name ) ) <EOL> rv = constants . INCOMPLETE <EOL> if rv == constants . ABORT : <EOL> break <EOL> def _finalize ( self ) : <EOL> try : <EOL> self . finalize ( ) <EOL> except KeyboardInterrupt : <EOL> if self . _nested : <EOL> raise TestSuiteAbort ( <EOL> "<STR_LIT>" . format ( self . test_name ) ) <EOL> else : <EOL> self . info ( "<STR_LIT>" ) <EOL> except : <EOL> ex , val , tb = sys . exc_info ( ) <EOL> if self . _debug : <EOL> print <EOL> debugger . post_mortem ( tb , ex , val ) <EOL> self . info ( "<STR_LIT>" % ( ex , val ) ) <EOL> if self . _nested : <EOL> raise TestSuiteAbort ( <EOL> "<STR_LIT>" . format ( self . test_name ) ) <EOL> self . _summarize ( ) <EOL> self . _report_summary ( ) <EOL> return self . result <EOL> def _summarize ( self ) : <EOL> """<STR_LIT>""" <EOL> resultset = { <EOL> constants . FAILED : <NUM_LIT:0> , <EOL> constants . PASSED : <NUM_LIT:0> , <EOL> constants . INCOMPLETE : <NUM_LIT:0> , <EOL> } <EOL> for entry in self . _tests : <EOL> if entry . result is None : <EOL> resultset [ constants . INCOMPLETE ] += <NUM_LIT:1> <EOL> elif entry . result . is_failed ( ) : <EOL> resultset [ constants . FAILED ] += <NUM_LIT:1> <EOL> elif entry . result . is_incomplete ( ) : <EOL> resultset [ constants . INCOMPLETE ] += <NUM_LIT:1> <EOL> elif entry . result . is_passed ( ) : <EOL> resultset [ constants . PASSED ] += <NUM_LIT:1> <EOL> if resultset [ constants . FAILED ] > <NUM_LIT:0> : <EOL> result = constants . FAILED <EOL> elif resultset [ constants . INCOMPLETE ] > <NUM_LIT:0> : <EOL> result = constants . INCOMPLETE <EOL> elif resultset [ constants . PASSED ] == <NUM_LIT:0> : <EOL> result = constants . INCOMPLETE <EOL> else : <EOL> result = constants . PASSED <EOL> self . result = TestResult ( result ) <EOL> def _report_summary ( self ) : <EOL> """<STR_LIT>""" <EOL> self . report . add_heading ( "<STR_LIT>" % self . __class__ . __name__ , <NUM_LIT:3> ) <EOL> entries = filter ( lambda te : te . result is not None , self . _tests ) <EOL> self . report . add_summary ( entries ) <EOL> resultmsg = "<STR_LIT>" % ( self . test_name , ) <EOL> result = int ( self . result ) <EOL> if not self . _nested : <EOL> if result == constants . PASSED : <EOL> self . report . passed ( resultmsg ) <EOL> elif result == constants . FAILED : <EOL> self . report . failed ( resultmsg ) <EOL> elif result == constants . INCOMPLETE : <EOL> self . report . incomplete ( resultmsg ) <EOL> def __str__ ( self ) : <EOL> s = [ "<STR_LIT>" ] <EOL> s . extend ( map ( str , self . _tests ) ) <EOL> return "<STR_LIT:\n>" . join ( s ) <EOL> def initialize ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def finalize ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class UseCase ( object ) : <EOL> """<STR_LIT>""" <EOL> @ staticmethod <EOL> def get_suite ( config ) : <EOL> return TestSuite ( config , name = "<STR_LIT>" ) <EOL> @ classmethod <EOL> def run ( cls , config ) : <EOL> suite = cls . get_suite ( config ) <EOL> suite . run ( ) </s>
