U e5dX@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__)selfZ method_name exception __class__'/usr/lib64/python3.8/unittest/loader.pyr sz_FailedTest.__init__cs*|jkrtt|Sfdd}|S)Ncs jdSr )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&s r"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..testSkippedZ ModuleSkipped)rskipstrtypeTestCase)r$rr r&attrsZ TestClassrrr_make_skipped_test4s   r,cCs*|dr|ddStj|dS)Nz $py.classir)lowerendswithospathsplitext)r0rrr_jython_aware_splitext<s r2cseZdZdZdZeejZdZ 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 r3r errorsset_loading_packagesrrrrr MszTestLoader.__init__cCsFt|tjrtd||}|s2t|dr2dg}|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?ZrunTest) issubclassr TestSuite TypeErrorgetTestCaseNameshasattrr map)r testCaseClassZ testCaseNamesZ loaded_suiterrrloadTestsFromTestCaseTs  z TestLoader.loadTestsFromTestCasepatternc Os:t|dksd|kr,tdt|ddt|dkrRt|d}td|t|dkrxt|d}td|g}t|D]4}t ||}t |t rt |t jr|||qt |dd} ||}| dk r6z| |||WStk r4} z,t|j| |j\} } |j| | WYSd} ~ XYnX|S) z>Return a suite of all test cases contained in the given modulerZuse_load_testsz(use_load_tests is deprecated and ignoredNrzCloadTestsFromModule() takes 1 positional argument but {} were givenz=loadTestsFromModule() got an unexpected keyword argument '{}' load_tests)lenwarningswarnDeprecationWarningpopr9formatsorteddirgetattr isinstancer)r7rr*appendr>r Exceptionr#rr4) r moduler@argsZkwsZ complainttestsrobjrAe error_case error_messagerrrloadTestsFromModulebs<           zTestLoader.loadTestsFromModulec CsX|d}d\}}|dkr|dd}|rzd|}t|}WqWq&tk r|}t||j\}}|s|j||YSYq&Xq&|dd}|} |D]} z| t | | } } Wqt k rN} zvt | dddk r|dk r|j||WYDSt | | |jdt f\}}|j||WY SW5d} ~ XYqXqt| tjrj|| St| trt| tjr|| St| tjrt| trt| tjr|d}| |} tt | |tjs|| gSnt| tjr| St| rH| }t|tjr|St|tjr6||gStd| |fn td | dS) 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__rrFr"r r4rLrJAttributeErrorrrrrKtypes ModuleTyperUr)r7rr*r> FunctionTyperr8callabler9)r rrNpartsrSrTZ parts_copy module_nameZnext_attributerQpartparentrRinstr%rrrloadTestsFromNames       $     zTestLoader.loadTestsFromNamecsfdd|D}|S)zReturn a suite of all test cases found using the given sequence of string specifiers. See 'loadTestsFromName()'. csg|]}|qSr)rf).0rrNr rr sz1TestLoader.loadTestsFromNames..)r )r namesrNZsuitesrrhrloadTestsFromNamesszTestLoader.loadTestsFromNamescs>fdd}tt|t}jr:|jtjd|S)zLReturn a sorted sequence of method names found within testCaseClass csZ|jsdSt|}t|s&dSdjj|fjdkpXtfddjDS)NFz%s.%s.%sc3s|]}t|VqdSr )r)rgr@ZfullNamerr szKTestLoader.getTestCaseNames..shouldIncludeMethod..) startswithtestMethodPrefixrJr`rrtestNamePatternsany)attrnameZtestFuncr r=rlrshouldIncludeMethods   z8TestLoader.getTestCaseNames..shouldIncludeMethod)key)listfilterrIsortTestMethodsUsingsort functools cmp_to_key)r r=rtZ testFnNamesrrsrr:s  zTestLoader.getTestCaseNamestest*.pyc CsJd}|dkr|jdk r|j}n|dkr.d}|}tj|}|tjkrRtjd|||_d}d}g}tjtj|rtj|}||krtjtj|d }npz t |Wnt k rd}YnJXtj |}| dd} ztjtj |j}Wntk rz |j} Wntk r8d} YnX| r| jdkr| jdk rd}|jD]P} |s|| |s|qb| |jdtjjd|_||j| |ddqbn*|jtjkrtddntd |dYnX|r|s || |_tj|n tj||r*t d ||s@t|||}| |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__.pyrV) 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_dirr/r0abspathsysinsertisdirisfilerZr[rmodulesrYdirname__file__r\__spec__loadersubmodule_search_locationsrWrnrreplacesepextend _find_testsbuiltin_module_namesr9rG _get_directory_containing_moduleremovervr ) r start_dirr@Z top_level_dirZset_implicit_topZis_not_importable is_namespacerPZ the_moduleZtop_partspecr0rrrdiscovers               zTestLoader.discovercCsRtj|}tj|j}tj|drBtj tj |Stj |SdS)Nr}) rrr/r0rrbasenamer-rnr)r rbrN full_pathrrrr`s  z+TestLoader._get_directory_containing_modulecCsh||jkrdSttj|}tj||j}tj|rBtd|drTtd| tjj d}|S)NrVzPath must be within the projectz..) rr2r/r0normpathrelpathisabsAssertionErrorrnrr)r r0Z_relpathrrrr_get_name_from_pathls zTestLoader._get_name_from_pathcCst|tj|Sr )r[rr)r rrrr_get_module_from_namexsz TestLoader._get_module_from_namecCs t||Sr )r)r r0rr@rrr _match_path|szTestLoader._match_pathFc cs||}|dkrD||jkrD||||\}}|dk r<|V|sDdStt|}|D]t}tj||} || ||\}}|dk r|V|rV|| }|j|z| | ||EdHW5|j |XqVdS)z/Used by discovery. Yields test suites it loads.rVN) rr6_find_test_pathrHr/listdirr0rZadddiscardr) r rr@r~rrPZshould_recursepathsr0rrrrrs6   zTestLoader._find_testsc Csttj|}tj|rVt|s(dS||||s:dS||}z||}Wnht j k r}zt |||j dfWYSd}~XYnt ||j \}} |j| |dfYSXtjt|d|} ttj| } ttj|} | | kr@tj| } ttj|}tj|}d}t||| |f|j||ddfSntj|rl|stjtj|dsdSd}d}||}z||}Wnjt j k r}zt |||j dfWYSd}~XYnt ||j \}} |j| |dfYSXt|dd}|j|z0|j||d}|dk rP|dfWS|d fWS|j|XndSdS) 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?r?r}rAT)r/r0rrVALID_MODULE_NAMEmatchrrrrZSkipTestr,r r"r4rLrrJr2realpathr-rrrUrrZr6rr)r rr@r~rrrNrRrSrTZmod_filerZfullpath_noextZ module_dirZmod_nameZ expected_dirmsgrArPpackagerrrrs|   &          &       zTestLoader._find_test_path)N)N)r|N)F)F)rrr__doc__ro staticmethodr three_way_cmprxrprr8r rr r>rUrfrkr:rrrrrrrrrrrrr3Bs&  ( N  n   "r3cCs&t}||_||_||_|r"||_|Sr )r3rxrorpr )prefix sortUsingr rprrrr _makeLoadersrcCst|||d|S)N)rp)rr:)r=rrrprrrr:sr:r%cCst||||Sr )rr>)r=rrr rrr makeSuites rcCst||||Sr )rrU)rNrrr rrr findTestCasess r)NN)"rr/rerrr]rzrCrrrrrZ __unittestcompile IGNORECASErr*rr"r#rr,r2objectr3ZdefaultTestLoaderrrr:r8rrrrrrs</