B
    ܇\f                 @   s  d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZddlZddl	Z	ddl
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 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ dZdZdZdZdZ dZ!dd Z"G dd de#Z$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' Z-ej.d(d) Z/G d*d+ d+e#Z0G d,d- d-e1Z2G d.d/ d/e3Z4G d0d1 d1e1Z5ej6G d2d3 d3e7Z8G d4d5 d5ej9Z:dS )6zF core implementation of testing process: init, session, runtest loop.     )absolute_import)division)print_functionN)nodes)directory_arg)hookimpl)
UsageError)PYTEST_CONFIG_GLOBAL)exit)collect_one_node               c             C   s  | j ddddddddd	d
dgd | j dddg d | dd}|jdddddddf |jdddtdddd |jddd d! |jd"d#td$d%d& |jd'dd(d)d*d+ |jd,dd-d.d/ | d0d1}|jd2d3dd4d!f |jd5dd6d! |jd7d8d9d:d; |jd<d8d9d=d; |jd>d8d?d@d; |jdAdBd dCtjtdAdDdEdF |jdGddHd(dIdJ |jdKdLddMd(dNdJ |jdOddPd(dQdJ | dRdS}|jdTdUd dCdVdW d S )XNnorecursedirsz)directory patterns to avoid for recursionargsz.*ZbuildZdistZCVSZ_darcsz{arch}z*.eggvenv)typedefaultZ	testpathsz[directories to search for tests when no files or directories are given in the command line.Zgeneralzrunning and selection optionsz-xz--exitfirstZstore_constmaxfailr   z-exit instantly on first error or failed test.)actiondestZconsthelpz	--maxfailnumZstorer   z(exit after first num failures or errors.)metavarr   r   r   r   r   z--strict
store_truez8marks not registered in configuration file raise errors.)r   r   z-cfileZinifilenamezcload configuration from `file` instead of trying to locate one of the implicit configuration files.)r   r   r   r   z--continue-on-collection-errorsFcontinue_on_collection_errorsz5Force test execution even if collection errors occur.)r   r   r   r   z	--rootdirrootdirzDefine root directory for tests. Can be relative path: 'root_dir', './root_dir', 'root_dir/another_dir/'; absolute path: '/home/user/root_dir'; path with variables: '$HOME/root_dir'.)r   r   r   collect
collectionz--collectonlyz--collect-onlyz'only collect tests, don't execute them.z--pyargsz2try to interpret all arguments as python packages.z--ignoreappendpathz.ignore path during collection (multi-allowed).)r   r   r   z--ignore-globz6ignore path pattern during collection (multi-allowed).z
--deselectZnodeid_prefixz0deselect item during collection (multi-allowed).z--confcutdirZ
confcutdirdir)Zoptnamez2only load conftest.py's relative to specified dir.)r   r   r   r   r   z--noconftestZ
noconftestz!Don't load any conftest.py files.)r   r   r   r   z--keepduplicatesz--keep-duplicateskeepduplicateszKeep duplicate tests.z--collect-in-virtualenvcollect_in_virtualenvz2Don't ignore tests in a local virtualenv directoryZdebugconfigz(test session debugging and configurationz
--basetempZbasetempz\base temporary directory for this test run.(warning: this directory is removed if it exists))r   r   r   r   )	ZaddiniZgetgroupZ
_addoptionintstrZ	addoption	functoolspartialr   )parsergroup r-   +lib/python3.7/site-packages/_pytest/main.pypytest_addoption$   s    

r/   c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
_ConfigDeprecatedc             C   s   || j d< d S )N_config)__dict__)selfconfigr-   r-   r.   __init__   s    z_ConfigDeprecated.__init__c             C   s   t jtdd t| j|S )Nr   )
stacklevel)warningswarnr	   getattrr1   )r3   attrr-   r-   r.   __getattr__   s    z_ConfigDeprecated.__getattr__c             C   s   t jtdd t| j||S )Nr   )r6   )r7   r8   r	   setattrr1   )r3   r:   valr-   r-   r.   __setattr__   s    z_ConfigDeprecated.__setattr__c             C   s   d t| j| jS )Nz{}({!r}))formatr   __name__r1   )r3   r-   r-   r.   __repr__   s    z_ConfigDeprecated.__repr__N)r@   
__module____qualname__r5   r;   r>   rA   r-   r-   r-   r.   r0      s   r0   c             C   s   t | td_d S )NZpytest)r0   
__import__r4   )r4   r-   r-   r.   pytest_configure   s    rE   c          
   C   sx  t | }t|_d}z(y2|   d}| jj|d d}|| |pBd|_W n tk
