a
    ˆÏ?e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 �r dd#l,m-Z- e�rÊd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{®Gázt?zJMinimal duration in seconds for inclusion in slowest list. Default: 0.005.)ZgetgroupZ	addoptionÚintÚfloat)r'   Úgroup© r4   úQ/home/yuhw/wc/wire-cell-python/wcpy/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› �¡ q²d 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�¡ |r�d| _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 )zŸUpdate :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}n¸tg}tj d¡}|d urJ| |j¡ t	|jj
t|ƒƒr¨d}ˆ  |jd¡}t	|tƒs„J t|ƒƒ‚|j}|s’J ‚t|jƒ|j|jf}n4d}ˆ  |j¡}t|d	ƒsØt	|tƒsÐJ ‚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rÜt| 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 )zÍTeardown 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ž   Z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¤   Z	dataclassr¢   r—   rÍ   rV   rá   r4   r4   r4   r5   Ú<module>   s–    	 ÿ
þþ ÿþþ` 