U
    ˆjÜ�  ã                   @  s^  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 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 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 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)m*Z* d dl+m,Z, ddl-m.Z. e/dƒZ0dddœdd„Z1ddœdd„Z2ddd œd!d"„Z3ddd#œd$d%„Z4e$e4e3ƒZ5G d&d'„ d'ej6ƒZ7G d(d)„ d)ej6ƒZ8G d*d+„ d+ej6ƒZ9G d,d-„ d-ej6ƒZ:G d.d/„ d/ej6ƒZ;G d0d1„ d1ej6ƒZ<G d2d3„ d3ej6ƒZ=G d4d5„ d5ej>ƒZ?G d6d7„ d7ej>ƒZ@G d8d9„ d9ej>ƒZAG d:d;„ d;ej>ƒZBG d<d=„ d=ej>ƒZCG d>d?„ d?ej>ƒZDdS )@é    )ÚannotationsN)ÚStringIO)ÚList)ÚskipIf)Úassert_thatÚcontains_string)Úgiven)Úsampled_from)ÚLogger)Úutil)ÚFilePathÚ	IFilePath)Ú
UsageError)Útrial)Úunittest)ÚDistTrialRunner)Úcompose)ÚDestructiveTestSuiteÚ
TestLoaderÚ	TestSuiteÚTrialRunnerÚ_Runner)Ú	testNamesé   )ÚfileContentsr   Ústr)ÚfilenameÚreturnc                 C  s   t  t| ¡S )z1
    For finding files in twisted/trial/test
    )r   ÚsibpathÚ__file__)r   © r    úB/tmp/pip-unpacked-wheel-j5h_c8_q/twisted/trial/test/test_script.pyr   (   s    r   ÚNone©r   c                   C  s   t ƒ  d¡ dS )z.
    Emit something to L{twisted.logger}.
    Ú	somethingN)r
   Úinfor    r    r    r!   ÚlogSomething/   s    r&   z	List[str]ztrial.Options)Úargvr   c                 C  s   t  ¡ }| | ¡ |S )z?
    Parse an argument list using trial's argument parser.
    )r   ÚOptionsÚparseOptions)r'   Úconfigr    r    r!   ÚparseArguments6   s    
r+   )r*   r   c                 C  s0   t  | ¡}tƒ |_tt t¡gƒ}| |¡ | S )zM
    Run L{logSomething} as a test method using the given configuration.
    )	r   Ú_makeRunnerr   Ústreamr   ÚpyunitÚFunctionTestCaser&   Úrun)r*   ÚrunnerÚsuiter    r    r!   ÚrunFromConfig?   s
    

r3   c                   @  sr   e Zd ZdZeedddgƒƒdddœdd	„ƒZeed
dgƒƒdddœdd„ƒZeed
dgƒƒdddœdd„ƒZdS )ÚLogfileTestsz)
    Tests for the --logfile option.
    zdir-azdir-bzdir-c/dir-dr   r"   )ÚworkingDirectoryr   c                 C  s4   t d|gƒ}t|ƒ |d ¡}t|ttdƒƒƒ dS )zƒ
        If no value is given for the option then logs are written to a log
        file constructed from a default value.
        ú--temp-directoryÚlogfiler$   N©ÚrunFromArgumentsr   ÚpreauthChildr   r   r   )Úselfr5   r*   ÚlogPathr    r    r!   Útest_defaultR   s    zLogfileTests.test_defaultzsomelog.txtzsomedir/somelog.txt)r7   r   c                 C  s4   t d|gƒ}t|d ƒ |¡}t|ttdƒƒƒ dS )z½
        If the value given for the option is a relative path then it is
        interpreted relative to trial's own temporary working directory and
        logs are written there.
        ú	--logfileútemp-directoryr$   Nr8   )r;   r7   r*   r<   r    r    r!   Útest_relativePathd   s    zLogfileTests.test_relativePathc                 C  s6   t dƒ |¡}td|jgƒ |}t|ttdƒƒƒ dS )z�
        If the value given for the option is an absolute path then it is
        interpreted absolutely and logs are written there.
        Ú.r>   r$   N)r   r:   r9   Úpathr   r   r   )r;   r7   r<   ZiPathr    r    r!   Útest_absolutePathv   s    zLogfileTests.test_absolutePathN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r	   r=   r@   rC   r    r    r    r!   r4   M   s4   ýÿÿ		þÿÿ
