U
    Zec                     @   sD  d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	m
Z
 ddlmZmZ ddlmZ ddlmZ ddlmZmZmZ dd	lmZmZ dd
lmZmZmZmZmZmZm Z m!Z!m"Z"m#Z# ddl$m%Z%m&Z&m'Z' ddlm(Z(m)Z) ddl*Z*e+e,Z-ej.j/Z0ej.j1Z2ej.j3Z4ej.j5Z6ej.j7Z8ddgZ9G dd dej:Z:e:Z;dS )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_methodismethod)FunctionTestCaseMethodTestCase)Failure)Config)Importeradd_pathremove_path)defaultSelectorTestAddress)
func_lineno
getpackageisclassisgenerator	ispackageregex_last_keyresolve_nametransplant_functransplant_classtest_address)ContextSuiteFactoryContextList	LazySuite)	sort_list
cmp_to_key
TestLoaderdefaultTestLoaderc                       s   e Zd ZdZdZdZdZdZdZd&ddZ	dd Z
dd Zd	d
 Zdd Zdd Zdd Zdd Zd'ddZd(ddZd)ddZ fddZdd Zd*ddZd+d d!Zd"d# Zd$d% Z  ZS ),r   aA  Test 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
    Nc                 C   s   |dkrt  }|dkr t|d}|dkr.|j}|dkr@t|}nt|rP||}|| _|| _tt|| _|| _	|j
rt|| t|d| _tg | _tj|  dS )a  Initialize 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)r   r	   
workingDirr   r   r   importerop_normpath
op_abspathselectoraddPathsr
   r   
suiteClassset_visitedPathsunittestr   __init__)selfr   r!   r    r$    r,   j/mounts/lovelace/software/anaconda3/envs/qiime2-amplicon-2024.2/lib/python3.8/site-packages/nose/loader.pyr*   <   s&    



zTestLoader.__init__c                 C   sf   | j jrtj| |S || jfdd}tt|t|}|sLt	|drLdg}| j
rbt|t| j
 |S )z_Override to select with selector, unless
        config.getTestCaseNamesCompat is True
        c                 S   s6   t || d }t|r t||}nt|s,dS ||S NFgetattrr   r   r   Z
wantMethodattrclsselitemr,   r,   r-   wantedm   s    z+TestLoader.getTestCaseNames.<locals>.wantedZrunTest)r   ZgetTestCaseNamesCompatr)   r   getTestCaseNamesr$   listfilterdirhasattrZsortTestMethodsUsingr   r   )r+   testCaseClassr6   casesr,   r,   r-   r7   f   s    zTestLoader.getTestCaseNamesc                 C   s   |d krdS || j kS r.   )r(   r+   pathr,   r,   r-   _haveVisited~   s    zTestLoader._haveVisitedc                 C   s   |d k	r| j | d S N)r(   addr>   r,   r,   r-   _addVisitedPath   s    zTestLoader._addVisitedPathc              	   #   s  t d| jj}|| jjr2t|j}t|}t	|t
jj |D ]}|drbqRtt|| t }d}|rd}j }n$t }|r|drqRj }t }	|rR sR  |r|  |drj ddV  n V  |  qR|	r0j ddV  qR fdd	V  qRg }
||D ]}|
| qVz|
r||
V  W n< tt fk
r    Y n"   t!t"#  gV  Y nX jjr|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.
        zload from dir %s.F_z.pyT
discoveredc                      s
     S rA   loadTestsFromDirr,   
entry_pathr+   r,   r-   <lambda>       z-TestLoader.loadTestsFromDir.<locals>.<lambda>N)&logdebugr   pluginsZbeforeDirectoryr%   r
   oslistdirr   r   Z	testMatch
startswithr#   op_join	op_isfiler$   ZwantFileop_isdirZwantDirectoryr   r@   rC   ZbeforeContextendswithloadTestsFromNameloadTestsFromFileZafterContextr&   rI   appendKeyboardInterrupt
SystemExitr   sysexc_infor   ZafterDirectory)r+   r?   rP   Zpaths_addedentriesentryis_filer6   is_dir
is_packageteststestpr,   rJ   r-   rI      sn    




 

 

zTestLoader.loadTestsFromDirc                 C   s   t d| zTdd | jj|D }|rDdd |D }| |W S t|d  td| W nV t	t
fk
rz    Y n>   t }| t|d |d |d	 |d
d
fdg Y S X d
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.
        zLoad from non-module file %sc                 S   s   g | ]}|qS r,   r,   ).0re   r,   r,   r-   
