U
    `[:                     @   s  d Z ddlmZmZ ddddddd	d
dddddddg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mZmZmZmZ ddlmZmZ ddlmZ ddlmZmZ ddlmZ ddlmZmZ ddlm Z m!Z! ddl"m#Z# ddl$m%Z% ddl&m'Z' e(dZ)dd Z*dd Z+d d Z,d!d Z-d6d"d#Z.d$d% Z/d&d' Z0G d(d de%Z1d)Z2G d*d de%Z3G d+d
 d
e%Z4d,d Z5d-d Z6e'eG d.d de7Z8G d/d de8Z9G d0d de7Z:G d1d2 d2e:Z;d3d4 Z<er[:e;Z:G d5d	 d	e7Z=dS )7zK
A miscellany of code used to run Trial tests.

Maintainer: Jonathan Lange
    )absolute_importdivision	TestSuiteDestructiveTestSuiteErrorHolderLoggedSuite
TestHolder
TestLoaderTrialRunner
TrialSuitefilenameToModule	isPackageisPackageDirectory
isTestCasenamesamefileNOT_IN_TESTN)reflectlogfailuremodulesfilepath)_PY3	_PY35PLUS)defer)utilunittest)	ITestCase)_ExitWrapperUncleanWarningsReporterWrapper) _ForceGarbageCollectionDecorator_iterateTests)_logObserver)r   )implementerr   c                 C   s2   t | tjsdS tjtj| jd }|dkS )z>Given an object return True if the object looks like a packageFr   __init__)
isinstancetypes
ModuleTypeospathsplitextbasename__file__)moduler+    r.   6/usr/lib/python3/dist-packages/twisted/trial/runner.pyr   2   s    c                 C   s>   dd }| D ]*}d| }t jt j| |r|  S qdS )z
    Is the directory at path 'dirname' a Python package directory?
    Returns the name of the __init__ file (it may have a weird extension)
    if dirname is a package directory.  Otherwise, returns False
    c                  S   s6   t rdd l} | j S dd l}tt|  d S d S Nr   )r   	importlib	machineryall_suffixesimplistzipZget_suffixes)r1   r4   r.   r.   r/   _getSuffixes@   s
    
z(isPackageDirectory.<locals>._getSuffixesr$   F)r(   r)   existsjoin)dirnamer7   ZextinitFiler.   r.   r/   r   :   s    	

c                 C   s   t j| t j|kS )z
    A hacky implementation of C{os.path.samefile}. Used by L{filenameToModule}
    when the platform doesn't provide C{os.path.samefile}. Do not use this.
    )r(   r)   abspath)Z	filename1	filename2r.   r.   r/   r   P   s    c              	   C   s   t j| std| f ztt| }W n  ttfk
rN   t|  Y S X t	|dddkrht| S t j
|jd d }t	t jdt}t j| r|| |stj|j= t| }|S )aV  
    Given a filename, do whatever possible to return a module object matching
    that file.

    If the file in question is a module in Python path, properly import and
    return that module. Otherwise, load the source manually.

    @param fn: A filename.
    @return: A module object.
    @raise ValueError: If C{fn} does not exist.
    z%r doesn't existr,   Nr   z.pyr   )r(   r)   r8   
ValueErrorr   namedAnyfilenameToModuleNameAttributeError_importFromFilegetattrr*   r,   r   isfilesysr   __name__)fnZretZretFileZsamer.   r.   r/   r   X   s    
c              	   C   s   t | } |s(tjtj| d d }|tjkr<tj| S trdd l}|j	
|| }|sbt| |j	|}|j| |tj|< n,dd l}t| d}||| |}W 5 Q R X |S )Nr   r)_resolveDirectoryr(   r)   r*   splitrE   r   r   r1   r   spec_from_file_locationSyntaxErrormodule_from_specloaderexec_moduler4   openZload_source)rG   Z
moduleNamer1   specr-   r4   fdr.   r.   r/   rB   {   s"    

rB   c                 C   s:   t j| r6t| }|r(t j| |} ntd| f | S )Nz%r is not a package directory)r(   r)   isdirr   r9   r>   )rG   r;   r.   r.   r/   rJ      s    rJ   c                 C   sH   t | j| jt | krBt| jD ] }t | j|t | kr |  S q | jS )z
    Find the attribute name on the method's class which refers to the method.

    For some methods, notably decorators which have not had __name__ set correctly:

    getattr(method.im_class, method.__name__) != method
    )rC   im_classrF   objectdir)methodaliasr.   r.   r/   _getMethodNameInClass   s
    
rZ   c                   @   s   e Zd ZdZdd ZdS )r   zQ
    A test suite which remove the tests once run, to minimize memory usage.
    c                 C   s(   | j r$|jrq$| j d}|| q |S )zn
        Almost the same as L{TestSuite.run}, but with C{self._tests} being
        empty at the end.
        r   )_testsZ
shouldStoppop)selfresulttestr.   r.   r/   run   s    
zDestructiveTestSuite.runN)rF   
__module____qualname____doc__r`   r.   r.   r.   r/   r      s   z<not in test>c                       s    e Zd ZdZ fddZ  ZS )r   z[
    Any errors logged in this suite will be reported to the L{TestResult}
    object.
    c                    sN   t }|  tt| | |  | D ]}|tt	| q,|
  dS )z
        Run the suite, storing all errors in C{result}. If an error is logged
        while no tests are running, then it will be added as an error to
        C{result}.

        @param result: A L{TestResult} object.
        N)r"   Z_addsuperr   r`   _removeZ	getErrorsaddErrorr   r   ZflushErrors)r]   r^   Zobservererror	__class__r.   r/   r`      s    zLoggedSuite.run)rF   ra   rb   rc   r`   __classcell__r.   r.   rh   r/   r      s   c                       s2   e Zd ZdZd
 fdd	Zdd Zdd	 Z  ZS )r   z
    Suite to wrap around every single test in a C{trial} run. Used internally
    by Trial to set up things necessary for Trial tests to work, regardless of
    what context they are run in.
    r.   Fc                    sJ   |r,g }|D ]}t |t}|| q|}t|}tt| |g d S N)r   decorater    appendr   rd   r   r$   )r]   testsforceGarbageCollectionZnewTestsr_   suiterh   r.   r/   r$      s     zTrialSuite.__init__c                    sH   ddl m} t  |dd fdd |d td  d S )Nr   )reactorZafterZshutdownc                      s
     d S rk   )callbackr.   dr.   r/   <lambda>       z"TrialSuite._bail.<locals>.<lambda>Zmktemp)	twisted.internetrq   r   ZDeferredZaddSystemEventTriggerZfireSystemEventr   TestCaseZ_wait)r]   rq   r.   rs   r/   _bail   s    

