o
    bXW                     @   s   d Z ddlZddlZddl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mZmZ ddlmZ dd	lmZ dd
lm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ZG dd dejZdS )z"
Tests for loading tests by name.
    N)md5)filepathutil)	getModule)ModuleNotFound)reporterrunnerunittest)_iterateTests)	ITestCase)packagesc                 C   s$   g }t | D ]	}||  q|S )zI
    Return the id of each test within the given test suite or case.
    )r
   appendid)testsnamestest r   @/usr/lib/python3/dist-packages/twisted/trial/test/test_loader.py	testNames   s   r   c                       s<   e Zd Z fddZdd Zdd Zdd Zd	d
 Z  ZS )FinderPy3Testsc                    s   t    t | _d S N)supersetUpr   
TestLoaderloaderself	__class__r   r   r   "   s   
zFinderPy3Tests.setUpc                 C      |  t| jjd dS )z
        findByName, if given something findable up until the last entry, will
        raise AttributeError (as it cannot tell if 'nonexistent' here is
        supposed to be a module or a class).
        ztwisted.trial.test.nonexistentN)assertRaisesAttributeErrorr   
findByNamer   r   r   r   test_findNonModule&   s   
z!FinderPy3Tests.test_findNonModulec                 C      |  t| jjd d S )N	nonextant)r    r   r   r"   r   r   r   r   test_findNonPackage0   s   z"FinderPy3Tests.test_findNonPackagec                 C   s"   t td}| t| jj| dS )z
        findByName, given a file path that doesn't exist, will raise a
        ValueError saying that it is not a Python file.
        znonexistent.pyN)r   sibpath__file__r    
ValueErrorr   r"   )r   pathr   r   r   test_findNonFile3   s   zFinderPy3Tests.test_findNonFilec                 C   r   )zn
        findByName will re-raise ImportErrors inside modules that it has found
        and imported.
        zunimportablepackage.test_moduleN)r    ImportErrorr   r"   r   r   r   r   test_findFileWithImportError;   s   
z+FinderPy3Tests.test_findFileWithImportError)	__name__
__module____qualname__r   r#   r&   r+   r-   __classcell__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 )	FileTestsz/
    Tests for L{runner.filenameToModule}.
    c                 C   s$   |  ttjd}| t|d dS )zo
        L{runner.filenameToModule} raises a C{ValueError} when a non-existing
        file is passed.
        itz'it' doesn't existN)r    r)   r   filenameToModuleassertEqualstr)r   errr   r   r   test_notFileJ   s   zFileTests.test_notFilec                 C   s.   t ttd}ddlm} | || dS )z
        If the file in question is a module on the Python path, then it should
        properly import and return that module.
        z	sample.pyr   sampleN)r   r4   r   r'   r(   twisted.trial.testr:   r5   r   sample1sample2r   r   r   test_moduleInPathR   s   zFileTests.test_moduleInPathc                 C   sr   |  | j ttj| jdd}| |j	d | 
  |  | j ddlm} | || | |j|j dS )a  
        If passed the path to a file containing the implementation of a
        module within a package which is not on the import path,
        L{runner.filenameToModule} returns a module object loosely
        resembling the module defined by that file anyway.
        goodpackageztest_sample.pyzgoodpackage.test_sampler   )test_sampleN)mangleSysPatholdPathr   r4   osr*   joinparentr5   r.   cleanUpModulesnewPathr@   rA   assertIsNot__spec__r<   r   r   r   test_moduleNotInPath\   s   zFileTests.test_moduleNotInPathc                 C   s,   t tj| jd}| |tjd  dS )z
        If the file in question is a package on the Python path, then it should
        properly import and return that package.
        r@   N)	r   r4   rD   r*   rE   rF   assertIssysmodules)r   package1r   r   r   test_packageInPathq   s   zFileTests.test_packageInPathc                 C   sl   |  | j ttj| jd}| |j	d | 
  |  | j ddl}| || | |j|j dS )z
        If passed the path to a directory which represents a package which
        is not on the import path, L{runner.filenameToModule} returns a
        module object loosely resembling the package defined by that
        directory anyway.
        r@   r   N)rB   rC   r   r4   rD   r*   rE   rF   r5   r.   rG   rH   r@   rI   rJ   )r   rO   r@   r   r   r   test_packageNotInPathz   s   zFileTests.test_packageNotInPathc                 C   sH   t | jd}|  | ttj|j	}| 
