U ec@sDdZddlZddlZddlZddlZddlZddlmZddlm Z m Z ddl m Z m Z ddlmZddlmZddlmZmZmZdd lmZmZdd lmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#dd l$m%Z%m&Z&m'Z'dd lm(Z(m)Z)ddl*Z*e+e,Z-ej.j/Z0ej.j1Z2ej.j3Z4ej.j5Z6ej.j7Z8d dgZ9Gdd d ej:Z:e:Z;dS)z Test Loader ----------- nose's test loader implements the same basic functionality as its superclass, unittest.TestLoader, but extends it by more liberal interpretations of what may be a test and how a test may be named. N) isfunction)unbound_methodismethod)FunctionTestCaseMethodTestCase)Failure)Config)Importeradd_path remove_path)defaultSelector TestAddress) func_lineno getpackageisclass isgenerator ispackageregex_last_key resolve_nametransplant_functransplant_class test_address)ContextSuiteFactory ContextList LazySuite) sort_list cmp_to_key TestLoaderdefaultTestLoadercseZdZdZdZdZdZdZdZd&ddZ ddZ ddZ d d Z d d Z d dZddZddZd'ddZd(ddZd)ddZfddZddZd*ddZd+d d!Zd"d#Zd$d%ZZS),raATest loader that extends unittest.TestLoader to: * Load tests from test-like functions and classes that are not unittest.TestCase subclasses * Find and load test modules in a directory * Support tests that are generators * Support easy extensions of or changes to that behavior through plugins NcCs|dkrt}|dkr t|d}|dkr.|j}|dkr@t|}nt|rP||}||_||_tt||_||_ |j rt ||t |d|_ tg|_tj|dS)aInitialize a test loader. Parameters (all optional): * config: provide a `nose.config.Config`_ or other config class instance; if not provided a `nose.config.Config`_ with default values is used. * importer: provide an importer instance that implements `importFromPath`. If not provided, a `nose.importer.Importer`_ is used. * workingDir: the directory to which file and module names are relative. If not provided, assumed to be the current working directory. * selector: a selector class or instance. If a class is provided, it will be instantiated with one argument, the current config. If not provided, a `nose.selector.Selector`_ is used. N)config)rr workingDirr rrimporter op_normpath op_abspathselectoraddPathsr r suiteClassset _visitedPathsunittestr__init__)selfrr!r r$r,9/opt/hc_python/lib/python3.8/site-packages/nose/loader.pyr*<s&     zTestLoader.__init__cCsf|jjrtj||S||jfdd}tt|t|}|sLt |drLdg}|j rbt |t |j |S)z_Override to select with selector, unless config.getTestCaseNamesCompat is True cSs6t||d}t|r t||}n t|s,dS||SNFgetattrrrrZ wantMethodattrclsselitemr,r,r-wantedms   z+TestLoader.getTestCaseNames..wantedZrunTest) rZgetTestCaseNamesCompatr)rgetTestCaseNamesr$listfilterdirhasattrZsortTestMethodsUsingrr)r+ testCaseClassr6casesr,r,r-r7fszTestLoader.getTestCaseNamescCs|dkr dS||jkSr.)r(r+pathr,r,r- _haveVisited~szTestLoader._haveVisitedcCs|dk r|j|dSN)r(addr>r,r,r-_addVisitedPathszTestLoader._addVisitedPathc #std|jj}||jjr2t|j}t|}t |t jj |D]}| drbqRt t||t}d}|rd}j}n$t}|r| drqRj}t} |rRsR|r||drjddVn V|qR| r0jddVqRfdd VqRg} ||D]} | | qVz| r|| VWn<tt fk rYn"t!t"#gVYnXjjr|D]} t$| q|%|d S) zLoad tests from the directory at path. This is a generator -- each suite of tests from a module or other file is yielded and is expected to be executed before the next file is examined. zload from dir %s.F_z.pyT discoveredcs SrAloadTestsFromDirr, entry_pathr+r,r-z-TestLoader.loadTestsFromDir..N)&logdebugrpluginsZbeforeDirectoryr%r oslistdirrrZ testMatch startswithr#op_join op_isfiler$ZwantFileop_isdirZ wantDirectoryrr@rCZ beforeContextendswithloadTestsFromNameloadTestsFromFileZ afterContextr&rIappendKeyboardInterrupt SystemExitrsysexc_infor ZafterDirectory) r+r?rPZ paths_addedentriesentryis_filer6is_dir is_packageteststestpr,rJr-rIsn                zTestLoader.loadTestsFromDirc Cstd|zTdd|jj|D}|rDdd|D}||WSt|dtd|WnVt t fk rzYn>t }|t |d|d|d |d d fd gYSXd S) zLoad tests from a non-module file. Default is to raise a ValueError; plugins may implement `loadTestsFromFile` to provide a list of tests loaded from the file. zLoad from non-module file %scSsg|]}|qSr,r,).0rer,r,r- sz0TestLoader.loadTestsFromFile..cSsg|] }|r|qSr,r,)rgZ_fr,r,r-rhsrz!Unable to load tests from file %srNaddress)rNrOrrPrYr&openclose ValueErrorr[r\r]r^r)r+filenamerdexcr,r,r-rYs*    zTestLoader.loadTestsFromFilecs$|ffdd }j|ddS)zLazy-load tests from a generator function. The generator function may yield either: * a callable, or * a function name resolvable within the same module c 3szD|D]8}|\}}t|tjs0t||}t|||dVqWnJtk rZYn6t}t |d|d|dt dVYnXdS)NargZ descriptorrrjrkrl) parseGeneratedTest isinstance collectionsCallabler0rr[r]r^rr)gmre test_funcrtrr generatorr+r,r-generates   z3TestLoader.loadTestsFromGenerator..generateFcontextZ can_split)r&)r+r}moduler~r,r|r-loadTestsFromGenerators z!TestLoader.loadTestsFromGeneratorcsLtdrjj}|}j}t|||ffdd }j|ddS)zLazy-load tests from a generator method. This is more complicated than loading from a generator function, since a generator method may yield: * a function * a bound or unbound method, or * a method name Zim_classc 3sz|D]v}|\}}t|tjs6t|t||}t|rPt|||dVqt|tjrnt|||dVqtt d|VqWnJt k rYn6t }t|d|d|dt dVYnXdS)Nrs)rertz%s is not a callable or methodrrjrkrl)rurvrwrxrr0rrr TypeErrorr[r]r^r)rycrer{rtrrr|r,r-r~s(    z9TestLoader.loadTestsFromGeneratorMethod..generateFr)r;__self__ __class____name__r0r&)r+r}r3instmethodr~r,r|r-loadTestsFromGeneratorMethod s  z'TestLoader.loadTestsFromGeneratorMethodFc stdg}g}g}|r(jrtD]N}t|d}t|r`j|r~||q0t |r0j |r0||q0t |ddt |t fdd||D}tdg} |rt jt j|}| D]~} td| td |t j| t jt j| jjs:|r:t jt j| |rt j| r|| q؈jj|D]}||qht|d S) zLoad all tests from module and return a suite containing them. If the module has been discovered and is not test-like, the suite will be empty by default, though plugins may add their own tests. zLoad from module %sNcSs|jSrA)r)xr,r,r-rLKrMz0TestLoader.loadTestsFromModule..csg|]}j|dqS))parent)makeTest)rgtrr+r,r-rhMsz2TestLoader.loadTestsFromModule..__path__zLoad tests from module path %s?z!path: %s os.path.realpath(%s): %sr)rNrOr$Z wantModuler:r0rZ wantClassrZrZ wantFunctionrrrQr?normcaserealpathrZtraverseNamespacerSisdirextendrIrPloadTestsFromModuler&r) r+rr?rGrdZ test_classesZ test_funcsr5reZ module_paths module_pathr,rr-r5sJ            zTestLoader.loadTestsFromModulec std||j}jj||}|r0||St|jd}|r|jrN|j} ||\}}t |rt |dd|j krt |tst||j }t ||j }td|||t |tr||gS|t||g|dSn|jrz\|jdkrt|j}nBjj|j|jzj|j|j}W5jj|j|jXWnTttfk r\Yn:t} |t| d| d| d |d gYSX|jr|j|Sj||j|d Sn|jrh|j|jrt} | dkr |tt d |jf|d gSj|j| d St!r6t"fddSt#rJ$S|tt%d|d gSn|tt d||d gSdS)zLoad tests from the entity with the given name. The name may indicate a file, directory, module, or any object within a module. See `nose.util.split_test_name` for details on test name parsing. zload from %s (%s))r __module__Nzparent %s obj %s module %srrrjrkrlrFz>Can't find callable %s in file %s: file is not a python module)rcs SrArHr,r?r+r,r-rLrMz.TestLoader.loadTestsFromName..zNo such file %szUnresolvable test name %s)&rNrOr&rrPrXr r callresolverr0rrvrrrrrrqrZ beforeImportZ afterImportr!ZimportFromPathr[r\r]r^ZtotuplerrrprVrrUrYOSError) r+namerrGsuite plug_testsaddrrobjrrpackager,rr-rXis                zTestLoader.loadTestsFromNamecCsP|jj||}|r@|\}}|r@|||tj|||gStj|||S)zSLoad tests from all names, returning a suite containing all tests. )rrPloadTestsFromNamesr&r)r)r+namesrZplug_resrr,r,r-rszTestLoader.loadTestsFromNamescsdg}|jj}||D]}||q|s:tt||S|ddtt||D||S)z6Load tests from a unittest.TestCase subclass. cSsg|]}|qSr,r,rgcaser,r,r-rhsz4TestLoader.loadTestsFromTestCase..)rrPloadTestsFromTestCaserZsuperrrr&)r+r<r=rPrrr,r-rs z TestLoader.loadTestsFromTestCasecs^jfdd}fddt|tD}jjD]}||q<t|dS)a!Load tests from a test class that is *not* a unittest.TestCase subclass. In this case, we can't depend on the class's `__init__` taking method name arguments, so we have to compose a MethodTestCase for each method in the class that looks testlike. cSs6t||d}t|r t||}n t|s,dS||Sr.r/r1r,r,r-r6s   z1TestLoader.loadTestsFromTestClass..wantedcsg|]}t|qSr,)rr0rr3r+r,r-rhsz5TestLoader.loadTestsFromTestClass..r) r$r9r:rrPloadTestsFromTestClassrZr&r)r+r3r6r=rer,rr-rs   z!TestLoader.loadTestsFromTestClassc Csz|||WSttfk r(Ynbt}z t|}Wn$tk rXYnd}YnXt|d|d|d|dYSXdS)Nrrjrkrl) _makeTestr[r\r]r^rr)r+rrrrrr,r,r-r s  zTestLoader.makeTestc Csg}z t|}Wn$tk r&Ynd}YnX|jj||D]}||qFz|rh||WSWnHttfk rYn0t }t |d|d|d|dYSXt |r|rt |t jst||}t |tjr|St|r4|r|j|jkrt||j}t|tjr(||S||Snt|r|dkrN|j}t|tjrf||jSt|r||||St|Sn\t |r|r|j|jkrt||j}t|r|||St |Snt t!d||dSdS)zVGiven a test object and its parent, return a test case or test suite. NrrjrkrlzCan't make a test from %s)"rr[rrPrrZr&r\r]r^rrrvtypes ModuleTyperr)ZTestCaserrrr issubclassrrrrrrrrrrr)r+rrrrrerrr,r,r-rsZ   "                 zTestLoader._makeTestcCsH|}|d}|D]}|t||d}}q|dkr@ttd|}||fS)z#Resolve name within module rDNzNo such test %s)splitr0rrp)r+rrrpartspartrr,r,r-rSs zTestLoader.resolvecCsdt|ts|t}}nDt|dkr6|dt}}n&t|dksFt|d|dd}}||fS)zGiven the yield value of a test generator, return a func and args. This is used in the two loadTestsFromGenerator* methods. rjrN)rvtuplelenAssertionError)r+rer{rtr,r,r-ru_s  zTestLoader.parseGeneratedTest)NNNN)NF)NF)N)N)N)rr __qualname____doc__rr!r r$r&r*r7r@rCrIrYrrrrXrrrrrrru __classcell__r,r,rr-r-s2 *M, 4 l    ; )s:    0 E