a
    ¡Öa_b  ã                   @   sF  d Z ddlmZ ddlZddlZddlZddlZddlZdZdd„ Z	G dd„ dej
ƒZd d
d„Zd!dd„Zdd„ ZejZejZeeddƒdu rÆzddlZe ¡ ZejZdZW qÊ eyÂ   d	ZY qÊ0 ndZdZdZ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eƒZe dkrBedej!ej"d dS )"znRun a unittest testcase reporting results as Subunit.

  $ python -m samba.subunit.run mylib.tests.test_suite
é    )ÚUTCNTc           
         s„   dd„ ‰ ‡ fdd„}|\}}}t r6|r6ˆ |ƒr6|j}q"tj}t rj|jrjt||jƒrj||ƒ}|||||ƒ}	n||||ƒ}	|  |	¡ dS )zŠConverts a sys.exc_info()-style tuple of values into a string.

    Copied from Python 2.7's unittest.TestResult._exc_info_to_string.
    c                 S   s   d| j jv S )NZ
__unittest)Útb_frameÚ	f_globals)Útb© r   ú3/usr/lib/python3/dist-packages/samba/subunit/run.pyÚ_is_relevant_tb_level/   s    z.write_traceback.<locals>._is_relevant_tb_levelc                    s$   d}| r ˆ | ƒs |d7 }| j } q|S )Nr   é   )Útb_next)r   Úlength©r   r   r   Ú_count_relevant_tb_levels2   s
    z2write_traceback.<locals>._count_relevant_tb_levelsN)ÚHIDE_INTERNAL_STACKr
   Ú	tracebackÚformat_exceptionZfailureExceptionÚ
isinstanceÚ
writelines)
ÚstreamÚerrÚtestr   ÚexctypeÚvaluer   r   r   ZmsgLinesr   r   r   Úwrite_traceback*   s    


ÿr   c                       s†   e Zd ZdZdd„ Zdd„ Zddd„Zdd	d
„Zddd„Zddd„Z	d dd„Z
dd„ Zdd„ Z‡ fdd„Z‡ fdd„Zdd„ Z‡  ZS )!ÚTestProtocolClientaæ  A TestResult which generates a subunit stream for a test run.

    # Get a TestSuite or TestCase to run
    suite = make_suite()
    # Create a stream (any object with a 'write' method). This should accept
    # bytes not strings: subunit is a byte orientated protocol.
    stream = open('tests.log', 'wb')
    # Create a subunit result object which will output to the stream
    result = subunit.TestProtocolClient(stream)
    # Optionally, to get timing data for performance analysis, wrap the
    # serialiser with a timing decorator
    result = subunit.test_results.AutoTimingTestResultDecorator(result)
    # Run the test suite reporting to the subunit result object
    suite.run(result)
    # Close the stream.
    stream.close()
    c                 C   s   t j | ¡ || _d| _d S )NF)ÚunittestÚ
TestResultÚ__init__Ú_streamÚfailed)Úselfr   r   r   r   r   ^   s    zTestProtocolClient.__init__c                 C   s   | j  S ©N)r   ©r   r   r   r   ÚwasSuccessfulc   s    z TestProtocolClient.wasSuccessfulNc                 C   s   | j d||d d| _dS )z‰Report an error in test test.

        :param error: Standard unittest positional argument form - an
            exc_info tuple.
        Úerror©r#   TN©Ú_addOutcomer   ©r   r   r#   r   r   r   ÚaddErrorf   s    zTestProtocolClient.addErrorc                 C   s   | j d||d dS )z”Report an expected failure in test test.

        :param error: Standard unittest positional argument form - an
            exc_info tuple.
        Zxfailr$   N©r&   r'   r   r   r   ÚaddExpectedFailureo   s    z%TestProtocolClient.addExpectedFailurec                 C   s   | j d||d d| _dS )zŠReport a failure in test test.

        :param error: Standard unittest positional argument form - an
            exc_info tuple.
        Zfailurer$   TNr%   r'   r   r   r   Ú
