B
    ֐<[c                 @   s<  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) e*e+Z,ej-j.Z/ej-j0Z1ej-j2Z3ej-j4Z5ej-j6Z7ddgZ8G dd dej9Z9e9Z: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,   *lib/python3.7/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   
wantMethod)attrclsselitemr,   r,   r-   wantedl   s    z+TestLoader.getTestCaseNames.<locals>.wantedZrunTest)r   ZgetTestCaseNamesCompatr)   r   getTestCaseNamesr$   listfilterdirhasattrZsortTestMethodsUsingr   r   )r+   testCaseClassr4   casesr,   r,   r-   r5   e   s    zTestLoader.getTestCaseNamesc             C   s   |d krdS || j kS )NF)r(   )r+   pathr,   r,   r-   _haveVisited}   s    zTestLoader._haveVisitedc             C   s   |d k	r| j | d S )N)r(   add)r+   r<   r,   r,   r-   _addVisitedPath   s    zTestLoader._addVisitedPathc          	   #   s  t d| jj}|| jjr2t|j}t|}t	|t
jj  x|D ]}|drfqVtt|| t }d}|rd}j }n$t }|r|drqVj }t }	|rV sV  |r|  |drj ddV  n V  |  qV|	r4j ddV  qV fdd	V  qVW g }
x||D ]}|
| q^W y|
r|
V  W n< tt fk
r    Y n"   t!t"#  gV  Y nX jjrx|D ]}t$| qW |%| 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 )N)loadTestsFromDirr,   )
entry_pathr+   r,   r-   <lambda>   s    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=   r?   ZbeforeContextendswithloadTestsFromNameloadTestsFromFileZafterContextr&   rC   appendKeyboardInterrupt
SystemExitr   sysexc_infor   ZafterDirectory)r+   r<   rH   Zpaths_addedentriesentryis_filer4   is_dir
is_packageteststestpr,   )rD   r+   r-   rC      sd    







zTestLoader.loadTestsFromDirc             C   s   t d| yRdd | jj|D }|rBdd |D }| |S t|d  td| W nP t	t
fk
rx    Y n8   t }| t|d |d |d	 |d
d
fdgS 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,   ).0r]   r,   r,   r-   
<listcomp>   s    z0TestLoader.loadTestsFromFile.<locals>.<listcomp>c             S   s   g | ]}|r|qS r,   r,   )r_   Z_fr,   r,   r-   r`      s    rz!Unable to load tests from file %sr         N)address)rF   rG   r   rH   rQ   r&   openclose
ValueErrorrS   rT   rU   rV   r   )r+   filenamer\   excr,   r,   r-   rQ      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   yDx>|  D ]4} |\}}t|s.t||}t||| dV  q
W W nJ tk
rZ    Y n6   t }t|d |d |d t dV  Y nX d S )N)arg
descriptorr   rb   rc   )rd   )	parseGeneratedTestcallabler.   r   rS   rU   rV   r   r   )gmr]   	test_funcrj   ri   )	generatorr+   r,   r-   generate   s    
z3TestLoader.loadTestsFromGenerator.<locals>.generateF)context	can_split)r&   )r+   rq   modulerr   r,   )rq   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   y~xx|  D ]n} |\}}t|s4t|t||}t|rNt||| dV  q
t|rht| ||dV  q
ttd| V  q
W W nJ tk
r    Y n6   t	
 }t|d |d |d t dV  Y nX d S )N)rj   rk   )r]   rj   z%s is not a callable or methodr   rb   rc   )rd   )rl   rm   r   r.   r   r   r   	TypeErrorrS   rU   rV   r   )rn   cr]   rp   rj   ri   )rq   r+   r,   r-   rr     s$    z9TestLoader.loadTestsFromGeneratorMethod.<locals>.generateF)rs   rt   )r9   __self__	__class____name__r.   r&   )r+   rq   r1   Zinstmethodrr   r,   )rq   r+   r-   loadTestsFromGeneratorMethod  s    

z'TestLoader.loadTestsFromGeneratorMethodFc                s  t d  g }g }g }|r(j rxZt D ]N}t |d}t|rbj|r|| q2t	|r2j
|r2|| q2W t|dd  t|t  fdd|| D }t dg }	|rtjtj|}x|	D ]~}
t d|
 t d	|tj|
tjtj|
 jjs@|r@tjtj|
|rtj|
r||
 qW x$jj |D ]}|| qrW 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 )N)r{   )xr,   r,   r-   rE   J  s    z0TestLoader.loadTestsFromModule.<locals>.<lambda>c                s   g | ]}j | d qS ))parent)makeTest)r_   t)ru   r+   r,   r-   r`   L  s    z2TestLoader.loadTestsFromModule.<locals>.<listcomp>__path__zLoad tests from module path %s?z!path: %s os.path.realpath(%s): %s)rs   )rF   rG   r$   Z