zTrialSuite._bailc                 C   s    zt| | W 5 |    X d S rk   )ry   r   r`   r]   r^   r.   r.   r/   r`      s    zTrialSuite.run)r.   F)rF   ra   rb   rc   r$   ry   r`   rj   r.   r.   rh   r/   r      s   c                 C   s@   t | rt| }n(z|  }W n tk
r:   | j}Y nX |S )z
    @param thing: an object from modules (instance of PythonModule,
        PythonAttribute), a TestCase subclass, or an instance of a TestCase.
    )r   r   qualidrA   r   )thingZtheNamer.   r.   r/   r     s    c                 C   s*   zt | tjW S  tk
r$   Y dS X dS )z
    @return: C{True} if C{obj} is a class that contains test cases, C{False}
        otherwise. Used to find all the tests in a module.
    FN)
issubclasspyunitrx   	TypeError)objr.   r.   r/   r     s    c                   @   sD   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dS )r   z
    Placeholder for a L{TestCase} inside a reporter. As far as a L{TestResult}
    is concerned, this looks exactly like a unit test.
    Nc                 C   s
   || _ dS )zM
        @param description: A string to be displayed L{TestResult}.
        Ndescription)r]   r   r.   r.   r/   r$   *  s    zTestHolder.__init__c                 C   s
   |  |S rk   r`   rz   r.   r.   r/   __call__1  s    zTestHolder.__call__c                 C   s   | j S rk   r   r]   r.   r.   r/   r|   5  s    zTestHolder.idc                 C   s   dS r0   r.   r   r.   r.   r/   countTestCases9  s    zTestHolder.countTestCasesc                 C   s"   | |  ||  ||  dS )z
        This test is just a placeholder. Run the test successfully.

        @param result: The C{TestResult} to store the results in.
        @type result: L{twisted.trial.itrial.IReporter}.
        N)	startTest
