U
    42iK                     @  s  d 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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  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j)dk r|ddl*m+Z+ erdd l,m-Z- dd!l.m/Z/ d"d#d$d%d&Z0d'd#d(d)d*Z1d+d#d,d-d.Z2d+d#d,d/d0Z3d1d2d3d4d5d6Z4dhd1d3d2d8d9d:d;Z5d1d#d<d=d>Z6d1d#d<d?d@Z7d1d#d<dAdBZ8d1d2d#d4dCdDZ9d1dEd#dFdGdHZ:dIdJdKdLdMZ;did1dNd3dOdPdQdRZ<dSdId3dTdUdVZ=edWd7dXZ>eej?G dYdZ dZee> Z@d1d[dOd\d]d^ZAd_d`dadbdcZBG ddde deZCd_d`dadfdgZDdS )jz3Basic collect and runtest protocol implementations.    )annotationsN)Callable)cast)final)Generic)Literal)TYPE_CHECKING)TypeVar   )
BaseReport)CollectErrorRepr)CollectReport)
TestReport)timing)ExceptionChainRepr)ExceptionInfo)TerminalRepr)Parser)check_ispytest)	Collector)	Directory)Item)Node)Exit)OutcomeException)Skipped)TEST_OUTCOME)      )BaseExceptionGroup)Session)TerminalReporterr   None)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)afterz--durations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 r0   2/tmp/pip-unpacked-wheel-7n2p7kht/_pytest/runner.pypytest_addoption2   s"    r2   r!   )terminalreporterr$   c           	      C  s.  | j jj}| j jj}| j  }|d kr*d S | }g }|j D ]"}|D ]}t|drD|| qDq<|shd S |j	dd dd |s|
dd n |
dd| d	 |d | }t|D ]t\}}|d
k r|j|k r|d |dt||  d|dd  q*||jdd|jdd|j  qd S )Ndurationc                 S  s   | j S N)r4   )xr0   r0   r1   <lambda>U       z)pytest_terminal_summary.<locals>.<lambda>T)keyreverse=zslowest durationszslowest z
 durations    (z durations < gz,s hidden.  Use -vv to show these durations.)z02.2fzs z<8 )configoption	durationsdurations_minZget_verbositystatsvalueshasattrappendsortZ	write_sep	enumerater4   Z
write_linelenwhennodeid)	r3   rC   rD   verbosetrZdlistZreplistrepir0   r0   r1   pytest_terminal_summaryG   s4    