þÿÿr4   c                   @  sd   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zd
dœdd„Zddœdd„Zddœdd„Z	dS )ÚForceGarbageCollectionTestsz*
    Tests for the --force-gc option.
    r"   r#   c                 C  s>   t  ¡ | _g | _|  td| j¡ t | j	¡}t
||gƒ| _d S )NÚcollect)r   r(   r*   ÚlogÚpatchÚgcrI   r.   r/   Ú
simpleTestr   Útest)r;   rN   r    r    r!   ÚsetUp’   s
    
z!ForceGarbageCollectionTests.setUpc                 C  s   | j  d¡ dS )zD
        A simple test method that records that it was run.
        rN   N©rJ   Úappend©r;   r    r    r!   rM   ™   s    z&ForceGarbageCollectionTests.simpleTestc                 C  s   | j  d¡ dS )zI
        A replacement for gc.collect that logs calls to itself.
        rI   NrP   rR   r    r    r!   rI   Ÿ   s    z#ForceGarbageCollectionTests.collectr   c                 C  s   t  | j¡}tƒ |_|S )zN
        Return a L{TrialRunner} object that is safe to use in tests.
        )r   r,   r*   r   r-   ©r;   r1   r    r    r!   Ú
makeRunner¥   s    z&ForceGarbageCollectionTests.makeRunnerc              	   C  sF   d| j d< | j  ¡  |  ¡ }| | j¡ |  | jddddddg¡ 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-gcrI   rN   N©r*   ZpostOptionsrT   r0   rN   ÚassertEqualrJ   rS   r    r    r!   Útest_forceGc­   s    

 ÿz(ForceGarbageCollectionTests.test_forceGcc                 C  s4   | j  ¡  |  ¡ }| | j¡ |  | jddg¡ dS )z>
        By default, no garbage collection is forced.
        rN   NrU   rS   r    r    r!   Útest_unforceGcº   s    
z*ForceGarbageCollectionTests.test_unforceGcN)
rD   rE   rF   rG   rO   rM   rI   rT   rW   rX   r    r    r    r!   rH   �   s   rH   c                   @  s:   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zd
S )ÚSuiteUsedTestsz?
    Check the category of tests suite used by the loader.
    r"   r#   c                 C  s   t  ¡ | _dS )z6
        Create a trial configuration object.
        N©r   r(   r*   rR   r    r    r!   rO   É   s    zSuiteUsedTests.setUpc                 C  s   t  | j¡}|  |jt¡ dS )zK
        By default, the loader should use L{DestructiveTestSuite}
        N)r   Ú
