o
    ¯b�R  ã                   @   sl  d dl Z d dlZd dlZd dl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mZmZmZ d dlmZ d dlmZ d dlmZmZmZ d	d	d
d
dddœZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z dej!fde fdœZ"dd„ Z#G dd„ dƒZ$G d d!„ d!e$ej%ej&ƒZ%d"d#„ Z'd$d%„ Z(d&d'„ Z)d(d)„ Z*G d*d+„ d+e+ƒZ,d,d-„ Z-d.d/„ Z.dS )0é    N)Úplugin)Úapp)Údefer)ÚfailureÚreflectÚusage)ÚFilePath)ÚnamedModule)ÚitrialÚreporterÚrunnerÚdefaultÚbriefÚverbose)Úplainr   Úemacsr   Úcgitbr   c                 C   s´   d}|   |¡t|ƒ }|  |¡}|dks|dkrt| ›d�ƒ‚| ||…  d¡}i }|D ]+}t| ¡ ƒdkr7q,| d¡}t|ƒdkrKt| ›d|›�ƒ‚|d	  ¡ ||d  ¡ < q,|S )
a  
    Accepts a single line in Emacs local variable declaration format and
    returns a dict of all the variables {name: value}.
    Raises ValueError if 'line' is in the wrong format.

    See http://www.gnu.org/software/emacs/manual/html_node/File-Variables.html
    z-*-éÿÿÿÿz' not a valid local variable declarationú;r   ú:é   z contains invalid declaration é   )ÚfindÚlenÚrfindÚ
ValueErrorÚsplitÚstrip)ÚlineÚparenÚstartÚendÚitemsÚ	localVarsÚitemr   © r%   ú7/usr/lib/python3/dist-packages/twisted/scripts/trial.pyÚ_parseLocalVariables#   s   

r'   c              	   C   sf   t | ƒ�}| ¡ | ¡ g}W d  ƒ n1 sw   Y  |D ]}zt|ƒW   S  ty0   Y qw i S )zÑ
    Accepts a filename and attempts to load the Emacs variable declarations
    from that file, simulating what Emacs does.

    See http://www.gnu.org/software/emacs/manual/html_node/File-Variables.html
    N)ÚopenÚreadliner'   r   )ÚfilenameÚfÚlinesr   r%   r%   r&   ÚloadLocalVariables<   s   
ÿÿr-   c                 C   s&   t | ƒ dd ¡}|d u rg S | d¡S )Nztest-case-nameú,)r-   Úgetr   )r*   ÚtestCaseVarr%   r%   r&   ÚgetTestModulesM   s   
r1   c                 C   s*   t j | ¡}| d¡ot j |¡d dkS )z‰
    Returns true if 'filename' looks like a file containing unit tests.
    False otherwise.  Doesn't care whether filename exists.
    Útest_r   z.py)ÚosÚpathÚbasenameÚ
startswithÚsplitext)r*   r5   r%   r%   r&   Ú
isTestFileT   s   r8   c                   C   s   t  dd„ t tj¡D ƒ¡S )Nc                 S   s   g | ]}|j ‘qS r%   )ÚlongOpt)Ú.0Úpr%   r%   r&   Ú
<listcomp>^   s    z#_reporterAction.<locals>.<listcomp>)r   ÚCompleteListr   Ú
getPluginsr
   Ú	IReporterr%   r%   r%   r&   Ú_reporterAction]   s   r@   c              	   C   sb   t | ddƒ}|durt | |ƒ} t | ddƒ}|dur|jS zt | ¡d W S  ttfy0   Y dS w )aH  
    Try to find the source line of the given test thing.

    @param testThing: the test item to attempt to inspect
    @type testThing: an L{TestCase}, test method, or module, though only the
        former two have a chance to succeed
    @rtype: int
    @return: the starting source line, or -1 if one couldn't be found
    Ú_testMethodNameNÚ__code__r   r   )ÚgetattrÚco_firstlinenoÚinspectÚgetsourcelinesÚOSErrorÚ	TypeError)Ú	testThingÚmethodÚcoder%   r%   r&   Ú_maybeFindSourceLinea   s   