addFailurew   s    zTestProtocolClient.addFailureTc                 C   s~   | j  d| | ¡  ¡ |r*|du r6t‚n|dur6t‚|durZ| j  d¡ t| j ||ƒ n| j  d¡ |durz| j  d¡ dS )ay  Report a failure in test test.

        :param outcome: A string describing the outcome - used as the
            event name in the subunit stream.
        :param error: Standard unittest positional argument form - an
            exc_info tuple.
        :param error_permitted: If True then error must be supplied.
            If False then error must not be supplied.
        z%s: Nz [
Ú
ú]
)r   ÚwriteÚidÚ
ValueErrorr   )r   Zoutcomer   r#   Úerror_permittedr   r   r   r&   €   s    
zTestProtocolClient._addOutcomec                 C   sN   |du r| j d|dd n0| j d| ¡  ¡ | j d| ¡ | j d¡ dS )zReport a skipped test.NÚskipr$   zskip: %s [
ú%s
r-   )r&   r   r.   r/   ©r   r   Úreasonr   r   r   ÚaddSkip™   s
    zTestProtocolClient.addSkipc                 C   s   | j d|dd dS )zReport a success in a test.Z
successfulF©r1   Nr)   ©r   r   r   r   r   Ú
addSuccess¢   s    zTestProtocolClient.addSuccessc                 C   s   | j d|dd d| _dS )z3Report an unexpected success in test test.
        Z	uxsuccessFr7   TNr%   r8   r   r   r   ÚaddUnexpectedSuccess¦   s    z'TestProtocolClient.addUnexpectedSuccessc                    s6   t t| ƒ |¡ | j d| ¡  d ¡ | j ¡  dS )z%Mark a test as starting its test run.ztest: r,   N)Úsuperr   Ú	startTestr   r.   r/   Úflushr8   ©Ú	__class__r   r   r<   ¬   s    zTestProtocolClient.startTestc                    s   t t| ƒ |¡ | j ¡  d S r    )r;   r   ÚstopTestr   r=   r8   r>   r   r   r@   ²   s    zTestProtocolClient.stopTestc              
   C   s:   |  t¡}| j d|j|j|j|j|j|j	|j
f ¡ dS )z^Inform the client of the time.

        ":param datetime: A datetime.datetime object.
        z*time: %04d-%02d-%02d %02d:%02d:%02d.%06dZ
N)Z
astimezoner   r   r.   ZyearZmonthZdayZhourZminuteÚsecondZmicrosecond)r   Ú
a_datetimeÚtimer   r   r   rC   ¶   s
    
þzTestProtocolClient.time)N)N)N)NT)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r"   r(   r*   r+   r&   r6   r9   r:   r<   r@   rC   Ú__classcell__r   r   r>   r   r   K   s   
	

	

	r   Fc                 C   sª   zt | ƒ}W n  ty,   |  ¡ | fg Y S 0 t| ƒtjfv sB|rbg }|D ]}| t|ƒ¡ qJ|S d }t| ƒ}|D ]}| ¡ } q„qrt	| dd ƒd urœ|  
¡  || fgS d S )NÚ
sort_tests)ÚiterÚ	TypeErrorr/   Útyper   Ú	TestSuiteÚextendÚ_flatten_testsÚiterate_testsÚgetattrrI   )Úsuite_or_caseÚunpack_outerÚtestsÚresultr   Zsuite_idr   r   r   rO   Á   s&    ÿrO   c                 C   s(   t | |d}| ¡  t dd„ |D ƒ¡S )z;Sort suite_or_case while preserving non-vanilla TestSuites.)rS   c                 S   s   g | ]\}}|‘qS r   r   )Ú.0Zsort_keyr   r   r   r   Ú
<listcomp>à   ó    z sorted_tests.<locals>.<listcomp>)rO   Úsortr   rM   )rR   rS   rT   r   r   r   Úsorted_testsÜ   s    rZ   c                 c   sH   zt | ƒ}W n ty$   | V  Y n 0 |D ]}t|ƒD ]
}|V  q6q*dS )z>Iterate through all of the test cases in 'test_suite_or_case'.N)rJ   rK   rP   )Ztest_suite_or_caseZsuiter   Zsubtestr   r   r   rP   ã   s    rP   Údiscoverz7  -c, --catch      Catch control-C and display results
z=  -b, --buffer     Buffer stdout and stderr during test runs
a/  Usage: %(progName)s [options] [tests]

