o
    br                     @   sz  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 d dlmZ d dlmZ d dlmZ d d	lmZmZmZmZ d d
lmZ edZdd ZG dd dejZG dd dejZG dd dejZ G dd dejZ!G dd dejZ"G dd dej#Z$G dd dej#Z%G dd dej#Z&G dd dej#Z'G d d! d!ej#Z(G d"d# d#ej#Z)dS )$    N)StringIO)util)FilePath)
UsageError)trial)unittest)DistTrialRunner)DestructiveTestSuite
TestLoader	TestSuiteTrialRunner)	testNamesr   c                 C   s   t t| S )z1
    For finding files in twisted/trial/test
    )r   sibpath__file__)filename r   @/usr/lib/python3/dist-packages/twisted/trial/test/test_script.pyr      s   r   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 Z	dS )ForceGarbageCollectionTestsz*
    Tests for the --force-gc option.
    c                 C   s>   t  | _g | _| td| j t| j	}t
||g| _d S )Ncollect)r   Optionsconfiglogpatchgcr   pyunitFunctionTestCase
simpleTestr   test)selfr   r   r   r   setUp)   s
   
z!ForceGarbageCollectionTests.setUpc                 C      | j d dS )zD
        A simple test method that records that it was run.
        r   Nr   appendr   r   r   r   r   0      z&ForceGarbageCollectionTests.simpleTestc                 C   r    )zI
        A replacement for gc.collect that logs calls to itself.
        r   Nr!   r#   r   r   r   r   6   r$   z#ForceGarbageCollectionTests.collectc                 C   s   t | j}t |_|S )zN
        Return a L{TrialRunner} object that is safe to use in tests.
        )r   _makeRunnerr   r   streamr   runnerr   r   r   
makeRunner<   s   z&ForceGarbageCollectionTests.makeRunnerc                 C   s>   d| j d< | j   |  }|| j | | jg d dS )z
        Passing the --force-gc option to the trial script forces the garbage
        collector to run before and after each test.
        Tzforce-gc)r   r   r   r   r   r   Nr   postOptionsr)   runr   assertEqualr   r'   r   r   r   test_forceGcD   s   


z(ForceGarbageCollectionTests.test_forceGcc                 C   s4   | j   |  }|| j | | jddg dS )z>
        By default, no garbage collection is forced.
        r   Nr*   r'   r   r   r   test_unforceGcQ   s   
z*ForceGarbageCollectionTests.test_unforceGcN)
__name__
__module____qualname____doc__r   r   r   r)   r.   r/   r   r   r   r   r   $   s    r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	SuiteUsedTestsz?
    Check the category of tests suite used by the loader.
    c                 C      t  | _dS )z6
        Create a trial configuration object.
        Nr   r   r   r#   r   r   r   r   `      zSuiteUsedTests.setUpc                 C   s   t | j}| |jt dS )zK
        By default, the loader should use L{DestructiveTestSuite}
        N)r   
_getLoaderr   r-   suiteFactoryr	   r   loaderr   r   r   test_defaultSuitef   s   z SuiteUsedTests.test_defaultSuitec                 C   s(   d| j d< t| j }| |jt dS )zw
        The C{until-failure} configuration uses the L{TestSuite} to keep
        instances alive across runs.
        Tzuntil-failureN)r   r   r8   r-   r9   r   r:   r   r   r   test_untilFailureSuitem   s   
z%SuiteUsedTests.test_untilFailureSuiteN)r0   r1   r2   r3   r   r<   r=   r   r   r   r   r4   [   s
    r4   c                   @   s   e 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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d)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5S )6TestModuleTestsc                 C      t  | _d S Nr6   r#   r   r   r   r   x      zTestModuleTests.setUpc                 C   s
   d | _ d S r@   )r   r#   r   r   r   tearDown{      
