o nnh8@sdZddlmZddlmZddlZddlZddl m Z ddl m Z m Z mZmZddlmZddlmZdd lmZd#ddZd$ddZGdddZGdddeZGdddeZGdddeZGdd d eZGd!d"d"ZdS)%z0Classes for storing and processing test results.) annotationsN)display) get_docs_urlwrite_text_test_resultswrite_json_test_results ResultType)Metadata) TestConfig) junit_xmlchoicestuple[tuple[str, int], ...]metadatar returnintcCs,d}|D]\}}t|||}t||}q|S)zOReturn the best confidence value available from the given choices and metadata.r)calculate_confidencemax)r rbest_confidencepathline confidencerF/usr/local/lib/python3.10/dist-packages/ansible_test/_internal/test.pycalculate_best_confidences    rrstrrcs>|j|}|s dStfdd|DrdSdkrdSdS)zbReturn the confidence level for a test result associated with the given file path and line number.rc3s0|]}|dko|dknVqdS)rrNr).0rrrr 2s.z'calculate_confidence..dK2)changesgetany)rrrrangesrrrr)s rc@sbeZdZdZdd d d Zd!ddZd"ddZd"ddZd!ddZd!ddZ d#ddZ d$ddZ dS)% TestResultzBase class for test results.Ncommandrtestpython_versiont.Optional[str]rNonecCsB||_||_||_|jp|j|_|jr|jd|j7_dSdS)N -python-%s)r'r(r)nameselfr'r(r)rrr__init__@szTestResult.__init__argsr cCs8||||jr||jr||dSdS,Write the test results to various locations.N) write_console write_botlint write_lintjunit write_junitr/r1rrrwriteIs zTestResult.writecCdS)Write results to console.Nrr/rrrr4TzTestResult.write_consolecCr<)Write lint results to stdout.Nrr>rrrr7Wr?zTestResult.write_lintcCr<)3Write results to a file for ansibullbot to consume.Nrr:rrrr5Zr?zTestResult.write_botcCr<)"Write results to a junit XML file.Nrr:rrrr9]r?zTestResult.write_junit extensioncCs>d|j}|jr|d|j7}|jr|d|j7}||7}|S)z=Return the name of the result file using the given extension.zansible-test-%sz-%sr,r'r(r))r/rCr-rrrcreate_result_name`s zTestResult.create_result_name test_casejunit_xml.TestCasecCsTtjtjd|gtjjtjjddgd}|}|jrdSt t j | d|dS)z6Save the given test case results to disk as JUnit XML. ansible-testtz)r-cases timestampsuitesN.xml) r TestSuites TestSuitedatetimenowtimezoneutc to_pretty_xmlexplainrrJUNITrE)r/r1rFrNreportrrr save_junitns zTestResult.save_junitNr'rr(rr)r*rr+r1r rr+rr+)rCrrr)r1r rFrGrr+) __name__ __module__ __qualname____doc__r0r;r4r7r5r9rErZrrrrr&=s     r&cs,eZdZdZd fdd Zd d d ZZS) TestTimeoutz Test timeout.timeout_duration int | floatrr+cstjddd||_dS)Ntimeout)r'r()superr0rd)r/rd __class__rrr0 zTestTimeout.__init__r1r c Csd|j}d}|jr|d7}|d7}tjtjdtjjtjjdtj ddtj |dgd gd gd }| }t t j|d |d S)r3zrrrr4szTestSkipped.write_consoler1r cCs*tj|j|j|jp dd}|||dS)rBrx)rmr-skippedN)r rpr'r-rwrZrurrrr9s zTestSkipped.write_junitr[r\r^r])r_r`rarbr0r4r9rsrrrirrvs  rvcseZdZdZ   d(d)fd d Zd*fdd Zd+ddZd+ddZd*ddZd*ddZ d,ddZ d-dd Z d.d!d"Z d/d0d$d%Z d-d&d'ZZS)1 TestFailurez Test failure.Nr'rr(r)r*messages#t.Optional[c.Sequence[TestMessage]]summarycs2t||||rt|}ng}||_||_dSr[)rhr0sortedr~r)r/r'r(r)r~rrirrr0s   zTestFailure.__init__r1r rr+cs$|jjr ||jt|dSr2)rr"populate_confidencerhr;r:rirrr;s zTestFailure.writecCs|jr t|jdS|jrd|j}nd}tdt|j|jp"|j|f|jD] }t|jddq*| }|rEt d|dSdS)r=z on python %srgz1Found %d %s issue(s)%s which need to be resolved:T)show_confidencezSee documentation for help: %sN) rrerrorr)lenr~r(r'format find_docsr{)r/ specifierrldoc_urlrrrr4s " zTestFailure.write_consolecCsH|jr|}d|}d}t||}t|dS|jD]}t|qdS)r@z4The test `%s` failed. See stderr output for details.rgN)rformat_command TestMessageprintr~)r/r'rlrrrrr7s    zTestFailure.write_lintcCs@|}|}tj|j|jtj||dgd}|||dS)rBrlrr)rmr-failuresN) format_title format_blockr rpr'r-r}rZ)r/r1titlerrrFrrrr9s zTestFailure.write_junitcCsv|}|j|d}|}|jrtdd|jD}nd}t||t||dgd}|jr/dSttj | d|dS) rA) help_linkcss|] }|jpddkVqdS)rr!N)rrmrrrr-sz(TestFailure.write_bot..Fr)verifieddocsresultsNz.json) rrrr~alldictrWrrBOTrE)r/r1rrlrrrbot_datarrrr5&s$  zTestFailure.write_botrr cCs,|jD]}|jdurt|j|j||_qdS)z|jdkrdS|jr|jdnd}td|jd|}|S)zBReturn the docs URL for this test or None if there is no docs URL.sanityNz.htmlrgz>https://docs.ansible.com/ansible-core/devel/dev_guide/testing//)r'r(r)r/filenameurlrrrrSs zTestFailure.find_docsrcCsX|}|jr d}nt|jdkrdndt|j}|r!d|}nd}d|||f}|S)zvReturn a string containing a title/heading for this test failure, including an optional help link to explain the test.z the errorrz1 errorz %d errorsz [[explain](%s)]rgzThe test `%s`%s failed with %s:)rrrr~)r/rr'rwhelp_link_markuprrrrr]s  zTestFailure.format_titlecCs>|jr|j}n ddd|jD}|}|tjd}|S)zMFormat the test summary or messages as a block of text and return the result. css|]}|VqdSr[rrrrrrtsz+TestFailure.format_block..rg)rjoinr~stripreplacerclear)r/blockrlrrrros zTestFailure.format_block)NNN) r'rr(rr)r*r~rrr*r]r^)rr rr+rrrr*r[)rr*rr)r_r`rarbr0r;r4r7r9r5rrrrrrsrrrirr}s       r}c@seZdZdZ     d7d8ddZed9ddZed:ddZed:ddZed9ddZ ed;ddZ ed9ddZ edd5d6ZdS)?rz!Single test message for one file.rrNrlrrrrcolumnlevelcoder*rt.Optional[int]cCs.||_||_||_||_||_||_||_dSr[)_TestMessage__path_TestMessage__line_TestMessage__column_TestMessage__level_TestMessage__code_TestMessage__messager)r/rlrrrrrrrrrr0s  zTestMessage.__init__rcC|jS)zReturn the path.)rr>rrrrzTestMessage.pathcCr)z2Return the line number, or 0 if none is available.)rr>rrrrrzTestMessage.linecCr)z4Return the column number, or 0 if none is available.)rr>rrrrrzTestMessage.columncCr)zReturn the level.)rr>rrrrrzTestMessage.levelcCr)zReturn the code, if any.)rr>rrrrrzTestMessage.codecCr)zReturn the message.)rr>rrrrlrzTestMessage.message/tuple[str, int, int, str, t.Optional[str], str]cCs|j|j|j|j|j|jfS)zBReturn a tuple with all the immutable values of this test message.)rrrrrrr>rrrtupleszTestMessage.tuplecCs |j|jkSr[rr/otherrrr__lt__ zTestMessage.__lt__cCs |j|jkSr[rrrrr__le__rzTestMessage.__le__cCs |j|jkSr[rrrrr__eq__rzTestMessage.__eq__cCs |j|jkSr[rrrrr__ne__rzTestMessage.__ne__cCs |j|jkSr[rrrrr__gt__rzTestMessage.__gt__cCs |j|jkSr[rrrrr__ge__rzTestMessage.__ge__cCs t|jSr[)hashrr>rrr__hash__s zTestMessage.__hash__cCs|Sr[rr>rrr__str__szTestMessage.__str__FrboolcCsP|jr d|j|jf}n|j}|r|jdur|d|j7}d|j|j|j|fS)zZReturn a string representation of this message, optionally including the confidence level.z%s: %sNz (%d%%)z %s:%s:%s: %s)rrrrrr)r/rmsgrrrrs zTestMessage.format)rrrNN)rlrrrrrrrrrrr*rrr)rrr)rr)F)rrrr)r_r`rarbr0propertyrrrrrrlrrrrrrrrrrrrrrr~s>        r)r r rr rr)rrrrrr rr)rb __future__rcollections.abcabccrRtypingtutilr util_commonrrrrrr configr rgr rrr&rcrtrvr}rrrrrs&        E3 &