ýrL   zCalphabetical order for test methods, arbitrary order for test caseszDattempt to run test cases and methods in the order they were defined)ÚalphabeticalÚtoptobottomc                 C   s,   | t vrt dd dd„ t D ƒ¡f ¡‚| S )ai  
    Check that the given order is a known test running order.

    Does nothing else, since looking up the appropriate callable to sort the
    tests should be done when it actually will be used, as the default argument
    will not be coerced by this function.

    @param order: one of the known orders in C{_runOrders}
    @return: the order unmodified
    z9--order must be one of: %s. See --help-orders for detailsz, c                 s   s   � | ]}t |ƒV  qd S ©N)Úrepr)r:   Úorderr%   r%   r&   Ú	<genexpr>š   s   € z&_checkKnownRunOrder.<locals>.<genexpr>)Ú
_runOrdersr   Ú
UsageErrorÚjoin)rQ   r%   r%   r&   Ú_checkKnownRunOrderŒ   s   ÿÿrV   c                	   @   s0  e Zd ZdZdZddgg d¢g d¢g d¢g d¢g d	¢g d
¢g d¢gZddddegg d¢g d¢g d¢gZej	e 
e¡eejdd�ejdd�dœejdddd�gd�ZejZ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-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Z d6d7„ Z!d8d9„ Z"d:d;„ Z#dS )<Ú_BasicOptionszC
    Basic options shared between trial and its local workers.
    zutrial loads and executes a suite of unit tests, obtained from modules, packages and files listed on the command line.ÚhelpÚh)ú
no-recurseÚNzDon't recurse into packages)zhelp-ordersNz%Help on available test running orders)zhelp-reportersNz,Help on available output plugins (reporters))ÚrterrorsÚez;realtime errors, print out tracebacks as soon as they occur)úunclean-warningsNz'Turn dirty reactor errors into warnings)úforce-gcNz<Have Trial run gc.collect() before and after each test case.)Ú	exitfirstÚxzSExit after the first non-successful result (cannot be specified along with --jobs).rQ   ÚoNzRSpecify what order to run test cases and methods. See --help-orders for more info.)ÚrandomÚzNz2Run tests in random order using the specified seed)útemp-directoryNÚ_trial_tempz+Path to use as working directory for tests.)r   Nr   zKThe reporter to use for this test run.  See --help-reporters for more info.úlog file name)Údescrzrandom seed)rQ   r   Úlogfilerc   z*.pyz/file | module | package | TestCase | testMethodT)rh   Úrepeat)Ú
optActionsÚextraActionsc                 C   s   g | d< t j | ¡ d S ©NÚtests)r   ÚOptionsÚ__init__©Úselfr%   r%   r&   rp   í   s   z_BasicOptions.__init__c                 C   sB   t  tjd ¡}| d¡rd tj tj	¡| 
dd¡¡}d |¡S )Nr   z	.__main__z{} -m {}Ú zD{} [options] [[file|package|module|TestCase|testmethod]...]
        )r   ÚfilenameToModuleNameÚsysÚargvÚendswithÚformatr3   r4   r5   Ú
executableÚreplace)rr   ÚexecutableNamer%   r%   r&   ÚgetSynopsisñ   s   

