U
    42iSS                     @  s:  d dl mZ d dl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 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# Z,G d$d% d%Z-d&d'd(d)d*d+Z.e
G d,d- d-e-Z/e
G d.d/ d/e-Z0G d0d1 d1eZ1d2d3d4d5d6Z2d7d8d9d:d;Z3d%d7d4d<d=Z4d7d7d>d?d@Z5dS )A    )annotationsN)StringIO)pprint)Any)cast)final)Iterable)Iterator)Literal)Mapping)NoReturn)Sequence)TYPE_CHECKING)ExceptionChainRepr)ExceptionInfo)ExceptionRepr)	ReprEntry)ReprEntryNative)ReprExceptionInfoReprFileLocation)ReprFuncArgs)
ReprLocalsReprTraceback)TerminalRepr)TerminalWriter)Config)	Collector)Item)fail)skip)Self)CallInfoc              
   C  sd   z| j W S  tk
r^   | j}dj|d d d  }d|d |d ||d  | _ }| Y S X d S )Nz{}.{}.{}version_info   z[{}] {} -- Python {} {}idZsysplatform
executable)Z_workerinfocacheAttributeErrorZ
workerinfoformat)nodedvers r.   3/tmp/pip-unpacked-wheel-7n2p7kht/_pytest/reports.pygetworkerinfoline,   s       
r0   c                   @  sj  e Zd ZU ded< ded< ded< ded< d	ed
< ded< dddddZer^d	ddddZdddddZd	ddddZe	d	dddZ
e	d	dd d!Ze	d	dd"d#Ze	d	dd$d%Ze	d&dd'd(Ze	d&dd)d*Ze	d&dd+d,Ze	d	dd-d.Ze	d&dd/d0Ze	ddd1d2Zd3d4d5d6d7d8Zd9dd:d;Zed<d=d>d?d@ZdAS )B
BaseReportz
str | Nonewhen"tuple[str, int | None, str] | NonelocationONone | ExceptionInfo[BaseException] | tuple[str, int, str] | str | TerminalReprlongreprzlist[tuple[str, str]]sectionsstrnodeid(Literal[('passed', 'failed', 'skipped')]outcomer   None)kwreturnc                 K  s   | j | d S N)__dict__update)selfr=   r.   r.   r/   __init__B   s    zBaseReport.__init__)keyr>   c                 C  s   d S r?   r.   )rB   rD   r.   r.   r/   __getattr__G       zBaseReport.__getattr__r   outr>   c                 C  s   t | dr"t| j}|r"|| | j}|d kr4d S t |drTtt|}|| n0zt|}W n t	k
rx   d}Y nX || d S )Nr*   
toterminalz<unprintable longrepr>)
hasattrr0   r*   liner6   r   r   rI   r8   UnicodeEncodeError)rB   rH   Zworker_infor6   Zlongrepr_terminalr-   r.   r.   r/   rI   I   s    





zBaseReport.toterminalzIterator[tuple[str, str]])prefixr>   c                 c  s(   | j D ]\}}||r||fV  qd S r?   )r7   
startswith)rB   rM   namecontentr.   r.   r/   get_sections]   s    
zBaseReport.get_sectionsr>   c                 C  s.   t  }t|}d|_| | | }| S )zRead-only property that returns the full string representation of
        ``longrepr``.

        .. versionadded:: 3.0
        F)r   r   Z	hasmarkuprI   getvaluestrip)rB   fileZtwexcr.   r.   r/   longreprtextb   s    
zBaseReport.longreprtextc                 C  s   d dd | dD S )z_Return captured log lines, if log capturing is enabled.

        .. versionadded:: 3.5
        