_getLoaderr*   rV   ÚsuiteFactoryr   ©r;   Úloaderr    r    r!   Útest_defaultSuiteÏ   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   r[   rV   r\   r   r]   r    r    r!   Útest_untilFailureSuiteÖ   s    
z%SuiteUsedTests.test_untilFailureSuiteN)rD   rE   rF   rG   rO   r_   r`   r    r    r    r!   rY   Ä   s   rY   c                   @  s|  e Zd Zddœdd„Zddœdd„Zddœdd„Zd	d
ddœdd„Zddœdd„Zddœdd„Zddœdd„Z	ddœdd„Z
ddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœd d!„Zddœd"d#„Zddœd$d%„Zddœd&d'„Zddœd(d)„Zddœd*d+„Zddœd,d-„Zddœd.d/„Zddœd0d1„Zddœd2d3„Zddœd4d5„Zddœd6d7„Zddœd8d9„Zd:S );ÚTestModuleTestsr"   r#   c                 C  s   t  ¡ | _d S ©NrZ   rR   r    r    r!   rO   á   s    zTestModuleTests.setUpc                 C  s
   d | _ d S rb   )r*   rR   r    r    r!   ÚtearDownä   s    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)rV   r   ÚidrR   r    r    r!   Útest_testNamesç   s    zTestModuleTests.test_testNamesr   z	list[str])Útest1Únamesr   c                 C  sB   t ƒ }t|ƒ}ttt|j|ƒƒƒ}| ¡  | ¡  |  ||¡ d S rb   )r   r   r   ÚmapÚ
loadByNameÚsortrV   )r;   rf   rg   r^   Znames1Znames2r    r    r!   ÚassertSuitesEqualî   s    z!TestModuleTests.assertSuitesEqualc                 C  s   |   dt| jd ƒ¡ d S )Nr   Útests)rV   Úlenr*   rR   r    r    r!   Útest_baseStateö   s    zTestModuleTests.test_baseStatec                 C  s*   | 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   rk   r   Ú	_getSuiterR   r    r    r!   Útest_testmoduleOnModuleù   s
    
 ÿ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.
        ro   rp   Nrq   rR   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.
        ro   ztest_log.pyrp   Nrq   rR   r    r    r!   Ú test_testmoduleOnSourceAndTarget  s    
 ÿz0TestModuleTests.test_testmoduleOnSourceAndTargetc                 C  s*   | j  tdƒ¡ |  t | j ¡dg¡ dS )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.moduleselfNrq   rR   r    r    r!   Útest_testmoduleOnSelfModule  s
    
 ÿ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.pyrp   útwisted.trial.test.test_runnerNrq   rR   r    r    r!   Útest_testmoduleOnScript$  s
    
þz'TestModuleTests.test_testmoduleOnScriptc                 C  sd   t ƒ }tj| }t_d}z>| j |¡ |  dt| jd ƒ¡ |  d|›d�| ¡ ¡ W 5 |t_X dS )zz
        Check that --testmodule displays a meaningful error message when
        passed a non-existent filename.
        ztest_thisbetternoteverexist.pyr   rl   úFile ú doesn't exist
N©r   ÚsysÚstderrr*   rr   rV   rm   Úgetvalue)r;   Úbuffyr   r   r    r    r!   Ú test_testmoduleOnNonexistentFile/  s    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   rl   N)r*   rr   r   rV   rm   rR   r    r    r!   Útest_testmoduleOnEmptyVars>  s    z*TestModuleTests.test_testmoduleOnEmptyVarsc                 C  sd   t ƒ }tj| }t_d}z>| j |¡ |  dt| jd ƒ¡ |  d|›d�| ¡ ¡ W 5 |t_X dS )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   rl   r{   r|   Nr}   )r;   r�   r   Z
moduleNamer    r    r!   Útest_testmoduleOnModuleNameF  s    z+TestModuleTests.test_testmoduleOnModuleNamec                 C  s"   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   Ú_parseLocalVariablesrV   ©r;   ZdeclarationÚ	localVarsr    r    r!   Útest_parseLocalVariableU  s    
z'TestModuleTests.test_parseLocalVariablec                 C  s"   d}t  |¡}|  ddi|¡ d S )Nz6-*- test-case-name: twisted.trial.test.test_tests; -*-r†   r‡   rˆ   rŠ   r    r    r!   Útest_trailingSemicolonZ  s    
z&TestModuleTests.test_trailingSemicolonc                 C  s$   d}t  |¡}|  dddœ|¡ d S )Nz?-*- test-case-name: twisted.trial.test.test_tests; foo: bar -*-r‡   Úbar)r†   Úfoorˆ   rŠ   r    r    r!   Útest_parseLocalVariables_  s    ÿ
 ÿz(TestModuleTests.test_parseLocalVariablesc                 C  s"   d}t  |¡}|  ddi|¡ d S )Nz:## -*- test-case-name: twisted.trial.test.test_tests -*- #r†   r‡   rˆ   rŠ   r    r    r!   Útest_surroundingGuffh  s    
z$TestModuleTests.test_surroundingGuffc                 C  s   |   ttjd¡ d S ©Nr�   ©ÚassertRaisesÚ
ValueErrorr   r‰   rR   r    r    r!   Útest_invalidLinem  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“   rR   r    r    r!   Útest_invalidDeclarationp  s      ÿ  ÿz'TestModuleTests.test_invalidDeclarationc                 C  s"   t  tdƒ¡}|  ddi|¡ d S )Nro   r†   rp   ©r   ZloadLocalVariablesr   rV   ©r;   r‹   r    r    r!   Útest_variablesFromFiley  s    z&TestModuleTests.test_variablesFromFilec                 C  s   t  tdƒ¡}|  i |¡ d S )Nrƒ   r˜   r™   r    r    r!   Útest_noVariablesInFile}  s    z&TestModuleTests.test_noVariablesInFilec                 C  s"   t  tdƒ¡}|  ddi|¡ d S )Nrx   r†   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 )Nro   rp   )r   ÚgetTestModulesr   rV   ©r;   Úmodulesr    r    r!   Útest_getTestModulesŒ  s    z#TestModuleTests.test_getTestModulesc                 C  s"   t  tdƒ¡}|  t|ƒd¡ d S )Nrƒ   r   )r   r�   r   rV   rm   rž   r    r    r!   Útest_getTestModules_noVars�  s    z*TestModuleTests.test_getTestModules_noVarsc                 C  s&   t  tdƒ¡}|  t|ƒddh¡ d S )Nrx   rp   ry   )r   r�   r   rV   Ú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�¡ q4d S )N)ztest_script.pyz!twisted/trial/test/test_script.pyz should be a test filez twisted/trial/test/moduletest.pyrx   ztest_foo.batz should *not* be a test file)Ú
assertTruer   Z
isTestFiler   ZassertFalse)r;   r   r    r    r!   Útest_looksLikeTestModule›  s    þýþz(TestModuleTests.test_looksLikeTestModuleN)rD   rE   rF   rO   rc   re   rk   rn   rt   ru   rv   rw   rz   r‚   r„   r…   rŒ   r�   r�   r‘   r–   r—   rš   r›   rœ   r    r¡   r£   r¥   r    r    r    r!   ra   à   s4   

		ra   c                   @  sr   e Zd ZdZddœdd„Zddœdd„Zddœd	d
„Zddœdd„Zddœdd„Zddœdd„Z	ddœdd„Z
dS )ÚWithoutModuleTestsz*
    Test the C{without-module} flag.
    r"   r#   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*   Údictr~   rŸ   ÚsavedModulesrR   r    r    r!   rO   ±  s    
zWithoutModuleTests.setUpc                 C  s8   dD ].}|| j kr$| j | tj|< qtj |d¡ qdS )z)
        Restore C{sys.modules}.
        )ÚimaplibÚsmtplibN)r¨   r~   rŸ   Úpop)r;   Úmoduler    r    r!   rc   ¹  s    
zWithoutModuleTests.tearDownÚobjectc                 C  s   ddl }|S )zE
        Try to import the C{smtplib} module, and return it.
        r   N)rª   )r;   rª   r    r    r!   Ú
_checkSMTPÃ  s    zWithoutModuleTests._checkSMTPc                 C  s   ddl }|S )zE
        Try to import the C{imaplib} module, and return it.
        r   N)r©   )r;   r©   r    r    r!   Ú
_checkIMAPË  s    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*   r)   r”   ÚImportErrorr®   r~   rŸ   ÚassertIsInstanceÚtypesÚ
ModuleTyperR   r    r    r!   Útest_disableOneModuleÓ  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¯   r~   rŸ   r²   r³   r´   rR   r    r    r!   Útest_disableMultipleModulesÞ  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´   ZassertWarnsÚRuntimeWarningr   r   r*   r)   r”   r±   rR   r    r    r!   Ú!test_disableAlreadyImportedModuleì  s    ûz4WithoutModuleTests.test_disableAlreadyImportedModuleN)rD   rE   rF   rG   rO   rc   r®   r¯   rµ   r¶   r¸   r    r    r    r!   r¦   ¬  s   
r¦   c                   @  s†   e Zd ZdZejdd… dkZeeddƒdk	Z	e
e dƒddœd	d
„ƒZe
epRe	 dƒddœdd„ƒZddœdd„Zddœdd„ZdS )ÚAutoJobsTestsz0
    Test {twisted.scripts.trial._autoJobs}
    Né   )é   é   Úsched_getaffinityzRequires os.process_cpu_count()r"   r#   c                 C  s   t  ¡ }|  t ¡ |¡ dS )zt
        When `os.process_cpu_count()` is available (Python 3.13+), `_autoJobs`
        returns its result.
        N)ÚosÚprocess_cpu_countrV   r   Ú	_autoJobs)r;   Úcountr    r    r!   Útest_processCpuCount  s    z"AutoJobsTests.test_processCpuCountz>Requires os.sched_getaffinity() without os.process_cpu_count()c                 C  s   |   t ¡ tt d¡ƒ¡ dS )a  
        When `os.sched_getaffinity()` is available but `os.process_cpu_count()`
        is not (Python 3.3 through Python 3.12 on "some Unix platforms"),
        `_autoJobs` uses it to returns the number of CPUs that the current
        process is restricted to.
        r   N)rV   r   rÀ   rm   r¾   r½   rR   r    r    r!   Útest_schedGetAffinity  s    z#AutoJobsTests.test_schedGetAffinityc                 C  s   |   t ¡ t ¡ ¡ dS )zb
        When only `os.cpu_count()` is available, `_autoJobs` returns
        its result.
        N)rV   r   rÀ   r¾   Ú	cpu_countrR   r    r    r!   Útest_cpuCount  s    zAutoJobsTests.test_cpuCountc                 C  sV   | j r|  tddd„ ¡ | jr0|  tddd„ ¡ |  tddd„ ¡ |  t ¡ d¡ d	S )
zç
        When `os.cpu_count()` returns L{None}, `_autoJobs`
        rounds up to 1.

        This test relies on patching because all platforms currently
        supported by Twisted provide a functioning `os.cpu_count()`.
        r¿   c                   S  s   d S rb   r    r    r    r    r!   Ú<lambda>)  ó    z4AutoJobsTests.test_cpuCountUnknown.<locals>.<lambda>r½   c                 S  s   t ƒ S rb   )r¢   )Úpidr    r    r!   rÆ   +  rÇ   rÄ   c                   S  s   d S rb   r    r    r    r    r!   rÆ   ,  rÇ   r   N)ÚhasProcessCpuCountrK   r¾   ÚhasSchedGetaffinityrV   r   rÀ   rR   r    r    r!   Útest_cpuCountUnknown   s    z"AutoJobsTests.test_cpuCountUnknown)rD   rE   rF   rG   r~   Úversion_inforÉ   Úgetattrr¾   rÊ   r   rÂ   rÃ   rÅ   rË   r    r    r    r!   r¹   û  s   
þ	r¹   c                   @  s\   e Zd ZdZeeddƒdkr dZddœdd„Zddœd	d
„Zddœdd„Z	ddœdd„Z
dS )ÚCoverageTestsz+
    Tests for the I{coverage} option.
    ÚgettraceNz;Cannot test trace hook installation without inspection API.r"   r#   c                 C  s   |   tjt ¡ ¡ dS )zf
        Arrange for the current trace hook to be restored when the
        test is complete.
        N)Ú
addCleanupr~   ÚsettracerÏ   rR   r    r    r!   rO   9  s    zCoverageTests.setUpc                 C  s:   t  ¡ }| dg¡ |jdk	s"t‚|  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)   ZtracerÚAssertionErrorrV   r~   rÏ   Z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.
        rA   r?   ÚcoverageN)r   r(   rV   Úcoverdirr   Z
descendantrÓ   r    r    r!   Útest_coverdirDefaultN  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.
        r6   rÖ   N)Úmktempr   r(   r)   rV   r×   r   Úchild)r;   rB   rÔ   r    r    r!   Útest_coverdirOverriddenY  s    z%CoverageTests.test_coverdirOverridden)rD   rE   rF   rG   rÍ   r~   ÚskiprO   rÕ   rØ   rÛ   r    r    r    r!   rÎ   1  s   rÎ   c                   @  sd   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœd
d„Zddœdd„Zddœdd„Z	dS )ÚOptionsTestsz%
    Tests for L{trial.Options}.
    r"   r#   c                 C  s   t  ¡ | _dS )zE
        Build an L{Options} object to be used in the tests.
        N©r   r(   rÔ   rR   r    r    r!   rO   i  s    zOptionsTests.setUpc                 C  sp   |   tjt ¡ ¡ t ¡ r&|   tj¡ | j dddddg¡ | 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}.
        z--recursionlimitÚ2000ú--randomÚ4z--disablegcN)rÐ   r~   ÚsetrecursionlimitÚgetrecursionlimitrL   Ú	isenabledÚenablerÔ   r)   Z_getWorkerArgumentsÚassertInÚremoverV   )r;   Úargsr    r    r!   Útest_getWorkerArgumentso  s    ÿ

z$OptionsTests.test_getWorkerArgumentsc                 C  s,   |   t| jjdddg¡}|  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)   rV   r   ©r;   Úerrorr    r    r!   Útest_jobsConflictWithDebug�  s      ÿz'OptionsTests.test_jobsConflictWithDebugc                 C  s,   |   t| jjdddg¡}|  dt|ƒ¡ dS )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�  s      ÿz)OptionsTests.test_jobsConflictWithProfilec                 C  s,   |   t| jjdddg¡}|  dt|ƒ¡ dS )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  s.   |   t| jjddddg¡}|  dt|ƒ¡ dS )zr
        C{parseOptions} raises a C{UsageError} when C{--order} is passed along
        with C{--random}.
        ú--orderÚalphabeticalrà   Z1234z-You can't specify --random when using --orderNrì   rí   r    r    r!   Útest_orderConflictWithRandom©  s    
ýz)OptionsTests.test_orderConflictWithRandomN)
rD   rE   rF   rG   rO   ré   rï   rð   rñ   rô   r    r    r    r!   rÝ   d  s   rÝ   c                   @  s€   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœd
d„Zddœdd„Zddœdd„Z	ddœdd„Z
ddœdd„ZdS )ÚMakeRunnerTestsz.
    Tests for the L{_makeRunner} helper.
    r"   r#   c                 C  s   t  ¡ | _d S rb   rÞ   rR   r    r    r!   rO   »  s    zMakeRunnerTests.setUpc                 C  sZ   | j  dddg¡ t | j ¡}t|tƒs,t‚|  |t¡ |  d|j	¡ |  dg|j
¡ dS )zÝ
        L{_makeRunner} returns a L{DistTrialRunner} instance when the C{--jobs}
        option is passed.  The L{DistTrialRunner} knows how many workers to
        run and the C{workerArguments} to pass to them.
        rê   rá   z
--force-gcé   N)rÔ   r)   r   r,   Ú
isinstancer   rÒ   r²   rV   Ú_maxWorkersZ_workerArgumentsrS   r    r    r!   Ú	test_jobs¾  s    zMakeRunnerTests.test_jobsc                 C  s*   |   t| jjddg¡}|  dt|ƒ¡ dS )z`
        C{parseOptions} raises a C{UsageError} when C{--jobs} is passed a non-integer.
        rê   Únanz-Expecting integer argument to jobs, got 'nan'Nrì   ©r;   Úexcr    r    r!   Útest_jobsNotANumberË  s      ÿz#MakeRunnerTests.test_jobsNotANumberc                 C  s*   |   t| jjddg¡}|  dt|ƒ¡ dS )zq
        C{parseOptions} raises a C{UsageError} when C{--jobs} is passed a non-positive
        integer.
        rê   Ú0z>Argument to jobs must be a strictly positive integer or 'auto'Nrì   rû   r    r    r!   Útest_jobsNonPositiveÔ  s
     ÿz$MakeRunnerTests.test_jobsNonPositivec                 C  sR   |   tddd„ ¡ t ¡ }| ddg¡ t |¡}t|tƒs@t‚|  d|j	¡ dS )zF
        C{--jobs auto} guesses the number of workers to use.
        rÀ   c                   S  s   dS )NéM   r    r    r    r    r!   rÆ   â  rÇ   zEMakeRunnerTests.test_autoDetectAvailableCpusForJobs.<locals>.<lambda>rê   Úautor   N)