þÿÿz_BasicOptions.getSynopsisc                 C   s,   d}t | d ƒ |¡}td|j› d�ƒ |S )zx
        Return a L{FilePath} representing the directory into which coverage
        results should be written.
        Úcoveragere   zSetting coverage directory to Ú.)r   ÚchildÚprintr4   )rr   ÚcoverdirÚresultr%   r%   r&   r�   ÿ   s   z_BasicOptions.coverdirc                 C   s2   ddl }|jddd�| _t | jj¡ d| d< dS )z…
        Generate coverage information in the coverage file in the
        directory specified by the temp-directory option.
        r   Nr   )ÚcountÚtraceTr}   )r„   ÚTraceÚtracerru   ÚsettraceÚglobaltrace)rr   r„   r%   r%   r&   Úopt_coverage  s   z_BasicOptions.opt_coveragec                 C   s`   t j |¡stj d|›d�¡ dS t j |¡}t|ƒr%| d  |¡ dS | d  	t
|ƒ¡ dS )zG
        Filename to grep for test cases (-*- test-case-name).
        zFile z doesn't exist
Nrn   )r3   r4   Úisfileru   ÚstderrÚwriteÚabspathr8   ÚappendÚextendr1   )rr   r*   r%   r%   r&   Úopt_testmodule  s   z_BasicOptions.opt_testmodulec                 C   s   ddl m} t |¡ dS )zŒ
        Print an insanely verbose log of everything that happens.  Useful
        when debugging freezes or locks in complex code.
        r   )ÚspewerN)Útwisted.python.utilr‘   ru   r‡   )rr   r‘   r%   r%   r&   Úopt_spew3  s   z_BasicOptions.opt_spewc                 C   sB   d}t |ƒ tt ¡ ƒD ]\}\}}t d|d|ƒ qt d¡ d S )Nz’Trial can attempt to run test cases and their methods in a few different orders. You can select any of the following options using --order=<foo>.
ú   ú	r   )r€   ÚsortedrS   r"   ru   Úexit)rr   ÚsynopsisÚnameÚdescriptionÚ_r%   r%   r&   Úopt_help_orders<  s   ÿz_BasicOptions.opt_help_ordersc                 C   s>   d}t |ƒ t tj¡D ]}t d|jd|jƒ qt d¡ d S )Nz†Trial's output can be customized using plugins called Reporters. You can
select any of the following reporters using --reporter=<foo>
r”   r•   r   )	r€   r   r>   r
   r?   r9   rš   ru   r—   )rr   r˜   r;   r%   r%   r&   Úopt_help_reportersH  s   ÿz _BasicOptions.opt_help_reportersc                 C   s   d| d< t  ¡  dS )z/
        Disable the garbage collector
        TÚ	disablegcN)ÚgcÚdisablerq   r%   r%   r&   Úopt_disablegcS  s   z_BasicOptions.opt_disablegcc                 C   s,   z	t | | d< W dS  ty   t d¡‚w )z¹
        Specify the format to display tracebacks with. Valid formats are
        'plain', 'emacs', and 'cgitb' which uses the nicely verbose stdlib
        cgitb.text function
        Útbformatz.tbformat must be 'plain', 'emacs', or 'cgitb'.N)ÚTBFORMAT_MAPÚKeyErrorr   rT   )rr   Úoptr%   r%   r&   Úopt_tbformatZ  s
   
ÿz_BasicOptions.opt_tbformatc              	   C   s@   z	t  t|ƒ¡ W n ttfy   t d¡‚w t|ƒ| d< dS )z-
        see sys.setrecursionlimit()
        z-argument to recursionlimit must be an integerÚrecursionlimitN)ru   ÚsetrecursionlimitÚintrH   r   r   rT   )rr   Úargr%   r%   r&   Úopt_recursionlimite  s   
ÿz _BasicOptions.opt_recursionlimitc                 C   sh   zt |ƒ| d< W n ty   t d¡‚w | d dk r t d¡‚| d dkr2t t ¡ d ƒ| d< d S d S )Nrc   z/Argument to --random must be a positive integerr   éd   )r©   r   r   rT   Útime)rr   Úoptionr%   r%   r&   Ú
opt_randomp  s   
ÿÿÿz_BasicOptions.opt_randomc                 C   sD   || d< |  d¡D ]}|tjv rtjd|f td� dtj|< q	dS )zP
        Fake the lack of the specified modules, separated with commas.
        úwithout-moduler.   z/Module '%s' already imported, disabling anyway.)ÚcategoryN)r   ru   ÚmodulesÚwarningsÚwarnÚRuntimeWarning)rr   r®   Úmoduler%   r%   r&   Úopt_without_module}  s   
þúz _BasicOptions.opt_without_modulec                 G   s   | d   |¡ d S rm   )r�   )rr   Úargsr%   r%   r&   Ú	parseArgsŠ  s   z_BasicOptions.parseArgsc                 C   sF   t  tj¡D ]}|j› d|j› �}|j|krt |¡  S qt	 
d¡‚)Nr~   zVOnly pass names of Reporter plugins to --reporter. See --help-reporters for more info.)r   r>   r
   r?   r¶   Úklassr9   r   ÚnamedAnyr   rT   )rr   r™   r;   Úqualr%   r%   r&   Ú_loadReporterByName�  s   
ÿÿz!_BasicOptions._loadReporterByNamec                 C   sL   |   | d ¡| d< d| vrd| d< | d d ur"| d d ur$t d¡‚d S d S )Nr   r¢   r   rQ   rc   z-You can't specify --random when using --order)r½   r   rT   rq   r%   r%   r&   ÚpostOptions˜  s   
ÿz_BasicOptions.postOptions)$Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚlongdescÚoptFlagsrV   ÚoptParametersr   ÚCompletionsr=   rS   r@   ÚCompleteFilesÚ	CompleterÚcompDatar   ÚTreeReporterÚfallbackReporterr†   rp   r|   r�   r‰   r�   r“   rœ   r�   r¡   r¦   r«   r¯   r·   r¹   r½   r¾   r%   r%   r%   r&   rW   Ÿ   sl    ÿðúð

üýÿù	rW   c                   @   s    e Zd ZdZg d¢g d¢g d¢g d¢g d¢g d¢gZg d¢g d	¢g d
¢gZeje g d¢¡e	dœd�Z
g d¢Zg d¢ZejZdZdZdd„ Zdd„ Zdd„ ZdS )ro   aç  
    Options to the trial command line tool.

    @ivar _workerFlags: List of flags which are accepted by trial distributed
        workers. This is used by C{_getWorkerArguments} to build the command
        line arguments.
    @type _workerFlags: C{list}

    @ivar _workerParameters: List of parameter which are accepted by trial
        distributed workers. This is used by C{_getWorkerArguments} to build
        the command line arguments.
    @type _workerParameters: C{list}
    )ÚdebugÚbziRun tests in a debugger. If that debugger is pdb, will load '.pdbrc' from current directory if it exists.)údebug-stacktracesÚBz2Report Deferred creation and callback stack traces)ÚnopmNzFdon't automatically jump into debugger for postmorteming of exceptions)údry-runÚnzdo everything but run the tests)ÚprofileNz#Run tests under the Python profiler)úuntil-failureÚuzRepeat test until it fails)ÚdebuggerNÚpdbzBthe fully qualified name of a debugger to use if --debug is passed)ri   Úlztest.logrg   )ÚjobsÚjNzNumber of local workers to run)r   r   r   )r¢   r   )rk   )rž   r_   r}   )r§   Úreactorr°   Nc                 C   sH   zt |ƒ}W n ty   t d| ¡‚w |dkrt d¡‚|| d< dS )zN
        Number of local workers to run, a strictly positive integer.
        z,Expecting integer argument to jobs, got '%s'r   z4Argument to jobs must be a strictly positive integerrÙ   N)r©   r   r   rT   )rr   Únumberr%   r%   r&   Úopt_jobsá  s   ÿÿÿzOptions.opt_jobsc                 C   sp   g }| j D ]}|  |¡dur| | r| d|› �¡ q| jD ]}|  |¡dur5| d|› �t| | ƒg¡ q|S )zJ
        Return a list of options to pass to distributed workers.
        Nz--)Ú_workerFlagsr/   rŽ   Ú_workerParametersr�   Ústr)rr   r¸   r®   r%   r%   r&   Ú_getWorkerArgumentsñ  s   
€
€zOptions._getWorkerArgumentsc                 C   sb   t  | ¡ | d rg d¢}|D ]}| | rt d| ¡‚q| d r/| d s*t d¡‚dt_d S d S )NrÙ   )rÌ   rÓ   rÎ   r`   z(You can't specify --%s when using --jobsrÐ   rÌ   z+You must specify --debug when using --nopm F)rW   r¾   r   rT   r   ÚDO_POST_MORTEM)rr   Ú	conflictsr®   r%   r%   r&   r¾   ÿ  s   
ÿÿ

ýzOptions.postOptions)r¿   rÀ   rÁ   rÂ   rÄ   rÅ   r   rÆ   r=   r@   rÉ   rÞ   rß   r   rÊ   rË   Úextrar†   rÝ   rá   r¾   r%   r%   r%   r&   ro   £  s4    íøþÿro   c                 C   s2   | d rt  ¡  | d s| d rt d¡ d S d S )NrÌ   rÎ   T)r   ÚstartDebugModer   ÚsetDebugging)Úconfigr%   r%   r&   Ú_initialDebugSetup  s
   ÿrè   c                 C   s$   t | ƒ}| d  }|j| d |d�S )NrZ   rn   )Úrecurse)Ú