r^    Y n tk
rv   t|_Y n t	t
jfk
r   tjj }t}|dkrt|jt
jrtjd|j|jj |jjdk	r|jj}| jj|d ||_Y nD   tjj }| || j t|_|tr4tjd Y nX W dd}|j !  |dkrh| jj"||jd	 | #  X |jS )
zSkeleton command line programr   r   )sessionr   z{}: {}
N)excinfoz&mainloop: caught Spurious SystemExit!
)rF   
exitstatus)$SessionEXIT_OKrH   Z_do_configurehookZpytest_sessionstartr   FailedEXIT_TESTSFAILEDKeyboardInterruptr
   	Exception_pytest_codeZExceptionInfoZfrom_currentEXIT_INTERRUPTED
isinstancevaluesysstderrwriter?   typenamemsg
returncodeZpytest_keyboard_interruptZnotify_exceptionoptionEXIT_INTERNALERRORZerrisinstance
SystemExitstartdirchdirZpytest_sessionfinishZ_ensure_unconfigure)r4   ZdoitrF   Z	initstaterG   rH   r-   r-   r.   wrap_session   sH    




r`   c             C   s
   t | tS )N)r`   _main)r4   r-   r-   r.   pytest_cmdline_main   s    rb   c             C   s8   | j j|d | j j|d |jr&tS |jdkr4tS dS )z] default command line protocol for initialization, session,
    running tests and reporting. )rF   r   N)rK   pytest_collectionpytest_runtestlooptestsfailedrM   testscollectedEXIT_NOTESTSCOLLECTED)r4   rF   r-   r-   r.   ra      s    
ra   c             C   s   |   S )N)perform_collect)rF   r-   r-   r.   rc     s    rc   c             C   s   | j r | jjjs | d| j  | jjjr.dS xpt| jD ]b\}}|d t| jk rb| j|d  nd }|jj	j
||d | jr| | j| jr:| | jq:W dS )Nz%d errors during collectionTr   )itemnextitem)re   r4   r[   r   InterruptedZcollectonly	enumerateitemslenrK   Zpytest_runtest_protocol
shouldfailrL   
shouldstop)rF   iri   rj   r-   r-   r.   rd     s    
$rd   c                sD   |  tjdrdnd}| s&dS d t fdd| D S )zAttempts to detect if ``path`` is the root of a Virtual Environment by
    checking for the existence of the appropriate activate scriptwinZScriptsbinF)Zactivatezactivate.cshzactivate.fishZActivatezActivate.batzActivate.ps1c                s   g | ]}|j  kqS r-   )basename).0fname)	activatesr-   r.   
<listcomp>$  s    z_in_venv.<locals>.<listcomp>)joinrU   platform
startswithisdiranylistdir)r#   Zbindirr-   )rw   r.   _in_venv  s
    r   c                s   |j d  d}|pg }|d}|r<|dd |D  tj |krPdS |j d  d}|phg }|d}|r|d	d |D  t fd
d|D rdS |d}|st rdS dS )NZcollect_ignore)r#   ignorec             S   s   g | ]}t j|qS r-   )pyr#   local)ru   xr-   r-   r.   rx   ,  s    z)pytest_ignore_collect.<locals>.<listcomp>TZcollect_ignore_globZignore_globc             S   s   g | ]}t j|qS r-   )r   r#   r   )ru   r   r-   r-   r.   rx   7  s    c             3   s&   | ]}t  t t|V  qd S )N)fnmatchsixZ	text_type)ru   Zglob)r#   r-   r.   	<genexpr>:  s   z(pytest_ignore_collect.<locals>.<genexpr>r&   F)	Z_getconftest_pathlistdirpath	getoptionextendr   r#   r   r}   r   )r#   r4   Zignore_pathsZ
excludeoptZignore_globsZexcludegloboptZallow_in_venvr-   )r#   r.   pytest_ignore_collect'  s*    