rK   r   r(   r)   r,   r÷   r   rÒ   rV   rø   )r;   rÔ   r1   r    r    r!   Ú#test_autoDetectAvailableCpusForJobsÞ  s    
z3MakeRunnerTests.test_autoDetectAvailableCpusForJobsc                 C  sL   | j  dddg¡ t | j ¡}t|tƒs,t‚|  |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Ò   r²   rV   ZDRY_RUNÚmoderS   r    r    r!   Útest_dryRunWithJobsë  s
    z#MakeRunnerTests.test_dryRunWithJobsc                   sZ   t jj‰ dddœ‡ fdd„}|  t jd|¡ t  ¡ }| ddd	g¡ |  t jt j|¡ d S )
Nr   r­   )Úfqnr   c                   s   | dkrt j | ¡‚ˆ | ƒS )NÚ	doNotFind)r   ÚreflectZModuleNotFound)r  ©ÚnamedAnyr    r!   ÚnamedAnyExceptdoNotFindù  s    zFMakeRunnerTests.test_DebuggerNotFound.<locals>.namedAnyExceptdoNotFindr	  rë   z
--debuggerr  )	r   r  r	  rK   r(   r)   r”   Ú_DebuggerNotFoundr,   )r;   r
  rÔ   r    r  r!   Útest_DebuggerNotFoundö  s    z%MakeRunnerTests.test_DebuggerNotFoundc                 C  s8   | j  dg¡ t | j ¡}t|tƒs(t‚|  |j¡ dS )z‚
        Passing C{--exitfirst} wraps the reporter with a
        L{reporter._ExitWrapper} that stops on any non-success.
        z--exitfirstN)	rÔ   r)   r   r,   r÷   r   rÒ   r¤   Z
