<s> import sys <EOL> from nose . plugins . base import Plugin <EOL> from nose . suite import ContextSuite <EOL> from django . test . testcases import TransactionTestCase , TestCase <EOL> from django_nose . testcases import FastFixtureTestCase <EOL> from django_nose . utils import process_tests , is_subclass_at_all <EOL> class AlwaysOnPlugin ( Plugin ) : <EOL> """<STR_LIT>""" <EOL> def options ( self , parser , env ) : <EOL> """<STR_LIT>""" <EOL> def configure ( self , * args , ** kw_args ) : <EOL> super ( AlwaysOnPlugin , self ) . configure ( * args , ** kw_args ) <EOL> self . enabled = True <EOL> class ResultPlugin ( AlwaysOnPlugin ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT:result>' <EOL> def finalize ( self , result ) : <EOL> self . result = result <EOL> class DjangoSetUpPlugin ( AlwaysOnPlugin ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> score = <NUM_LIT> <EOL> def __init__ ( self , runner ) : <EOL> super ( DjangoSetUpPlugin , self ) . __init__ ( ) <EOL> self . runner = runner <EOL> self . sys_stdout = sys . stdout <EOL> def prepareTest ( self , test ) : <EOL> """<STR_LIT>""" <EOL> sys_stdout = sys . stdout <EOL> sys . stdout = self . sys_stdout <EOL> self . runner . setup_test_environment ( ) <EOL> self . old_names = self . runner . setup_databases ( ) <EOL> sys . stdout = sys_stdout <EOL> def finalize ( self , result ) : <EOL> self . runner . teardown_databases ( self . old_names ) <EOL> self . runner . teardown_test_environment ( ) <EOL> class Bucketer ( object ) : <EOL> def __init__ ( self ) : <EOL> self . buckets = { } <EOL> self . remainder = [ ] <EOL> def add ( self , test ) : <EOL> """<STR_LIT>""" <EOL> if is_subclass_at_all ( test . context , FastFixtureTestCase ) : <EOL> key = ( frozenset ( getattr ( test . context , '<STR_LIT>' , [ ] ) ) , <EOL> getattr ( test . context , <EOL> '<STR_LIT>' , <EOL> False ) ) <EOL> self . buckets . setdefault ( key , [ ] ) . append ( test ) <EOL> else : <EOL> self . remainder . append ( test ) <EOL> class TestReorderer ( AlwaysOnPlugin ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> def options ( self , parser , env ) : <EOL> super ( TestReorderer , self ) . options ( parser , env ) <EOL> parser . add_option ( '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> dest = '<STR_LIT>' , <EOL> default = env . get ( '<STR_LIT>' , False ) , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def configure ( self , options , conf ) : <EOL> super ( TestReorderer , self ) . configure ( options , conf ) <EOL> self . should_bundle = options . with_fixture_bundling <EOL> def _put_transaction_test_cases_last ( self , test ) : <EOL> """<STR_LIT>""" <EOL> def filthiness ( test ) : <EOL> """<STR_LIT>""" <EOL> test_class = test . context <EOL> if ( is_subclass_at_all ( test_class , TestCase ) or <EOL> ( is_subclass_at_all ( test_class , TransactionTestCase ) and <EOL> getattr ( test_class , '<STR_LIT>' , False ) ) ) : <EOL> return <NUM_LIT:1> <EOL> return <NUM_LIT:2> <EOL> flattened = [ ] <EOL> process_tests ( test , flattened . append ) <EOL> flattened . sort ( key = filthiness ) <EOL> return ContextSuite ( flattened ) <EOL> def _bundle_fixtures ( self , test ) : <EOL> """<STR_LIT>""" <EOL> def suite_sorted_by_fixtures ( suite ) : <EOL> """<STR_LIT>""" <EOL> bucketer = Bucketer ( ) <EOL> process_tests ( suite , bucketer . add ) <EOL> flattened = [ ] <EOL> for ( ( fixtures , is_exempt ) , fixture_bundle ) in bucketer . buckets . items ( ) : <EOL> if fixtures and not is_exempt : <EOL> first = fixture_bundle [ <NUM_LIT:0> ] . context <EOL> first . _fb_should_setup_fixtures = True <EOL> for cls in fixture_bundle [ <NUM_LIT:1> : ] : <EOL> cls . context . _fb_should_setup_fixtures = False <EOL> last = fixture_bundle [ - <NUM_LIT:1> ] . context <EOL> last . _fb_should_teardown_fixtures = True <EOL> for cls in fixture_bundle [ : - <NUM_LIT:1> ] : <EOL> cls . context . _fb_should_teardown_fixtures = False <EOL> flattened . extend ( fixture_bundle ) <EOL> flattened . extend ( bucketer . remainder ) <EOL> return ContextSuite ( flattened ) <EOL> return suite_sorted_by_fixtures ( test ) <EOL> def prepareTest ( self , test ) : <EOL> """<STR_LIT>""" <EOL> test = self . _put_transaction_test_cases_last ( test ) <EOL> if self . should_bundle : <EOL> test = self . _bundle_fixtures ( test ) <EOL> return test </s>