r   c             C   st   t |dpg }|sd S g }g }x.| D ]&}|j|rD|| q(|| q(W |rp|jj|d || d d < d S )NZdeselect)rm   )tupler   nodeidr{   r"   rK   Zpytest_deselected)rm   r4   Zdeselect_prefixesZ	remainingZ
deselectedZcolitemr-   r-   r.   pytest_collection_modifyitemsF  s    
r   c              c   sB   t jr8ddd} tjj}| tj_z
dV  W d|tj_X ndV  dS )a  Patch bug in pkgutil.ImpImporter.find_module

    When using pkgutil.find_loader on python<3.4 it removes symlinks
    from the path due to a call to os.path.realpath. This is not consistent
    with actually doing the import (in these versions, pkgutil and __import__
    did not share the same underlying code). This can break conftest
    discovery for pytest where symlinks are involved.

    The only supported python<3.4 by pytest is python 2.7.
    Nc             S   sz   | dd }||kr$| jd kr$d S | jd kr4d }n| jg}ytj||\}}}W n tk
rh   d S X t||||S )N.)splitr#   pkgutilimpfind_moduleImportError	ImpLoader)r3   fullnamer#   subnamer   filenameetcr-   r-   r.   find_module_patchedf  s    
z1_patched_find_module.<locals>.find_module_patched)N)r   PY2r   ImpImporterr   )r   Zold_find_moduler-   r-   r.   _patched_find_moduleX  s    

r   c               @   s   e Zd Zdd Zdd ZdS )FSHookProxyc             C   s   || _ || _|| _d S )N)fspathpmremove_mods)r3   r   r   r   r-   r-   r.   r5     s    zFSHookProxy.__init__c             C   s    | j j|| jd}|| j|< |S )N)Zremove_plugins)r   Zsubset_hook_callerr   r2   )r3   namer   r-   r-   r.   r;     s    
zFSHookProxy.__getattr__N)r@   rB   rC   r5   r;   r-   r-   r-   r.   r     s   r   c               @   s   e Zd ZdZdS )NoMatchz4 raised if matching cannot locate a matching names. N)r@   rB   rC   __doc__r-   r-   r-   r.   r     s   r   c               @   s   e Zd ZdZdZdS )rk   z" signals an interrupted test run. builtinsN)r@   rB   rC   r   r-   r-   r-   r.   rk     s   rk   c               @   s   e Zd ZdZdS )rL   z$ signals a stop as failed test run. N)r@   rB   rC   r   r-   r-   r-   r.   rL     s   rL   c               @   s   e Zd Ze Zdd ZdS )_bestrelpath_cachec             C   s   | j |}|| |< |S )N)r#   Zbestrelpath)r3   r#   rr-   r-   r.   __missing__  s    z_bestrelpath_cache.__missing__N)r@   rB   rC   r:   Zibr#   r   r-   r-   r-   r.   r     s   r   c               @   s   e Zd ZeZeZdd Zdd Zedddd Zeddd	d
 Z	e	Z
dd Zdd Zd)ddZdd Zdd Zdd Zd*ddZdd Zejredd Znedd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( ZdS )+rI   c             C   s   t jj| |jd || dd d| _d| _d| _d| _|jj	
d| _|d| _tj | _t | _i | _t|j| _i | _| jjj| dd d S )	N )parentr4   rF   r   r   Fr!   r   rF   )r   )r   FSCollectorr5   r   re   rf   rp   ro   tracerootgetZgetini_norecursepatternsr   r#   r   r^   	frozenset_initialpaths_node_cacher   _bestrelpathcache
_pkg_rootsr4   pluginmanagerregister)r3   r4   r-   r-   r.   r5     s    zSession.__init__c             C   s
   | j | S )N)r   )r3   Z	node_pathr-   r-   r.   _node_location_to_relpath  s    z!Session._node_location_to_relpathT)Ztryfirstc             C   s(   | j r| | j | jr$| | jd S )N)ro   rL   rp   rk   )r3   r-   r-   r.   pytest_collectstart  s    zSession.pytest_collectstartc             C   sH   |j rDt|dsD|  jd7  _| jd}|rD| j|krDd| j | _d S )NZwasxfailr   r   zstopping after %d failures)Zfailedhasattrre   r4   getvaluero   )r3   reportr   r-   r-   r.   pytest_runtest_logreport  s
    z Session.pytest_runtest_logreportc             C   s
   || j kS )N)r   )r3   r#   r-   r-   r.   