_exitFirstrS   r    r    r!   Útest_exitfirst  s    zMakeRunnerTests.test_exitfirstN)rD   rE   rF   rG   rO   rù   rý   rÿ   r  r  r  r  r    r    r    r!   rõ   ¶  s   	
rõ   c                   @  s,   e Zd ZdZddœdd„Zddœdd„ZdS )	ÚRunTestsz(
    Tests for the L{run} function.
    r"   r#   c                 C  s   |   tjddd„ ¡ d S )Nr)   c                 S  s   d S rb   r    rR   r    r    r!   rÆ     rÇ   z RunTests.setUp.<locals>.<lambda>)rK   r   r(   rR   r    r    r!   rO     s    zRunTests.setUpc              
   C  sn   ddddœdd„}|   td|¡ zt ¡  W n2 tk
r^ } z|  dt|ƒ¡ W 5 d}~X Y nX |  d	¡ dS )
zY
        When a debugger is not found, an error message is printed to the user.

        r­   r"   )rè   Úkwargsr   c                  _  s   t  d¡‚d S r’   )r   r  )rè   r  r    r    r!   r,      s    z3RunTests.test_debuggerNotFound.<locals>._makeRunnerr,   r�   Nz0Should have exited due to non-existent debugger!)rK   r   r0   Ú