<listcomp>   s     z0TestLoader.loadTestsFromFile.<locals>.<listcomp>c                 S   s   g | ]}|r|qS r,   r,   )rg   _fr,   r,   r-   rh      s      rz!Unable to load tests from file %sr         Naddress)rN   rO   r   rP   rY   r&   openclose
ValueErrorr[   r\   r]   r^   r   )r+   filenamerd   excr,   r,   r-   rY      s*    
zTestLoader.loadTestsFromFilec                    s$    |f fdd	}j | ddS )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              	   3   s   zD|  D ]8} |\}}t|tjs0t||}t||| dV  qW nJ tk
rZ    Y n6   t }t	|d |d |d t
 dV  Y nX d S )NargZ
descriptorr   rk   rl   rm   )parseGeneratedTest
isinstancecollectionsCallabler0   r   r[   r]   r^   r   r   )gmre   	test_funcru   rs   	generatorr+   r,   r-   generate   s    

z3TestLoader.loadTestsFromGenerator.<locals>.generateFcontextZ	can_split)r&   )r+   r~   moduler   r,   r}   r-   loadTestsFromGenerator   s    z!TestLoader.loadTestsFromGeneratorc                    sL   t  dr jj}| } j}t||  |f fdd	}j| ddS )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
        Zim_classc              	   3   s   z|  D ]v} |\}}t|tjs6t|t||}t|rPt||| dV  qt|tjrnt| ||dV  qtt	d| V  qW nJ t
k
r    Y n6   t }t|d |d |d t dV  Y nX d S )Nrt   )re   ru   z%s is not a callable or methodr   rk   rl   rm   )rv   rw   rx   ry   r   r0   r   r   r   	TypeErrorr[   r]   r^   r   )rz   cre   r|   ru   rs   r}   r,   r-   r     s(    
z9TestLoader.loadTestsFromGeneratorMethod.<locals>.generateFr   )r;   __self__	__class____name__r0   r&   )r+   r~   r3   instmethodr   r,   r}   r-   loadTestsFromGeneratorMethod	  s    

z'TestLoader.loadTestsFromGeneratorMethodFc                    s  t d  g }g }g }|r(j rt D ]N}t |d}t|r`j|r~|| q0t	|r0j
|r0|| q0t|dd  t|t  fdd|| D }t dg }	|rtjtj|}|	D ]~}
t d|
 t d	|tj|
tjtj|
 jjs:|r:tjtj|
|rtj|
r||
 q؈jj |D ]}|| qht| 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.
        zLoad from module %sNc                 S   s   | j S rA   )r   )xr,   r,   r-   rL   K  rM   z0TestLoader.loadTestsFromModule.<locals>.<lambda>c                    s   g | ]}j | d qS ))parent)makeTest)rg   tr   r+   r,   r-   rh   M  s     z2TestLoader.loadTestsFromModule.<locals>.<listcomp>__path__zLoad tests from module path %s?z!path: %s os.path.realpath(%s): %sr   )rN   rO   r$   Z
wantModuler:   r0   r   Z	wantClassrZ   r   ZwantFunctionr   r   rQ   r?   normcaserealpathr   ZtraverseNamespacerS   isdirextendrI   rP   loadTestsFromModuler&   r   )r+   r   r?   rG   rd   Ztest_classesZ
test_funcsr5   re   Zmodule_pathsmodule_pathr,   r   r-   r   5  sJ    
 

zTestLoader.loadTestsFromModulec              
      s  t d|| j}jj||}|r0||S t|jd}|r|jrN|j}	||\}}t
|rt|dd|jkrt|tst||j}t||j}t d||| t|tr||gS |t||g|dS n|jrz\|jdkrt|j}nBjj|j|j zj|j|j}W 5 jj|j|j X W nT ttfk
r\    Y n:   t }	|t|	d |	d |	d	 | d
g Y S X |jr|j|S j||j|dS n|jrh|j |jrt }
|
dkr
|tt d|j f | d
gS j|j|
dS t! r6t" fddS t# rJ$ S |tt%d  | d
gS n|tt d| | d
gS dS )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.
        zload from %s (%s))r    
__module__Nzparent %s obj %s module %sr   r   rk   rl   rm   rF   z>Can't find callable %s in file %s: file is not a python module)r   c                      s
     S rA   rH   r,   r?   r+   r,   r-   rL     rM   z.TestLoader.loadTestsFromName.<locals>.<lambda>zNo such file %szUnresolvable test name %s)&rN   rO   r&   r   rP   rX   r   r    callresolver   r0   r   rw   r   r   r   r   r   rr   r   ZbeforeImportZafterImportr!   ZimportFromPathr[   r\   r]   r^   Ztotupler   r   rq   rV   r   rU   rY   OSError)r+   namer   rG   suite