isinitpath  s    zSession.isinitpathc             C   s<   | j j}||}|j|}|r0t|||}n| j j}|S )N)r4   r   Z_getconftestmodulesZ_conftest_plugins
differencer   rK   )r3   r   r   Zmy_conftestmodulesr   proxyr-   r-   r.   gethookproxy  s    
zSession.gethookproxyNc          	   C   sT   | j j}z.| ||}| j j  |j| | j |d W d |j| d X t|| _|S )N)rF   r4   rm   )rF   )	r4   rK   _perform_collectr   Zcheck_pendingr   Zpytest_collection_finishrn   rf   )r3   r   genitemsrK   rm   r-   r-   r.   rh     s    
zSession.perform_collectc             C   s8  |d kr| j j}| d| | | jj jd7  _g | _g }g | _g  | _}x0|D ](}| |}| j	| |	|d  qPW t
|| _t| }| jj|d | jj jd8  _| jrg }x6| jD ],\}}	d||	jd f }
|	d||
f  qW t| |s|jS |jr0x"|jD ]}| j| | qW |S d S )Nrh   r   r   )r   z(no name %r in any of %r)znot found: %s
%s)r4   r   r   r   indent	_notfound_initialpartsrm   	_parseargr"   r   r   r   ihookpytest_collectreportr   resultpassedr   r   )r3   r   r   Zinitialpathsrm   argpartsreperrorsexclinenoder-   r-   r.   r     s8    



zSession._perform_collectc          	   c   s   x| j D ]}dtt|}| d| | jj jd7  _yx| |D ]
}|V  qHW W n, tk
r   | j	
|t d f Y nX | jj jd8  _qW d S )Nz::zprocessing argumentr   )r   ry   mapr(   r   r   r   _collectr   r   r"   rU   exc_info)r3   Zinitialpartr   r   r-   r-   r.   r      s    zSession.collectc             c   sn  ddl m} | |}|d}| jjjs| jj}xt|	 D ]}|j
rX|j
|rXP | r@|d}| r@|| jkr@| j|dd}|r@t|d |r|d | j|< |d g| j|d j< q@W |jddr|rtd|f t }	x|j| j| jd	d	d
D ]}
|
 }||	krh|	| |d}| rhx0| |D ]"}|V  t||rB|| j|< qBW || jkrxqxL| |
D ]>}t||jf}|| jkr| j| V  n|| j|< |V  qW qW n|jddst|| jkr| j| }n.| j|j| }|j|dd}|r&|| j|< |  ||}|j!dkrTt"|d # V  d S x|D ]}|V  qZW d S )Nr   )Packagez__init__.pyF)handle_dupesr   )r$   zinvalid arg %rT)ZfilZrecZbfsort)r   )$Z_pytest.pythonr   r   popr4   r[   Zdoctestmodulesr   reversedr   Z_confcutdirZreltor|   ry   isfiler   _collectfilerS   r   r   checkAssertionErrorsetZvisit_visit_filter_recurser   addexistsr   r   dirname
matchnodesrt   nextr    )r3   r   r   namesZargpathr   r   ZpkginitcolZ	seen_dirsr#   r   r   keyZcollect_rootmyr-   r-   r.   r     sh    