c                 s  s   | ]\}}|V  qd S r?   r.   .0rM   rP   r.   r.   r/   	<genexpr>v   s    z$BaseReport.caplog.<locals>.<genexpr>zCaptured logjoinrQ   rB   r.   r.   r/   caplogp   s    
zBaseReport.caplogc                 C  s   d dd | dD S )zbReturn captured text from stdout, if capturing is enabled.

        .. versionadded:: 3.0
         c                 s  s   | ]\}}|V  qd S r?   r.   rY   r.   r.   r/   r[      s    z'BaseReport.capstdout.<locals>.<genexpr>zCaptured stdoutr\   r^   r.   r.   r/   	capstdoutz   s    
zBaseReport.capstdoutc                 C  s   d dd | dD S )zbReturn captured text from stderr, if capturing is enabled.

        .. versionadded:: 3.0
        r`   c                 s  s   | ]\}}|V  qd S r?   r.   rY   r.   r.   r/   r[      s    z'BaseReport.capstderr.<locals>.<genexpr>zCaptured stderrr\   r^   r.   r.   r/   	capstderr   s    
zBaseReport.capstderrboolc                 C  s
   | j dkS )zWhether the outcome is passed.passedr;   r^   r.   r.   r/   rd      s    zBaseReport.passedc                 C  s
   | j dkS )zWhether the outcome is failed.failedre   r^   r.   r.   r/   rf      s    zBaseReport.failedc                 C  s
   | j dkS )zWhether the outcome is skipped.skippedre   r^   r.   r.   r/   rg      s    zBaseReport.skippedc                 C  s   | j dd S )z3The path portion of the reported node, as a string.z::r   )r9   splitr^   r.   r.   r/   fspath   s    zBaseReport.fspathc                 C  s   dS )a6  **Experimental** Whether this report should be counted towards the
        totals shown at the end of the test session: "1 passed, 1 failure, etc".

        .. note::

            This function is considered **experimental**, so beware that it is subject to changes
            even in patch releases.
        Tr.   r^   r.   r.   r/   count_towards_summary   s    
z BaseReport.count_towards_summaryc                 C  s   | j dk	r| j \}}}|S dS )a  **Experimental** The head line shown with longrepr output for this
        report, more commonly during traceback representation during
        failures::

            ________ Test.foo ________


        In the example above, the head_line is "Test.foo".

        .. note::

            This function is considered **experimental**, so beware that it is subject to changes
            even in patch releases.
        N)r4   )rB   ri   linenodomainr.   r.   r/   	head_line   s    
zBaseReport.head_liner   zMapping[str, bool]ztuple[str, Mapping[str, bool]])configdefault_markupr>   c                 C  sv   |j j| |d\}}}t|tr(||fS t|trbt|dkrb|\}}t|trbt|trb||fS td|d d S )N)reportrn      zSpytest_report_teststatus() hook (from a plugin) returned an invalid verbose value: z8.
Expected either a string or a tuple of (word, markup).)hookZpytest_report_teststatus
isinstancer8   r   lenr   r    )rB   rn   ro   Z	_categoryZ_shortverbosewordZmarkupr.   r.   r/   _get_verbose_word_with_markup   s     

z(BaseReport._get_verbose_word_with_markupdict[str, Any]c                 C  s   t | S )zReturn the contents of this report as a dict of builtin entries,
        suitable for serialization.

        This was originally the serialize_report() function from xdist (ca03269).

        Experimental method.
        )_report_to_jsonr^   r.   r.   r/   _to_json   s    zBaseReport._to_jsonzdict[str, object]r"   
reportdictr>   c                 C  s   t |}| f |S )a  Create either a TestReport or CollectReport, depending on the calling class.

        It is the callers responsibility to know which class to pass here.

        This was originally the serialize_report() function from xdist (ca03269).

        Experimental method.
        )_report_kwargs_from_json)clsr|   kwargsr.   r.   r/   
_from_json   s    
zBaseReport._from_jsonN)__name__
__module____qualname____annotations__rC   r   rE   rI   rQ   propertyrW   r_   ra   rb   rd   rf   rg   ri   rj   rm   rw   rz   classmethodr   r.   r.   r.   r/   r1   8   sF   
			
r1   r8   ztype[BaseReport]r   )	type_namereport_classr>   c                 C  st   d}t  }td|d td|  |d td| |d t||d td| |d td|d t| d S )Nz+https://github.com/pytest-dev/pytest/issueszd----------------------------------------------------------------------------------------------------)streamz,INTERNALERROR: Unknown entry type returned: zreport_name: zPlease report this bug at )r   r   RuntimeErrorrS   )r   r   r|   urlr   r.   r.   r/   _report_unserialization_failure   s    r   c                   @  sh   e Zd ZU dZdZded< dddd	d
dddddddddddZddddZeddd dddZ	dS )
TestReportzBasic test report object (also used for setup and teardown calls if
    they fail).

    Reports can contain arbitrary extra attributes.
    Fr8   Zwasxfailr.   r   Nztuple[str, int | None, str]zMapping[str, Any]r:   r5   z&Literal[('setup', 'call', 'teardown')]Iterable[tuple[str, str]]floatz#Iterable[tuple[str, object]] | Noner<   )r9   r4   keywordsr;   r6   r2   r7   durationstartstopuser_propertiesr>   c                 K  s^   || _ || _|| _|| _|| _|| _t|p,g | _t|| _|| _	|	| _
|
| _| j| d S r?   )r9   r4   r   r;   r6   r2   listr   r7   r   r   r   r@   rA   )rB   r9   r4   r   r;   r6   r2   r7   r   r   r   r   extrar.   r.   r/   rC     s    
zTestReport.__init__rR   c              	   C  s(   d| j j d| jd| jd| jd	S )N< z when=	 outcome=>)	__class__r   r9   r2   r;   r^   r.   r.   r/   __repr__I  s    zTestReport.__repr__r   zCallInfo[None])itemcallr>   c                 C  sf  |j }|dkst|j}|j}|j}dd |jD }|j}g }	|jsNd}
d}nt|tsbd}
|}nt|j	t
jrd}
| }|dk	std|j	jr| dd	 \}}|dk	stt||d
 |jf}nt|j|j|jf}n2d}
|j dkr||}n|j||jddd}|jD ]&\}}}|	d| d| |f q| |j|j||
|||	||||jdS )zCreate and fill a TestReport with standard item and call info.

        :param item: The item.
        :param call: The call info.
        collectc                 S  s   i | ]
}|d qS )   r.   rZ   xr.   r.   r/   
<dictcomp>Y  s      z1TestReport.from_item_and_call.<locals>.<dictcomp>rd   Nrf   rg   z=There should always be a traceback entry for skipping a test.rq   r   r   Ztbstyleauto)stylez	Captured r   )r   )r2   AssertionErrorr   r   r   r   excinfors   r   valuer!   	ExceptionZ_getreprcrashZ_use_item_locationZ
reportinfoosri   messager8   pathrk   Zrepr_failureZ_repr_failure_pyrn   Z	getoptionZ_report_sectionsappendr9   r4   r   )r~   r   r   r2   r   r   r   r   r   r7   r;   r6   rr   rK   ZrwhenrD   rP   r.   r.   r/   from_item_and_callL  sb    

 zTestReport.from_item_and_call)r.   r   r   r   N)
r   r   r   __doc__Z__test__r   rC   r   r   r   r.   r.   r.   r/   r      s   
     &>r   c                   @  sN   e Zd ZdZdZddddddd	d
ddZeddddZddddZdS )CollectReportzSCollection report object.

    Reports can contain arbitrary extra attributes.
    r   r.   r8   r:   r5   zlist[Item | Collector] | Noner   r<   )r9   r;   r6   resultr7   r>   c                 K  s6   || _ || _|| _|pg | _t|| _| j| d S r?   )r9   r;   r6   r   r   r7   r@   rA   )rB   r9   r;   r6   r   r7   r   r.   r.   r/   rC     s    

zCollectReport.__init__r3   rR   c                 C  s   | j d | j fS r?   )ri   r^   r.   r.   r/   r4     s    zCollectReport.locationc                 C  s"   d| j dt| j d| jdS )Nz<CollectReport z lenresult=r   r   )r9   rt   r   r;   r^   r.   r.   r/   r     s    zCollectReport.__repr__N)r.   )	r   r   r   r   r2   rC   r   r4   r   r.   r.   r.   r/   r     s    !r   c                   @  s,   e Zd ZdddddZddddd	Zd
S )CollectErrorReprr8   r<   )msgr>   c                 C  s
   || _ d S r?   )r6   )rB   r   r.   r.   r/   rC     s    zCollectErrorRepr.__init__r   rG   c                 C  s   |j | jdd d S )NT)Zred)rK   r6   )rB   rH   r.   r.   r/   rI     s    zCollectErrorRepr.toterminalN)r   r   r   rC   rI   r.   r.   r.   r/   r     s   r   zCollectReport | TestReportdict[str, Any] | None)rp   r>   c                 C  s*   t | ttfr&|  }| jj|d< |S d S )N$report_type)rs   r   r   rz   r   r   )rp   datar.   r.   r/   pytest_report_to_serializable  s
    r   rx   z!CollectReport | TestReport | None)r   r>   c                 C  sN   d| krJ| d dkrt | S | d dkr4t| S dsJtd| d d S )Nr   r   r   Fz(Unknown report_type unserialize data: {})r   r   r   r   r)   )r   r.   r.   r/   pytest_report_from_serializable  s    


r   c                   s   ddddddddfdd	d
dddd ddd fdd}| j  }t| jdrt| jdrt| jdr|| |d< qt| j|d< n
| j|d< |D ]8}t|| tjrt|| ||< q|dkrd||< q|S )zReturn the contents of this report as a dict of builtin entries,
    suitable for serialization.

    This was originally the serialize_report() function from xdist (ca03269).
    zReprEntry | ReprEntryNativerx   )entryr>   c                 S  sH   t | }| D ] \}}t|drt |||< qt| j|d}|S )Nr@   )typer   )dataclassesasdictitemsrJ   r   r   )r   r   rD   r   
entry_datar.   r.   r/   serialize_repr_entry  s    

z-_report_to_json.<locals>.serialize_repr_entryr   )reprtracebackr>   c                   s&   t | } fdd| jD |d< |S )Nc                   s   g | ]} |qS r.   r.   r   r   r.   r/   
<listcomp>  s    zE_report_to_json.<locals>.serialize_repr_traceback.<locals>.<listcomp>reprentries)r   r   r   )r   r   r   r.   r/   serialize_repr_traceback  s
    


z1_report_to_json.<locals>.serialize_repr_tracebackzReprFileLocation | Noner   )	reprcrashr>   c                 S  s   | d k	rt | S d S d S r?   )r   r   )r   r.   r.   r/   serialize_repr_crash  s    
z-_report_to_json.<locals>.serialize_repr_crashr1   )repr>   c                   s   | j d k	sttt| j } |j|j|jd}t|trvg |d< |j	D ]&\}}}|d 
| ||f qLnd |d< |S )N)r   r   r7   chain)r6   r   r   r   r   r   r7   rs   r   r   r   )r   r6   r   Zrepr_tracebackZ
repr_crashdescription)r   r   r.   r/   serialize_exception_longrepr  s"    
z5_report_to_json.<locals>.serialize_exception_longreprrI   r   r   r6   r   N)	r@   copyrJ   r6   r8   rs   r   PathLikeri   )rp   r   r+   rO   r.   )r   r   r   r/   ry     s&    

 

ry   r{   c                   s   fdd  fdd}dddd}d	 rd
d	 krdd	 kr|d	 d }|d	 d
 }d	 d rg }d	 d D ]"\}}}| |||||f qt|}	nt||d}	d	 d D ]}
|	j|
  q|	d	< S )zReturn **kwargs that can be used to construct a TestReport or
    CollectReport instance.

    This was originally the serialize_report() function from xdist (ca03269).
    c                   s   | d }| d }|dkrd }d }d }|d r:t f |d }|d rPtf |d }|d rht|d d }t|d ||||d d	}n"|d
krt|d }nt|t  |S )Nr   r   r   reprfuncargsreprfileloc
reprlocalslinesr   )r   r   r   r   r   r   )r   r   r   r   r   r   r   )r   r   Z
entry_typer   r   r   Z	reprentry)r|   r.   r/   deserialize_repr_entry3  s.    z8_report_kwargs_from_json.<locals>.deserialize_repr_entryc                   s$    fdd| d D | d< t f | S )Nc                   s   g | ]} |qS r.   r.   r   r   r.   r/   r   O  s    zP_report_kwargs_from_json.<locals>.deserialize_repr_traceback.<locals>.<listcomp>r   r   )Zrepr_traceback_dictr   r.   r/   deserialize_repr_tracebackN  s    

z<_report_kwargs_from_json.<locals>.deserialize_repr_tracebackr   Zrepr_crash_dictc                 S  s   | d k	rt f | S d S d S r?   r   r   r.   r.   r/   deserialize_repr_crashT  s    
z8_report_kwargs_from_json.<locals>.deserialize_repr_crashr6   r   r   r   )r   r   r7   )r   r   r   Z
addsection)r|   r   r   r   r   r   Zrepr_traceback_dataZrepr_crash_datar   Zexception_infosectionr.   )r   r|   r/   r}   ,  sJ    


r}   )6
__future__r   r   ior   r   r   typingr   r   r   r   r	   r
   r   r   r   r   Z_pytest._code.coder   r   r   r   r   r   r   r   r   r   r   Z_pytest._ior   Z_pytest.configr   Z_pytest.nodesr   r   Z_pytest.outcomesr    r!   Ztyping_extensionsr"   Z_pytest.runnerr#   r0   r1   r   r   r   r   r   r   ry   r}   r.   r.   r.   r/   <module>   s`    9 3I