o
    v&iB                     @   s$  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ZddlmZ ddlmZ dd	lmZ e
rBdd
lmZ edejZdd Zdd Zdd ZG dd dejZdd Zdd Zejdddd ZG dd dZG dd dZej d d! Z!ej d"d# Z"d$d% Z#dS )&zCoverage plugin for pytest.    N)StringIO)Path)TYPE_CHECKING   )CovDisabledWarning)CovReportWarning)PytestCovWarning)CovController2unclosed database in <sqlite3.Connection object atc           	      C   s   g d}ddg}dg}|| }|  dd}|d }|g |dvr/d	|  d
| d}t|t|dkr9|d fS |d }||v rI||v rI||fS ||vr[d|  d
| d}t||S )N)annotatehtmlxmljsonmarkdownmarkdown-appendlcovtermzterm-missingzskip-covered:r   r    zinvalid choice: "z" (choose from "z")z%output specifier not supported for: ")splitargparseArgumentTypeErrorlen)	argfile_choicesterm_choicesterm_modifier_choicesall_choicesvaluesreport_typemsgreport_modifier r"   W/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/pytest_cov/plugin.pyvalidate_report   s$   

r$   c                 C   s^   zt | }W n ty#   zt| }W n ty    tdd w Y nw |dkr-td|S )Nz&An integer or float value is required.d   z|Your desire for over-achievement is admirable but misplaced. The maximum value is 100. Perhaps write more integration tests?)int
ValueErrorfloatr   r   )num_strvaluer"   r"   r#   validate_fail_under0   s   r+   c                 C   s   | dkr	t d| S )Ntestz#The only supported value is "test".)r   r   )r   r"   r"   r#   validate_context?   s   
r-   c                   @   s   e Zd ZdddZdd ZdS )StoreReportNc                    sX   |\}}| j |< |dv r|d u rd j |< t fdddD r*|  j | d S d S )N)r   r   zcoverage.mdc                 3   s    | ]}| j v V  qd S N)
cov_report).0x	namespacer"   r#   	<genexpr>M   s    z'StoreReport.__call__.<locals>.<genexpr>)r0   all_validate_markdown_dest_files)selfparserr4   r   option_stringr   filer"   r3   r#   __call__F   s   

zStoreReport.__call__c                 C   s>   |d }|d }||krd| d}|d7 }| | d S d S )Nr   r   zEmarkdown and markdown-append options cannot point to the same file: 'z'.zR Please redirect one of them using :DEST (e.g. --cov-report=markdown:dest_file.md))error)r8   cov_report_optionsr9   markdown_filemarkdown_append_fileerror_messager"   r"   r#   r7   P   s   z)StoreReport._validate_markdown_dest_filesr/   )__name__
__module____qualname__r<   r7   r"   r"   r"   r#   r.   E   s    

r.   c              
   C   s   |  dd}|jddg ddddd	d
 |jddg ddd |jdti dtdd |jdddddd |jddddd |jddddd |jd dd!td"d# |jd$ddd%d |jd&dd'd(d |jd)td'd*d+ |jd,dd-td.d# d'S )/z Add options to control coverage.covz3coverage reporting with distributed testing support--covappendSOURCE?T
cov_sourcezPath or package name to measure during execution (multi-allowed). Use --cov= to not do any source filtering and record everything.)actiondefaultmetavarnargsconstdesthelpz--cov-resetstore_constz1Reset cov sources accumulated in options so far. )rK   rO   rP   rQ   z--cov-reportTYPEac  Type of report to generate: term, term-missing, annotate, html, xml, json, markdown, markdown-append, lcov (multi-allowed). term, term-missing may be followed by ":skip-covered". annotate, html, xml, json, markdown, markdown-append and lcov may be followed by ":DEST" where DEST specifies the output location. Use --cov-report= to not generate any output.)rK   rL   rM   typerQ   z--cov-configstorez.coveragercPATHz.Config file for coverage. Default: .coveragerc)rK   rL   rM   rQ   z--no-cov-on-fail
store_trueFz8Do not report coverage if test run fails. Default: False)rK   rL   rQ   --no-covzIDisable coverage report completely (useful for debuggers). Default: Falsez--cov-fail-underMINz,Fail if the total coverage is less than MIN.)rK   rM   rT   rQ   z--cov-appendz<Do not delete coverage but append to current. Default: Falsez--cov-branchNzEnable branch coverage.z--cov-precisionz!Override the reporting precision.)rT   rL   rQ   z--cov-contextCONTEXTz(Dynamic contexts to use. "test" for now.)getgroup	addoptionr.   r$   r+   r&   r-   )r9   groupr"   r"   r#   pytest_addoptionY   s   
r^   c                 C   s   d| v rdS dd | D S )z
    Prepare cov_source so that:

     --cov --cov=foobar is equivalent to --cov (cov_source=None)
     --cov=foo --cov=bar is equivalent to cov_source=['foo', 'bar']
    TNc                 S   s   g | ]}|d ur|qS )Tr"   )r1   pathr"   r"   r#   