addSuccessstopTestrz   r.   r.   r/   r`   =  s    

zTestHolder.runc                 C   s   | j S rk   r   r   r.   r.   r/   shortDescriptionI  s    zTestHolder.shortDescription)rF   ra   rb   rc   ZfailureExceptionr$   r   r|   r   r`   r   r.   r.   r.   r/   r   !  s   c                       s0   e Zd ZdZ fddZdd Zdd Z  ZS )r   a  
    Used to insert arbitrary errors into a test suite run. Provides enough
    methods to look like a C{TestCase}, however, when it is run, it simply adds
    an error to the C{TestResult}. The most common use-case is for when a
    module fails to import.
    c                    s    t t| | t|| _dS )a+  
        @param description: A string used by C{TestResult}s to identify this
        error. Generally, this is the name of a module that failed to import.

        @param error: The error to be added to the result. Can be an `exc_info`
        tuple or a L{twisted.python.failure.Failure}.
        N)rd   r   r$   r   ZexcInfoOrFailureToExcInforg   )r]   r   rg   rh   r.   r/   r$   V  s    zErrorHolder.__init__c                 C   s   d| j | jd f S )Nz%<ErrorHolder description=%r error=%r>   )r   rg   r   r.   r.   r/   __repr__b  s     zErrorHolder.__repr__c                 C   s&   | |  || | j ||  dS )z
        Run the test, reporting the error.

        @param result: The C{TestResult} to store the results in.
        @type result: L{twisted.trial.itrial.IReporter}.
        N)r   rf   rg   r   rz   r.   r.   r/   r`   g  s    
zErrorHolder.run)rF   ra   rb   rc   r$   r   r`   rj   r.   r.   rh   r/   r   N  s   c                   @   s   e Zd ZdZdZdZdd Zdd Zdd	 Zd
d Z	dd Z
e
Zdd ZeZdd Zdd Zdd Zd$ddZdd Zd%ddZd&ddZeZd'd d!Zd"d# ZdS )(r	   a4  
    I find tests inside function, modules, files -- whatever -- then return
    them wrapped inside a Test (either a L{TestSuite} or a L{TestCase}).

    @ivar methodPrefix: A string prefix. C{TestLoader} will assume that all the
    methods in a class that begin with C{methodPrefix} are test cases.

    @ivar modulePrefix: A string prefix. Every module in a package that begins
    with C{modulePrefix} is considered a module full of tests.

    @ivar forceGarbageCollection: A flag applied to each C{TestCase} loaded.
    See L{unittest.TestCase} for more information.

    @ivar sorter: A key function used to sort C{TestCase}s, test classes,
    modules and packages.

    @ivar suiteFactory: A callable which is passed a list of tests (which
    themselves may be suites of tests). Must return a test suite.
    r_   Ztest_c                 C   s   t | _t| _g | _d S rk   )r   suiteFactoryr   sorterZ_importErrorsr   r.   r.   r/   r$     s    zTestLoader.__init__c                 C   s   t || jdS )zt
        Sort the given things using L{sorter}.

        @param xs: A list of test cases, class or modules.
        )key)sortedr   )r]   Zxsr.   r.   r/   sort  s    zTestLoader.sortc                 C   s4   g }t |D ]\}}t|r|| q| |S )z-Given a module, return all Trial test classes)inspectZ
getmembersr   rm   r   )r]   r-   classesr   valr.   r.   r/   findTestClasses  s
    zTestLoader.findTestClassesc                 C   s   t j|rt|S t|S )aB  
        Return a Python object given a string describing it.

        @param name: a string which may be either a filename or a
        fully-qualified Python name.

        @return: If C{name} is a filename, return the module. If C{name} is a
        fully-qualified Python name, return the object it refers to.
        )r(   r)   r8   r   r   r?   )r]   r   r.   r.   r/   
