<s> import os <EOL> from optparse import make_option <EOL> from django . conf import settings <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . test import TestCase <EOL> from django . test . utils import setup_test_environment , teardown_test_environment <EOL> from django . utils import unittest <EOL> from django . utils . unittest import TestSuite , defaultTestLoader <EOL> class DiscoverRunner ( object ) : <EOL> """<STR_LIT>""" <EOL> test_loader = defaultTestLoader <EOL> reorder_by = ( TestCase , ) <EOL> option_list = ( <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store>' , dest = '<STR_LIT>' , default = None , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> default = "<STR_LIT>" , <EOL> help = '<STR_LIT>' ) , <EOL> ) <EOL> def __init__ ( self , pattern = None , top_level = None , <EOL> verbosity = <NUM_LIT:1> , interactive = True , failfast = False , <EOL> ** kwargs ) : <EOL> self . pattern = pattern <EOL> self . top_level = top_level <EOL> self . verbosity = verbosity <EOL> self . interactive = interactive <EOL> self . failfast = failfast <EOL> def setup_test_environment ( self , ** kwargs ) : <EOL> setup_test_environment ( ) <EOL> settings . DEBUG = False <EOL> unittest . installHandler ( ) <EOL> def build_suite ( self , test_labels = None , extra_tests = None , ** kwargs ) : <EOL> suite = TestSuite ( ) <EOL> test_labels = test_labels or [ '<STR_LIT:.>' ] <EOL> extra_tests = extra_tests or [ ] <EOL> discover_kwargs = { } <EOL> if self . pattern is not None : <EOL> discover_kwargs [ '<STR_LIT>' ] = self . pattern <EOL> if self . top_level is not None : <EOL> discover_kwargs [ '<STR_LIT>' ] = self . top_level <EOL> for label in test_labels : <EOL> kwargs = discover_kwargs . copy ( ) <EOL> tests = None <EOL> label_as_path = os . path . abspath ( label ) <EOL> if not os . path . exists ( label_as_path ) : <EOL> tests = self . test_loader . loadTestsFromName ( label ) <EOL> elif os . path . isdir ( label_as_path ) and not self . top_level : <EOL> top_level = label_as_path <EOL> while True : <EOL> init_py = os . path . join ( top_level , '<STR_LIT>' ) <EOL> if os . path . exists ( init_py ) : <EOL> try_next = os . path . dirname ( top_level ) <EOL> if try_next == top_level : <EOL> break <EOL> top_level = try_next <EOL> continue <EOL> break <EOL> kwargs [ '<STR_LIT>' ] = top_level <EOL> if not ( tests and tests . countTestCases ( ) ) : <EOL> tests = self . test_loader . discover ( start_dir = label , ** kwargs ) <EOL> self . test_loader . _top_level_dir = None <EOL> suite . addTests ( tests ) <EOL> for test in extra_tests : <EOL> suite . addTest ( test ) <EOL> return reorder_suite ( suite , self . reorder_by ) <EOL> def setup_databases ( self , ** kwargs ) : <EOL> return setup_databases ( self . verbosity , self . interactive , ** kwargs ) <EOL> def run_suite ( self , suite , ** kwargs ) : <EOL> return unittest . TextTestRunner ( <EOL> verbosity = self . verbosity , <EOL> failfast = self . failfast , <EOL> ) . run ( suite ) <EOL> def teardown_databases ( self , old_config , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> old_names , mirrors = old_config <EOL> for connection , old_name , destroy in old_names : <EOL> if destroy : <EOL> connection . creation . destroy_test_db ( old_name , self . verbosity ) <EOL> def teardown_test_environment ( self , ** kwargs ) : <EOL> unittest . removeHandler ( ) <EOL> teardown_test_environment ( ) <EOL> def suite_result ( self , suite , result , ** kwargs ) : <EOL> return len ( result . failures ) + len ( result . errors ) <EOL> def run_tests ( self , test_labels , extra_tests = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . setup_test_environment ( ) <EOL> suite = self . build_suite ( test_labels , extra_tests ) <EOL> old_config = self . setup_databases ( ) <EOL> result = self . run_suite ( suite ) <EOL> self . teardown_databases ( old_config ) <EOL> self . teardown_test_environment ( ) <EOL> return self . suite_result ( suite , result ) <EOL> def dependency_ordered ( test_databases , dependencies ) : <EOL> """<STR_LIT>""" <EOL> ordered_test_databases = [ ] <EOL> resolved_databases = set ( ) <EOL> dependencies_map = { } <EOL> for sig , ( _ , aliases ) in test_databases : <EOL> all_deps = set ( ) <EOL> for alias in aliases : <EOL> all_deps . update ( dependencies . get ( alias , [ ] ) ) <EOL> if not all_deps . isdisjoint ( aliases ) : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % aliases ) <EOL> dependencies_map [ sig ] = all_deps <EOL> while test_databases : <EOL> changed = False <EOL> deferred = [ ] <EOL> for signature , ( db_name , aliases ) in test_databases : <EOL> if dependencies_map [ signature ] . issubset ( resolved_databases ) : <EOL> resolved_databases . update ( aliases ) <EOL> ordered_test_databases . append ( ( signature , ( db_name , aliases ) ) ) <EOL> changed = True <EOL> else : <EOL> deferred . append ( ( signature , ( db_name , aliases ) ) ) <EOL> if not changed : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" ) <EOL> test_databases = deferred <EOL> return ordered_test_databases <EOL> def reorder_suite ( suite , classes ) : <EOL> """<STR_LIT>""" <EOL> class_count = len ( classes ) <EOL> bins = [ unittest . TestSuite ( ) for i in range ( class_count + <NUM_LIT:1> ) ] <EOL> partition_suite ( suite , classes , bins ) <EOL> for i in range ( class_count ) : <EOL> bins [ <NUM_LIT:0> ] . addTests ( bins [ i + <NUM_LIT:1> ] ) <EOL> return bins [ <NUM_LIT:0> ] <EOL> def partition_suite ( suite , classes , bins ) : <EOL> """<STR_LIT>""" <EOL> for test in suite : <EOL> if isinstance ( test , unittest . TestSuite ) : <EOL> partition_suite ( test , classes , bins ) <EOL> else : <EOL> for i in range ( len ( classes ) ) : <EOL> if isinstance ( test , classes [ i ] ) : <EOL> bins [ i ] . addTest ( test ) <EOL> break <EOL> else : <EOL> bins [ - <NUM_LIT:1> ] . addTest ( test ) <EOL> def setup_databases ( verbosity , interactive , ** kwargs ) : <EOL> from django . db import connections , DEFAULT_DB_ALIAS <EOL> mirrored_aliases = { } <EOL> test_databases = { } <EOL> dependencies = { } <EOL> default_sig = connections [ DEFAULT_DB_ALIAS ] . creation . test_db_signature ( ) <EOL> for alias in connections : <EOL> connection = connections [ alias ] <EOL> if connection . settings_dict [ '<STR_LIT>' ] : <EOL> mirrored_aliases [ alias ] = ( <EOL> connection . settings_dict [ '<STR_LIT>' ] ) <EOL> else : <EOL> item = test_databases . setdefault ( <EOL> connection . creation . test_db_signature ( ) , <EOL> ( connection . settings_dict [ '<STR_LIT>' ] , set ( ) ) <EOL> ) <EOL> item [ <NUM_LIT:1> ] . add ( alias ) <EOL> if '<STR_LIT>' in connection . settings_dict : <EOL> dependencies [ alias ] = ( <EOL> connection . settings_dict [ '<STR_LIT>' ] ) <EOL> else : <EOL> if alias != DEFAULT_DB_ALIAS and connection . creation . test_db_signature ( ) != default_sig : <EOL> dependencies [ alias ] = connection . settings_dict . get ( <EOL> '<STR_LIT>' , [ DEFAULT_DB_ALIAS ] ) <EOL> old_names = [ ] <EOL> mirrors = [ ] <EOL> for signature , ( db_name , aliases ) in dependency_ordered ( <EOL> test_databases . items ( ) , dependencies ) : <EOL> test_db_name = None <EOL> for alias in aliases : <EOL> connection = connections [ alias ] <EOL> if test_db_name is None : <EOL> test_db_name = connection . creation . create_test_db ( <EOL> verbosity , autoclobber = not interactive ) <EOL> destroy = True <EOL> else : <EOL> connection . settings_dict [ '<STR_LIT>' ] = test_db_name <EOL> destroy = False <EOL> old_names . append ( ( connection , db_name , destroy ) ) <EOL> for alias , mirror_alias in mirrored_aliases . items ( ) : <EOL> mirrors . append ( ( alias , connections [ alias ] . settings_dict [ '<STR_LIT>' ] ) ) <EOL> connections [ alias ] . settings_dict [ '<STR_LIT>' ] = ( <EOL> connections [ mirror_alias ] . settings_dict [ '<STR_LIT>' ] ) <EOL> return old_names , mirrors </s>
