o [dX@sdZddlZddlZddlZddlZddlZddlZddlZddlmZm Z ddl m Z m Z m Z dZedejZGdd d e jZd d Zd d ZddZddZddZGdddeZeZdddZe jdfddZde je jfddZ de je jfddZ!dS) zLoading unittests.N)fnmatch fnmatchcase)casesuiteutilTz[_a-z]\w*\.py$cs,eZdZdZfddZfddZZS) _FailedTestNcs||_tt||dSN) _exceptionsuperr__init__)self method_name exception __class__&/usr/lib/python3.10/unittest/loader.pyr sz_FailedTest.__init__cs*|jkr tt|Sfdd}|S)Ncsjr )r rr rr testFailure!sz,_FailedTest.__getattr__..testFailure)_testMethodNamer r __getattr__)r namerrrrrs  z_FailedTest.__getattr__)__name__ __module__ __qualname__rr r __classcell__rrrrrs rcCs"d|tf}t|t|||S)Nz#Failed to import test module: %s %s) traceback format_exc_make_failed_test ImportError)r suiteClassmessagerrr_make_failed_import_test&sr#cCsdtf}t||||S)NzFailed to call load_tests: %s)rrr)rrr!r"rrr_make_failed_load_tests+sr$cCst||}||f|fSr )r) methodnamerr!r"testrrrr0s rcCs<tt|dd}||i}tdtjf|}|||fS)NcSsdSr rrrrr testSkipped5sz'_make_skipped_test..testSkipped ModuleSkipped)rskipstrtypeTestCase)r%rr!r'attrs TestClassrrr_make_skipped_test4s  r/cCs*|dr |ddStj|dS)Nz $py.classir)lowerendswithospathsplitext)r3rrr_jython_aware_splitext<s r5cseZdZdZdZeejZdZ e j Z dZ fddZddZddd d Zd!d d Zd!d dZddZd"ddZddZddZddZddZd#ddZd#dd ZZS)$ TestLoaderz This class is responsible for loading tests according to various criteria and returning them wrapped in a TestSuite r&Ncs tt|g|_t|_dSr )r r6r errorsset_loading_packagesrrrrr Ms zTestLoader.__init__cCsFt|tjr td||}|st|drdg}|t||}|S)z;Return a suite of all test cases contained in testCaseClasszYTest cases should not be derived from TestSuite. Maybe you meant to derive from TestCase?runTest) issubclassr TestSuite TypeErrorgetTestCaseNameshasattrr!map)r testCaseClass testCaseNames loaded_suiterrrloadTestsFromTestCaseTs  z TestLoader.loadTestsFromTestCasepatternc Os4t|dks d|vrtdt|ddt|dkr)t|d}td|t|dkrReturn a suite of all test cases contained in the given moduleruse_load_testsz(use_load_tests is deprecated and ignoredNrzCloadTestsFromModule() takes 1 positional argument but {} were givenz=loadTestsFromModule() got an unexpected keyword argument '{}' load_tests)lenwarningswarnDeprecationWarningpopr=formatsorteddirgetattr isinstancer+r;rr,appendrDr! Exceptionr$rr7) r modulerFargskws complainttestsrobjrHe error_case error_messagerrrloadTestsFromModulebs>           zTestLoader.loadTestsFromModulec Cs2|d}d\}}|durJ|dd}|rDz d|}t|}Wn#tyA|}t||j\}}|s?|j||YSYnw|s|dd}|} |D]S} z | t | | } } WqNt y} z;t | dddur~|dur~|j||WYd} ~ St | | |jdt f\}}|j||WYd} ~ Sd} ~ wwt| tjr|| St| trt| tjr|| St| tjrt| trt| tjr|d}| |} tt | |tjs|| gSnt| tjr| St| r| }t|tjr|St|tjr ||gStd| |ftd | ) aSReturn a suite of all test cases given a string specifier. The name may resolve either to a module, a test case class, a test method within a test case class, or a callable object which returns a TestCase or TestSuite instance. The method optionally resolves the names relative to a given module. .NNNr__path__zFailed to access attribute: %sz"calling %s returned %s, not a testz$don't know how to make test from: %s)splitjoin __import__r rMr#r!r7rSrQAttributeErrorrrrrRtypes ModuleTyper^r+r;rr,rD FunctionTyperr<callabler=)r rrUpartsr\r] parts_copy module_namenext_attributerZpartparentr[instr&rrrloadTestsFromNames                   zTestLoader.loadTestsFromNamecsfdd|D}|S)zReturn a suite of all test cases found using the given sequence of string specifiers. See 'loadTestsFromName()'. csg|]}|qSr)rr).0rrUr rr sz1TestLoader.loadTestsFromNames..)r!)r namesrUsuitesrrtrloadTestsFromNamess zTestLoader.loadTestsFromNamescs>fdd}tt|t}jr|jtjd|S)zLReturn a sorted sequence of method names found within testCaseClass csZ|jsdSt|}t|sdSdjj|fjdup,tfddjDS)NFz%s.%s.%sc3s|]}t|VqdSr )r)rsrFfullNamerr szKTestLoader.getTestCaseNames..shouldIncludeMethod..) startswithtestMethodPrefixrQrjrrtestNamePatternsany)attrnametestFuncr rAryrshouldIncludeMethods    z8TestLoader.getTestCaseNames..shouldIncludeMethod)key)listfilterrPsortTestMethodsUsingsort functools cmp_to_key)r rAr testFnNamesrrrr>s  zTestLoader.getTestCaseNamestest*.pyc Cs&d}|dur|jdur|j}n|durd}|}tj|}|tjvr)tjd|||_d}d}g}tjtj|rStj|}||krRtjtj|d }nzt |Wn t ydd}Ynwtj |}| dd} z tjtj |j}Wndtyz|j} Wn tyd} Ynw| r| jdur| jdurd}|jD]$} |s| |sq| |jdtjjd|_||j| |ddqn|jtjvrtddtd |dYnw|r|s|| |_tj|ntj||rt d ||st|||}| |S) a%Find and return all test modules from the specified start directory, recursing into subdirectories to find them and return all tests found within them. Only test files that match the pattern will be loaded. (Using shell style pattern matching.) All test modules must be importable from the top level of the project. If the start directory is not the top level directory then the top level directory must be specified separately. If a test package name (directory with '__init__.py') matches the pattern then the package will be checked for a 'load_tests' function. If this exists then it will be called with (loader, tests, pattern) unless the package has already had load_tests called from the same discovery invocation, in which case the package module object is not scanned for tests - this ensures that when a package uses discover to further discover child tests that infinite recursion does not happen. If load_tests exists then discovery does *not* recurse into the package, load_tests is responsible for loading all tests in the package. The pattern is deliberately not stored as a loader attribute so that packages can continue discovery themselves. top_level_dir is stored so load_tests does not need to pass this argument in to loader.discover(). Paths are sorted before being imported to ensure reproducible execution order even on filesystems with non-alphabetical ordering like ext3/4. FNTr __init__.pyr_) namespacez2Can not use builtin modules as dotted module namesz$don't know how to discover from {!r}z%Start directory is not importable: %r)!_top_level_dirr2r3abspathsysinsertisdirisfilerdrer modulesrcdirname__file__rf__spec__loadersubmodule_search_locationsrar|rreplacesepextend _find_testsbuiltin_module_namesr=rN _get_directory_containing_moduleremoverr!) r start_dirrF top_level_dirset_implicit_topis_not_importable is_namespacerY the_moduletop_partspecr3rrrdiscovers                   zTestLoader.discovercCsNtj|}tj|j}tj|dr!tj tj |Stj |S)Nr) rrr2r3rrbasenamer0r|r)r rmrU full_pathrrrr`s  z+TestLoader._get_directory_containing_modulecCsh||jkrdSttj|}tj||j}tj|r!Jd|dr*Jd|tjj d}|S)Nr_zPath must be within the projectz..) rr5r2r3normpathrelpathisabsr|rr)r r3_relpathrrrr_get_name_from_pathls zTestLoader._get_name_from_pathcCst|tj|Sr )rerr)r rrrr_get_module_from_namexs z TestLoader._get_module_from_namecCs t||Sr )r)r r3rrFrrr _match_path|s zTestLoader._match_pathFc cs||}|dkr#||jvr#||||\}}|dur|V|s#dStt|}|D]@}tj||} || ||\}}|durE|V|rl|| }|j|z| | ||EdHW|j |q,|j |wq,dS)z/Used by discovery. Yields test suites it loads.r_N) rr9_find_test_pathrOr2listdirr3rdaddrdiscard) r rrFrrrYshould_recursepathsr3rrrrrs4   zTestLoader._find_testsc Csltj|}tj|rt|sdS||||sdS||}z||}Wn2t j yD}zt |||j dfWYd}~Sd}~wt ||j \}} |j| |dfYStjt|d|} ttj| } ttj|} | | krtj| } ttj|}tj|}d}t||| |f|j||ddfStj|r4|stjtj|dsdSd}d}||}z||}Wn2t j y}zt |||j dfWYd}~Sd}~wt ||j \}} |j| |dfYSt|dd}|j|z"|j||d}|dur"|dfW|j|S|d fW|j|S|j|wdS) zUsed by discovery. Loads tests from a single file, or a directories' __init__.py when passed the directory. Returns a tuple (None_or_tests_from_file, should_recurse). )NFFNrzW%r module incorrectly imported from %r. Expected %r. Is this module globally installed?rErrHT)r2r3rrVALID_MODULE_NAMEmatchrrrrSkipTestr/r!r#r7rSrrQr5realpathr0rr r^rrdr9rr)r rrFrrrrUr[r\r]mod_filerfullpath_noext module_dirmod_name expected_dirmsgrHrYpackagerrrrs                     zTestLoader._find_test_pathr )rN)F)rrr__doc__r} staticmethodr three_way_cmprr~rr<r!rr rDr^rrrxr>rrrrrrrrrrrrr6Bs(   ( N n   "r6cCs&t}||_||_||_|r||_|Sr )r6rr}r~r!)prefix sortUsingr!r~rrrr _makeLoadersrcCst|||d|S)N)r~)rr>)rArrr~rrrr>sr>r&cCt||||Sr )rrD)rArrr!rrr makeSuite rcCrr )rr^)rUrrr!rrr findTestCasesrrr`)"rr2rerrrgrrJrrrrr __unittestcompile IGNORECASErr,rr#r$rr/r5objectr6defaultTestLoaderrrr>r<rrrrrrs>/