<listcomp>   s    z'_prepare_cov_source.<locals>.<listcomp>r"   )rJ   r"   r"   r#   _prepare_cov_source   s   ra   T)tryfirstc                 C   sr   | j }d }|_|D ]}t|}|dkrd}q
|dr#|r#d|_ nq
| j jr7t|| j}| j|d d S d S )NFrX   TrF   _cov)known_args_namespaceno_cov_should_warnstr
startswithrJ   	CovPluginpluginmanagerregister)early_configr9   argsoptionsno_covr   pluginr"   r"   r#   pytest_load_initial_conftests   s   
rp   c                   @   s   e Zd ZdZd!dejfddZd"ded	 fd
dZdd Z	dd Z
ejdddd Zejdddd Zdd Zejdddd Zdd Zdd Zejdddd  ZdS )#rh   zUse coverage package to produce code coverage reports.

    Delegates all work to a particular implementation based on whether
    this test process is centralised, a distributed master or a
    distributed worker.
    TFrm   c                 C   s   d| _ d| _t | _d| _d| _d| _d| _d| _|| _	d| _
t|ddp2t|ddp2t|dddk}t|ddr>d| _dS | j	jsHd	g| j	_nt| j	jd
krZd| j	jv rZi | j	_t| j	j| j	_d
dlm} |rt|rt| |j dS |r~| |j dS dS )zCreates a coverage pytest plugin.

        We read the rc file that coverage uses to get the data file
        name.  This is needed since we give coverage through it's API
        the data file name.
        NFnumprocessesdistloaddistnorn   Tr   r   r   engine)pidcov_controllerr   r0   	cov_totalfailed_started_start_path	_disabledrm   _wrote_headinggetattrr   ra   rJ   r   rv   start
DistMasterCentral)r8   rm   ri   r   re   is_distrv   r"   r"   r#   __init__   s2   	(zCovPlugin.__init__Ncontroller_clsr	   c                    s   |d u rG  fddd}| }| j || _ j  d _t  _ jjj} j j	d u r:t
|dr:|j j _	 j jd u rJt|dd j _d S d S )Nc                       s   e Zd Z jZdS )zCovPlugin.start.<locals>.ConfigN)rB   rC   rD   rm   optionr"   r8   r"   r#   Config  s    
r   T
fail_under	precisionr   )rm   rx   r   r{   r   cwdr|   rE   configcov_fail_underhasattrr   cov_precisionr   )r8   r   r   nodeidr   
cov_configr"   r   r#   r      s   



zCovPlugin.startc                 C   s   t |jdd d uS )Nworkerinput)r   r   )r8   sessionr"   r"   r#   
_is_worker  s   zCovPlugin._is_workerc                 C   s   | j jr	d| _dS ddlm} t | _| |r,|j	j
d|j}| |j|j	| n	| js5| |j | j jdkrH|j	jt| jd dS dS )zAAt session start determine our implementation and delegate to it.TNr   ru   workeridr,   _cov_contexts)rm   rn   r}   r   rv   osgetpidrw   r   r   r   getr   r   
DistWorkerr{   r   cov_contextri   rj   TestContextPluginrx   )r8   r   rv   r   r"   r"   r#   pytest_sessionstart  s   

zCovPlugin.pytest_sessionstart)optionalhookc                 C   s   | j s| j| dS dS zlDelegate to our implementation.

        Mark this hook as optional in case xdist is not installed.
        N)r}   rx   configure_node)r8   noder"   r"   r#   pytest_configure_node)  s   zCovPlugin.pytest_configure_nodec                 C   s   | j s| j|| dS dS r   )r}   rx   testnodedown)r8   r   r=   r"   r"   r#   pytest_testnodedown2  s   zCovPlugin.pytest_testnodedownc                 C   s$   | j jp| j j}|o| jo| j j S r/   )rm   r0   r   rz   no_cov_on_fail)r8   neededr"   r"   r#   _should_report;  s   zCovPlugin._should_report)wrapperc              
   c   s    | j rd V S tjD ]\}}}}}|tu r|tkr nq
