B °-_®Xã@sdZddlZddlZddlZddlZddlZddlZddlZddlmZm Z ddl m Z m Z m Z dZe dej¡ZGdd „d e jƒZd d „Zd d „Zdd„Zdd„Zdd„ZGdd„deƒZeƒZddd„Ze jdfdd„Zde je jfdd„Z de je jfdd„Z!dS) zLoading unittests.éN)ÚfnmatchÚ fnmatchcaseé)ÚcaseÚsuiteÚutilTz[_a-z]\w*\.py$cs,eZdZdZ‡fdd„Z‡fdd„Z‡ZS)Ú _FailedTestNcs||_tt|ƒ |¡dS)N)Ú _exceptionÚsuperrÚ__init__)ÚselfZ method_nameÚ exception)Ú __class__©ú%/usr/lib/python3.7/unittest/loader.pyr sz_FailedTest.__init__cs*|ˆjkrttˆƒ |¡S‡fdd„}|S)Ncs ˆj‚dS)N)r r)r rrÚ testFailure!sz,_FailedTest.__getattr__..testFailure)Ú_testMethodNamer rÚ __getattr__)r Únamer)r)r rrs  z_FailedTest.__getattr__)Ú__name__Ú __module__Ú __qualname__rr rÚ __classcell__rr)rrrs rcCs"d|t ¡f}t|t|ƒ||ƒS)Nz#Failed to import test module: %s %s)Ú tracebackÚ format_excÚ_make_failed_testÚ ImportError)rÚ suiteClassÚmessagerrrÚ_make_failed_import_test&srcCsdt ¡f}t||||ƒS)NzFailed to call load_tests: %s)rrr)rr rrrrrÚ_make_failed_load_tests+sr cCst||ƒ}||fƒ|fS)N)r)Ú methodnamer rrÚtestrrrr0s rcCs<t t|ƒ¡dd„ƒ}||i}tdtjf|ƒ}|||ƒfƒS)NcSsdS)Nr)r rrrÚ testSkipped5sz'_make_skipped_test..testSkippedZ ModuleSkipped)rÚskipÚstrÚtypeÚTestCase)r!r rr#ÚattrsZ TestClassrrrÚ_make_skipped_test4sr)cCs*| ¡ d¡r|dd…Stj |¡dS)Nz $py.classi÷ÿÿÿr)ÚlowerÚendswithÚosÚpathÚsplitext)r-rrrÚ_jython_aware_splitext<s r/cs¬eZdZdZdZeejƒZdZ e j Z dZ ‡fdd„Zdd„Zddœd d „Zd!d d „Zd"d d„Zdd„Zd#dd„Zdd„Zdd„Zdd„Zdd„Zd$dd„Zd%dd „Z‡ZS)&Ú TestLoaderz This class is responsible for loading tests according to various criteria and returning them wrapped in a TestSuite r"Ncs tt|ƒ ¡g|_tƒ|_dS)N)r r0r ÚerrorsÚsetÚ_loading_packages)r )rrrr MszTestLoader.__init__cCsFt|tjƒrtdƒ‚| |¡}|s2t|dƒr2dg}| 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Ú TypeErrorÚgetTestCaseNamesÚhasattrrÚmap)r Ú testCaseClassZ testCaseNamesZ loaded_suiterrrÚloadTestsFromTestCaseTs  z TestLoader.loadTestsFromTestCase)Úpatternc Os4t|ƒdksd|kr,t dt¡| dd¡t|ƒdkrRt|ƒd}td |¡ƒ‚t|ƒdkrxt|ƒd}td |¡ƒ‚g}x@t|ƒD]4}t ||ƒ}t |t ƒr†t |t jƒr†| | |¡¡q†Wt |ddƒ} | |¡}| dk r0y | |||ƒStk r.} z$t|j| |jƒ\} } |j | ¡| Sd} ~ 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)ÚlenÚwarningsÚwarnÚDeprecationWarningÚpopr6ÚformatÚsortedÚdirÚgetattrÚ isinstancer&r4rr'Úappendr;rÚ Exceptionr rr1) r Úmoduler<ÚargsZkwsZ complaintÚtestsrÚobjr=ÚeÚ error_caseÚ error_messagerrrÚloadTestsFromModulebs4           zTestLoader.loadTestsFromModulec Cs>| d¡}d\}}|dkr–|dd…}xb|rˆyd |¡}t|ƒ}PWq(tk r„| ¡}t||jƒ\}}|s€|j |¡|SYq(Xq(W|dd…}|} xœ|D]”} y| t | | ƒ} } Wq t k r2} zZt | ddƒdk rô|dk rô|j |¡|St | | |jdt   ¡fƒ\}}|j |¡|SWdd} ~ XYq Xq Wt| tjƒrP| | ¡St| tƒrtt| tjƒrt| | ¡St| tjƒrÎt| tƒrÎt| tjƒrÎ|d}| |ƒ} tt | |ƒtjƒsà| | g¡Snt| tjƒrà| St| ƒr.| ƒ}t|tjƒr|St|tjƒr| |g¡Std| |fƒ‚n 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: %séÿÿÿÿz"calling %s returned %s, not a testz$don't know how to make test from: %s)ÚsplitÚjoinÚ __import__rrBrrr1rHrFÚAttributeErrorrrrrGÚtypesÚ ModuleTyperQr&r4rr'r;Ú FunctionTyperr5Úcallabler6)r rrJÚpartsrOrPZ parts_copyÚ module_nameZnext_attributerMÚpartÚparentrNZinstr"rrrÚloadTestsFromNameŠsl             zTestLoader.loadTestsFromNamecs‡‡fdd„|Dƒ}ˆ |¡S)zReturn a suite of all test cases found using the given sequence of string specifiers. See 'loadTestsFromName()'. csg|]}ˆ |ˆ¡‘qSr)ra)Ú.0r)rJr rrú Üsz1TestLoader.loadTestsFromNames..)r)r ÚnamesrJZsuitesr)rJr rÚloadTestsFromNamesØszTestLoader.loadTestsFromNamescs>‡‡fdd„}tt|tˆƒƒƒ}ˆjr:|jt ˆj¡d|S)zLReturn a sorted sequence of method names found within testCaseClass csZ| ˆj¡sdStˆ|ƒ}t|ƒs&dSdˆjˆj|f‰ˆjdkpXt‡fdd„ˆjDƒƒS)NFz%s.%s.%sc3s|]}tˆ|ƒVqdS)N)r)rbr<)ÚfullNamerrú ìszKTestLoader.getTestCaseNames..shouldIncludeMethod..)Ú startswithÚtestMethodPrefixrFr\rrÚtestNamePatternsÚany)ZattrnameZtestFunc)r r:)rfrÚshouldIncludeMethodâs   z8TestLoader.getTestCaseNames..shouldIncludeMethod)Úkey)ÚlistÚfilterrEÚsortTestMethodsUsingÚsortÚ functoolsÚ cmp_to_key)r r:rlZ testFnNamesr)r r:rr7ßs  zTestLoader.getTestCaseNamesútest*.pyc CsNd}|dkr|jdk r|j}n|dkr.d}|}tj |¡}|tjkrRtj d|¡||_d}d}g}tj tj |¡¡r¨tj |¡}||kr¤tj tj |d¡¡ }nty t |ƒWnt k rÎd}YnNXtj |}|  d¡d} ytj tj  |j¡¡}Wnætk rèy |j} Wntk r8d} YnX| rº| jdkrº| jdk räd}x†|jD]P} |s~|  |¡s~qd|   |j dtjj¡¡d|_| |j| |dd¡qdWn*|jtjkrÔtdƒd‚ntd  |¡ƒd‚YnX|r|s| | ¡|_tj |¡n tj |¡|r.t d |ƒ‚|sDt| ||¡ƒ}|  |¡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. FNTrz __init__.pyrR)Ú 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,r-ÚabspathÚsysÚinsertÚisdirÚisfilerVrWrÚmodulesrUÚdirnameÚ__file__rXÚ__spec__ÚloaderÚsubmodule_search_locationsrSrhrÚreplaceÚsepÚextendÚ _find_testsÚbuiltin_module_namesr6rCÚ _get_directory_containing_moduleÚremovernr) r Ú start_dirr<Z top_level_dirZset_implicit_topZis_not_importableÚ is_namespacerLZ the_moduleZtop_partÚspecr-rrrÚdiscoveròsv                zTestLoader.discovercCsRtj|}tj |j¡}tj |¡ ¡ d¡rBtj  tj  |¡¡Stj  |¡SdS)Nz __init__.py) rxr|r,r-rwr~Úbasenamer*rhr})r r^rJÚ full_pathrrrr‡`s  z+TestLoader._get_directory_containing_modulecCsh||jkrdSttj |¡ƒ}tj ||j¡}tj |¡rBtdƒ‚| d¡rTtdƒ‚|  tjj d¡}|S)NrRzPath must be within the projectz..) rvr/r,r-ÚnormpathÚrelpathÚisabsÚAssertionErrorrhr‚rƒ)r r-Z_relpathrrrrÚ_get_name_from_pathls zTestLoader._get_name_from_pathcCst|ƒtj|S)N)rWrxr|)r rrrrÚ_get_module_from_namexsz TestLoader._get_module_from_namecCs t||ƒS)N)r)r r-rŽr<rrrÚ _match_path|szTestLoader._match_pathFc csÔ| |¡}|dkrD||jkrD| |||¡\}}|dk r<|V|sDdStt |¡ƒ}x||D]t}tj ||¡} | | ||¡\}}|dk rŠ|V|rX| | ¡}|j |¡z|  | ||¡EdHWd|j  |¡XqXWdS)z/Used by discovery. Yields test suites it loads.rRN) r“r3Ú_find_test_pathrDr,Úlistdirr-rVÚaddr…Údiscard) r r‰r<rurrLZshould_recurseÚpathsr-rŽrrrr…€s*    zTestLoader._find_testsc CsPtj |¡}tj |¡rHt |¡s(dS| |||¡s:dS| |¡}y| |¡}WnZt j k r„}zt |||j ƒdfSd}~XYnÀt ||j ƒ\}} |j | ¡|dfStj t|d|ƒ¡} ttj | ¡ƒ} ttj |¡ƒ} |  ¡|  ¡kr2tj | ¡} ttj |¡ƒ}tj |¡}d}t||| |fƒ‚|j||ddfSntj |¡rH|sxtj tj |d¡¡sxdSd}d}| |¡}y| |¡}Wn\t j k rÌ}zt |||j ƒdfSd}~XYnzt ||j ƒ\}} |j | ¡|dfSt|ddƒ}|j |¡z(|j||d}|dk r0|dfS|d fS|j |¡XndSdS) zÉUsed 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). )NFFNr~zW%r module incorrectly imported from %r. Expected %r. Is this module globally installed?)r<z __init__.pyr=T)r,r-rr{ÚVALID_MODULE_NAMEÚmatchr•r“r”rZSkipTestr)rrr1rHrwrFr/Úrealpathr*r}rrQrzrVr3r˜r™)r rŽr<rurrrJrNrOrPZmod_filerZfullpath_noextZ module_dirZmod_nameZ expected_dirÚmsgr=rLÚpackagerrrr–¢sl           zTestLoader._find_test_path)N)N)rtN)F)F)rrrÚ__doc__riÚ staticmethodrÚ three_way_cmprprjrr5rrvr r;rQrarer7rŒr‡r“r”r•r…r–rrr)rrr0Bs&  ( N  n   "r0cCs&tƒ}||_||_||_|r"||_|S)N)r0rprirjr)ÚprefixÚ sortUsingrrjr€rrrÚ _makeLoaderñsr¥cCst|||d |¡S)N)rj)r¥r7)r:r£r¤rjrrrr7úsr7r"cCst|||ƒ |¡S)N)r¥r;)r:r£r¤rrrrÚ makeSuiteýs r¦cCst|||ƒ |¡S)N)r¥rQ)rJr£r¤rrrrÚ findTestCasess r§)NN)"r r,ÚrerxrrYrrr?rrÚrrrZ __unittestÚcompileÚ IGNORECASEr›r'rrr rr)r/Úobjectr0ZdefaultTestLoaderr¥r¢r7r5r¦r§rrrrÚs8/