rR   r    )sessionr$   c                 C  s   t  | _d S r5   )
SetupState_setupstaterS   r0   r0   r1   pytest_sessionstartf   s    rW   c                 C  s   | j d  d S r5   )rU   teardown_exactrV   r0   r0   r1   pytest_sessionfinishj   s    rY   r   Item | Nonebool)itemnextitemr$   c                 C  s:   | j }|j| j| jd t| |d |j| j| jd dS )N)rM   locationr]   T)ihookZpytest_runtest_logstartrM   r^   runtestprotocolZpytest_runtest_logfinish)r\   r]   r`   r0   r0   r1   pytest_runtest_protocoln   s
    rb   Tzlist[TestReport])r\   logr]   r$   c                 C  s   t | d}|r| js|   t| d|}|g}|jrj| jddrJt|  | jddsj|t| d| | j	j
sz| j	jr~d }|t| d||d |rd| _d | _|S )	N_requestsetupZ	setupshowFZ	setuponlycallteardownr_   )rG   rd   Z_initrequestcall_and_reportpassedrA   	getoptionshow_test_itemrH   rS   Z
shouldfailZ
shouldstopZfuncargs)r\   rc   r]   Z
hasrequestrP   reportsr0   r0   r1   ra   v   s"    

ra   r\   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)
rA   Zget_terminal_writerlinewriterM   sortedgetattrformatjoinflush)r\   ZtwZused_fixturesr0   r0   r1   rk      s    

rk   c                 C  s   t | d | jj|  d S )Nre   )_update_current_test_varrS   rU   re   )r\   r0   r0   r1   pytest_runtest_setup   s    
rv   c              
   C  s   t | d zt`t`t`tjdkr&t`W n tk
r<   Y nX z|   W n\ t	k
r } z>t
|t_|t_tjdkr||t_|jd k	st|jjt_ W 5 d }~X Y nX d S )Nrf   )r      r   )ru   sys	last_type
last_valuelast_tracebackversion_infolast_excAttributeErrorZruntest	Exceptionr)   __traceback__AssertionErrortb_next)r\   er0   r0   r1   pytest_runtest_call   s&    




r   c                 C  s&   t | d | jj| t | d  d S )Nrg   )ru   rS   rU   rX   )r\   r]   r0   r0   r1   pytest_runtest_teardown   s    
r   z-Literal[('setup', 'call', 'teardown')] | None)r\   rL   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)rM   replaceosenvironpop)r\   rL   var_namevaluer0   r0   r1   ru      s    ru   r   ztuple[str, str, str] | None)reportr$   c                 C  s&   | j dkr"| jrdS | jrdS dS d S )N)re   rg   )errorEERROR)skippedsZSKIPPED)r=   r=   r=   )rL   failedr   r   r0   r0   r1   pytest_report_teststatus   s    
r   z&Literal[('setup', 'call', 'teardown')]r   )r\   rL   rc   r$   c                   s    j }|dkr|jn2|dkr&|jn"|dkr6|jndsHtd| tf} jddsf|tf7 }t	j
 fdd||d	}|j |d
}|r|j|d t||r|j ||d |S )Nre   rf   rg   FzUnhandled runtest hook case: Zusepdbc                     s   f d iS )Nr\   r0   r0   r\   kwdsZruntest_hookr0   r1   r7      r8   z!call_and_report.<locals>.<lambda>)rL   reraiser\   rf   r   noderf   r   )r`   rv   r   r   r   r   rA   rj   KeyboardInterruptCallInfo	from_callpytest_runtest_makereportZpytest_runtest_logreportcheck_interactive_exceptionpytest_exception_interact)r\   rL   rc   r   r`   r   rf   r   r0   r   r1   rh      s,    
  
rh   zCallInfo[object])rf   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)excinforG   
isinstancer   r   bdbZBdbQuit)rf   r   r0   r0   r1   r      s    

r   TResult)	covariantc                
   @  s   e Zd ZU dZded< ded< ded< ded< ded	< d
ed< dddddddd
dddddZeddddZed!dd
dddddZ	dddd Z
dS )"r   z/Result/Exception info of a function invocation.zTResult | None_resultz#ExceptionInfo[BaseException] | Noner   r.   startstopr4   z1Literal[('collect', 'setup', 'call', 'teardown')]rL   F)	_ispytestr[   r"   )resultr   r   r   r4   rL   r   r$   c                C  s0   t | || _|| _|| _|| _|| _|| _d S r5   )r   r   r   r   r   r4   rL   )selfr   r   r   r   r4   rL   r   r0   r0   r1   __init__  s    zCallInfo.__init__r   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   r~   r   r   r   r   r0   r0   r1   r   2  s    
zCallInfo.resultNzCallable[[], TResult]z<type[BaseException] | tuple[type[BaseException], ...] | NonezCallInfo[TResult])funcrL   r   r$   c              	   C  s   d}t  }t  }z
| }W n6 tk
rT   t }|dk	rLt|j|rL d}Y nX t  }|| }	t  }
| ||
|	|||ddS )a  Call func, wrapping the result in a CallInfo.

        :param func:
            The function to call. Called without arguments.
        :type func: Callable[[], _pytest.runner.TResult]
        :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   r4   rL   r   r   r   )r   timeperf_counterBaseExceptionr   Zfrom_currentr   r   )clsr   rL   r   r   r   Zprecise_startr   Zprecise_stopr4   r   r0   r0   r1   r   ?  s,    

zCallInfo.from_callstrc                 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   rL   r   r   r0   r0   r1   __repr__i  s    
zCallInfo.__repr__)N)__name__
__module____qualname____doc____annotations__r   propertyr   classmethodr   r   r0   r0   r0   r1   r     s   
  )r   zCallInfo[None])r\   rf   r$   c                 C  s   t | |S r5   )r   Zfrom_item_and_callr   r0   r0   r1   r   o  s    r   r   r   )	collectorr$   c                   s  dd fdd}t j|dttfd}d }|js6d}ntg}tjd}|d k	r\|	|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 )Nzlist[Item | Collector]r   c                     sB   t  tr6 jjj j jd jj jdd t	 
 S )NZ
importmodeconsider_namespace_packages)rootpathr   )r   r   rA   ZpluginmanagerZ_loadconftestmodulespathrj   r   Zgetinilistcollectr0   r   r0   r1   r   t  s    

	z+pytest_make_collect_report.<locals>.collectr   )r   ri   unittestr   rn   r   Z
toterminal)r   r   r   
SystemExitr   r   rx   modulesgetrH   ZSkipTestr   r   tupleZ_repr_failure_pyr   r   reprZ	reprcrashr   r   linenomessageZrepr_failurerG   r   r   r   rM   rf   )r   r   rf   ZlongreprZoutcomeZskip_exceptionsr   Zr_rZ	errorinfor   rP   r0   r   r1   pytest_make_collect_reports  s<      
r   c                   @  sP   e Zd ZdZddddZddddd	Zd
dddddZdddddZdS )rT   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!
    r"   r   c                 C  s
   i | _ d S r5   )stackr   r0   r0   r1   r     s    
zSetupState.__init__r   rm   c                 C  s   |  }| j D ]2\}\}}||ks.td|r|d |d q|t| jd D ]t}|| jksjt|jgdf| j|< z|  W qX tk
r } z$| j| d ||j	ff| j|<  W 5 d}~X Y qXX qXdS )z4Setup objects along the collector chain to the item.z(previous item was not torn down properlyr   r
   N)
	listchainr   itemsr   with_tracebackrK   rg   re   r   r   )r   r\   needed_collectorscol
finalizersexcr0   r0   r1   re     s    zSetupState.setupzCallable[[], object]r   )	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   r   callabler   rH   )r   r   r   r0   r0   r1   addfinalizer  s    zSetupState.addfinalizerrZ   )r]   r$   c              
   C  s$  |r|  pg }g }| jrt| j |dt| j kr<q| j \}\}}g }|r| }z
|  W qR tk
r }	 z||	 W 5 d}	~	X Y qRX qRt|dkr|	| q|rd|}
|t
|
|ddd  qt|dkr|d n|r
t
d|ddd |dkr | jr t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.
        Nr
   zerrors while tearing down r   zerrors during test teardown)r   r   r   keysrK   popitemr   r   rH   extendr   r   )r   r]   r   
exceptionsr   r   _Zthese_exceptionsZfinr   msgr0   r0   r1   rX     s0     



zSetupState.teardown_exactN)r   r   r   r   r   re   r   rX   r0   r0   r0   r1   rT     s
   ?
rT   c                 C  sN   | j }|j| d |j| d}|jdd }|rJt||rJ|j| ||d |S )Nr   rf   r   )r`   Zpytest_collectstartr   __dict__r   r   r   )r   r`   rP   rf   r0   r0   r1   collect_one_node4  s    r   )TN)T)Er   
__future__r   r   Zdataclassesr   rx   typestypingr   r   r   r   r   r   r	   rl   r   r   r   r   Z_pytestr   Z_pytest._code.coder   r   r   Z_pytest.config.argparsingr   Z_pytest.deprecatedr   Z_pytest.nodesr   r   r   r   Z_pytest.outcomesr   r   r   r   r|   Zexceptiongroupr   Z_pytest.mainr    Z_pytest.terminalr!   r2   rR   rW   rY   rb   ra   rk   rv   r   r   ru   r   rh   r   r   Z	dataclassr   r   r   rT   r   r0   r0   r0   r1   <module>   s|   	    _3 