t||j	d dS )z
        L{runner.filenameToModule} raises a C{ValueError} when the name of an
        empty directory is passed that isn't considered a valid Python package
        because it doesn't contain a C{__init__.py} file.
        emptyDirectoryz is not a package directoryN)r   FilePathrF   childcreateDirectoryr    r)   r   r4   r*   r5   r6   )r   emptyDirr7   r   r   r   test_directoryNotPackage   s   z"FileTests.test_directoryNotPackagec                 C   s2   t | jd}|d | ttj|j	 dS )zn
        L{runner.filenameToModule} raises a C{SyntaxError} when a non-Python
        file is passed.
        	notpythons   This isn't pythonN)
r   rS   rF   rT   
setContentr    SyntaxErrorr   r4   r*   )r   filenamer   r   r   test_filenameNotPython   s   
z FileTests.test_filenameNotPythonc                 C   sZ   t | jd}|tjd zt	|j
}| |j
|j W |  dS |  w )zX
        The C{__file__} attribute of the module should match the package name.
        zgoodpackage.pyutf8N)r   rS   rF   rT   rY   r   
testModuleencoder   r4   r*   r5   r(   remove)r   r[   moduler   r   r   test_filenameMatchesPackage   s   z%FileTests.test_filenameMatchesPackagec                 C   s   t | jd}|  |dd z)t|j}| 	|j
d t|jtjj }| 	|j
d W |  dS |  w )z
        Test loader against a filesystem directory containing an empty
        C{__init__.py} file. It should handle 'path' and 'path/' the same way.
        goodDirectory__init__.py    N)r   rS   rF   rT   rU   rY   r   r4   r*   