wantModuler8   r.   r   Z	wantClassrR   r   ZwantFunctionr   r   rI   r<   normcaserealpathr   ZtraverseNamespacerK   isdirextendrC   rH   loadTestsFromModuler&   r   )r+   ru   r<   rB   r\   Ztest_classesZ
test_funcsr3   r]   Zmodule_pathsZmodule_pathr,   )ru   r+   r-   r   4  s>    

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ry\|jdkrt|j}nBjj|j|j zj|j|j}W djj|j|j X W nN ttfk
r\    Y n4   t }	|t|	d |	d |	d	 | d
gS |jr|j|S j||j|dS n|jrb|j |jrt }
|
dkr|tt d|j f | d
gS j|j|
dS t! r0t" fddS t# rD$ 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 %s)rs   r   rb   rc   )rd   )rB   z>Can't find callable %s in file %s: file is not a python module)ru   c                  s
     S )N)rC   r,   )r<   r+   r,   r-   rE     s    z.TestLoader.loadTestsFromName.<locals>.<lambda>zNo such file %szUnresolvable test name %s)&rF   rG   r&   r   rH   rP   r   r    Zcallresolver   r.   r{   
isinstancer   r   r   r   ru   rh   r   ZbeforeImportr!   ZimportFromPathZafterImportrS   rT   rU   rV   Ztotupler   r   rg   rN   r   rM   rQ   OSError)r+   nameru   rB   suite
plug_testsaddrr   objri   packager,   )r<   r+   r-   rP   h  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   rH   loadTestsFromNamesr&   r)   r   )r+   namesru   Zplug_resr   r,   r,   r-   r     s    zTestLoader.loadTestsFromNamesc                sh   g }| j j}x||D ]}|| qW |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,   )r_   caser,   r,   r-   r`     s    z4TestLoader.loadTestsFromTestCase.<locals>.<listcomp>)r   rH   loadTestsFromTestCaserR   superr   r   r&   )r+   r:   r;   rH   r   )rz   r,   r-   r     s    z TestLoader.loadTestsFromTestCasec                sb    j fdd} fddt|t D }x jj D ]}|| q>W 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 )NF)r.   r   r   r   r/   )r0   r1   r2   r3   r,   r,   r-   r4     s    z1TestLoader.loadTestsFromTestClass.<locals>.wantedc                s   g | ]} t | qS r,   )r   r.   )r_   r   )r1   r+   r,   r-   r`     s   z5TestLoader.loadTestsFromTestClass.<locals>.<listcomp>)rs   )	r$   r7   r8   r   rH   loadTestsFromTestClassrR   r&   r   )r+   r1   r4   r;   r]   r,   )r1   r+   r-   r     s    z!TestLoader.loadTestsFromTestClassc             C   s   y|  ||S  ttfk
r&    Y n\   t }yt|}W n$ tk
rV    Y n   d }Y nX t|d |d |d |dS d S )Nr   rb   rc   )rd   )	_makeTestrS   rT   rU   rV   r   r   )r+   r   r   ri   r   r,   r,   r-   r     s    
zTestLoader.makeTestc          	   C   s  g }yt |}W n$ tk
r&    Y n   d}Y nX x"| jj||D ]}|| qHW y|rj| |S W nB ttfk
r    Y n*   t	 }t
|d |d |d |dS t|r|rt|tjst||}t|tjr|S t|r0|r|j|jkrt||j}t|tjr$| |S | |S nt|r|dkrJ|j}t|tjrb||jS t|rx| ||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   rb   rc   )rd   zCan't make a test from %s)"r   rS   r   rH   r   rR   r&   rT   rU   rV   r   r   r   types
ModuleTyper   r)   ZTestCaser   r   r{   r   
issubclassr   r   r   rz   r   r}   r   r   rv   r   rw   )r+   r   r   r   r   r]   ri   r,   r,   r-   r     sX    











zTestLoader._makeTestc             C   sL   |}| d}x|D ]}|t||d }}qW |dkrDttd| }||fS )z#Resolve name within module
        r@   NzNo such test %s)splitr.   r   rg   )r+   r   ru   r   partspartr   r,   r,   r-   r   R  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.

        rb   r   N)r   tuplelenAssertionError)r+   r]   rp   rj   r,   r,   r-   rl   ^  s    
zTestLoader.parseGeneratedTest)NNNN)NF)NF)N)N)N)r{   r   __qualname____doc__r   r!   r    r$   r&   r*   r5   r=   r?   rC   rQ   rv   r}   r   rP   r   r   r   r   r   r   rl   __classcell__r,   r,   )rz   r-   r   ,   s0    
)M,
4
l


;);r   ZloggingrI   rU   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   Z	getLoggerr{   rF   r<   normpathr"   abspathr#   joinrL   r   rN   isfilerM   __all__r   r   r,   r,   r,   r-   <module>   s8   0
    E