a
    !fH                     @   sd  d 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% ddl&m'Z' ddl&m(Z( ddl&m)Z) dd l&m*Z* ej+dd! d"k rdd#l,m-Z- erdd$l.m/Z/ dd%l0m1Z1 dd&l2m3Z3 edd'd(d)Z4d*dd+d,d-Z5d.dd/d0d1Z6d.dd/d2d3Z7e$ee$ e8d4d5d6Z9dce$e8ee$ e
e d8d9d:Z:e$dd;d<d=Z;e$dd;d>d?Z<e$dd;d@dAZ=e$ee$ dd4dBdCZ>e$edD ddEdFdGZ?eeee@e@e@f  dHdIdJZAdde$dDe8edKdLdMZBdNee8dOdPdQZCe$dDdRdEdSdTZDedUd7dVZEeejFG dWdX dXe	eE ZGe$eGd edYdZd[ZHe#ed\d]d^ZIG d_d` d`ZJe#ed\dadbZKdS )ez3Basic 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   )      )BaseExceptionGroup)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)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 r4   O/var/www/html/python-backend/venv/lib/python3.9/site-packages/_pytest/runner.pypytest_addoption2   s"    r6   r&   )terminalreporterr(   c           	      C   s   | j jj}| j jj}| j d}|d u r,d S | }g }|j D ]"}|D ]}t|drF|| qFq>|sjd S |j	dd dd |s|
dd n|
dd	|  |d | }t|D ]h\}}|d
k r|j|k r|d |dt|| |f   q||jdd|jdd|j  qd S )Nverbosedurationc                 S   s   | j S N)r9   )xr4   r4   r5   <lambda>U       z)pytest_terminal_summary.<locals>.<lambda>T)keyreverse=zslowest durationszslowest %s durationsr     z>(%s durations < %gs hidden.  Use -vv to show these durations.)z02.2fzs z<8 )configoption	durationsdurations_mingetvaluestatsvalueshasattrappendsortZ	write_sep	enumerater9   Z
write_linelenwhennodeid)	r7   rE   rF   r8   trZdlistZreplistrepir4   r4   r5   pytest_terminal_summaryG   s8    



rT   r%   )sessionr(   c                 C   s   t  | _d S r:   )
SetupState_setupstaterU   r4   r4   r5   pytest_sessionstartg   s    rY   c                 C   s   | j d  d S r:   )rW   teardown_exactrX   r4   r4   r5   pytest_sessionfinishk   s    r[   )itemnextitemr(   c                 C   s:   | j }|j| j| jd t| |d |j| j| jd dS )N)rP   locationr]   T)ihookZpytest_runtest_logstartrP   r^   runtestprotocolZpytest_runtest_logfinish)r\   r]   r`   r4   r4   r5   pytest_runtest_protocolo   s
    rb   T)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| |t| d||d |rd| _d | _	|S )	N_requestsetupZ	setupshowFZ	setuponlycallteardownr_   )
rJ   rd   Z_initrequestcall_and_reportpassedrC   	getoptionshow_test_itemrK   Zfuncargs)r\   rc   r]   Z
hasrequestrR   reportsr4   r4   r5   ra   w   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)
rC   Zget_terminal_writerlinewriterP   sortedgetattrformatjoinflush)r\   ZtwZused_fixturesr4   r4   r5   rk      s    

rk   c                 C   s   t | d | jj|  d S )Nre   )_update_current_test_varrU   rW   re   )r\   r4   r4   r5   pytest_runtest_setup   s    
rv   c              
   C   s   t | d zt`t`t`W n ty,   Y n0 z|   W nP ty } z8t|t_|t_|j	d ushJ |j	j
t_|W Y d }~n
d }~0 0 d S )Nrf   )ru   sys	last_type
last_valuelast_tracebackAttributeErrorZruntest	Exceptionr-   __traceback__tb_next)r\   er4   r4   r5   pytest_runtest_call   s    