zSession._collectc             C   s   |  s(td|| | | f | |}| |sP|j|| jdrPdS |r| j	d}|s| jj
j}||krzdS || |j|| dS )Nz1%r is not a file (isdir=%r, exists=%r, islink=%r))r#   r4   r-   r%   )r#   r   )r   r   r|   r   islinkr   r   r   r4   r   r   Z_duplicatepathsr   Zpytest_collect_file)r3   r#   r   r   r%   Zduplicate_pathsr-   r-   r.   r   k  s"    



zSession._collectfilec             C   sl   |j dkrdS | | }|j|| jdr0dS x| jD ]}|j|dr8dS q8W | |}|j|| d dS )N__pycache__F)r#   r4   )r   )r#   r   T)rt   r   r   r   r4   r   r   Zpytest_collect_directory)r3   r   r   Zpatr-   r-   r.   r     s    

zSession._recursec             C   s   | j ddo| jd S )Nr   )r   z*.pyc)r   Zstrpathendswith)fr-   r-   r.   r     s    zSession._visit_filterc             C   s   | j ddS )Nr   )r   )r   )r   r-   r-   r.   r     s    c          	   C   s   y t   t|}W dQ R X W n tk
r4   |S X |dkrB|S y t   ||}W dQ R X W n$ tk
r   |j| d j}Y nX ||rt	j
|}|S )z%Convert a dotted module name to path.Nr   )r   r   find_loaderr   get_filenameAttributeErrormodulesco_filename
is_packageosr#   r   )r3   r   loaderr#   r-   r-   r.   _tryconvertpyarg  s    
zSession._tryconvertpyargc             C   s   t |d}| jjjr*| |d |d< |d dtj}| jj	j
|dd}| s|| jjjrptd| d td| | |d< |S )	z> return (fspath, names) tuple after checking the file exists. z::r   /T)abszfile or package not found: z (missing __init__.py?)zfile not found: )r(   r   r4   r[   Zpyargsr   replacer   sepZinvocation_dirry   r   r   realpath)r3   r   r   relpathr#   r-   r-   r.   r     s    

zSession._parseargc             C   sr   |  d|| | j j jd7  _| ||}t|}|  d|d | j j jd8  _|dkrnt||d d |S )Nr   r   zmatchnodes finished -> r   r   )r   r   r   _matchnodesrn   r   )r3   matchingr   r   r   r-   r-   r.   r     s    zSession.matchnodesc             C   sH  |r|s|S |d }|st |dd  }g }x|D ]
}t|tjrV|s4|| q4t|tjsft t||jf}|| jkr| j| }nt	|}|| j|< |j
r2d}	xD|jD ]:}
|
j|ks|
jdd |kr|| |
g| d}	qW |	s@t|jdkr@|
jdkr@|d| || |
g| q4|jj|d q4W |S )Nr   r   F[Tz())r   )r   rS   r   Itemr"   	Collectorr   r   r   r   r   r   r   r   r   r   rn   insertr   r   )r3   r   r   r   Z	nextnamesZresultnodesr   r   r   Zhas_matchedr   r-   r-   r.   r     s8    


"zSession._matchnodesc             c   s   |  d| t|tjr.|jj|d |V  nVt|tjs>tt|}|j	rvx(|j
D ]}x| |D ]
}|V  qdW qTW |jj|d d S )Nr   )ri   )r   )r   rS   r   r  r   Zpytest_itemcollectedr  r   r   r   r   r   r   )r3   r   r   Zsubnoder   r-   r-   r.   r     s    zSession.genitems)NT)T)r@   rB   rC   rk   rL   r5   r   r   r   r   r   r   r   rh   r   r    r   r   r   r   r   staticmethodr   r   r   r   r   r   r-   r-   r-   r.   rI     s.   
 M
%rI   );r   Z
__future__r   r   r   
contextlibr   r)   r   r   rU   r7   r:   r   r   Z_pytest._coderP   r   Z_pytest.configr   r   r   Z_pytest.deprecatedr	   Z_pytest.outcomesr
   Z_pytest.runnerr   rJ   rM   rR   r\   ZEXIT_USAGEERRORrg   r/   objectr0   rE   r`   rb   ra   rc   rd   r   r   r   contextmanagerr   r   rO   r   rN   rk   rL   sdictr   r   rI   r-   r-   r-   r.   <module>   s^    +(	