assertTruer.   endswithrD   sepr`   )r   goodDirra   r   r   r   test_directory   s   zFileTests.test_directoryN)r.   r/   r0   __doc__r8   r?   rK   rP   rQ   rW   r\   rb   rj   r   r   r   r   r2   E   s    
		r2   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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d0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Z d<d= Z!d>d? Z"d@S )ALoaderTestsz(
    Tests for L{trial.TestLoader}.
    c                 C   s   t  | _tj|  d S r   )r   r   r   r   SysPathManglingTestr   r   r   r   r   r      s   
zLoaderTests.setUpc                    s   ddl m} | j|j}| g ddd |jD  g d}tt|t	d  fdd	| j_
| j|j}| |d
d |jD  d S )Nr   r9   )test_atest_btest_cc                 S      g | ]}|j qS r   _testMethodName.0r   r   r   r   
<listcomp>       z.LoaderTests.test_sortCases.<locals>.<listcomp>)ro   rp   rn      c                    s     |  dS )N)getshortDescription)xsortDictr   r   <lambda>   s    z,LoaderTests.test_sortCases.<locals>.<lambda>c                 S   rq   r   rr   rt   r   r   r   rv      rw   )r;   r:   r   	loadClassAlphabetTestr5   _testsdictziprangesorter)r   r:   suitenewOrderr   r}   r   test_sortCases   s   zLoaderTests.test_sortCasesc                 C   sB   | j d}t }|| | |jd | t|jd dS )z{
        Loading a test that fails and getting the result of it ends up with one
        test ran and one failure.
        z6twisted.trial.test.erroneous.TestRegularFail.test_fail   N)	r   
loadByNamer   
TestResultrunr5   testsRunlenfailures)r   r   resultr   r   r   test_loadFailure   s   
zLoaderTests.test_loadFailurec                 C   J   ddl m} | jj|jj|jg dd}| d|  | d|j dS )z
        A decorated test method for which the decorator has failed to set the
        method's __name__ correctly is loaded and its name in the class scope
        discovered.
        r   r9   )r:   DecorationTesttest_badDecoratorrF   qualNamer   r   N)	r;   r:   r   loadAnythingr   r   r5   countTestCasesrs   r   r:   r   r   r   r   test_loadBadDecorator      z!LoaderTests.test_loadBadDecoratorc                 C   r   )z
        A decorated test method for which the decorator has set the method's
        __name__ correctly is loaded and the only name by which it goes is used.
        r   r9   )r:   r   test_goodDecoratorr   r   r   N)	r;   r:   r   r   r   r   r5   r   rs   r   r   r   r   test_loadGoodDecorator   s   z"LoaderTests.test_loadGoodDecoratorc                 C   r   )z
        Load a decorated method which has been copied to a new name inside the
        class.  Thus its __name__ and its key in the class's __dict__ no
        longer match.
        r   r9   )r:   r   test_renamedDecoratorr   r   r   N)	r;   r:   r   r   r   r   r5   r   rs   r   r   r   r   test_loadRenamedDecorator  r   z%LoaderTests.test_loadRenamedDecoratorc                 C   sJ   ddl m} | j|j}| d|  | ddgdd |jD  d S )Nr   r9      test_bartest_fooc                 S   rq   r   rr   rt   r   r   r   rv     rw   z.LoaderTests.test_loadClass.<locals>.<listcomp>)r;   r:   r   r   FooTestr5   r   r   r   r   r   r   test_loadClass  s   zLoaderTests.test_loadClassc                 C   s\   ddl m} | t| jj| | t| jj|jj | t| jjd | t| jjd d S Nr   r9   string)foobar)r;   r:   r    	TypeErrorr   r   r   r   r   r:   r   r   r   test_loadNonClass  s
   zLoaderTests.test_loadNonClassc                 C   s$   ddl m} | t| jj|j d S Nr   r9   )r;   r:   r    r)   r   r   NotATestr   r   r   r   test_loadNonTestCase$  s   z LoaderTests.test_loadNonTestCasec                 C   s,   ddl m} | j|}| d|  d S )Nr   r9   
   )r;   r:   r   
loadModuler5   r   r   r   r   r   test_loadModule)  s   zLoaderTests.test_loadModulec                 C   ^   ddl m} | t| jj|j | t| jj|jj | t| jjd | t| jjd d S r   )r;   r:   r    r   r   r   r   r   r   r   r   r   test_loadNonModule/  
   zLoaderTests.test_loadNonModulec                 C   s(   dd l }| j|}| d|  d S Nr      r@   r   loadPackager5   r   r   r@   r   r   r   r   test_loadPackage7  s   zLoaderTests.test_loadPackagec                 C   r   r   )r;   r:   r    r   r   r   r   r   r   r   r   r   test_loadNonPackage=  r   zLoaderTests.test_loadNonPackagec                 C   s"   ddl m} | t| jj| d S r   )r;   r:   r    r   r   r   r   r   r   r   test_loadModuleAsPackageE  s   z$LoaderTests.test_loadModuleAsPackagec                 C   s,   dd l }| jj|dd}| d|  d S Nr   Trecurse   r   r   r   r   r   test_loadPackageRecursiveK  s   z%LoaderTests.test_loadPackageRecursivec                 C   s:   ddl m} | j|}| |j|jd jd jj d S r   )	r;   r:   r   r   r5   r.   r   r   r/   r   r   r   r   test_loadAnythingOnModuleQ  s
   z%LoaderTests.test_loadAnythingOnModulec                 C   s.   ddl m} | j|j}| d|  d S )Nr   r9   r   )r;   r:   r   r   r   r5   r   r   r   r   r   test_loadAnythingOnClassY  s   z$LoaderTests.test_loadAnythingOnClassc                 C   s<   dd l }| j|}| t|| jj | d|  d S r   r@   r   r   rf   
isinstancesuiteFactoryr5   r   r   r   r   r   test_loadAnythingOnPackage_  s   z&LoaderTests.test_loadAnythingOnPackagec                 C   s@   dd l }| jj|dd}| t|| jj | d|  d S r   r   r   r   r   r   #test_loadAnythingOnPackageRecursivef  s   z/LoaderTests.test_loadAnythingOnPackageRecursivec                 C   r$   )Nr@   )r    r   r   r   r   r   r   r   test_loadAnythingOnStringm  s   z%LoaderTests.test_loadAnythingOnStringc                 C   sx   dd l }| jj|dd}t }|| | d|  | dt|j	 dd |j	D }|
  | |dd	g d S )
Nr   Tr   Fr   c                 S   s   g | ]\}}|  qS r   )r   )ru   r   errorr   r   r   rv   z  s    z1LoaderTests.test_importErrors.<locals>.<listcomp>zpackage.test_bad_modulezpackage.test_import_module)packager   r   r   Reporterr   r5   wasSuccessfulr   errorssort)r   r   r   r   r   r   r   r   test_importErrorsr  s   
zLoaderTests.test_importErrorsc                 C   sB   G dd dt j}| j|}t }|| | |  dS )z
        L{TestLoader.loadClass} returns a suite with each test method
        represented by a different instances of the L{TestCase} they are
        defined on.
        c                   @   s   e Zd Zdd Zdd ZdS )z>LoaderTests.test_differentInstances.<locals>.DistinctInstancesc                 S   s
   d| _ d S )Ntest1Run)firstr   r   r   r   test_1  s   
zELoaderTests.test_differentInstances.<locals>.DistinctInstances.test_1c                 S   s   |  t| d d S )Nr   )assertFalsehasattrr   r   r   r   test_2  s   zELoaderTests.test_differentInstances.<locals>.DistinctInstances.test_2N)r.   r/   r0   r   r   r   r   r   r   DistinctInstances  s    r   N)	pyunitTestCaser   r   r   r   r   rf   r   )r   r   r   r   r   r   r   test_differentInstances  s
   
z#LoaderTests.test_differentInstancesc                 C   @   ddl m} | j|}| d|  | dt|dd dS )zp
        Check that C{test_suite} is used when present and other L{TestCase}s are
        not included.
        r   )mockcustomsuiteMyCustomSuitenameN)r;   r   r   r   r5   r   getattr)r   r   r   r   r   r   test_loadModuleWith_test_suite     z*LoaderTests.test_loadModuleWith_test_suitec                 C   r   )zo
        Check that C{testSuite} is used when present and other L{TestCase}s are
        not included.
        r   mockcustomsuite2r   r   N)r;   r   r   r   r5   r   r   )r   r   r   r   r   r   test_loadModuleWith_testSuite  r   z)LoaderTests.test_loadModuleWith_testSuitec                 C   s0   ddl m} | j|}| dt|dd dS )z
        Check that if C{testSuite} and C{test_suite} are both present in a
        module then C{testSuite} gets priority.
        r   )mockcustomsuite3	testSuiter   N)r;   r   r   r   r5   r   )r   r   r   r   r   r   test_loadModuleWithBothCustom  s   z)LoaderTests.test_loadModuleWithBothCustomc                 C   sJ   dd }ddl m} ||j|_}z| t| jj| W ||_dS ||_w )zz
        Make sure that any C{AttributeError}s raised by C{testSuite} are not
        swallowed by L{TestLoader}.
        c                   S   s   t d)Nzshould be reraised)r!   r   r   r   r   r     s   zBLoaderTests.test_customLoadRaisesAttributeError.<locals>.testSuiter   r   N)r;   r   r   r    r!   r   r   )r   r   r   originalr   r   r   #test_customLoadRaisesAttributeError  s   z/LoaderTests.test_customLoadRaisesAttributeErrorc                 C   s0   t |}t |}|  |  | || d S r   )r   r   r5   )r   test1test2names1names2r   r   r   assertSuitesEqual  s
   zLoaderTests.assertSuitesEqualc                 C   s4   d}| j ||gd}| j |d}| || dS )z@
        Check that loadByNames ignores duplicate names
        twisted.trial.test.test_logTN)r   loadByNamesr   r   )r   ra   suite1suite2r   r   r   test_loadByNamesDuplicate  s   z%LoaderTests.test_loadByNamesDuplicatec                 C   s@   g d}| j |}tt| j j|}| t|t| dS )zX
        L{TestLoader.loadByNames} preserves the order of tests provided to it.
        )$inheritancepackage.test_x.A.test_fooztwisted.trial.test.sampler@   r   z!twisted.trial.test.sample.FooTestpackage.test_moduleN)r   r   r   	TestSuitemapr   r5   r   r   rN   r   r   r   r   r   test_loadByNamesPreservesOrder  s   z*LoaderTests.test_loadByNamesPreservesOrderc                 C   8   ddg}| j |}tt| j j|}| || dS )zM
        Check that loadByNames loads all the names that it is given
        r@   r   Nr   r   r   r   r   r   r   r   r   r   r   test_loadDifferentNames  s   z#LoaderTests.test_loadDifferentNamesc                 C   r   )zu
        Check that test methods names which are inherited from are all
        loaded rather than just one.
        r   z$inheritancepackage.test_x.B.test_fooNr   )r   methodsr   r   r   r   r   test_loadInheritedMethods  s   z%LoaderTests.test_loadInheritedMethodsN)#r.   r/   r0   rk   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rl      sB    	
		rl   c                   @   s   e Zd Zdd ZdS )ZipLoadingTestsc                 C   sN   ddl m} t|  || j| jd  |  jd7  _| | j| jg  d S )Nr   )zipitz.zip) twisted.python.test.test_zippathr  rl   r   rF   rB   rC   )r   r  r   r   r   r     s
   
zZipLoadingTests.setUpN)r.   r/   r0   r   r   r   r   r   r     s    r   c                   @   s:   e Zd Zdd Zdd ZejfddZdd Zd	d
 Z	dS )PackageOrderingTestsc                 C   sl   t  | _|  | _tj| jd}t| t	tj|dd
  tj| | | | j| jg  d S )Nuberpackagerd   wb)r   r   r   mktemptopDirrD   r*   rE   makedirsopencloser   rm   r   rB   rC   )r   rF   r   r   r   r     s   


zPackageOrderingTests.setUpc                 c   s    t d}g }| D ]}|jdd dr|| qt||d}|D ]S}|d}|du r5|V  q&g }| D ]}	t	
|	 rI||	 q;t||d}
|
D ]&}g }| D ]}|jdd drl|| qZt||d}|E dH  qRq&dS )a  
        Right now, halfway by accident, trial sorts like this:

            1. all modules are grouped together in one list and sorted.

            2. within each module, the classes are grouped together in one list
               and sorted.

            3. finally within each class, each test method is grouped together
               in a list and sorted.

        This attempts to return a sorted list of testable thingies following
        those rules, so that we can compare the behavior of loadPackage.

        The things that show as 'cases' are errors from modules which failed to
        import, and test methods.  Let's gather all those together.
        r  .ry   test_)keyNr   )r   walkModulesr   split
startswithr   sortedloaditerAttributesr   
isTestCase)r   r   pkgtestModulesr^   sortedModulesmodinfora   testClassesattribsortedClassesclsinfotestMethodsattrsortedMethodsr   r   r   _trialSortAlgorithm  s8   



z(PackageOrderingTests._trialSortAlgorithmc           	   
   C   s   ddl }|| j_| jj|dd}t|t}tt|}t| 	|}t
t||D ]\}\}}| |j| d||j| f  q*| t|t| dS )zG
        Verify that packages are loaded in the correct order.
        r   NTr   z#%d: %s != %s)r  r   r   r   r	   decorater   listr
   r   	enumerater   r5   r   r   r   )	r   r   r  r   resultingTestsmanifestnumbermanifestTest
actualTestr   r   r   loadSortedPackages;  s   z'PackageOrderingTests.loadSortedPackagesc                 C   s   |    d S r   r)  r   r   r   r   test_sortPackagesDefaultOrderR  s   z2PackageOrderingTests.test_sortPackagesDefaultOrderc                 C   s   dd }|  | d S )Nc                 S   s    t | }t|d }|S )Nr]   )r   r   r   r_   	hexdigest)sndr   r   r   sillySorterV  s   
zEPackageOrderingTests.test_sortPackagesSillyOrder.<locals>.sillySorterr*  )r   r0  r   r   r   test_sortPackagesSillyOrderU  s   	z0PackageOrderingTests.test_sortPackagesSillyOrderN)
r.   r/   r0   r   r   r   r   r)  r+  r1  r   r   r   r   r    s    	+r  )rk   rD   rM   r	   r   hashlibr   twisted.pythonr   r   twisted.python.modulesr   twisted.python.reflectr   twisted.trialr   r   twisted.trial._asyncrunnerr
   twisted.trial.itrialr   r;   r   r   rm   r   r2   rl   r   r  r   r   r   r   <module>   s(   
${  >