tddt tjD ]\}}}}}|tu r3 nq&tdt ddlm	} tjD ]\}}}}}||u rP nqCtd| d V }t
|j| _| jd urj| j  | |s|  rddlm} ddlm} ddlm}	 z
| j| j| _W n5 |y }
 z)d	|
 d
}|jjdjd| d
ddd tjt|dd d| _W Y d }
~
nd }
~
ww | jd usJ d| jj }| jj!}|d u s| jj"rd S |	| j||rdj#|| j|||d}|jjdjd| d
ddd | jd7  _|S )NrL   r
   oncer   )CoverageWarning)CoverageException)display_covered)should_fail_underzFailed to generate report: 
terminalreporterz

WARNING: Tredboldr   
stacklevelz$Test coverage should never be `None`zMCoverage failure: total of {total} is less than fail-under={fail_under:.{p}f})totalr   pz
ERROR: )$r}   warningsfiltersResourceWarningCOVERAGE_SQLITE_WARNING_REfilterwarningsr   simplefiltercoverage.exceptionsr   booltestsfailedrz   rx   finishr   r   coverage.miscr   coverage.resultsr   r   summaryr0   ry   r   ri   	getpluginwritewarnr   rm   r   r   collectonlyformat)r8   r   _messagecategoryr   resultr   r   r   excr   r   r"   r"   r#   pytest_runtestloopA  sd   

"
"zCovPlugin.pytest_runtestloopc                 C   s    | j s|dd d| _ d S d S )N=ztests coverageT)r~   	write_sep)r8   r   r"   r"   r#   write_heading~  s   
zCovPlugin.write_headingc                 C   s  | j r%| jjr#| | d}|jd| dddd tjt|dd d S | jd u r,d S | j	d u r3d S | j
 }|rD| | || | jjd ur| jjdkr| | | j	| jjk }|rcdddnd	di}d
j| jj| j	|rrdnd|rwdndd}|j|fi | d S d S d S )Nz&Coverage disabled via --no-cov switch!z	WARNING: r   Tr   r   r   r   greenzU{fail}Required test coverage of {required}% {reached}. Total coverage: {actual:.2f}%
zFAIL r   znot reachedreached)requiredactualfailr   )r}   rm   re   r   r   r   r   r   rx   ry   r0   getvaluer   r   )r8   r   r   reportrz   markupr"   r"   r#   pytest_terminal_summary  s6   








z!CovPlugin.pytest_terminal_summary)hookwrapperc                 c   sD    | dsdt|ddv r| j  d V  | j  d S d V  d S )Nno_coverfixturenamesr"   )get_closest_markerr   rx   pauseresumer8   itemr"   r"   r#   pytest_runtest_call  s   

zCovPlugin.pytest_runtest_call)TF)NN)rB   rC   rD   __doc__r   	Namespacer   rT   r   r   r   pytesthookimplr   r   r   r   r   r   r   r"   r"   r"   r#   rh      s"    *





<
!rh   c                   @   s>   e Zd ZU ded< dd Zdd Zdd Zd	d
 Zdd ZdS )r   r	   rx   c                 C   s
   || _ d S r/   )rx   )r8   rx   r"   r"   r#   r     s   
zTestContextPlugin.__init__c                 C      |  |d d S )Nsetupswitch_contextr   r"   r"   r#   pytest_runtest_setup     z&TestContextPlugin.pytest_runtest_setupc                 C   r   )Nteardownr   r   r"   r"   r#   pytest_runtest_teardown  r   z)TestContextPlugin.pytest_runtest_teardownc                 C   r   )Nrunr   r   r"   r"   r#   r     r   z%TestContextPlugin.pytest_runtest_callc                 C   s*   | j jr| j j|j d|  d S d S )N|)rx   startedrE   r   r   )r8   r   whenr"   r"   r#   r     s   z TestContextPlugin.switch_contextN)	rB   rC   rD   __annotations__r   r   r   r   r   r"   r"   r"   r#   r     s   
 r   c                   C   s   dS )z%A pytest fixture to disable coverage.Nr"   r"   r"   r"   r#   r     s    r   c                 C   s.   | j jdr| j jd}|jr|jjS dS )zEA pytest fixture to provide access to the underlying coverage object.rc   N)r   ri   	haspluginr   rx   rE   )requestro   r"   r"   r#   rE     s
   rE   c                 C   s   |  dd d S )Nmarkersz)no_cover: disable coverage for this test.)addinivalue_line)r   r"   r"   r#   pytest_configure  r   r   )$r   r   r   rer   ior   pathlibr   typingr   r   r   r   r   r   rv   r	   compileIr   r$   r+   r-   Actionr.   r^   ra   r   rp   rh   r   fixturer   rE   r   r"   r"   r"   r#   <module>   s>    X


 c