findByName  s    
zTestLoader.findByNamec                 C   s   t |tjstd|f t|dr,| S t|dr>| S |  }| |D ]}|	| 
| qPt|dst|S |  }|jD ]}|	| | q| ||gS )a  
        Return a test suite with all the tests from a module.

        Included are TestCase subclasses and doctests listed in the module's
        __doctests__ module. If that's not good for you, put a function named
        either C{testSuite} or C{test_suite} in your module that returns a
        TestSuite, and I'll use the results of that instead.

        If C{testSuite} and C{test_suite} are both present, then I'll use
        C{testSuite}.
        z%r is not a module	testSuite
test_suite__doctests__)r%   r&   r'   r   hasattrr   r   r   r   addTest	loadClassr   loadDoctests)r]   r-   rp   Z	testClassZdocSuiteZdocTestr.   r.   r/   
loadModule  s    



zTestLoader.loadModulec                    sh   t  ts$t  tjs$td f t s:td f  } fdd|D }	|S )zq
        Given a class which contains test cases, return a sorted list of
        C{TestCase} instances.
        %r is not a class%r is not a test casec                    s   g | ]}  j| qS r.   	_makeCasemethodPrefix.0r   klassr]   r.   r/   
<listcomp>  s   z(TestLoader.loadClass.<locals>.<listcomp>)
r%   typer&   	ClassTyper   r   r>   getTestCaseNamesr   r   r]   r   namesrn   r.   r   r/   r     s    
zTestLoader.loadClassc                 C   s   t || jS )z
        Given a class that contains C{TestCase}s, return a list of names of
        methods that probably contain tests.
        )r   ZprefixedMethodNamesr   )r]   r   r.   r.   r/   r     s    zTestLoader.getTestCaseNamesc                 C   s,   t |tjstd|f | |jt|S )z~
        Given a method of a C{TestCase} that represents a test, return a
        C{TestCase} instance for that test.
        z%r not a method)r%   r&   
MethodTyper   r   rU   rZ   r]   rX   r.   r.   r/   
loadMethod  s    zTestLoader.loadMethodc                 C   s   ||S rk   r.   )r]   r   Z
methodNamer.   r.   r/   r     s    zTestLoader._makeCaseFc                 C   s   t |std|f t|j}|r0| }n| }g }|D ]&}|jdd 	| j
r@|| q@|  }| |D ]B}z| }	W n   t|jt }
Y nX | |	}
||
 qz|S )a  
        Load tests from a module object representing a package, and return a
        TestSuite containing those tests.

        Tests are only loaded from modules whose name begins with 'test_'
        (or whatever C{modulePrefix} is set to).

        @param package: a types.ModuleType object (or reasonable facsimile
        obtained by importing) which may contain tests.

        @param recurse: A boolean.  If True, inspect modules within packages
        within the given package (and so on), otherwise, only inspect modules
        in the package itself.

        @raise: TypeError if 'package' is not a package.

        @return: a TestSuite created with my suiteFactory, containing all the
        tests.
        z%r is not a package.rH   )r   r   r   Z	getModulerF   ZwalkModulesZiterModulesr   rK   
startswithmodulePrefixrm   r   r   loadr   r   Failurer   r   )r]   packagerecurseZpkgobjZ	discoveryZ
discoveredZdiscorp   Zmodinfor-   Z
thingToAddr.   r.   r/   loadPackage  s&    

zTestLoader.loadPackagec                 C   sx   t |tr4zt|}W n   t|t  Y S X t|sLt	
d dS i }tjdkrjdd }||d< tj|f|S )z
        Return a suite of tests for all the doctests defined in C{module}.

        @param module: A module object or a module name.
        z&trial only supports doctesting modulesN)      c                 S   s    t | d| j| _| j | _dS )z
                Save C{test.globs} and replace it with a copy so that if
                necessary, the original will be available for the next test
                run.
                _savedGlobalsN)rC   Zglobsr   copy)r_   r.   r.   r/   saveGlobals,  s    z,TestLoader.loadDoctests.<locals>.saveGlobalsZsetUp)r%   strr   r?   r   r   r   r   ZismodulewarningswarnrE   version_infodoctestZDocTestSuite)r]   r-   Z	extraArgsr   r.   r.   r/   r     s    



