B
    `:                 @   s   d Z ddlmZmZmZ eZddlZddlZddl	m
Z ddlmZmZ ddlmZmZmZ ddlmZ d	d
 Zdd ZG dd dZG dd deZG dd deZG dd deZG dd deZG dd dZdS )z0Classes for storing and processing test results.    )absolute_importdivisionprint_functionN   )types)displayget_ansible_version)write_text_test_resultswrite_json_test_results
ResultType)
TestConfigc             C   s0   d}x&| D ]\}}t |||}t||}q
W |S )z\
    :type choices: tuple[tuple[str, int]]
    :type metadata: Metadata
    :rtype: int
    r   )calculate_confidencemax)choicesmetadataZbest_confidencepathline
confidence r   J/home/dcms/DCMS/lib/python3.7/site-packages/ansible_test/_internal/test.pycalculate_best_confidence   s
    r   c                s>   |j | }|sdS t fdd|D r.dS  dkr:dS dS )zZ
    :type path: str
    :type line: int
    :type metadata: Metadata
    :rtype: int
    r   c             3   s8   | ]0}|d     ko*|d   ko*|kn  V  qdS )r   r   Nr   ).0r)r   r   r   	<genexpr>7   s    z'calculate_confidence.<locals>.<genexpr>d   K   2   )changesgetany)r   r   r   rangesr   )r   r   r   )   s    r   c               @   sT   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dZdS )
TestResultzBase class for test results.Nc             C   sj   || _ || _|| _| jp| j | _| jr:|  jd| j 7  _yddl}W n tk
r^   d}Y nX || _dS )z^
        :type command: str
        :type test: str
        :type python_version: str
        z
-python-%sr   N)commandtestpython_versionname	junit_xmlImportErrorjunit)selfr"   r#   r$   r&   r   r   r   __init__D   s    
zTestResult.__init__c             C   sJ   |    | | |jr |   |jrF| jr8| | ntjddd dS )z(
        :type args: TestConfig
        zOSkipping junit xml output because the `junit-xml` python package was not found.T)uniqueN)write_console	write_botZlint
write_lintr(   write_junitr   warning)r)   argsr   r   r   writeY   s    
zTestResult.writec             C   s   dS )zWrite results to console.Nr   )r)   r   r   r   r,   i   s    zTestResult.write_consolec             C   s   dS )zWrite lint results to stdout.Nr   )r)   r   r   r   r.   l   s    zTestResult.write_lintc             C   s   dS )z(
        :type args: TestConfig
        Nr   )r)   r1   r   r   r   r-   o   s    zTestResult.write_botc             C   s   dS )z(
        :type args: TestConfig
        Nr   )r)   r1   r   r   r   r/   t   s    zTestResult.write_junitc             C   s>   d| j  }| jr|d| j 7 }| jr2|d| j 7 }||7 }|S )z:
        :type extension: str
        :rtype: str
        zansible-test-%sz-%sz
-python-%s)r"   r#   r$   )r)   	extensionr%   r   r   r   create_result_namey   s    
zTestResult.create_result_namec             C   s   | j jd|gtj jdd |dg}y| j j}W n tk
rT   | j jj}Y nX ||ddd}|j	rndS t
tj| d	| dS )
z
        :type args: TestConfig
        :type test_case: junit_xml.TestCase
        :type properties: dict[str, str] | None
        :rtype: str | None
        zansible-testr   )microsecond)r%   Z
test_cases	timestamp
propertiesTzutf-8)test_suitesZprettyprintencodingNz.xml)r(   Z	TestSuitedatetimeutcnowreplace	isoformatZto_xml_report_stringAttributeErrorto_xml_stringexplainr	   r   JUNITr4   )r)   r1   	test_caser7   r8   r?   reportr   r   r   
save_junit   s    
zTestResult.save_junit)N)N)__name__
__module____qualname____doc__r*   r2   r,   r.   r-   r/   r4   rD   r   r   r   r   r!   B   s   
r!   c                   s(   e Zd ZdZ fddZdd Z  ZS )TestTimeoutzTest timeout.c                s   t t| jddd || _dS )z-
        :type timeout_duration: int
        timeout )r"   r#   N)superrI   r*   timeout_duration)r)   rM   )	__class__r   r   r*      s    zTestTimeout.__init__c             C   sd   d| j  }d}|jr|d7 }|d7 }tj jdd }d|||f }ttj| 	d|
  d	S )
z(
        :type args: TestConfig
        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.r   )r5   a  
<?xml version="1.0" encoding="utf-8"?>
<testsuites disabled="0" errors="1" failures="0" tests="1" time="0.0">
	<testsuite disabled="0" errors="1" failures="0" file="None" log="None" name="ansible-test" skipped="0" tests="1" time="0" timestamp="%s" url="None">
		<testcase classname="timeout" name="timeout">
			<error message="%s" type="error">%s</error>
		</testcase>
	</testsuite>
</testsuites>
z.xmlN)rM   Zcoverager:   r;   r<   r=   r	   r   rA   r4   lstrip)r)   r1   messageoutputr6   xmlr   r   r   r2      s    
	zTestTimeout.write)rE   rF   rG   rH   r*   r2   __classcell__r   r   )rN   r   rI      s   rI   c               @   s   e Zd ZdZdd ZdS )TestSuccesszTest success.c             C   s$   | j j| j| jd}| || dS )z(
        :type args: TestConfig
        )	classnamer%   N)r(   TestCaser"   r%   rD   )r)   r1   rB   r   r   r   r/      s    zTestSuccess.write_junitN)rE   rF   rG   rH   r/   r   r   r   r   rT      s   rT   c               @   s    e Zd ZdZdd Zdd ZdS )TestSkippedzTest skipped.c             C   s   t jddd dS )zWrite results to console.zNo tests applicable.r   )	verbosityN)r   info)r)   r   r   r   r,      s    zTestSkipped.write_consolec             C   s.   | j j| j| jd}|d | || dS )z(
        :type args: TestConfig
        )rU   r%   zNo tests applicable.N)r(   rV   r"   r%   Zadd_skipped_inforD   )r)   r1   rB   r   r   r   r/      s    
zTestSkipped.write_junitN)rE   rF   rG   rH   r,   r/   r   r   r   r   rW      s   rW   c                   sx   e Zd ZdZd fdd	Z fd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  ZS )TestFailurezTest failure.Nc                s6   t t| ||| |r"t|}ng }|| _|| _dS )z
        :type command: str
        :type test: str
        :type python_version: str | None
        :type messages: list[TestMessage] | None
        :type summary: unicode | None
        N)rL   rZ   r*   sortedmessagessummary)r)   r"   r#   r$   r\   r]   )rN   r   r   r*      s    
zTestFailure.__init__c                s(   |j jr| |j  tt| | dS )z(
        :type args: TestConfig
        N)r   r   populate_confidencerL   rZ   r2   )r)   r1   )rN   r   r   r2     s    zTestFailure.writec             C   s   | j rt| j  nt| jr&d| j }nd}tdt| j| jpB| j|f  x | jD ]}t|jdd qTW | 	 }|rt
d|  dS )zWrite results to console.z on python %srK   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_docsrY   )r)   	specifierrP   Zdoc_urlr   r   r   r,     s    "zTestFailure.write_consolec             C   sJ   | j r.|  }d| }d}t||}t| nx| jD ]}t| q6W dS )zWrite lint results to stdout.z4The test `%s` failed. See stderr output for details.rK   N)r]   format_commandTestMessageprintr\   )r)   r"   rP   r   r   r   r   r.   $  s    

zTestFailure.write_lintc             C   sF   |   }|  }| jj| j| jd}|j|d| d | || dS )z(
        :type args: TestConfig
        )rU   r%   z
%s)rP   rQ   N)format_titleformat_blockr(   rV   r"   r%   Zadd_failure_inforD   )r)   r1   titlerQ   rB   r   r   r   r/   0  s
    zTestFailure.write_junitc             C   sv   |   }| j|d}|  }| jr8tdd | jD }nd}t||t||dgd}|jr^dS ttj	| 
d| dS )	z(
        :type args: TestConfig
        )	help_linkc             s   s   | ]}|j pd dkV  qdS )r   r   N)r   )r   mr   r   r   r   H  s    z(TestFailure.write_bot.<locals>.<genexpr>F)rP   rQ   )verifieddocsresultsNz.json)rc   rh   ri   r\   alldictr@   r
   r   ZBOTr4   )r)   r1   rn   rP   rQ   rm   Zbot_datar   r   r   r-   ?  s    zTestFailure.write_botc             C   s0   x*| j D ] }|jdkrt|j|j||_qW dS )z*
        :type metadata: Metadata
        N)r\   r   r   r   r   )r)   r   rP   r   r   r   r^   \  s    
zTestFailure.populate_confidencec             C   s6   d| j  }| jr|d| j 7 }| jr2|d| j 7 }|S )z
        :rtype: str
        zansible-test %sz
 --test %sz --python %s)r"   r#   r$   )r)   r"   r   r   r   re   d  s    
zTestFailure.format_commandc             C   sj   | j dkrdS t }d}td|r<d|ddd }d| }d|| j f }| jrf|d	| j 7 }|S )
z
        :rtype: str
        ZsanityNZdevelz	^[0-9.]+$.   z5https://docs.ansible.com/ansible/%s/dev_guide/testingz%s/%s/z%s.html)r"   r   researchjoinsplitr#   )r)   Zansible_versionZurl_versionZtesting_docs_urlurlr   r   r   rc   r  s    
zTestFailure.find_docsc             C   sX   |   }| jrd}n t| jdkr&dndt| j }|rBd| }nd}d|||f }|S )zA
        :type help_link: str | None
        :rtype: str
        z	the errorr   z1 errorz	%d errorsz [[explain](%s)]rK   zThe test `%s`%s failed with %s:)re   r]   ra   r\   )r)   rk   r"   reasonZhelp_link_markuprj   r   r   r   rh     s     
zTestFailure.format_titlec             C   s>   | j r| j }nddd | jD }| }|tjd}|S )z
        :rtype: str
        
c             s   s   | ]}|  V  qd S )N)rb   )r   rl   r   r   r   r     s    z+TestFailure.format_block.<locals>.<genexpr>rK   )r]   rv   r\   stripr<   r   clear)r)   blockrP   r   r   r   ri     s    zTestFailure.format_block)NNN)N)rE   rF   rG   rH   r*   r2   r,   r.   r/   r-   r^   re   rc   rh   ri   rS   r   r   )rN   r   rZ      s   	
rZ   c               @   s   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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S )*rf   z!Single test message for one file.r   r`   Nc             C   s.   || _ || _|| _|| _|| _|| _|| _dS )z
        :type message: str
        :type path: str
        :type line: int
        :type column: int
        :type level: str
        :type code: str | None
        :type confidence: int | None
        N)_TestMessage__path_TestMessage__line_TestMessage__column_TestMessage__level_TestMessage__code_TestMessage__messager   )r)   rP   r   r   columnlevelcoder   r   r   r   r*     s    