_getLoaderÚloadByNames)rç   Úloaderré   r%   r%   r&   Ú	_getSuite  s   
rí   c                    sx   t  ¡ }| d r#t ¡ ‰ ˆ  | d ¡ ‡ fdd„|_td| d  ƒ n| d r2t| d  \}}||_| d s:t j|_	|S )Nrc   c                    s   ˆ   ¡ S rO   )rc   )ra   ©Úrandomerr%   r&   Ú<lambda>!  s    z_getLoader.<locals>.<lambda>z$Running tests shuffled with seed %d
rQ   rÔ   )
r   Ú
TestLoaderrc   ÚRandomÚseedÚsorterr€   rS   ÚDestructiveTestSuiteÚsuiteFactory)rç   rì   r›   rô   r%   rî   r&   rê     s   rê   c               	   C   sž   t  ¡ } ztdƒ W n ty   tdƒ Y nw dD ]2}tj |¡rLzt|ƒ}W n	 t	y1   Y qw |� | j
 | ¡ ¡ W d  ƒ n1 sGw   Y  q| S )zR
    Wrap an instance of C{pdb.Pdb} with readline support and load any .rcs.

    r)   zreadline module not available)z.pdbrcÚpdbrcN)r×   ÚPdbr	   ÚImportErrorr€   r3   r4   Úexistsr(   rG   ÚrcLinesr�   Ú	readlines)Údbgr4   ÚrcFiler%   r%   r&   Ú_wrappedPdb+  s$   ÿÿÿ€rÿ   c                   @   s   e Zd ZdZdS )Ú_DebuggerNotFoundzk
    A debugger import failed.

    Used to allow translating these errors into usage error messages.

    N)r¿   rÀ   rÁ   rÂ   r%   r%   r%   r&   r   B  s    r   c                 C   s
  t j}| d | d | d | d | d | d dœ}| d r#t jj|d	< n[| d
 r<ddlm} |}| d
 |d< |  ¡ |d< nB| d rlt jj|d	< | d }|dkrgz	t |¡|d< W n tj	yf   t