Options:
  -h, --help       Show this message
  -v, --verbose    Verbose output
  -q, --quiet      Minimal output
  -l, --list       List tests rather than executing them.
  --load-list      Specifies a file containing test ids, only tests matching
                   those ids are executed.
%(catchbreak)s%(buffer)s
Examples:
  %(progName)s test_module               - run tests from test_module
  %(progName)s module.TestClass          - run tests from module.TestClass
  %(progName)s module.Class.test_method  - run specified test method

All options must come before [tests].  [tests] can be a list of any number of
test modules, classes and test methods.

Alternative Usage: %(progName)s discover [options]

Options:
  -v, --verbose    Verbose output
s%(catchbreak)s%(buffer)s  -s directory     Directory to start discovery ('.' default)
  -p pattern       Pattern to match test files ('test*.py' default)
  -t directory     Top level directory of project (default to
                   start directory)
  -l, --list       List tests rather than executing them.
  --load-list      Specifies a file containing test ids, only tests matching
                   those ids are executed.

For test discovery all test modules must be importable from the top
level directory of the project.
c                   @   s    e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zd#dd„Z	d$dd„Z
dd„ Zd%dd„Zd&dd„Zdd„ Zdd„ Zedd„ ƒZdd„ Zedd „ ƒZd!d"„ ZdS )'ÚTestResultDecoratora  General pass-through decorator.

    This provides a base that other TestResults can inherit from to
    gain basic forwarding functionality. It also takes care of
    handling the case where the target doesn't support newer methods
    or features by degrading them.
    c                 C   s
   || _ dS )z5Create a TestResultDecorator forwarding to decorated.N)Ú	decorated©r   r]   r   r   r   r   G  s    zTestResultDecorator.__init__c                 C   s   | j  |¡S r    )r]   r<   r8   r   r   r   r<   L  s    zTestResultDecorator.startTestc                 C   s
   | j  ¡ S r    )r]   ÚstartTestRunr!   r   r   r   r_   O  s    z TestResultDecorator.startTestRunc                 C   s   | j  |¡S r    )r]   r@   r8   r   r   r   r@   R  s    zTestResultDecorator.stopTestc                 C   s
   | j  ¡ S r    )r]   ÚstopTestRunr!   r   r   r   r`   U  s    zTestResultDecorator.stopTestRunNc                 C   s   | j  ||¡S r    )r]   r(   ©r   r   r   r   r   r   r(   X  s    zTestResultDecorator.addErrorc                 C   s   | j  ||¡S r    )r]   r+   ra   r   r   r   r+   [  s    zTestResultDecorator.addFailurec                 C   s   | j  |¡S r    )r]   r9   r8   r   r   r   r9   ^  s    zTestResultDecorator.addSuccessc                 C   s   | j  ||¡S r    )r]   r6   r4   r   r   r   r6   a  s    zTestResultDecorator.addSkipc                 C   s   | j  ||¡S r    )r]   r*   ra   r   r   r   r*   d  s    z&TestResultDecorator.addExpectedFailurec                 C   s   | j  |¡S r    )r]   r:   r8   r   r   r   r:   g  s    z(TestResultDecorator.addUnexpectedSuccessc                 C   s
   | j  ¡ S r    )r]   r"   r!   r   r   r   r"   j  s    z!TestResultDecorator.wasSuccessfulc                 C   s   | j jS r    ©r]   Ú
shouldStopr!   r   r   r   rc   m  s    zTestResultDecorator.shouldStopc                 C   s
   | j  ¡ S r    )r]   Ústopr!   r   r   r   rd   q  s    zTestResultDecorator.stopc                 C   s   | j jS r    )r]   ÚtestsRunr!   r   r   r   re   t  s    zTestResultDecorator.testsRunc                 C   s   | j  |¡S r    )r]   rC   ©r   rB   r   r   r   rC   x  s    zTestResultDecorator.time)N)N)N)N)rD   rE   rF   rG   r   r<   r_   r@   r`   r(   r+   r9   r6   r*   r:   r"   Úpropertyrc   rd   re   rC   r   r   r   r   r\   >  s&   





r\   c                       sœ   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zd!dd„Z	d"dd„Z
dd„ Zd#dd„Zd$dd„Zdd„ Zdd„ Zedd„ ƒZdd„ Zdd „ Z‡  ZS )%ÚHookedTestResultDecoratorz/A TestResult which calls a hook on every event.c                    s   t t| ƒ| _ | j  |¡ d S r    )r;   rh   r   r^   r>   r   r   r     s    z"HookedTestResultDecorator.__init__c                 C   s   |   ¡  | j |¡S r    )Ú_before_eventr;   r<   r8   r   r   r   r<   ƒ  s    z#HookedTestResultDecorator.startTestc                 C   s   |   ¡  | j ¡ S r    )ri   r;   r_   r!   r   r   r   r_   ‡  s    z&HookedTestResultDecorator.startTestRunc                 C   s   |   ¡  | j |¡S r    )ri   r;   r@   r8   r   r   r   r@   ‹  s    z"HookedTestResultDecorator.stopTestc                 C   s   |   ¡  | j ¡ S r    )ri   r;   r`   r!   r   r   r   r`     s    z%HookedTestResultDecorator.stopTestRunNc                 C   s   |   ¡  | j ||¡S r    )ri   r;   r(   ra   r   r   r   r(   “  s    z"HookedTestResultDecorator.addErrorc                 C   s   |   ¡  | j ||¡S r    )ri   r;   r+   ra   r   r   r   r+   —  s    z$HookedTestResultDecorator.addFailurec                 C   s   |   ¡  | j |¡S r    )ri   r;   r9   r8   r   r   r   r9   ›  s    z$HookedTestResultDecorator.addSuccessc                 C   s   |   ¡  | j ||¡S r    )ri   r;   r6   r4   r   r   r   r6   Ÿ  s    z!HookedTestResultDecorator.addSkipc                 C   s   |   ¡  | j ||¡S r    )ri   r;   r*   ra   r   r   r   r*   £  s    z,HookedTestResultDecorator.addExpectedFailurec                 C   s   |   ¡  | j |¡S r    )ri   r;   r:   r8   r   r   r   r:   §  s    z.HookedTestResultDecorator.addUnexpectedSuccessc                 C   s   |   ¡  | j ¡ S r    )ri   r;   r"   r!   r   r   r   r"   «  s    z'HookedTestResultDecorator.wasSuccessfulc                 C   s   |   ¡  | jjS r    )ri   r;   rc   r!   r   r   r   rc   ¯  s    z$HookedTestResultDecorator.shouldStopc                 C   s   |   ¡  | j ¡ S r    )ri   r;   rd   r!   r   r   r   rd   ´  s    zHookedTestResultDecorator.stopc                 C   s   |   ¡  | j |¡S r    )ri   r;   rC   rf   r   r   r   rC   ¸  s    zHookedTestResultDecorator.time)N)N)N)N)rD   rE   rF   rG   r   r<   r_   r@   r`   r(   r+   r9   r6   r*   r:   r"   rg   rc   rd   rC   rH   r   r   r>   r   rh   |  s"   




rh   c                       s<   e Zd ZdZ‡ fdd„Zdd„ Zedd„ ƒZdd	„ Z‡  Z	S )
ÚAutoTimingTestResultDecoratora  Decorate a TestResult to add time events to a test run.

    By default this will cause a time event before every test event,
    but if explicit time data is being provided by the test run, then
    this decorator will turn itself off to prevent causing confusion.
    c                    s   d | _ tt| ƒ |¡ d S r    )Ú_timer;   rj   r   r^   r>   r   r   r   Å  s    z&AutoTimingTestResultDecorator.__init__c                 C   s4   | j }|d urd S tj ¡ jtd}| j |¡ d S )N)Ztzinfo)rk   ÚdatetimeZutcnowÚreplacer   r]   rC   )r   rC   r   r   r   ri   É  s
    z+AutoTimingTestResultDecorator._before_eventc                 C   s   | j jS r    rb   r!   r   r   r   rc   Ð  s    z(AutoTimingTestResultDecorator.shouldStopc                 C   s   || _ | j |¡S )a\  Provide a timestamp for the current test activity.

        :param a_datetime: If None, automatically add timestamps before every
            event (this is the default behaviour if time() is not called at
            all).  If not None, pass the provided time onto the decorated
            result object and disable automatic timestamps.
        )rk   r]   rC   rf   r   r   r   rC   Ô  s    z"AutoTimingTestResultDecorator.time)
