
    Vh8                       d Z ddlmZ ddlmZ ddlZddlZddl	m
Z
 ddlmZmZmZmZ ddlmZ ddlmZ dd	lmZ dd
ZddZ G d d      Z G d de      Z G d de      Z G d de      Z G d de      Z G d d      Zy)z0Classes for storing and processing test results.    )annotationsN   )display)get_docs_urlwrite_text_test_resultswrite_json_test_results
ResultType)Metadata)
TestConfig)	junit_xmlc                P    d}| D ]  \  }}t        |||      }t        ||      }  |S )zOReturn the best confidence value available from the given choices and metadata.r   )calculate_confidencemax)choicesmetadatabest_confidencepathline
confidences         K/home/dcms/DCMS/lib/python3.12/site-packages/ansible_test/_internal/test.pycalculate_best_confidencer      s<    O ;
d)$h?
j/:;     c                x    |j                   j                  |       }|syt        fd|D              rydk(  ryy)zbReturn the confidence level for a test result associated with the given file path and line number.r   c              3  J   K   | ]  }|d    cxk  xr |d   k  nc   yw)r   r   N ).0rr   s     r   	<genexpr>z'calculate_confidence.<locals>.<genexpr>2   s&     
0A1Q441Q4
0s    #d   K   2   )changesgetany)r   r   r   rangess    `  r   r   r   )   sE    !!$'F  
0
00 qy r   c                  R    e Zd ZdZdddZddZddZddZddZddZ	dd	Z
dd
Zy)
TestResultzBase class for test results.Nc                    || _         || _        || _        | j                  xs | j                   | _        | j                  r#| xj                  d| j                  z  z  c_        y y )N
-python-%s)commandtestpython_versionname)selfr*   r+   r,   s       r   __init__zTestResult.__init__@   sS    	,II-	II(;(;;;I r   c                    | j                          | j                  |       |j                  r| j                          |j                  r| j                  |       yy,Write the test results to various locations.N)write_console	write_botlint
write_lintjunitwrite_junitr.   argss     r   writezTestResult.writeI   sE    t99OO::T" r   c                     y)Write results to console.Nr   r.   s    r   r3   zTestResult.write_consoleT       r   c                     y)Write lint results to stdout.Nr   r>   s    r   r6   zTestResult.write_lintW   r?   r   c                     y)3Write results to a file for ansibullbot to consume.Nr   r9   s     r   r4   zTestResult.write_botZ   r?   r   c                     y)"Write results to a junit XML file.Nr   r9   s     r   r8   zTestResult.write_junit]   r?   r   c                    d| j                   z  }| j                  r|d| j                  z  z  }| j                  r|d| j                  z  z  }||z  }|S )z=Return the name of the result file using the given extension.zansible-test-%sz-%sr)   r*   r+   r,   )r.   	extensionr-   s      r   create_result_namezTestResult.create_result_name`   sW     4<</99EDII%%DL4#6#666D	r   c           	     Z   t        j                  t        j                  d|gt        j                  j	                  t        j
                  j                              g      }|j                         }|j                  ryt        t        j                  | j                  d      |       y)z6Save the given test case results to disk as JUnit XML.ansible-testtz)r-   cases	timestampsuitesN.xml)r   
TestSuites	TestSuitedatetimenowtimezoneutcto_pretty_xmlexplainr   r	   JUNITrI   )r.   r:   	test_caserQ   reports        r   
save_junitzTestResult.save_junitn   s    %%##'$+&//33x7H7H7L7L3M
 %%'<<
 0 0$2I2I&2QSYZr   Nr*   strr+   ra   r,   t.Optional[str]returnNoner:   r   rc   rd   rc   rd   )rH   ra   rc   ra   )r:   r   r\   zjunit_xml.TestCaserc   rd   )__name__
__module____qualname____doc__r/   r;   r3   r6   r4   r8   rI   r^   r   r   r   r'   r'   =   s/    &<	#(,B1[r   r'   c                  ,     e Zd ZdZd fdZddZ xZS )TestTimeoutzTest timeout.c                6    t         |   dd       || _        y )Ntimeout )r*   r+   )superr/   timeout_duration)r.   rq   	__class__s     r   r/   zTestTimeout.__init__   s    4 0r   c                   d| j                   z  }d}|j                  r|dz  }|dz  }t        j                  t        j                  dt
        j
                  j                  t
        j                  j                        t        j                  ddt        j                  |      g	      g
      g      }|j                         }t        t        j                  | j                  d      |       y)r2   z<Tests were aborted after exceeding the %d minute time limit.aA  
One or more of the following situations may be responsible:

- Code changes have resulted in tests that hang or run for an excessive amount of time.
- Tests have been added which exceed the time limit when combined with existing tests.
- Test infrastructure and/or external dependencies are operating slower than normal.zd
- Additional overhead from collecting code coverage has resulted in tests exceeding the time limit.zO

Consult the console log for additional details on where the timeout occurred.rK   rL   rn   )message)r-   	classnameerrors)r-   rO   rN   rP   rR   N)rq   coverager   rS   rT   rU   rV   rW   rX   TestCase	TestErrorrY   r   r	   r[   rI   )r.   r:   rt   outputrQ   r]   s         r   r;   zTestTimeout.write   s    PSWShShhX ==}}Fee%%##'&//33x7H7H7L7L3M!**!*&/ ) 3 3,3!"$

( %%'
 0 0$2I2I&2QSYZr   )rq   zint | floatrc   rd   re   )rg   rh   ri   rj   r/   r;   __classcell__rr   s   @r   rl   rl      s    1
([r   rl   c                      e Zd ZdZddZy)TestSuccesszTest success.c                ~    t        j                  | j                  | j                        }| j	                  ||       y)rE   )ru   r-   N)r   rx   r*   r-   r^   r.   r:   r\   s      r   r8   zTestSuccess.write_junit   s*    &&DIIN	i(r   Nre   )rg   rh   ri   rj   r8   r   r   r   r~   r~      s
    )r   r~   c                  6     e Zd ZdZdd fdZddZddZ xZS )	TestSkippedzTest skipped.c                6    t         |   |||       d | _        y r_   )rp   r/   reason)r.   r*   r+   r,   rr   s       r   r/   zTestSkipped.__init__   s    $7'+r   c                    | j                   r t        j                  | j                          yt        j                  dd       y)r=   No tests applicable.r   )	verbosityN)r   r   warninginfor>   s    r   r3   zTestSkipped.write_console   s(    ;;OODKK(LL/1=r   c                    t        j                  | j                  | j                  | j                  xs d      }| j                  ||       y)rE   r   )ru   r-   skippedN)r   rx   r*   r-   r   r^   r   s      r   r8   zTestSkipped.write_junit   s=    &&llKK9#9
	 	i(r   r_   r`   rf   re   )rg   rh   ri   rj   r/   r3   r8   r{   r|   s   @r   r   r      s    ,
>)r   r   c                       e Zd ZdZ	 	 	 d	 	 	 	 	 	 	 	 	 d fdZd fdZddZddZddZddZ	ddZ
dd	Zdd
ZdddZddZ xZS )TestFailurezTest failure.c                d    t         |   |||       |rt        |      }ng }|| _        || _        y r_   )rp   r/   sortedmessagessummary)r.   r*   r+   r,   r   r   rr   s         r   r/   zTestFailure.__init__   s6     	$7h'HH r   c                    |j                   j                  r| j                  |j                          t        |   |       yr1   )r   r"   populate_confidencerp   r;   )r.   r:   rr   s     r   r;   zTestFailure.write   s.    ==  $$T]]3dr   c                   | j                   r t        j                  | j                          y| j                  rd| j                  z  }nd}t        j                  dt	        | j
                        | j                  xs | j                  |fz         | j
                  D ]'  }t        j                  |j                  d             ) | j                         }|rt        j                  d|z         yy)r=   z on python %sro   z1Found %d %s issue(s)%s which need to be resolved:T)show_confidencezSee documentation for help: %sN)r   r   errorr,   lenr   r+   r*   format	find_docsr   )r.   	specifierrt   doc_urls       r   r3   zTestFailure.write_console   s    <<MM$,,'""+d.A.AA		MMMQTUYUbUbQceienene~rvr~r~  AJ  QK  K  L== DgnnTnBCD nn&G=GH r   c                    | j                   r/| j                         }d|z  }d}t        ||      }t        |       y| j                  D ]  }t        |        y)rA   z4The test `%s` failed. See stderr output for details.ro   N)r   format_commandTestMessageprintr   )r.   r*   rt   r   s       r   r6   zTestFailure.write_lint  sS    <<))+GLwVGD!'40G'N== gr   c                    | j                         }| j                         }t        j                  | j                  | j
                  t        j                  ||      g      }| j                  ||       y)rE   rt   rz   )ru   r-   failuresN)format_titleformat_blockr   rx   r*   r-   r   r^   )r.   r:   titlerz   r\   s        r   r8   zTestFailure.write_junit  se    !!#""$&&ll%%!!	
	 	i(r   c                `   | j                         }| j                  |      }| j                         }| j                  rt	        d | j                  D              }nd}t        ||t        ||      g      }|j                  ryt        t        j                  | j                  d      |       y)rC   )	help_linkc              3  B   K   | ]  }|j                   xs d dk\    yw)r   r!   N)r   r   ms     r   r   z(TestFailure.write_bot.<locals>.<genexpr>-  s     LALL-A"4Ls   Fr   )verifieddocsresultsNz.json)r   r   r   r   alldictrZ   r   r	   BOTrI   )r.   r:   r   rt   rz   r   bot_datas          r   r4   zTestFailure.write_bot&  s    ~~##d#3""$==LdmmLLHH#!	
 <<
0G0G0PRZ[r   c                    | j                   D ]5  }|j                  t        |j                  |j                  |      |_        7 y)z<Populate test result confidence using the provided metadata.N)r   r   r   r   r   )r.   r   rt   s      r   r   zTestFailure.populate_confidenceA  s=    }} 	`G!!)%9',,V^%_"	`r   c                    d| j                   z  }| j                  r|d| j                  z  z  }| j                  r|d| j                  z  z  }|S )zNReturn a string representing the CLI command associated with the test failure.zansible-test %sz
 --test %sz --python %srG   )r.   r*   s     r   r   zTestFailure.format_commandG  sM    #dll299|dii//G~(;(;;;Gr   c                    | j                   dk7  ry| j                  r| j                   dnd}t        d| j                    d|       }|S )zBReturn the docs URL for this test or None if there is no docs URL.sanityNz.htmlro   z>https://docs.ansible.com/ansible-core/devel/dev_guide/testing//)r*   r+   r   )r.   filenameurls      r   r   zTestFailure.find_docsS  sQ    <<8#*.))dii[&[\`\h\h[iijksjtuv