SystemExitræ   r   Zfail)r;   r,   Úer    r    r!   Útest_debuggerNotFound  s    "zRunTests.test_debuggerNotFoundN)rD   rE   rF   rG   rO   r  r    r    r    r!   r    s   r  c                   @  s,   e Zd ZdZddœdd„Zddœdd„ZdS )	ÚTestArgumentOrderTestszQ
    Tests for the order-preserving behavior on provided command-line tests.
    r"   r#   c                 C  s   t  ¡ | _tƒ | _d S rb   )r   r(   r*   r   r^   rR   r    r    r!   rO   2  s    
zTestArgumentOrderTests.setUpc                 C  sT   dddg}| j  |¡ t | j ¡}t|ƒ}tt| jj|ƒƒ}t|ƒ}|  	||¡ dS )zN
        Multiple tests passed on the command line are not reordered.
        r‡   z"twisted.trial.test.test_assertionsz twisted.trial.test.test_deferredN)
r*   r)   r   rs   r   r   rh   r^   ri   rV   )r;   rl   r2   rg   ZexpectedSuiteZexpectedNamesr    r    r!   Útest_preserveArgumentOrder6  s    ýz1TestArgumentOrderTests.test_preserveArgumentOrderN)rD   rE   rF   rG   rO   r  r    r    r    r!   r  -  s   r  c                   @  sŽ   e Zd ZdZddœdd„Zddœdd„Zddœdd	„Zddœd
d„Zddœdd„Zddœdd„Z	ddœdd„Z
ddœdd„Zddœdd„ZdS )Ú
OrderTestsz'
    Tests for the --order option.
    r"   r#   c                 C  s   t  ¡ | _d S rb   rZ   rR   r    r    r!   rO   O  s    zOrderTests.setUpc                 C  sJ   | j  dddg¡ t | j ¡}| | j d ¡}|  t|ƒddddg¡ d	S )
