<s> import os <EOL> import re <EOL> import sys <EOL> import unittest <EOL> class TestDiscovery ( unittest . TestCase ) : <EOL> def test_get_name_from_path ( self ) : <EOL> loader = unittest . TestLoader ( ) <EOL> loader . _top_level_dir = '<STR_LIT>' <EOL> name = loader . _get_name_from_path ( '<STR_LIT>' ) <EOL> self . assertEqual ( name , '<STR_LIT>' ) <EOL> if not __debug__ : <EOL> return <EOL> with self . assertRaises ( AssertionError ) : <EOL> loader . _get_name_from_path ( '<STR_LIT>' ) <EOL> def test_find_tests ( self ) : <EOL> loader = unittest . TestLoader ( ) <EOL> original_listdir = os . listdir <EOL> def restore_listdir ( ) : <EOL> os . listdir = original_listdir <EOL> original_isfile = os . path . isfile <EOL> def restore_isfile ( ) : <EOL> os . path . isfile = original_isfile <EOL> original_isdir = os . path . isdir <EOL> def restore_isdir ( ) : <EOL> os . path . isdir = original_isdir <EOL> path_lists = [ [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , ] ] <EOL> os . listdir = lambda path : path_lists . pop ( <NUM_LIT:0> ) <EOL> self . addCleanup ( restore_listdir ) <EOL> def isdir ( path ) : <EOL> return path . endswith ( '<STR_LIT>' ) <EOL> os . path . isdir = isdir <EOL> self . addCleanup ( restore_isdir ) <EOL> def isfile ( path ) : <EOL> return not path . endswith ( '<STR_LIT>' ) and not '<STR_LIT>' in path <EOL> os . path . isfile = isfile <EOL> self . addCleanup ( restore_isfile ) <EOL> loader . _get_module_from_name = lambda path : path + '<STR_LIT>' <EOL> loader . loadTestsFromModule = lambda module : module + '<STR_LIT>' <EOL> top_level = os . path . abspath ( '<STR_LIT>' ) <EOL> loader . _top_level_dir = top_level <EOL> suite = list ( loader . _find_tests ( top_level , '<STR_LIT>' ) ) <EOL> expected = [ name + '<STR_LIT>' for name in <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> expected . extend ( [ ( '<STR_LIT>' % name ) + '<STR_LIT>' for name in <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( suite , expected ) <EOL> def test_find_tests_with_package ( self ) : <EOL> loader = unittest . TestLoader ( ) <EOL> original_listdir = os . listdir <EOL> def restore_listdir ( ) : <EOL> os . listdir = original_listdir <EOL> original_isfile = os . path . isfile <EOL> def restore_isfile ( ) : <EOL> os . path . isfile = original_isfile <EOL> original_isdir = os . path . isdir <EOL> def restore_isdir ( ) : <EOL> os . path . isdir = original_isdir <EOL> directories = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> path_lists = [ directories , [ ] , [ ] , [ ] ] <EOL> os . listdir = lambda path : path_lists . pop ( <NUM_LIT:0> ) <EOL> self . addCleanup ( restore_listdir ) <EOL> os . path . isdir = lambda path : True <EOL> self . addCleanup ( restore_isdir ) <EOL> os . path . isfile = lambda path : os . path . basename ( path ) not in directories <EOL> self . addCleanup ( restore_isfile ) <EOL> class Module ( object ) : <EOL> paths = [ ] <EOL> load_tests_args = [ ] <EOL> def __init__ ( self , path ) : <EOL> self . path = path <EOL> self . paths . append ( path ) <EOL> if os . path . basename ( path ) == '<STR_LIT>' : <EOL> def load_tests ( loader , tests , pattern ) : <EOL> self . load_tests_args . append ( ( loader , tests , pattern ) ) <EOL> return '<STR_LIT>' <EOL> self . load_tests = load_tests <EOL> def __eq__ ( self , other ) : <EOL> return self . path == other . path <EOL> __hash__ = None <EOL> loader . _get_module_from_name = lambda name : Module ( name ) <EOL> def loadTestsFromModule ( module , use_load_tests ) : <EOL> if use_load_tests : <EOL> raise self . failureException ( '<STR_LIT>' ) <EOL> return module . path + '<STR_LIT>' <EOL> loader . loadTestsFromModule = loadTestsFromModule <EOL> loader . _top_level_dir = '<STR_LIT>' <EOL> suite = list ( loader . _find_tests ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . assertEqual ( suite , <EOL> [ '<STR_LIT>' , '<STR_LIT>' + '<STR_LIT>' ] ) <EOL> self . assertEqual ( Module . paths , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( Module . load_tests_args , <EOL> [ ( loader , '<STR_LIT>' + '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> def test_discover ( self ) : <EOL> loader = unittest . TestLoader ( ) <EOL> original_isfile = os . path . isfile <EOL> original_isdir = os . path . isdir <EOL> def restore_isfile ( ) : <EOL> os . path . isfile = original_isfile <EOL> os . path . isfile = lambda path : False <EOL> self . addCleanup ( restore_isfile ) <EOL> orig_sys_path = sys . path [ : ] <EOL> def restore_path ( ) : <EOL> sys . path [ : ] = orig_sys_path <EOL> self . addCleanup ( restore_path ) <EOL> full_path = os . path . abspath ( os . path . normpath ( '<STR_LIT>' ) ) <EOL> with self . assertRaises ( ImportError ) : <EOL> loader . discover ( '<STR_LIT>' , top_level_dir = '<STR_LIT>' ) <EOL> self . assertEqual ( loader . _top_level_dir , full_path ) <EOL> self . assertIn ( full_path , sys . path ) <EOL> os . path . isfile = lambda path : True <EOL> os . path . isdir = lambda path : True <EOL> def restore_isdir ( ) : <EOL> os . path . isdir = original_isdir <EOL> self . addCleanup ( restore_isdir ) <EOL> _find_tests_args = [ ] <EOL> def _find_tests ( start_dir , pattern ) : <EOL> _find_tests_args . append ( ( start_dir , pattern ) ) <EOL> return [ '<STR_LIT>' ] <EOL> loader . _find_tests = _find_tests <EOL> loader . suiteClass = str <EOL> suite = loader . discover ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> top_level_dir = os . path . abspath ( '<STR_LIT>' ) <EOL> start_dir = os . path . abspath ( '<STR_LIT>' ) <EOL> self . assertEqual ( suite , "<STR_LIT>" ) <EOL> self . assertEqual ( loader . _top_level_dir , top_level_dir ) <EOL> self . assertEqual ( _find_tests_args , [ ( start_dir , '<STR_LIT>' ) ] ) <EOL> self . assertIn ( top_level_dir , sys . path ) <EOL> def test_discover_with_modules_that_fail_to_import ( self ) : <EOL> loader = unittest . TestLoader ( ) <EOL> listdir = os . listdir <EOL> os . listdir = lambda _ : [ '<STR_LIT>' ] <EOL> isfile = os . path . isfile <EOL> os . path . isfile = lambda _ : True <EOL> orig_sys_path = sys . path [ : ] <EOL> def restore ( ) : <EOL> os . path . isfile = isfile <EOL> os . listdir = listdir <EOL> sys . path [ : ] = orig_sys_path <EOL> self . addCleanup ( restore ) <EOL> suite = loader . discover ( '<STR_LIT:.>' ) <EOL> self . assertIn ( os . getcwd ( ) , sys . path ) <EOL> self . assertEqual ( suite . countTestCases ( ) , <NUM_LIT:1> ) <EOL> test = list ( list ( suite ) [ <NUM_LIT:0> ] ) [ <NUM_LIT:0> ] <EOL> with self . assertRaises ( ImportError ) : <EOL> test . test_this_does_not_exist ( ) <EOL> def test_command_line_handling_parseArgs ( self ) : <EOL> program = object . __new__ ( unittest . TestProgram ) <EOL> args = [ ] <EOL> def do_discovery ( argv ) : <EOL> args . extend ( argv ) <EOL> program . _do_discovery = do_discovery <EOL> program . parseArgs ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( args , [ ] ) <EOL> program . parseArgs ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:foo>' , '<STR_LIT:bar>' ] ) <EOL> self . assertEqual ( args , [ '<STR_LIT:foo>' , '<STR_LIT:bar>' ] ) <EOL> def test_command_line_handling_do_discovery_too_many_arguments ( self ) : <EOL> class Stop ( Exception ) : <EOL> pass <EOL> def usageExit ( ) : <EOL> raise Stop <EOL> program = object . __new__ ( unittest . TestProgram ) <EOL> program . usageExit = usageExit <EOL> with self . assertRaises ( Stop ) : <EOL> program . _do_discovery ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_command_line_handling_do_discovery_calls_loader ( self ) : <EOL> program = object . __new__ ( unittest . TestProgram ) <EOL> class Loader ( object ) : <EOL> args = [ ] <EOL> def discover ( self , start_dir , pattern , top_level_dir ) : <EOL> self . args . append ( ( start_dir , pattern , top_level_dir ) ) <EOL> return '<STR_LIT>' <EOL> program . _do_discovery ( [ '<STR_LIT>' ] , Loader = Loader ) <EOL> self . assertEqual ( program . verbosity , <NUM_LIT:2> ) <EOL> self . assertEqual ( program . test , '<STR_LIT>' ) <EOL> self . assertEqual ( Loader . args , [ ( '<STR_LIT:.>' , '<STR_LIT>' , None ) ] ) <EOL> Loader . args = [ ] <EOL> program = object . __new__ ( unittest . TestProgram ) <EOL> program . _do_discovery ( [ '<STR_LIT>' ] , Loader = Loader ) <EOL> self . assertEqual ( program . test , '<STR_LIT>' ) <EOL> self . assertEqual ( Loader . args , [ ( '<STR_LIT:.>' , '<STR_LIT>' , None ) ] ) <EOL> Loader . args = [ ] <EOL> program = object . __new__ ( unittest . TestProgram ) <EOL> program . _do_discovery ( [ ] , Loader = Loader ) <EOL> self . assertEqual ( program . test , '<STR_LIT>' ) <EOL> self . assertEqual ( Loader . args , [ ( '<STR_LIT:.>' , '<STR_LIT>' , None ) ] ) <EOL> Loader . args = [ ] <EOL> program = object . __new__ ( unittest . TestProgram ) <EOL> program . _do_discovery ( [ '<STR_LIT>' ] , Loader = Loader ) <EOL> self . assertEqual ( program . test , '<STR_LIT>' ) <EOL> self . assertEqual ( Loader . args , [ ( '<STR_LIT>' , '<STR_LIT>' , None ) ] ) <EOL> Loader . args = [ ] <EOL> program = object . __new__ ( unittest . TestProgram ) <EOL> program . _do_discovery ( [ '<STR_LIT>' , '<STR_LIT>' ] , Loader = Loader ) <EOL> self . assertEqual ( program . test , '<STR_LIT>' ) <EOL> self . assertEqual ( Loader . args , [ ( '<STR_LIT>' , '<STR_LIT>' , None ) ] ) <EOL> Loader . args = [ ] <EOL> program = object . __new__ ( unittest . TestProgram ) <EOL> program . _do_discovery ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , Loader = Loader ) <EOL> self . assertEqual ( program . test , '<STR_LIT>' ) <EOL> self . assertEqual ( Loader . args , [ ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> Loader . args = [ ] <EOL> program = object . __new__ ( unittest . TestProgram ) <EOL> program . _do_discovery ( [ '<STR_LIT>' , '<STR_LIT>' ] , Loader = Loader ) <EOL> self . assertEqual ( program . test , '<STR_LIT>' ) <EOL> self . assertEqual ( Loader . args , [ ( '<STR_LIT>' , '<STR_LIT>' , None ) ] ) <EOL> Loader . args = [ ] <EOL> program = object . __new__ ( unittest . TestProgram ) <EOL> program . _do_discovery ( [ '<STR_LIT>' , '<STR_LIT>' ] , Loader = Loader ) <EOL> self . assertEqual ( program . test , '<STR_LIT>' ) <EOL> self . assertEqual ( Loader . args , [ ( '<STR_LIT:.>' , '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> Loader . args = [ ] <EOL> program = object . __new__ ( unittest . TestProgram ) <EOL> program . _do_discovery ( [ '<STR_LIT>' , '<STR_LIT>' ] , Loader = Loader ) <EOL> self . assertEqual ( program . test , '<STR_LIT>' ) <EOL> self . assertEqual ( Loader . args , [ ( '<STR_LIT:.>' , '<STR_LIT>' , None ) ] ) <EOL> self . assertFalse ( program . failfast ) <EOL> self . assertFalse ( program . catchbreak ) <EOL> Loader . args = [ ] <EOL> program = object . __new__ ( unittest . TestProgram ) <EOL> program . _do_discovery ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:-c>' ] , <EOL> Loader = Loader ) <EOL> self . assertEqual ( program . test , '<STR_LIT>' ) <EOL> self . assertEqual ( Loader . args , [ ( '<STR_LIT>' , '<STR_LIT>' , None ) ] ) <EOL> self . assertEqual ( program . verbosity , <NUM_LIT:2> ) <EOL> self . assertTrue ( program . failfast ) <EOL> self . assertTrue ( program . catchbreak ) <EOL> def test_detect_module_clash ( self ) : <EOL> class Module ( object ) : <EOL> __file__ = '<STR_LIT>' <EOL> sys . modules [ '<STR_LIT:foo>' ] = Module <EOL> full_path = os . path . abspath ( '<STR_LIT:foo>' ) <EOL> original_listdir = os . listdir <EOL> original_isfile = os . path . isfile <EOL> original_isdir = os . path . isdir <EOL> def cleanup ( ) : <EOL> os . listdir = original_listdir <EOL> os . path . isfile = original_isfile <EOL> os . path . isdir = original_isdir <EOL> del sys . modules [ '<STR_LIT:foo>' ] <EOL> if full_path in sys . path : <EOL> sys . path . remove ( full_path ) <EOL> self . addCleanup ( cleanup ) <EOL> def listdir ( _ ) : <EOL> return [ '<STR_LIT>' ] <EOL> def isfile ( _ ) : <EOL> return True <EOL> def isdir ( _ ) : <EOL> return True <EOL> os . listdir = listdir <EOL> os . path . isfile = isfile <EOL> os . path . isdir = isdir <EOL> loader = unittest . TestLoader ( ) <EOL> mod_dir = os . path . abspath ( '<STR_LIT:bar>' ) <EOL> expected_dir = os . path . abspath ( '<STR_LIT:foo>' ) <EOL> msg = re . escape ( r"<STR_LIT>" <EOL> "<STR_LIT>" % ( mod_dir , expected_dir ) ) <EOL> self . assertRaisesRegexp ( <EOL> ImportError , '<STR_LIT>' % msg , loader . discover , <EOL> start_dir = '<STR_LIT:foo>' , pattern = '<STR_LIT>' <EOL> ) <EOL> self . assertEqual ( sys . path [ <NUM_LIT:0> ] , full_path ) <EOL> def test_discovery_from_dotted_path ( self ) : <EOL> loader = unittest . TestLoader ( ) <EOL> tests = [ self ] <EOL> expectedPath = os . path . abspath ( os . path . dirname ( unittest . test . __file__ ) ) <EOL> self . wasRun = False <EOL> def _find_tests ( start_dir , pattern ) : <EOL> self . wasRun = True <EOL> self . assertEqual ( start_dir , expectedPath ) <EOL> return tests <EOL> loader . _find_tests = _find_tests <EOL> suite = loader . discover ( '<STR_LIT>' ) <EOL> self . assertTrue ( self . wasRun ) <EOL> self . assertEqual ( suite . _tests , tests ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