zTestLoader.loadDoctestsNc                 C   s|   t |tjr*t|r | ||S | |S t |tjr@| |S t |trT| |S t |tj	rj| 
|S td|f dS )a  
        Given a Python object, return whatever tests that are in it. Whatever
        'in' might mean.

        @param thing: A Python object. A module, method, class or package.
        @param recurse: Whether or not to look in subpackages of packages.
        Defaults to False.

        @param parent: For compatibility with the Python 3 loader, does
            nothing.
        @param qualname: For compatibility with the Python 3 loader, does
            nothing.

        @return: A C{TestCase} or C{TestSuite}.
        z#No loader for %r. Unrecognized typeN)r%   r&   r'   r   r   r   r   r   r   r   r   r   )r]   r}   r   parentqualNamer.   r.   r/   loadAnything7  s    




zTestLoader.loadAnythingc                 C   s6   z|  |}W n   t|t  Y S X | ||S )a`  
        Given a string representing a Python object, return whatever tests
        are in that object.

        If C{name} is somehow inaccessible (e.g. the module can't be imported,
        there is no Python object with that name etc) then return an
        L{ErrorHolder}.

        @param name: The fully-qualified name of a Python object.
        )r   r   r   r   r   )r]   r   r   r}   r.   r.   r/   
loadByNameS  s
    zTestLoader.loadByNamec              
      sv   g }g }|D ]:}z| | W q   | t|t  Y qX q fdd|D }|| |S )a  
        Construct a TestSuite containing all the tests found in 'names', where
        names is a list of fully qualified python names and/or filenames. The
        suite returned will have no duplicate tests, even if the same object
        is named twice.
        c                    s   g | ]} | qS r.   )r   )r   r}   r   r]   r.   r/   r   t  s   z*TestLoader.loadByNames.<locals>.<listcomp>)rm   r   r   r   r   _uniqueTestsextendr   )r]   r   r   thingserrorsr   Zsuitesr.   r   r/   loadByNamesf  s    
zTestLoader.loadByNamesc                 c   sN   t  }|D ]>}t|tjr&||jf}n|f}||kr
|d V  || q
dS )  
        Gather unique suite objects from loaded things. This will guarantee
        uniqueness of inherited methods on TestCases which would otherwise hash
        to same value and collapse to one test unexpectedly if using simpler
        means: e.g. set().
        r   N)setr%   r&   r   rU   add)r]   r   seenr}   r.   r.   r/   r   z  s    
zTestLoader._uniqueTests)F)FNN)F)F)rF   ra   rb   rc   r   r   r$   r   r   r   r   loadTestsFromModuler   loadTestsFromTestCaser   r   r   r   r   r   r   ZloadTestsFromNamer   r   r.   r.   r.   r/   r	   t  s*   	
*


c                   @   sZ   e Zd Z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
dd ZdS )Py3TestLoaderz
    A test loader finds tests from the functions, modules, and files that is
    asked to and loads them into a L{TestSuite} or L{TestCase}.

    See L{TestLoader} for further details.
    Fc                 C   s\   ddl m} t|}z||| }| j||dW S  tk
rV   td|Y nX dS )a1  
        Load a file, and then the tests in that file.

        @param fileName: The file name to load.
        @param recurse: A boolean. If True, inspect modules within packages
            within the given package (and so on), otherwise, only inspect
            modules in the package itself.
        r   )SourceFileLoaderr   z{} is not a Python file.N)	importlib.machineryr   r   r@   load_moduler   OSErrorr>   format)r]   ZfileNamer   r   r   r-   r.   r.   r/   loadFile  s    	
zPy3TestLoader.loadFilec           
   	   C   s8  t j|krFt|}zt| W qJ tk
rB   | j||d Y S X n|}d } }}t|D ]N\}}zt|}|}W  qW q^ tk
r   |dkrt	d
|Y q^X q^|dkrt|}|dtd|jd d }z|D ]}	|t||	 }}qW n$ tk
r$   td
|Y nX | j||||dS )	a  
        Find and load tests, given C{name}.

        This partially duplicates the logic in C{unittest.loader.TestLoader}.

        @param name: The qualified name of the thing to load.
        @param recurse: A boolean. If True, inspect modules within packages
            within the given package (and so on), otherwise, only inspect
            modules in the package itself.
        r   N zThe module {} does not exist.r   r   z{} does not exist.)r   r   r   )r(   sepr   r@   
__import__ImportErrorr   _qualNameWalkerZnamedModuleZModuleNotFoundr   r?   rK   lenrF   rC   rA   r   )
r]   _namer   r   r   r   Z	remainingZ
searchNameZremainingNamepartr.   r.   r/   r     s6    




"
zPy3TestLoader.findByNameNc                 C   s   t |tjr,t|r"| j||dS | |S t |trLt|tj	rL| 
|S t |tjrt |trt|tj	r|d }||}t||jj|kst|S t |tr|S td|f dS )a  
        Load absolutely anything (as long as that anything is a module,
        package, class, or method (with associated parent class and qualname).

        @param obj: The object to load.
        @param recurse: A boolean. If True, inspect modules within packages
            within the given package (and so on), otherwise, only inspect
            modules in the package itself.
        @param parent: If C{obj} is a method, this is the parent class of the
            method. C{qualName} is also required.
        @param qualName: If C{obj} is a method, this a list containing is the
            qualified name of the method. C{parent} is also required.
        r   rH   z$don't know how to make test from: %sN)r%   r&   r'   r   r   r   r   r~   r   rx   r   FunctionTyperC   Z_testMethodName__func__AssertionErrorr   r   )r]   r   r   r   r   r   instr.   r.   r/   r     s$    



zPy3TestLoader.loadAnythingc              	   C   s@   z|  | j||dgW S    |  t|t g Y S X dS )a*  
        Load some tests by name.

        @param name: The qualified name for the test to load.
        @param recurse: A boolean. If True, inspect modules within packages
            within the given package (and so on), otherwise, only inspect
            modules in the package itself.
        r   N)r   r   r   r   r   )r]   r   r   r.   r.   r/   r     s    	zPy3TestLoader.loadByNamec              
   C   sf   g }g }|D ]>}z| | j||d W q   | t|t  Y qX q|| | | |S )a+  
        Load some tests by a list of names.

        @param names: A L{list} of qualified names.
        @param recurse: A boolean. If True, inspect modules within packages
            within the given package (and so on), otherwise, only inspect
            modules in the package itself.
        r   )rm   r   r   r   r   r   r   r   )r]   r   r   r   r   r   r.   r.   r/   r   )  s    	
zPy3TestLoader.loadByNamesc                    s\   t  tstd f t s.td f  } fdd|D }|S )z
        Given a class which contains test cases, return a list of L{TestCase}s.

        @param klass: The class to load tests from.
        r   r   c                    s   g | ]}  j| qS r.   r   r   r   r.   r/   r   H  s   z+Py3TestLoader.loadClass.<locals>.<listcomp>)r%   r   r   r   r>   r   r   r   r   r.   r   r/   r   =  s    

zPy3TestLoader.loadClassc                 C   s   t dd S )NzCan't happen on Py3)NotImplementedErrorr   r.   r.   r/   r   M  s    zPy3TestLoader.loadMethodc                 c   sD   t  }|D ]4}|j}|D ]$}t||kr|V  |t| qq
dS )r   N)r   r[   r   r   )r]   r   r   Z	testthingZ
testthingsr}   r.   r.   r/   r   Q  s    zPy3TestLoader._uniqueTests)F)F)FNN)F)F)rF   ra   rb   rc   r   r   r   r   r   r   r   r   r.   r.   r.   r/   r     s   

D
-

r   c                 c   sP   | g fV  |  d}tdt|D ](}d|d|  || d fV  q"dS )a3  
    Given a Python qualified name, this function yields a 2-tuple of the most
    specific qualified name first, followed by the next-most-specific qualified
    name, and so on, paired with the remainder of the qualified name.

    @param qualName: A Python qualified name.
    @type qualName: L{str}
    r   r   N)rK   ranger   r9   )r   Z	qualPartsindexr.   r.   r/   r   b  s    


r   c                   @   s   e Zd ZdZdZdZdd Zdd ZeZ	dd	 Z
d
dejddddd
dd
dfddZdd Zdd Zdd ZdddZdd Zd
S )r
   z=
    A specialised runner that the trial front end uses.
    debugzdry-runc                 C   s<   |    t }t| j}t|\}| _t	|j
 |S rk   )_tearDownLogFiler(   getcwdr   ZFilePathworkingDirectoryr   Z_unusedTestDirectory_testDirLockchdirr)   )r]   Z
currentDirbaseZtestdirr.   r.   r/   _setUpTestdir  s    zTrialRunner._setUpTestdirc                 C   s   t | | j  d S rk   )r(   r   r   Zunlock)r]   oldDirr.   r.   r/   _tearDownTestdir  s    
zTrialRunner._tearDownTestdirc                 C   s8   |  | j| j| j| j}| jr&t|}| jr4t|}|S rk   )	reporterFactorystreamtbformatrterrors_log
_exitFirstr   uncleanWarningsr   )r]   Zreporterr.   r.   r/   _makeResult  s     zTrialRunner._makeResultNztest.logFdefaultc                 C   sp   || _ || _|| _|| _|| _|| _|| _d | _|	p6d| _d | _	d | _
|
| _|| _|| _|rlt| jd| _d S )NZ_trial_tempzprofile.data)r   logfilemoder   r   r   r   Z_resultr   _logFileObserver_logFileObject_forceGarbageCollectiondebuggerr   r   Zprofiledr`   )r]   r   r   r   r   ZprofileZtracebackFormatZrealTimeErrorsr   r   ro   r  Z	exitFirstr.   r.   r/   r$     s     
zTrialRunner.__init__c                 C   s<   | j d k	rt| j j d | _ | jd k	r8| j  d | _d S rk   )r   r   ZremoveObserveremitr  closer   r.   r.   r/   r     s    