zTestModuleTests.tearDownc                 C   s   |  t| |  g dS )zq
        Check that the testNames helper method accurately collects the
        names of tests in suite.
        N)r-   r   idr#   r   r   r   test_testNames~   s   zTestModuleTests.test_testNamesc                 C   sB   t  }t|}ttt|j|}|  |  | || d S r@   )r
   r   r   map
loadByNamesortr-   )r   test1namesr;   names1names2r   r   r   assertSuitesEqual   s   z!TestModuleTests.assertSuitesEqualc                 C   s   |  dt| jd  d S )Nr   tests)r-   lenr   r#   r   r   r   test_baseState   s   zTestModuleTests.test_baseStatec                 C   *   | j td | t| j dg dS )z
        Check that --testmodule loads a suite which contains the tests
        referred to in test-case-name inside its parameter.
        moduletest.pytwisted.trial.test.test_logNr   opt_testmoduler   rM   r   	_getSuiter#   r   r   r   test_testmoduleOnModule      z'TestModuleTests.test_testmoduleOnModulec                 C   s:   | j td | j td | t| j dg dS )z
        When the same module is specified with two --testmodule flags, it
        should only appear once in the suite.
        rR   rS   NrT   r#   r   r   r   test_testmoduleTwice   s
   z$TestModuleTests.test_testmoduleTwicec                 C   s:   | j td | j td | t| j dg dS )z
        If --testmodule is specified twice, once for module A and once for
        a module which refers to module A, then make sure module A is only
        added once.
        rR   ztest_log.pyrS   NrT   r#   r   r   r    test_testmoduleOnSourceAndTarget   s
   z0TestModuleTests.test_testmoduleOnSourceAndTargetc                 C   rQ   )z
        When given a module that refers to *itself* in the test-case-name
        variable, check that --testmodule only adds the tests once.
        zmoduleself.pyztwisted.trial.test.moduleselfNrT   r#   r   r   r   test_testmoduleOnSelfModule   rX   z+TestModuleTests.test_testmoduleOnSelfModulec                 C   s,   | j td | t| j ddg dS )zm
        Check that --testmodule loads tests referred to in test-case-name
        buffer variables.
        scripttest.pyrS   twisted.trial.test.test_runnerNrT   r#   r   r   r   test_testmoduleOnScript   s
   
z'TestModuleTests.test_testmoduleOnScriptc                 C   h   t  }tj|}t_d}z#| j| | dt| jd  | d|d|  W |t_dS |t_w )zz
        Check that --testmodule displays a meaningful error message when
        passed a non-existent filename.
        ztest_thisbetternoteverexist.pyr   rN   File  doesn't exist