zTestMessage.__init__c             C   s   | j S )zReturn the path.)r~   )r)   r   r   r   r     s    zTestMessage.pathc             C   s   | j S )z2Return the line number, or 0 if none is available.)r   )r)   r   r   r   r     s    zTestMessage.linec             C   s   | j S )z4Return the column number, or 0 if none is available.)r   )r)   r   r   r   r     s    zTestMessage.columnc             C   s   | j S )zReturn the level.)r   )r)   r   r   r   r     s    zTestMessage.levelc             C   s   | j S )zReturn the code, if any.)r   )r)   r   r   r   r     s    zTestMessage.codec             C   s   | j S )zReturn the message.)r   )r)   r   r   r   rP     s    zTestMessage.messagec             C   s   | 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)   r   r   r   tuple  s    zTestMessage.tuplec             C   s   | j |j k S )N)r   )r)   otherr   r   r   __lt__  s    zTestMessage.__lt__c             C   s   | j |j kS )N)r   )r)   r   r   r   r   __le__  s    zTestMessage.__le__c             C   s   | j |j kS )N)r   )r)   r   r   r   r   __eq__  s    zTestMessage.__eq__c             C   s   | j |j kS )N)r   )r)   r   r   r   r   __ne__  s    zTestMessage.__ne__c             C   s   | j |j kS )N)r   )r)   r   r   r   r   __gt__  s    zTestMessage.__gt__c             C   s   | j |j kS )N)r   )r)   r   r   r   r   __ge__  s    zTestMessage.__ge__c             C   s
   t | jS )N)hashr   )r)   r   r   r   __hash__  s    zTestMessage.__hash__c             C   s   |   S )N)rb   )r)   r   r   r   __str__  s    zTestMessage.__str__Fc             C   sP   | j rd| j | jf }n| j}|r:| jdk	r:|d| j 7 }d| j| j| j|f S )zA
        :type show_confidence: bool
        :rtype: str
        z%s: %sNz (%d%%)z%s:%s:%s: %s)r   r   r   r~   r   r   )r)   r_   msgr   r   r   rb     s    zTestMessage.format)r   r   r`   NN)F)rE   rF   rG   rH   r*   propertyr   r   r   r   r   rP   r   r   r   r   r   r   r   r   r   rb   r   r   r   r   rf     s$   
rf   )rH   
__future__r   r   r   type__metaclass__r:   rt   rK   r   tutilr   r   Zutil_commonr	   r
   r   configr   r   r   r!   rI   rT   rW   rZ   rf   r   r   r   r   <module>   s"   h/ <