zTrialRunner._tearDownLogFilec                 C   sL   |    | jdkrtj}nt| jd}|| _t|| _t	| jj
d d S )N-ar   )r   r   rE   stdoutrQ   r  r   ZFileLogObserverr   ZstartLoggingWithObserverr  )r]   ZlogFiler.   r.   r/   _setUpLogFile  s    
zTrialRunner._setUpLogFilec                 C   s   t |t}| || jS )zC
        Run the test or suite and return a result object.
        )r   rl   r   _runWithoutDecorationr  )r]   r_   r.   r.   r/   r`     s    zTrialRunner.runc           	   	      s4     t|g|t }jjkrVtD ]"} |  |  | q0n\jj	krt fdd}n fdd}
 }z  |  W 5   | X t }t dd}|dkr(tjdt j tdd      j  d	 j||   d
    n    S )zR
        Private helper that runs the given test but doesn't decorate it.
        c                      s   j j S rk   )r  Zruncallr`   r.   r^   r]   rp   r.   r/   ru     rv   z3TrialRunner._runWithoutDecoration.<locals>.<lambda>c                      s
     S rk   r   r.   )r^   rp   r.   r/   ru     rv   doneNzR%s should implement done() but doesn't. Falling back to printErrors() and friends.   )category
stacklevelzRan %d tests in %.3fs
)r   r   timer   DRY_RUNr!   r   r   r   DEBUGr   r   r   r	  rC   r   r   r   r{   ri   DeprecationWarningZprintErrorsZwritelnZ	separatortestsRunwriteZprintSummaryr  )	r]   r_   ro   Z	startTimeZsingler`   r   ZendTimer  r.   r  r/   r
    sH    




 


z!TrialRunner._runWithoutDecorationc                 C   sX   d}|d7 }| j d|f  |dkr2| |}n
| |}|jdkrHqT| sqTq|S )z8
        Repeatedly run C{test} until it fails.
        r   r   zTest Pass %d
)r   r  r`   r
  r  ZwasSuccessful)r]   r_   countr^   r.   r.   r/   runUntilFailure  s    

zTrialRunner.runUntilFailure)F)rF   ra   rb   rc   r  r  r   r   r   r   r   rE   r  r$   r   r	  r`   r
  r  r.   r.   r.   r/   r
   ~  s0   	


/)N)>rc   Z
__future__r   r   __all__r   r   r(   rE   r  r&   r   Ztwisted.pythonr   r   r   r   r   Ztwisted.python.compatr   r   rw   r   Ztwisted.trialr   r   Ztwisted.trial.itrialr   Ztwisted.trial.reporterr   r   Ztwisted.trial._asyncrunnerr    r!   Ztwisted.trial._synctestr"   Ztwisted.trial.unittestr   Zzope.interfacer#   r   r   r   r   r   r   rB   rJ   rZ   r   r   r   r   r   r   rV   r   r   r	   r   r   r
   r.   r.   r.   r/   <module>   sz             
#

&,&   U