Nr   sysstderrr   rU   r-   rO   getvalue)r   buffyrd   r   r   r   r    test_testmoduleOnNonexistentFile      z0TestModuleTests.test_testmoduleOnNonexistentFilec                 C   s*   | j td | dt| j d  dS )z
        Check that --testmodule adds no tests to the suite for modules
        which lack test-case-name buffer variables.
        	novars.pyr   rN   N)r   rU   r   r-   rO   r#   r   r   r   test_testmoduleOnEmptyVars   s   z*TestModuleTests.test_testmoduleOnEmptyVarsc                 C   r_   )z
        Check that --testmodule does *not* support module names as arguments
        and that it displays a meaningful error message.
        ztwisted.trial.test.test_scriptr   rN   r`   ra   Nrb   )r   rf   rd   
moduleNamer   r   r   test_testmoduleOnModuleName   rh   z+TestModuleTests.test_testmoduleOnModuleNamec                 C   "   d}t |}| ddi| d S )Nz5-*- test-case-name: twisted.trial.test.test_tests -*-test-case-nametwisted.trial.test.test_testsr   _parseLocalVariablesr-   r   declaration	localVarsr   r   r   test_parseLocalVariable      
z'TestModuleTests.test_parseLocalVariablec                 C   rm   )Nz6-*- test-case-name: twisted.trial.test.test_tests; -*-rn   ro   rp   rr   r   r   r   test_trailingSemicolon   rv   z&TestModuleTests.test_trailingSemicolonc                 C   s$   d}t |}| ddd| d S )Nz?-*- test-case-name: twisted.trial.test.test_tests; foo: bar -*-ro   bar)rn   foorp   rr   r   r   r   test_parseLocalVariables   s   

z(TestModuleTests.test_parseLocalVariablesc                 C   rm   )Nz:## -*- test-case-name: twisted.trial.test.test_tests -*- #rn   ro   rp   rr   r   r   r   test_surroundingGuff   rv   z$TestModuleTests.test_surroundingGuffc                 C   s   |  ttjd d S Nry   assertRaises
ValueErrorr   rq   r#   r   r   r   test_invalidLine  s   z TestModuleTests.test_invalidLinec                 C   s4   |  ttjd |  ttjd |  ttjd d S )Nz-*- foo -*-z-*- foo: bar; qux -*-z-*- foo: bar: baz; qux: qax -*-r}   r#   r   r   r   test_invalidDeclaration  s   z'TestModuleTests.test_invalidDeclarationc                 C   "   t td}| ddi| d S )NrR   rn   rS   r   loadLocalVariablesr   r-   r   rt   r   r   r   test_variablesFromFile     z&TestModuleTests.test_variablesFromFilec                 C   s   t td}| i | d S )Nri   r   r   r   r   r   test_noVariablesInFile  s   z&TestModuleTests.test_noVariablesInFilec                 C   r   )Nr\   rn   z:twisted.trial.test.test_log,twisted.trial.test.test_runnerr   r   r   r   r   test_variablesFromScript  s   z(TestModuleTests.test_variablesFromScriptc                 C   s    t td}| |dg d S )NrR   rS   )r   getTestModulesr   r-   r   modulesr   r   r   test_getTestModules#  s   z#TestModuleTests.test_getTestModulesc                 C   s"   t td}| t|d d S )Nri   r   )r   r   r   r-   rO   r   r   r   r   test_getTestModules_noVars'  r   z*TestModuleTests.test_getTestModules_noVarsc                 C   s&   t td}| t|ddh d S )Nr\   rS   r]   )r   r   r   r-   setr   r   r   r   test_getTestModules_multiple+  s
   z,TestModuleTests.test_getTestModules_multiplec                 C   sV   dD ]}|  t||d qdtdtdfD ]}| t||d qd S )N)ztest_script.pyz!twisted/trial/test/test_script.pyz should be a test filez twisted/trial/test/moduletest.pyr\   ztest_foo.batz should *not* be a test file)
assertTruer   
isTestFiler   assertFalse)r   r   r   r   r   test_looksLikeTestModule2  s   z(TestModuleTests.test_looksLikeTestModuleN)r0   r1   r2   r   rB   rE   rM   rP   rW   rY   rZ   r[   r^   rg   rj   rl   ru   rw   rz   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r>   w   s6    

		r>   c                   @   H   e Zd 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S )WithoutModuleTestsz*
    Test the C{without-module} flag.
    c                 C   s   t  | _ttj| _dS )zl
        Create a L{trial.Options} object to be used in the tests, and save
        C{sys.modules}.
        N)r   r   r   dictrc   r   savedModulesr#   r   r   r   r   H  s   
zWithoutModuleTests.setUpc                 C   s8   dD ]}|| j v r| j | tj|< qtj|d qdS )z)
        Restore C{sys.modules}.
        )imaplibsmtplibN)r   rc   r   pop)r   moduler   r   r   rB   P  s
   
zWithoutModuleTests.tearDownc                 C      ddl }|S )zE
        Try to import the C{smtplib} module, and return it.
        r   N)r   )r   r   r   r   r   
_checkSMTPZ     zWithoutModuleTests._checkSMTPc                 C   r   )zE
        Try to import the C{imaplib} module, and return it.
        r   N)r   )r   r   r   r   r   
_checkIMAPb  r   zWithoutModuleTests._checkIMAPc                 C   s<   | j ddg | t| j tjd= | |  tj	 dS )zT
        Check that after disabling a module, it can't be imported anymore.
        --without-moduler   N)
r   parseOptionsr~   ImportErrorr   rc   r   assertIsInstancetypes
ModuleTyper#   r   r   r   test_disableOneModulej  s   z(WithoutModuleTests.test_disableOneModulec                 C   sd   | j ddg | t| j | t| j tjd= tjd= | |  t	j
 | |  t	j
 dS )zE
        Check that several modules can be disabled at once.
        r   zsmtplib,imaplibr   r   N)r   r   r~   r   r   r   rc   r   r   r   r   r#   r   r   r   test_disableMultipleModulesu  s   z.WithoutModuleTests.test_disableMultipleModulesc                 C   s@   |  |  tj | tdtj| jj	ddg | 
t| j dS )zP
        Disabling an already imported module should produce a warning.
        z4Module 'smtplib' already imported, disabling anyway.r   r   N)r   r   r   r   assertWarnsRuntimeWarningr   r   r   r   r~   r   r#   r   r   r   !test_disableAlreadyImportedModule  s   z4WithoutModuleTests.test_disableAlreadyImportedModuleN)r0   r1   r2   r3   r   rB   r   r   r   r   r   r   r   r   r   r   C  s    
r   c                   @   sD   e Zd ZdZeedddu rdZdd Zdd Zd	d
 Z	dd Z
dS )CoverageTestsz+
    Tests for the I{coverage} option.
    gettraceNz;Cannot test trace hook installation without inspection API.c                 C   s   |  tjt  dS )zf
        Arrange for the current trace hook to be restored when the
        test is complete.
        N)
addCleanuprc   settracer   r#   r   r   r   r     s   zCoverageTests.setUpc                 C   s,   t  }|dg | t |jj dS )z}
        L{trial.Options} handles C{"--coverage"} by installing a trace
        hook to record coverage information.
        z
--coverageN)r   r   r   r-   rc   r   tracerglobaltracer   optionsr   r   r   test_tracerInstalled  s   z"CoverageTests.test_tracerInstalledc                 C   s.   t  }| | td|d dg dS )z
        L{trial.Options.coverdir} returns a L{FilePath} based on the default
        for the I{temp-directory} option if that option is not specified.
        .ztemp-directorycoverageN)r   r   r-   coverdirr   
descendantr   r   r   r   test_coverdirDefault  s
   z"CoverageTests.test_coverdirDefaultc                 C   s<   |   }t }|d|g | | t|d dS )z
        If a value is specified for the I{temp-directory} option,
        L{trial.Options.coverdir} returns a child of that path.
        z--temp-directoryr   N)mktempr   r   r   r-   r   r   child)r   pathr   r   r   r   test_coverdirOverridden  s   z%CoverageTests.test_coverdirOverridden)r0   r1   r2   r3   getattrrc   skipr   r   r   r   r   r   r   r   r     s    	r   c                   @   r   )OptionsTestsz%
    Tests for L{trial.Options}.
    c                 C   r5   )zE
        Build an L{Options} object to be used in the tests.
        Nr   r   r   r#   r   r   r   r     r7   zOptionsTests.setUpc                 C   sj   |  tjt  t r|  tj | jg d | j	 }| 
d| |d | ddg| dS )z
        C{_getWorkerArguments} discards options like C{random} as they only
        matter in the manager, and forwards options like C{recursionlimit} or
        C{disablegc}.
        )--recursionlimit2000--random4--disablegcr   r   r   N)r   rc   setrecursionlimitgetrecursionlimitr   	isenabledenabler   r   _getWorkerArgumentsassertInremover-   )r   argsr   r   r   test_getWorkerArguments  s   

z$OptionsTests.test_getWorkerArgumentsc                 C   *   |  t| jjg d}| dt| dS )z
        C{parseOptions} raises a C{UsageError} when C{--debug} is passed along
        C{--jobs} as it's not supported yet.

        @see: U{http://twistedmatrix.com/trac/ticket/5825}
        )--jobsr   --debugz+You can't specify --debug when using --jobsNr~   r   r   r   r-   strr   errorr   r   r   test_jobsConflictWithDebug     z'OptionsTests.test_jobsConflictWithDebugc                 C   r   )z
        C{parseOptions} raises a C{UsageError} when C{--profile} is passed
        along C{--jobs} as it's not supported yet.

        @see: U{http://twistedmatrix.com/trac/ticket/5827}
        )r   r   z	--profilez-You can't specify --profile when using --jobsNr   r   r   r   r   test_jobsConflictWithProfile  r   z)OptionsTests.test_jobsConflictWithProfilec                 C   r   )z
        C{parseOptions} raises a C{UsageError} when C{--debug-stacktraces} is
        passed along C{--jobs} as it's not supported yet.

        @see: U{http://twistedmatrix.com/trac/ticket/5826}
        )r   r   z--debug-stacktracesz7You can't specify --debug-stacktraces when using --jobsNr   r   r   r   r   %test_jobsConflictWithDebugStackTraces  s   z2OptionsTests.test_jobsConflictWithDebugStackTracesc                 C   r   )z
        C{parseOptions} raises a C{UsageError} when C{--exitfirst} is passed
        along C{--jobs} as it's not supported yet.

        @see: U{http://twistedmatrix.com/trac/ticket/6436}
        )r   r   --exitfirstz/You can't specify --exitfirst when using --jobsNr   r   r   r   r   test_jobsConflictWithExitFirst  r   z+OptionsTests.test_jobsConflictWithExitFirstc                 C   r   )zr
        C{parseOptions} raises a C{UsageError} when C{--order} is passed along
        with C{--random}.
        )--orderalphabeticalr   1234z-You can't specify --random when using --orderNr   r   r   r   r   test_orderConflictWithRandom  s   z)OptionsTests.test_orderConflictWithRandomN)r0   r1   r2   r3   r   r   r   r   r   r   r   r   r   r   r   r     s    r   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )MakeRunnerTestsz.
    Tests for the L{_makeRunner} helper.
    c                 C   r?   r@   r   r#   r   r   r   r   #  rA   zMakeRunnerTests.setUpc                 C   sJ   | j g d t| j }| |t | d|j | dg|j dS )z
        L{_makeRunner} returns a L{DistTrialRunner} instance when the C{--jobs}
        option is passed, and passes the C{workerNumber} and C{workerArguments}
        parameters to it.
        )r   r   
--force-gc   r   N)	r   r   r   r%   r   r   r-   _workerNumber_workerArgumentsr'   r   r   r   	test_jobs&  s
   zMakeRunnerTests.test_jobsc                 C   s<   | j g d t| j }| |t | tj|j dS )z
        L{_makeRunner} returns a L{TrialRunner} instance in C{DRY_RUN} mode
        when the C{--dry-run} option is passed, even if C{--jobs} is set.
        )r   r   z	--dry-runN)	r   r   r   r%   r   r   r-   DRY_RUNmoder'   r   r   r   test_dryRunWithJobs2  s   z#MakeRunnerTests.test_dryRunWithJobsc                    sP   t jj  fdd}| t jd| t  }|g d | t jt j| d S )Nc                    s   | dkr
t j|  | S )N	doNotFind)r   reflectModuleNotFound)fqnnamedAnyr   r   namedAnyExceptdoNotFind?  s   zFMakeRunnerTests.test_DebuggerNotFound.<locals>.namedAnyExceptdoNotFindr   )r   z
--debuggerr   )	r   r   r   r   r   r   r~   _DebuggerNotFoundr%   )r   r   r   r   r   r   test_DebuggerNotFound<  s   z%MakeRunnerTests.test_DebuggerNotFoundc                 C   s*   | j dg t| j }| |j dS )z
        Passing C{--exitfirst} wraps the reporter with a
        L{reporter._ExitWrapper} that stops on any non-success.
        r   N)r   r   r   r%   r   
_exitFirstr'   r   r   r   test_exitfirstK  s   zMakeRunnerTests.test_exitfirstN)	r0   r1   r2   r3   r   r   r   r   r   r   r   r   r   r     s    
r   c                   @       e Zd ZdZdd Zdd ZdS )RunTestsz(
    Tests for the L{run} function.
    c                 C   s   |  tjddd  d S )Nr   c                 S   s   d S r@   r   r#   r   r   r   <lambda>]  s    z RunTests.setUp.<locals>.<lambda>)r   r   r   r#   r   r   r   r   Z  s   zRunTests.setUpc              
   C   sh   dd }|  td| zt  W n ty, } z| dt| W Y d}~dS d}~ww | d dS )zY
        When a debugger is not found, an error message is printed to the user.

        c                  _   s
   t dr|   )r   r   )r   kwargsr   r   r   r%   e  rC   z3RunTests.test_debuggerNotFound.<locals>._makeRunnerr%   ry   Nz0Should have exited due to non-existent debugger!)r   r   r,   
SystemExitr   r   fail)r   r%   er   r   r   test_debuggerNotFound_  s   zRunTests.test_debuggerNotFoundN)r0   r1   r2   r3   r   r   r   r   r   r   r   U  s    r   c                   @   r   )TestArgumentOrderTestszQ
    Tests for the order-preserving behavior on provided command-line tests.
    c                 C   s   t  | _t | _d S r@   )r   r   r   r
   r;   r#   r   r   r   r   w  s   
zTestArgumentOrderTests.setUpc                 C   sR   g d}| j | t| j }t|}tt| jj|}t|}| 	|| dS )zN
        Multiple tests passed on the command line are not reordered.
        )ro   z"twisted.trial.test.test_assertionsz twisted.trial.test.test_deferredN)
r   r   r   rV   r   r   rF   r;   rG   r-   )r   rN   suiterJ   expectedSuiteexpectedNamesr   r   r   test_preserveArgumentOrder{  s   z1TestArgumentOrderTests.test_preserveArgumentOrderN)r0   r1   r2   r3   r   r  r   r   r   r   r   r  s    r   c                   @   sX   e Zd 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S )
OrderTestsz'
    Tests for the --order option.
    c                 C   r?   r@   r6   r#   r   r   r   r     rA   zOrderTests.setUpc                 C   D   | j g d t| j }|| j d }| t|g d dS )zn
        --order=alphabetical causes trial to run tests alphabetically within
        each test case.
        )r   r   %twisted.trial.test.ordertests.FooTestrN   )0twisted.trial.test.ordertests.FooTest.test_first1twisted.trial.test.ordertests.FooTest.test_fourth1twisted.trial.test.ordertests.FooTest.test_second0twisted.trial.test.ordertests.FooTest.test_thirdNr   r   r   r8   loadByNamesr-   r   r   r;   r   r   r   r   test_alphabetical  s   zOrderTests.test_alphabeticalc                 C   r  )zu
        --order=alphabetical causes trial to run test classes within a given
        module alphabetically.
        )r   r   twisted.trial.test.ordertestsrN   ).twisted.trial.test.ordertests.BarTest.test_bar.twisted.trial.test.ordertests.BazTest.test_bazr  r  r  r  Nr	  r  r   r   r   test_alphabeticalModule     z"OrderTests.test_alphabeticalModulec                 C   sV   | j g d t| j }|| j d }t|}| j|dd | |t| dS )z
        --order=alphabetical causes trial to run test modules within a given
        package alphabetically, with tests within each module alphabetized.
        )r   r   twisted.trial.testrN   zFailed to load any tests!msgN)	r   r   r   r8   r
  r   r   r-   sortedr   r;   r   rJ   r   r   r   test_alphabeticalPackage  s   z#OrderTests.test_alphabeticalPackagec                 C   r  )z
        --order=toptobottom causes trial to run test methods within a given
        test case from top to bottom as they are defined in the body of the
        class.
        )r   toptobottomr  rN   )r  r  r  r  Nr	  r  r   r   r   test_toptobottom  s   zOrderTests.test_toptobottomc                 C   r  )z
        --order=toptobottom causes trial to run test classes within a given
        module from top to bottom as they are defined in the module's source.
        r   r  r  rN   )r  r  r  r  r  r  Nr	  r  r   r   r   test_toptobottomModule  r  z!OrderTests.test_toptobottomModulec                 C   sP   | j g d t| j }|| j d }t|}| |t|dd d dS )z
        --order=toptobottom causes trial to run test modules within a given
        package alphabetically, with tests within each module run top to
        bottom.
        )r   r  r  rN   c                 S   s   |  dd d S )Nr   r   )split)namer   r   r   r     s    z4OrderTests.test_toptobottomPackage.<locals>.<lambda>)keyN)r   r   r   r8   r
  r   r-   r  r  r   r   r   test_toptobottomPackage  s   z"OrderTests.test_toptobottomPackagec                 C   s   |   }t|d}|  |dd |dtdd | j	}t
j	d| | t
j	j| ddlm} | t
jjd | t
jj|j |d  | jg d	 t| j}||}| t|g d
 dS )z{
        --order=toptobottom detects the source line of methods from modules
        whose source file is missing.
        twisted_toptobottom_tempz__init__.py    ztest_missing.pya   
        from twisted.trial.unittest import TestCase
        class TestMissing(TestCase):
            def test_second(self): pass
            def test_third(self): pass
            def test_fourth(self): pass
            def test_first(self): pass
        utf8r   )test_missingr  )z=twisted_toptobottom_temp.test_missing.TestMissing.test_secondz<twisted_toptobottom_temp.test_missing.TestMissing.test_thirdz=twisted_toptobottom_temp.test_missing.TestMissing.test_fourthz<twisted_toptobottom_temp.test_missing.TestMissing.test_firstN)r   r   r   makedirs
setContenttextwrapdedentencodeparentr   rc   insertr   r   r   r#  r   r   r0   r   r   r   r8   
loadModuler-   r   )r   tempdirpackage	pathEntryr#  r;   r   r   r   r   test_toptobottomMissingSource  s4   
	

z(OrderTests.test_toptobottomMissingSourcec                 C   s   |  t| jjddg dS )zL
        An unknown order passed to --order raises a L{UsageError}.
        r   zI don't existN)r~   r   r   r   r#   r   r   r   test_unknownOrder?  s   zOrderTests.test_unknownOrderN)r0   r1   r2   r3   r   r  r  r  r  r  r  r/  r0  r   r   r   r   r    s    .r  c                   @   s   e Zd ZdZdd ZdS )HelpOrderTestsz+
    Tests for the --help-orders flag.
    c                 C   s   |  tdt  | tt jdg}| |j	d tj
 }d}tj D ]"\}\}}tt| dt| |}| j||||f d q&dS )zS
        --help-orders prints each of the available orders and then exits.
        stdoutz--help-ordersr   z4%r with its description not properly described in %rz.*r  N)r   rc   r   r~   r   r   r   r   r-   coder2  re   
_runOrdersitemsresearchescaper   )r   excoutputr  	orderName	orderDesc_matchr   r   r   &test_help_ordersPrintsSynopsisAndQuitsN  s   
z5HelpOrderTests.test_help_ordersPrintsSynopsisAndQuitsN)r0   r1   r2   r3   r?  r   r   r   r   r1  I  s    r1  )*r   r6  rc   r&  r   ior   twisted.pythonr   twisted.python.filepathr   twisted.python.usager   twisted.scriptsr   twisted.trialr   twisted.trial._dist.disttrialr   twisted.trial.runnerr	   r
   r   r   twisted.trial.test.test_loaderr   
__import__r   r   SynchronousTestCaser   r4   r>   r   r   TestCaser   r   r   r   r  r1  r   r   r   r   <module>   s:   7 MO.^7 ;