r   c                 C   s&   t | d | jj| t | d  d S )Nrg   )ru   rU   rW   rZ   )r\   r]   r4   r4   r5   pytest_runtest_teardown   s    
r   z$Literal['setup', 'call', 'teardown'])r\   rO   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)rP   replaceosenvironpop)r\   rO   var_namevaluer4   r4   r5   ru      s    ru   )reportr(   c                 C   s&   | j dv r"| jrdS | jrdS dS d S )N)re   rg   )errorEERROR)skippedsZSKIPPED)rA   rA   rA   )rO   failedr   r   r4   r4   r5   pytest_report_teststatus   s    
r   )r\   rO   rc   r(   c                 K   sT   t | |fi |}| j}|j| |d}|r6|j|d t||rP|j| ||d |S )Nr\   rf   r   noderf   r   )call_runtest_hookr`   pytest_runtest_makereportZpytest_runtest_logreportcheck_interactive_exceptionpytest_exception_interact)r\   rO   rc   kwdsrf   hookr   r4   r4   r5   rh      s    
rh   zCallInfo[object])rf   r   r(   c                 C   s8   | j du 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)excinforJ   
isinstancer   r   bdbZBdbQuit)rf   r   r4   r4   r5   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sHJ d| tf}jddsf|tf7 }tj	 fdd||d	S )
Nre   rf   rg   FzUnhandled runtest hook case: Zusepdbc                      s    f diS )Nr\   r4   r4   r`   r\   r   r4   r5   r<     r=   z#call_runtest_hook.<locals>.<lambda>)rO   reraise)
r`   rv   r   r   r   rC   rj   KeyboardInterruptCallInfo	from_call)r\   rO   r   r   r4   r   r5   r      s    



r   TResult)	covariantc                
   @   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stopr9   z/Literal['collect', 'setup', 'call', 'teardown']rO   F)	_ispytestN)resultr   r   r   r9   rO   r   r(   c                C   s0   t | || _|| _|| _|| _|| _|| _d S r:   )r   r   r   r   r   r9   rO   )selfr   r   r   r   r9   rO   r   r4   r4   r5   __init__  s    zCallInfo.__init__r(   c                 C   s$   | j du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   r4   r4   r5   r   1  s    
zCallInfo.resultzCallable[[], TResult].zCallInfo[TResult])funcrO   r   r(   c              	   C   s   d}t  }t  }z
| }W n4 tyR   t }|durJt|j|rJ d}Y n0 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   r9   rO   r   r   r   )r   timeperf_counterBaseExceptionr   Zfrom_currentr   r   )clsr   rO   r   r   r   Zprecise_startr   Zprecise_stopr9   r   r4   r4   r5   r   >  s,    

zCallInfo.from_callc                 C   s6   | j d u r d| jd| jdS d| jd| j dS )Nz<CallInfo when=z	 result: >z	 excinfo=)r   rO   r   r   r4   r4   r5   __repr__i  s    
zCallInfo.__repr__)N)__name__
__module____qualname____doc__r   r   __annotations__r   r   r2   boolr   propertyr   classmethodr   r	   r   r   strr   r4   r4   r4   r5   r     s>   

 *r   )r\   rf   r(   c                 C   s   t | |S r:   )r   Zfrom_item_and_callr   r4   r4   r5   r   o  s    r   )	collectorr(   c                    s  t  fddd}d }|js$d}ntg}tjd}|d urJ||j t	|jj
t|rd} |jd}t	|tsJ t||j}|sJ t|j|j|jf}n4d} |j}t|d	st	|tsJ t|}|}|js|jnd }	t j|||	}
||
_|
S )
Nc                      s   t   S r:   )listcollectr4   r   r4   r5   r<   t  r=   z,pytest_make_collect_report.<locals>.<lambda>r   ri   unittestr   rn   r   Z
toterminal)r   r   r   r   rw   modulesgetrK   ZSkipTestr   r   tupleZ_repr_failure_pyr   reprZ	reprcrashr   pathlinenomessageZrepr_failurerJ   r   r   r   rP   rf   )r   rf   ZlongreproutcomeZskip_exceptionsr   Zr_rZ	errorinfor   rR   r4   r   r5   pytest_make_collect_reports  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 )rV   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!
    Nr   c                 C   s
   i | _ d S r:   )stackr   r4   r4   r5   r     s    
zSetupState.__init__rm   c                 C   s   |  }| j D ]$\}\}}||v s.J d|r|q|t| jd D ]r}|| jvs\J |jgdf| j|< z|  W qJ ty } z(| j| d |f| j|< |W Y d}~qJd}~0 0 qJdS )z4Setup objects along the collector chain to the item.z(previous item was not torn down properlyNr   )	listchainr   itemsrN   rg   re   r   )r   r\   needed_collectorscol
finalizersexcr4   r4   r5   re     s    zSetupState.setup)	finalizerr   r(   c                 C   sN   |rt |trJ t|sJ || jv s6J || 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   callabler   rK   )r   r   r   r4   r4   r5   addfinalizer  s    zSetupState.addfinalizer)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y }	 z||	 W Y d}	~	qRd}	~	0 0 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u r"| jr"J 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   keysrN   popitemr   r   rK   extendr#   )r   r]   r   
exceptionsr   r   _Zthese_exceptionsZfinr   msgr4   r4   r5   rZ     s0     
"


zSetupState.teardown_exact)r   r   r   r   r   r   re   r   objectr   r   r   rZ   r4   r4   r4   r5   rV     s
   ?
rV   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`   rR   rf   r4   r4   r5   collect_one_node   s    r   )TN)T)Lr   r   dataclassesr   rw   typingr   r   r   r   r   r   r   r	   r
   r   r   rl   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   version_infoZexceptiongroupr#   Ztyping_extensionsr$   Z_pytest.mainr%   Z_pytest.terminalr&   r6   rT   rY   r[   r   rb   ra   rk   rv   r   r   ru   r   r   rh   r   r   r   	dataclassr   r   r   rV   r   r4   r4   r4   r5   <module>   s    	 
 ` 