plug_testsaddrr   objrs   packager,   r   r-   rX   i  s    



   
 





zTestLoader.loadTestsFromNamec                 C   sP   | j j||}|r@|\}}|r@| | |tj| ||gS tj| ||S )zSLoad tests from all names, returning a suite containing all
        tests.
        )r   rP   loadTestsFromNamesr&   r)   r   )r+   namesr   Zplug_resr   r,   r,   r-   r     s    zTestLoader.loadTestsFromNamesc                    sd   g }| j j}||D ]}|| q|s:tt| |S |dd tt| |D  | |S )z6Load tests from a unittest.TestCase subclass.
        c                 S   s   g | ]}|qS r,   r,   rg   caser,   r,   r-   rh     s     z4TestLoader.loadTestsFromTestCase.<locals>.<listcomp>)r   rP   loadTestsFromTestCaserZ   superr   r   r&   )r+   r<   r=   rP   r   r   r,   r-   r     s    z TestLoader.loadTestsFromTestCasec                    s^    j fdd} fddt|t D }jj D ]}|| q<t| dS )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.
        c                 S   s6   t || d }t|r t||}nt|s,dS ||S r.   r/   r1   r,   r,   r-   r6     s    z1TestLoader.loadTestsFromTestClass.<locals>.wantedc                    s   g | ]} t | qS r,   )r   r0   r   r3   r+   r,   r-   rh     s   z5TestLoader.loadTestsFromTestClass.<locals>.<listcomp>r   )	r$   r9   r:   r   rP   loadTestsFromTestClassrZ   r&   r   )r+   r3   r6   r=   re   r,   r   r-   r     s    z!TestLoader.loadTestsFromTestClassc                 C   s   z|  ||W S  ttfk
r(    Y nb   t }zt|}W n$ tk
rX    Y n   d }Y nX t|d |d |d |d Y S X d S )Nr   rk   rl   rm   )	_makeTestr[   r\   r]   r^   r   r   )r+   r   r   rs   r   r,   r,   r-   r   	  s    
zTestLoader.makeTestc              	   C   s  g }zt |}W n$ tk
r&    Y n   d}Y nX | jj||D ]}|| qFz|rh| |W S W nH ttfk
r    Y n0   t	 }t
|d |d |d |d Y S X t|r|rt|tjst||}t|tjr|S t|r4|r|j|jkrt||j}t|tjr(| |S | |S nt|r|dkrN|j}t|tjrf||jS t|r|| ||S t|S n\t|r|r|j|jkrt||j}t|r| ||S t |S nt
t!d| |dS dS )zVGiven a test object and its parent, return a test case
        or test suite.
        Nr   rk   rl   rm   zCan't make a test from %s)"r   r[   r   rP   r   rZ   r&   r\   r]   r^   r   r   rw   types
ModuleTyper   r)   ZTestCaser   r   r   r   
issubclassr   r   r   r   r   r   r   r   r   r   r   )r+   r   r   r   r   re   rs   r,   r,   r-   r     sZ    
"










zTestLoader._makeTestc                 C   sH   |}| d}|D ]}|t||d }}q|dkr@ttd| }||fS )z#Resolve name within module
        rD   NzNo such test %s)splitr0   r   rq   )r+   r   r   r   partspartr   r,   r,   r-   r   S  s    
zTestLoader.resolvec                 C   sd   t |ts|t  }}nDt|dkr6|d t  }}n&t|dksFt|d |dd  }}||fS )zGiven the yield value of a test generator, return a func and args.

        This is used in the two loadTestsFromGenerator* methods.

        rk   r   N)rw   tuplelenAssertionError)r+   re   r|   ru   r,   r,   r-   rv   _  s    
zTestLoader.parseGeneratedTest)NNNN)NF)NF)N)N)N)r   r   __qualname____doc__r   r!   r    r$   r&   r*   r7   r@   rC   rI   rY   r   r   r   rX   r   r   r   r   r   r   rv   __classcell__r,   r,   r   r-   r   -   s2     
*M,
4
l


;)<r   loggingrQ   r]   r)   r   inspectr   Znose.pyversionr   r   Z	nose.caser   r   Znose.failurer   Znose.configr   Znose.importerr	   r
   r   Znose.selectorr   r   Z	nose.utilr   r   r   r   r   r   r   r   r   r   Z
nose.suiter   r   r   r   r   rx   	getLoggerr   rN   r?   normpathr"   abspathr#   joinrT   r   rV   isfilerU   __all__r   r   r,   r,   r,   r-   <module>   s:   
0
    E