rD   rE   rF   rG   r   ri   rg   rc   rC   rH   r   r   r>   r   rj   ½  s   
rj   c                   @   s   e Zd Zddd„Zdd„ ZdS )ÚSubunitTestRunnerNc                 C   s   |pt j| _dS )zhCreate a SubunitTestRunner.

        :param verbosity: Ignored.
        :param buffer: Ignored.
        N)ÚsysÚstdoutr   )r   Ú	verbosityÚbufferr   r   r   r   r   â  s    zSubunitTestRunner.__init__c                 C   s   t | jƒ}t|ƒ}||ƒ |S )z&Run the given test case or test suite.)r   r   rj   )r   r   rU   r   r   r   Úrunê  s    
zSubunitTestRunner.run)NNN)rD   rE   rF   r   rs   r   r   r   r   rn   à  s   
rn   c                
   @   sl   e Zd ZdZeZd Z ZZe ddde	dddddf
dd„Z
dd„ Zd	d
„ Zefdd„Zdd„ Zddd„ZdS )ÚTestProgramzA command-line program that runs a set of tests; this is primarily
       for making test modules conveniently executable.
    NTr	   c                 C   sÀ  |t krd | _nBt|tƒrLt|ƒ| _| d¡dd … D ]}t| j|ƒ| _q6n|| _|d u r`tj}|
d u rntj	}
|d u r|t
ƒ }|| _|| _|| _|	| _|| _d| _d | _|| _|| _|d }| dtjj ¡rè| tjj¡}d|d  }ntj |d ¡}|| _|  |¡ | jr†t| jdƒ}z| ¡ }W | ¡  n
| ¡  0 td	d
„ |D ƒƒ}t  ¡ }t!| j"ƒD ]}| #¡ |v r`| $|¡ q`|| _"| js˜|  %¡  n$t!| j"ƒD ]}|
 &d| #¡  ¡ q¢d S )NÚ.r	   Fr   z%srun.pyz%s.runéþÿÿÿÚrbc                 s   s   | ]}|  ¡  d ¡V  qdS )zutf-8N)ÚstripÚdecode)rV   Úliner   r   r   Ú	<genexpr>)  rX   z'TestProgram.__init__.<locals>.<genexpr>r3   )'rD   Úmoduler   ÚstrÚ
__import__ÚsplitrQ   ro   Úargvrp   rn   ÚexitÚ
catchbreakrq   rr   ÚdefaultTestÚ	listtestsÚ	load_listÚ
testRunnerÚ
testLoaderÚendswithÚosÚpathÚsepÚbasenameÚprogNameÚ	parseArgsÚopenÚ	readlinesÚcloseÚsetr   rM   rP   r   r/   ZaddTestÚrunTestsr.   )r   r|   rƒ   r€   r†   r‡   r   rq   r‚   rr   rp   Úpartr   ÚelementsÚsourceÚlinesZtest_idsZfilteredr   r   r   r   r   û  sX    




zTestProgram.__init__c                 C   s^  t |ƒdkr2|d  ¡ dkr2|  |dd … ¡ d S dd l}g d¢}zî| |dd … d|¡\}}|D ]€\}}|dv rz|  ¡  |dv rˆd| _|d	v r–d| _|d
v r®| jd u r®d| _|dv rÆ| jd u rÆd| _|dv rÔd| _|dkrb|| _	qbt |ƒdkr| j
d u rd | _n t |ƒdkr|| _n
| j
f| _|  ¡  W n( |jyX   |  t ¡ d ¡ Y n0 d S )Nr	   r[   é   r   )ÚhelpÚverboseÚquietZcatchrr   Úlistz
load-list=ZhHvqfcbl)z-hz-Hz--help)z-qz--quiet)ú-vú	--verbose)ú-cú--catchT)ú-bú--buffer)ú-lú--listú--load-list)ÚlenÚlowerÚ_do_discoveryÚgetoptÚ	usageExitrq   r‚   rr   r„   r…   rƒ   Ú	testNamesÚcreateTestsr#   ro   Úexc_info)r   r€   r©   Z	long_optsÚoptionsÚargsZoptr   r   r   r   rŽ   5  s@    


zTestProgram.parseArgsc                 C   s4   | j d u r| j | j¡| _n| j | j | j¡| _d S r    )r«   r‡   ZloadTestsFromModuler|   r   ZloadTestsFromNamesr!   r   r   r   r¬   ]  s
    

ÿzTestProgram.createTestsc                 C   sª  t stdƒ‚d| j | _dd l}| ¡ }| j|_|jdddddd	d
 | jdkrf|jdddddd	d
 | jdkr†|jdddddd	d
 |jdddddd |jdddddd |jddd d d!d |jd"d#d$dd	d%d& |jd'd(d d)d | 	|¡\}}t
|ƒd*kr|  ¡  td+|ƒD ]\}}t|||ƒ q| jd u rD|j| _| jd u rX|j| _|j| _|j| _|jrvd,| _|j}	|j}
|j}|ƒ }| |	|
|¡}t|ƒ| _d S )-NzYUnable to use discovery, must use python 2.7 or greater, or install the discover package.z%s discoverr   r   rž   rš   FzVerbose outputÚ
store_true)ÚdestÚdefaultr™   ÚactionrŸ   r    r‚   z'Catch ctrl-C and display results so farr¡   r¢   rr   z%Buffer stdout and stderr during testsz-sz--start-directoryÚstartru   z*Directory to start discovery ('.' default))r±   r²   r™   z-pz	--patternÚpatternztest*.pyz+Pattern to match tests ('test*.py' default)z-tz--top-level-directoryÚtopz<Top level directory of project (defaults to start directory)r£   r¤   r„   z$List tests rather than running them.)r±   r²   r³   r™   r¥   r…   z2Specify a filename containing the test ids to use.é   )r´   rµ   r¶   r˜   )Úhave_discoverÚAssertionErrorr   ÚoptparseZOptionParserÚprogZ
add_optionr‚   rr   Ú
parse_argsr¦   rª   ÚzipÚsetattrr„   r…   rš   rq   r´   rµ   r¶   r[   rZ   r   )r   r€   ÚLoaderrº   Úparserr®   r¯   Únamer   Z	start_dirrµ   Ztop_level_dirÚloaderZloadedr   r   r   r¨   d  sj    ÿ
þ
þÿÿÿÿ
ÿ
zTestProgram._do_discoveryc                 C   sJ   | j rttdd ƒd urt ¡  | j | j¡| _| jrFt	 | j 
¡  ¡ d S )NÚinstallHandler)r‚   rQ   r   rÃ   r†   rs   r   rU   r   ro   r"   r!   r   r   r   r“   ¦  s    ÿzTestProgram.runTestsc                 C   s†   |rt |ƒ | jdddœ}| jdkr,t|d< | jdkr>t|d< | j| }| d¡}| dd¡ | d	d¡ t d 	|¡ƒ t
 d¡ d S )
NÚ )r   r‚   rr   Fr‚   rr   r,   r˜   z)Run a test suite with a subunit reporter.r·   )Úprintr   r‚   Ú
CATCHBREAKrr   ÚBUFFEROUTPUTÚUSAGEr   ÚinsertÚjoinro   r   )r   ÚmsgZusageZ
usage_textZusage_linesr   r   r   rª   ®  s    ÿ



zTestProgram.usageExit)N)rD   rE   rF   rG   ÚUSAGE_AS_MAINrÈ   r‚   rr   r   ÚdefaultTestLoaderr   rŽ   r¬   ÚdefaultTestLoaderClsr¨   r“   rª   r   r   r   r   rt   ò  s   ý
:(Brt   Ú__main__)r|   r€   rp   )F)F)#rG   Ziso8601.iso8601r   rl   r‰   ro   r   r   r   r   r   r   rO   rZ   rP   rÍ   Z
TestLoaderrÎ   rQ   r[   ZDiscoveringTestLoaderr¸   ÚImportErrorrÆ   rÇ   rÌ   Úobjectr\   rh   rj   rn   rt   rD   r€   rp   r   r   r   r   Ú<module>   sD   !v

&>A# N