zn
        --order=alphabetical causes trial to run tests alphabetically within
        each test case.
        rò   ró   ú%twisted.trial.test.ordertests.FooTestrl   ú0twisted.trial.test.ordertests.FooTest.test_firstú1twisted.trial.test.ordertests.FooTest.test_fourthú1twisted.trial.test.ordertests.FooTest.test_secondú0twisted.trial.test.ordertests.FooTest.test_thirdN©r*   r)   r   r[   ÚloadByNamesrV   r   ©r;   r^   r2   r    r    r!   Útest_alphabeticalR  s    ÿüþzOrderTests.test_alphabeticalc              	   C  sN   | j  dddg¡ t | j ¡}| | j d ¡}|  t|ƒddddd	d
g¡ dS )zu
        --order=alphabetical causes trial to run test classes within a given
        module alphabetically.
        rò   ró   útwisted.trial.test.ordertestsrl   ú.twisted.trial.test.ordertests.BarTest.test_barú.twisted.trial.test.ordertests.BazTest.test_bazr  r  r  r  Nr  r  r    r    r!   Útest_alphabeticalModuleh  s    ÿúþz"OrderTests.test_alphabeticalModulec                 C  sX   | j  dddg¡ 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.testrl   zFailed to load any tests!©ÚmsgN)	r*   r)   r   r[   r  r   r¤   rV   Úsorted©r;   r^   r2   rg   r    r    r!   Útest_alphabeticalPackage  s    z#OrderTests.test_alphabeticalPackagec                 C  sJ   | j  dddg¡ t | j ¡}| | j d ¡}|  t|ƒddddg¡ d	S )
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  rl   r  r  r  r  Nr  r  r    r    r!   Útest_toptobottomŒ  s    ÿüþzOrderTests.test_toptobottomc              	   C  sN   | j  dddg¡ t | j ¡}| | j d ¡}|  t|ƒddddd	d
g¡ dS )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  rl   r  r  r  r  r!  r   Nr  r  r    r    r!   Útest_toptobottomModule£  s    ÿúþz!OrderTests.test_toptobottomModulec                 C  sR   | j  dddg¡ 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#  rl   c                 S  s   |   d¡d d… S )NrA   rö   )Úsplit)Únamer    r    r!   rÆ   É  rÇ   z4OrderTests.test_toptobottomPackage.<locals>.<lambda>)ÚkeyN)r*   r)   r   r[   r  r   rV   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 d	d
dg¡ t | j¡}| |¡}|  t|ƒddddg¡ dS )z{
        --order=toptobottom detects the source line of methods from modules
        whose source file is missing.
        Útwisted_toptobottom_tempz__init__.pyrÇ   ztest_missing.pya   
        from twisted.trial.unittest import TestCase
        class TestMissing(TestCase):
            def test_second(self) -> None: pass
            def test_third(self) -> None: pass
            def test_fourth(self) -> None: pass
            def test_first(self) -> None: pass
        Úutf8r   )Útest_missingrò   r)  r  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Ú   ÚmakedirsZ
