B
    WcF                 @   sN  d 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d
lmZ ddlmZ ddlmZ ddlm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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dl&m'Z' ddl&m(Z( ddl&m)Z) dd l&m*Z* erdd!l+m,Z, dd"l-m.Z. dd#l/m0Z0 edd$d%d&Z1d'dd(d)d*Z2d+dd,d-d.Z3d+dd,d/d0Z4e$e
e$ e5d1d2d3Z6dbe$e5e
e$ e	e d5d6d7Z7e$dd8d9d:Z8e$dd8d;d<Z9e$dd8d=d>Z:e$e
e$ dd1d?d@Z;e$e
dA ddBdCdDZ<ee
ee=e=e=f  dEdFdGZ>dce$dAe5edHdIdJZ?dKee5dLdMdNZ@e$dAdOdBdPdQZAedRd4dSZBeejCdTdTd4dUG dVdW dWeeB ZDe$eDd edXdYdZZEe#ed[d\d]ZFG d^d_ d_ZGe#ed[d`daZHdS )dz3Basic collect and runtest protocol implementations.    N)Callable)cast)Dict)Generic)List)Optional)Tuple)Type)TYPE_CHECKING)TypeVar)Union   )
BaseReport)CollectErrorRepr)CollectReport)
TestReport)timing)ExceptionChainRepr)ExceptionInfo)TerminalRepr)final)Parser)check_ispytest)	Collector)Item)Node)Exit)OutcomeException)Skipped)TEST_OUTCOME)Literal)Session)TerminalReporter)parserreturnc             C   s@   | j dddd}|jddtd ddd	 |jd
dtdddd	 d S )Nzterminal reportingZ	ReportingZgeneral)Zafterz--durationsZstoreNz1Show N slowest setup/test durations (N=0 for all))actiontypedefaultmetavarhelpz--durations-ming{Gzt?zJMinimal duration in seconds for inclusion in slowest list. Default: 0.005.)ZgetgroupZ	addoptionintfloat)r#   group r.   -lib/python3.7/site-packages/_pytest/runner.pypytest_addoption0   s    r0   r"   )terminalreporterr$   c       	      C   s(  | j jj}| j jj}| j d}|d kr,d S | }g }x4|j D ]&}x |D ]}t|drJ|| qJW q@W |srd S |j	dd dd |s|
dd n|
dd	|  |d | }xpt|D ]d\}}|d
k r|j|k r|d |dt|| |f  P ||jdd|jdd|j  qW d S )Nverbosedurationc             S   s   | j S )N)r3   )xr.   r.   r/   <lambda>S       z)pytest_terminal_summary.<locals>.<lambda>T)keyreverse=zslowest durationszslowest %s durations    z>(%s durations < %gs hidden.  Use -vv to show these durations.)z02.2fzs z<8 )configZoption	durationsdurations_mingetvalueZstatsvalueshasattrappendsortZ	write_sep	enumerater3   Z
write_linelenwhennodeid)	r1   r>   r?   r2   ZtrZdlistZreplistrepir.   r.   r/   pytest_terminal_summaryE   s4    




rK   r!   )sessionr$   c             C   s   t  | _d S )N)
SetupState_setupstate)rL   r.   r.   r/   pytest_sessionstarte   s    rO   c             C   s   | j d  d S )N)rN   teardown_exact)rL   r.   r.   r/   pytest_sessionfinishi   s    rQ   )itemnextitemr$   c             C   s:   | j }|j| j| jd t| |d |j| j| jd dS )N)rH   location)rS   T)ihookZpytest_runtest_logstartrH   rT   runtestprotocolZpytest_runtest_logfinish)rR   rS   rU   r.   r.   r/   pytest_runtest_protocolm   s
    rW   T)rR   logrS   r$   c             C   s   t | d}|r| js|   t| d|}|g}|jrj| jddrJt|  | jddsj|t| d| |t| d||d |rd| _d | _	|S )	N_requestsetupZ	setupshowFZ	setuponlycallteardown)rS   )
rB   rY   Z_initrequestcall_and_reportpassedr=   	getoptionshow_test_itemrC   Zfuncargs)rR   rX   rS   Z
hasrequestrI   reportsr.   r.   r/   rV   u   s    