|›d�ƒ‚w tƒ |d< | d |d< | d |d< | d |d< |di |¤ŽS )zå
    Return a trial runner class set up with the parameters extracted from
    C{config}.

    @return: A trial runner instance.
    @rtype: L{runner.TrialRunner} or C{DistTrialRunner} depending on the
        configuration.
    r   r¢   r\   r^   ri   re   )ÚreporterFactoryÚtracebackFormatÚrealTimeErrorsÚuncleanWarningsri   ÚworkingDirectoryrÑ   ÚmoderÙ   r   )ÚDistTrialRunnerÚworkerNumberÚworkerArgumentsrÌ   rÖ   r×   z debugger could not be found.r`   Ú	exitFirstrÓ   r_   ÚforceGarbageCollectionNr%   )r   ÚTrialRunnerÚDRY_RUNÚtwisted.trial._dist.disttrialr  rá   ÚDEBUGr   r»   ÚModuleNotFoundr   rÿ   )rç   Úclsr¸   r  rÖ   r%   r%   r&   Ú_makeRunnerK  s>   	úÿÿ
r  c               
   C   s  t tjƒdkrtj d¡ tƒ } z|  ¡  W n tjy/ } zttjd › d|› �ƒ‚d }~ww t	| ƒ zt
| ƒ}W n tyT } zttjd › dt|ƒ› �ƒ‚d }~ww t| ƒ}| d rc| |¡}n| |¡}| jr€t d ¡ | j ¡ }|jdd|  ¡ jd� t | ¡  ¡ d S )Nr   z--helpr   z: rÔ   F)Úshow_missingÚsummaryr�   )r   ru   rv   rŽ   ro   ÚparseOptionsr   ÚerrorÚ
SystemExitrè   r  r   rà   rí   ÚrunUntilFailureÚrunr†   r‡   ÚresultsÚwrite_resultsr�   r4   r—   ÚwasSuccessful)rç   ÚueÚtrialRunnerr]   ÚsuiteÚtest_resultr  r%   r%   r&   r  {  s6   €ÿ€ÿ


ÿr  )/rŸ   rE   r3   r×   rc   ru   r­   r³   Útwistedr   Útwisted.applicationr   Útwisted.internetr   Útwisted.pythonr   r   r   Útwisted.python.filepathr   Útwisted.python.reflectr	   Útwisted.trialr
   r   r   r£   r'   r-   r1   r8   r@   rL   r™   rS   rV   rW   ro   ÚReactorSelectionMixinrè   rí   rê   rÿ   Ú	Exceptionr   r  r  r%   r%   r%   r&   Ú<module>   s^   ú
	!þþû  k	0