setContentÚtextwrapÚdedentÚencodeÚparentrB   r~   ÚinsertrÐ   rç   r0  r2  rŸ   r«   rD   r*   r)   r   r[   Z
loadModulerV   r   )r;   ÚtempdirÚpackageZ	pathEntryr2  r^   r2   r    r    r!   Útest_toptobottomMissingSourceÌ  s>    
ÿ	÷ÿ
ÿ
üþ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)   rR   r    r    r!   Útest_unknownOrderü  s
      ÿzOrderTests.test_unknownOrderN)rD   rE   rF   rG   rO   r  r"  r(  r*  r+  r/  r;  r<  r    r    r    r!   r  J  s   0r  c                   @  s   e Zd ZdZddœdd„ZdS )ÚHelpOrderTestsz+
    Tests for the --help-orders flag.
    r"   r#   c           	      C  s˜   |   tdtƒ  }¡ |  tt ¡ jdg¡}|  |j	d¡ | 
¡ }d}tj ¡ D ]D\}\}}t t |¡› dt |¡› �|¡}| j||||f d� qNd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)rK   r~   r   r”   r  r   r(   r)   rV   Úcoder€   Z
_runOrdersÚitemsÚreÚsearchÚescaper¤   )	r;   r>  rü   Úoutputr%  Z	orderNameZ	orderDescÚ_Úmatchr    r    r!   Ú&test_help_ordersPrintsSynopsisAndQuits  s      ÿþz5HelpOrderTests.test_help_ordersPrintsSynopsisAndQuitsN)rD   rE   rF   rG   rG  r    r    r    r!   r=    s   r=  )EÚ
__future__r   rL   r¾   rA  r~   r4  r³   Úior   Útypingr   r   r   Zhamcrestr   r   Z
hypothesisr   Zhypothesis.strategiesr	   Ztwisted.loggerr
   Ztwisted.pythonr   Ztwisted.python.filepathr   r   Ztwisted.python.usager   Ztwisted.scriptsr   Ztwisted.trialZtwisted.trial._dist.disttrialr   Ztwisted.trial._dist.functionalr   Ztwisted.trial.runnerr   r   r   r   r   Ztwisted.trial.test.test_loaderr   Zmatchersr   Ú
__import__r.   r   r&   r+   r3   r9   ZSynchronousTestCaser4   rH   rY   ra   r¦   r¹   rÎ   ZTestCaserÝ   rõ   r  r  r  r=  r    r    r    r!   Ú<module>   sX   	
@7 MO63RZ =