rV   )rR   r$   c             C   s^   | j  }|  |d || j tt| dg }|rR|dd| |	  dS )zAShow test function, parameters and the fixtures of the test item.z        Zfixturenamesz (fixtures used: {})z, N)
r=   Zget_terminal_writerlinewriterH   sortedgetattrformatjoinflush)rR   ZtwZused_fixturesr.   r.   r/   r`      s    

r`   c             C   s   t | d | jj|  d S )NrZ   )_update_current_test_varrL   rN   rZ   )rR   r.   r.   r/   pytest_runtest_setup   s    
rj   c          
   C   s   t | d yt`t`t`W n tk
r.   Y nX y|   W nN tk
r } z0t|t_|t_|j	d k	slt
|j	jt_|W d d }~X Y nX d S )Nr[   )ri   sys	last_type
last_valuelast_tracebackAttributeErrorZruntest	Exceptionr'   __traceback__AssertionErrortb_next)rR   er.   r.   r/   pytest_runtest_call   s    


ru   c             C   s&   t | d | jj| t | d  d S )Nr\   )ri   rL   rN   rP   )rR   rS   r.   r.   r/   pytest_runtest_teardown   s    
rv   z$Literal['setup', 'call', 'teardown'])rR   rG   r$   c             C   sB   d}|r2| j  d| d}|dd}|tj|< ntj| dS )zUpdate :envvar:`PYTEST_CURRENT_TEST` to reflect the current item and stage.

    If ``when`` is None, delete ``PYTEST_CURRENT_TEST`` from the environment.
    ZPYTEST_CURRENT_TESTz () z(null)N)rH   replaceosenvironpop)rR   rG   Zvar_namevaluer.   r.   r/   ri      s    ri   )reportr$   c             C   s&   | j dkr"| jrdS | jrdS dS d S )N)rZ   r\   )errorEZERROR)skippedsZSKIPPED)r;   r;   r;   )rG   failedr   )r~   r.   r.   r/   pytest_report_teststatus   s    
r   )rR   rG   rX   r$   c             K   sP   t | |f|}| j}|j| |d}|r2|j|d t||rL|j| ||d |S )N)rR   r[   )r~   )noder[   r~   )call_runtest_hookrU   pytest_runtest_makereportZpytest_runtest_logreportcheck_interactive_exceptionpytest_exception_interact)rR   rG   rX   kwdsr[   hookr~   r.   r.   r/   r]      s    
r]   zCallInfo[object])r[   r~   r$   c             C   s8   | j dkrdS t|drdS t| j jttjfr4dS dS )zVCheck whether the call raised an exception that should be reported as
    interactive.NFZwasxfailT)excinforB   
isinstancer}   r   bdbZBdbQuit)r[   r~   r.   r.   r/   r      s    

r   zCallInfo[None]c                s   |dkrj j n6|dkr$j j n$|dkr6j j ndsHtd| tf}jddsf|tf7 }t	j
 fdd||d	S )
NrZ   r[   r\   FzUnhandled runtest hook case: Zusepdbc                  s    f diS )NrR   r.   r.   )rU   rR   r   r.   r/   r5     r6   z#call_runtest_hook.<locals>.<lambda>)rG   reraise)rU   rj   ru   rv   rr   r   r=   r_   KeyboardInterruptCallInfo	from_call)rR   rG   r   r   r.   )rU   rR   r   r/   r      s    



r   TResult)Z	covariantF)reprZinitZauto_attribsc            
   @   s   e Zd ZU dZee ed< eee  ed< e	ed< e	ed< e	ed< ded< d	d
ee eee  e	e	e	de
ddddZeedddZedddeeee eee df f  ddddZedddZdS )r   z/Result/Exception info of a function invocation._resultr   startstopr3   z/Literal['collect', 'setup', 'call', 'teardown']rG   F)	_ispytestN)resultr   r   r   r3   rG   r   r$   c            C   s0   t | || _|| _|| _|| _|| _|| _d S )N)r   r   r   r   r   r3   rG   )selfr   r   r   r   r3   rG   r   r.   r.   r/   __init__  s    zCallInfo.__init__)r$   c             C   s$   | j dk	rt| dtt| jS )zlThe return value of the call, if it didn't raise.

        Can only be accessed if excinfo is None.
        Nz has no valid result)r   ro   r   r   r   )r   r.   r.   r/   r   /  s    
zCallInfo.resultzCallable[[], TResult].zCallInfo[TResult])funcrG   r   r$   c          	   C   s   d}t  }t  }y
| }W n6 tk
rT   t }|dk	rLt|j|rL d}Y nX t  }|| }	t  }
| ||
|	|||ddS )av  Call func, wrapping the result in a CallInfo.

        :param func:
            The function to call. Called without arguments.
        :param when:
            The phase in which the function is called.
        :param reraise:
            Exception or exceptions that shall propagate if raised by the
            function, instead of being wrapped in the CallInfo.
        NT)r   r   r3   rG   r   r   r   )r   ZtimeZperf_counterBaseExceptionr   Zfrom_currentr   r}   )clsr   rG   r   r   r   Zprecise_startr   Zprecise_stopr3   r   r.   r.   r/   r   <  s*    

zCallInfo.from_callc             C   s6   | j d kr d| jd| jdS d| jd| j dS )Nz<CallInfo when=z	 result: >z	 excinfo=)r   rG   r   )r   r.   r.   r/   __repr__g  s    
zCallInfo.__repr__)N)__name__
__module____qualname____doc__r   r   __annotations__r   r   r,   boolr   propertyr   classmethodr   r	   r   r   strr   r.   r.   r.   r/   r     s   
 	$#r   )rR   r[   r$   c             C   s   t | |S )N)r   Zfrom_item_and_call)rR   r[   r.   r.   r/   r   m  s    r   )	collectorr$   c                s  t  fddd}d }|js$d}ntg}tjd}|d k	rJ||j t	|jj
t|rd} |jd}t	|tstt||j}|stt|j|j|jf}n4d} |j}t|d	st	|tstt|}|}|js|jnd }	t j|||	}
||
_|
S )
Nc                  s   t   S )N)listcollectr.   )r   r.   r/   r5   r  r6   z,pytest_make_collect_report.<locals>.<lambda>r   r^   unittestr   rb   r   Z
toterminal)r   r   r   r   rk   modulesgetrC   ZSkipTestr   r}   tupleZ_repr_failure_pyr   rr   r   Z	reprcrashr   pathlinenomessageZrepr_failurerB   r   r   r   rH   r[   )r   r[   ZlongreprZoutcomeZskip_exceptionsr   Zr_rZ	errorinfor   rI   r.   )r   r/   pytest_make_collect_reportq  s2    
r   c               @   s\   e Zd ZdZddddZeddddZeg ef e	dd	d
dZ
ee ddddZdS )rM   aE  Shared state for setting up/tearing down test items or collectors
    in a session.

    Suppose we have a collection tree as follows:

    <Session session>
        <Module mod1>
            <Function item1>
        <Module mod2>
            <Function item2>

    The SetupState maintains a stack. The stack starts out empty:

        []

    During the setup phase of item1, setup(item1) is called. What it does
    is:

        push session to stack, run session.setup()
        push mod1 to stack, run mod1.setup()
        push item1 to stack, run item1.setup()

    The stack is:

        [session, mod1, item1]

    While the stack is in this shape, it is allowed to add finalizers to
    each of session, mod1, item1 using addfinalizer().

    During the teardown phase of item1, teardown_exact(item2) is called,
    where item2 is the next item to item1. What it does is:

        pop item1 from stack, run its teardowns
        pop mod1 from stack, run its teardowns

    mod1 was popped because it ended its purpose with item1. The stack is:

        [session]

    During the setup phase of item2, setup(item2) is called. What it does
    is:

        push mod2 to stack, run mod2.setup()
        push item2 to stack, run item2.setup()

    Stack:

        [session, mod2, item2]

    During the teardown phase of item2, teardown_exact(None) is called,
    because item2 is the last item. What it does is:

        pop item2 from stack, run its teardowns
        pop mod2 from stack, run its teardowns
        pop session from stack, run its teardowns

    Stack:

        []

    The end!
    N)r$   c             C   s
   i | _ d S )N)stack)r   r.   r.   r/   r     s    
zSetupState.__init__)rR   r$   c             C   s   |  }x2| j D ]$\}\}}||ks0td|r|qW x|t| jd D ]p}|| jksbt|jgdf| j|< y|  W qP tk
r } z | j| d |f| j|< |W dd}~X Y qPX qPW dS )z4Setup objects along the collector chain to the item.z(previous item was not torn down properlyNr   )	listchainr   itemsrr   rF   r\   rZ   r   )r   rR   needed_collectorscol
finalizersexcr.   r.   r/   rZ     s    zSetupState.setup)	finalizerr   r$   c             C   sN   |rt |trtt|st|| jks6t|| jf| j| d | dS )zgAttach a finalizer to the given node.

        The node must be currently active in the stack.
        r   N)r   r   rr   callabler   rC   )r   r   r   r.   r.   r/   addfinalizer  s    zSetupState.addfinalizer)rS   r$   c       	   
   C   s   |r|  pg }d}x| jrt| j |dt| j kr>P | j \}\}}xJ|r| }y
|  W qR tk
r } z|dkr|}W dd}~X Y qRX qRW qW |r||dkr| jrtdS )zTeardown the current stack up until reaching nodes that nextitem
        also descends from.

        When nextitem is None (meaning we're at the last item), the entire
        stack is torn down.
        N)	r   r   r   keysrF   popitemr|   r   rr   )	r   rS   r   r   r   r   _Zfinrt   r.   r.   r/   rP     s"     
zSetupState.teardown_exact)r   r   r   r   r   r   rZ   r   objectr   r   r   rP   r.   r.   r.   r/   rM     s
   >
rM   c             C   sN   | j }|j| d |j| d}|jdd }|rJt||rJ|j| ||d |S )N)r   r[   )r   r[   r~   )rU   Zpytest_collectstartr   __dict__r|   r   r   )r   rU   rI   r[   r.   r.   r/   collect_one_node  s    r   )TN)T)Ir   r   rz   rk   typingr   r   r   r   r   r   r   r	   r
   r   r   attrra   r   r   r   r   Z_pytestr   Z_pytest._code.coder   r   r   Z_pytest.compatr   Z_pytest.config.argparsingr   Z_pytest.deprecatedr   Z_pytest.nodesr   r   r   Z_pytest.outcomesr   r   r   r   Ztyping_extensionsr    Z_pytest.mainr!   Z_pytest.terminalr"   r0   rK   rO   rQ   r   rW   rV   r`   rj   ru   rv   ri   r   r   r]   r   r   r   r   r   r   r   rM   r   r.   r.   r.   r/   <module>   s    	` 