r   c                    | j                         }| j                  rd}n2t        | j                        dk(  rdndt        | j                        z  }|rd|z  }nd}d|d|d	|d
}|S )zvReturn a string containing a title/heading for this test failure, including an optional help link to explain the test.z	the errorr   z1 errorz	%d errorsz [[explain](%s)]ro   z
The test ``z failed with :)r   r   r   r   )r.   r   r*   r   help_link_markupr   s         r   r   zTestFailure.format_title]  sl    %%'<< F"%dmm"4"9Y{SQUQ^Q^M_?_F1I=!5<>NPVWr   c                    | j                   r| j                   }n"dj                  d | j                  D              }|j                         }|j	                  t
        j                  d      }|S )zMFormat the test summary or messages as a block of text and return the result.
c              3  <   K   | ]  }|j                           y wr_   r   r   s     r   r   z+TestFailure.format_block.<locals>.<genexpr>t  s     @Qahhj@s   ro   )r   joinr   stripreplacer   clear)r.   blockrt   s      r   r   zTestFailure.format_blocko  sP    <<LLEII@$--@@E++- //'--4r   )NNN)
r*   ra   r+   ra   r,   rb   r   z#t.Optional[c.Sequence[TestMessage]]r   rb   re   rf   )r   r
   rc   rd   rc   ra   rc   rb   r_   )r   rb   rc   ra   )rg   rh   ri   rj   r/   r;   r3   r6   r8   r4   r   r   r   r   r   r{   r|   s   @r   r   r      s{     +/8<#'  (	
 6 !$I&
)$\6`
$r   r   c                      e Zd ZdZ	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZedd       Zedd       Zedd       Zedd       Z	edd       Z
edd	       Zedd
       Zd Zd Zd Zd Zd Zd Zd Zd ZdddZy)r   z!Single test message for one file.Nc                f    || _         || _        || _        || _        || _        || _        || _        y r_   )_TestMessage__path_TestMessage__line_TestMessage__column_TestMessage__level_TestMessage__code_TestMessage__messager   )r.   rt   r   r   columnlevelcoder   s           r   r/   zTestMessage.__init__  s5      $r   c                    | j                   S )zReturn the path.)r   r>   s    r   r   zTestMessage.path       {{r   c                    | j                   S )z2Return the line number, or 0 if none is available.)r   r>   s    r   r   zTestMessage.line  r   r   c                    | j                   S )z4Return the column number, or 0 if none is available.)r   r>   s    r   r   zTestMessage.column  s     }}r   c                    | j                   S )zReturn the level.)r   r>   s    r   r   zTestMessage.level  s     ||r   c                    | j                   S )zReturn the code, if any.)r   r>   s    r   r   zTestMessage.code  r   r   c                    | j                   S )zReturn the message.)r   r>   s    r   rt   zTestMessage.message  s     ~~r   c                    | j                   | j                  | j                  | j                  | j                  | j
                  fS )zBReturn a tuple with all the immutable values of this test message.)r   r   r   r   r   r   r>   s    r   tuplezTestMessage.tuple  s2     {{DKKdkkSWSaSaaar   c                4    | j                   |j                   k  S r_   r   r.   others     r   __lt__zTestMessage.__lt__      zzEKK''r   c                4    | j                   |j                   k  S r_   r   r   s     r   __le__zTestMessage.__le__      zzU[[((r   c                4    | j                   |j                   k(  S r_   r   r   s     r   __eq__zTestMessage.__eq__  r   r   c                4    | j                   |j                   k7  S r_   r   r   s     r   __ne__zTestMessage.__ne__  r   r   c                4    | j                   |j                   kD  S r_   r   r   s     r   __gt__zTestMessage.__gt__  r   r   c                4    | j                   |j                   k\  S r_   r   r   s     r   __ge__zTestMessage.__ge__  r   r   c                ,    t        | j                        S r_   )hashr   r>   s    r   __hash__zTestMessage.__hash__  s    DJJr   c                "    | j                         S r_   r   r>   s    r   __str__zTestMessage.__str__  s    {{}r   c                    | j                   r| j                   d| j                  }n| j                  }|r| j                  |d| j                  z  z  }| j                  d| j                  d| j
                  d|S )zZReturn a string representation of this message, optionally including the confidence level.z: z (%d%%)r   )r   r   r   r   r   r   )r.   r   msgs      r   r   zTestMessage.format  s^    ;;"kk4>>:C..Ct:9t..C!%dkk4==#NNr   )r   r   r   NN)rt   ra   r   ra   r   intr   r   r   ra   r   rb   r   zt.Optional[int]r   )rc   r   r   )rc   z/tuple[str, int, int, str, t.Optional[str], str])F)r   boolrc   ra   )rg   rh   ri   rj   r/   propertyr   r   r   r   r   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ~  s   +  $&*%% % 	%
 % % % $%&             b b()))() 
Or   r   )r   ztuple[tuple[str, int], ...]r   r
   rc   r   )r   ra   r   r   r   r
   rc   r   )rj   
__future__r   collections.abcabccrU   typingtutilr   util_commonr   r   r   r	   r   r
   configr   ro   r   r   r   r'   rl   r~   r   r   r   r   r   r   <module>r      s    6 "     (B[ B[J0[* 0[f)* ))* )4b* bJ[O [Or   