U
    sÍ@g}0  ã                   @   sà   d Z ddlZddlmZ ddlm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mZmZmZ G d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZeddd„ iƒZeddd„ iƒZG dd„ deƒZG dd„ deƒZdS )z3
Tests for parts of our release automation system.
é    N)Úparse_requirements)ÚDistribution)ÚTestCase)Ú_setupÚfilepath)Ú_PY3)ÚBuildPy3ÚgetSetupArgsÚConditionalExtensionÚ_EXTRAS_REQUIREc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú
SetupTestsz$
    Tests for L{getSetupArgs}.
    c                 C   s€   t ddgdd„ d}t ddgdd„ d}t||gd}|  |d ||g¡ |d	 }|d
 }|tƒ ƒ}| ¡  |  |j|g¡ dS )z‚
        Will return the arguments with a custom build_ext which knows how to
        check whether they should be built.
        Úwhateverú
whatever.cc                 S   s   dS )NT© ©Úbr   r   ú@/usr/lib/python3/dist-packages/twisted/python/test/test_setup.pyÚ<lambda>'   ó    z7SetupTests.test_conditionalExtensions.<locals>.<lambda>)Z	conditionc                 S   s   dS )NFr   r   r   r   r   r   )   r   ©Ú
extensionsZext_modulesÚcmdclassÚ	build_extN)r
   r	   ÚassertEqualr   Úprepare_extensionsr   )ÚselfZgood_extZbad_extÚargsr   r   Úbuilderr   r   r   Útest_conditionalExtensions!   s    ÿÿ
z%SetupTests.test_conditionalExtensionsc                 C   s\   t ddgdgd}t|gd}|d d tƒ ƒ}|  tdd	¡ | ¡  |  |jdd
g¡ dS )zn
        When building on Windows NT, the WIN32 macro will be defined as 1 on
        the extensions.
        r   r   )r   é   )Údefine_macrosr   r   r   ÚnameÚnt)ZWIN32é   N)r
   r	   r   ÚpatchÚosr   r   r    )r   Zextr   r   r   r   r   Útest_win32Definition7   s    ÿzSetupTests.test_win32DefinitionN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r&   r   r   r   r   r      s   r   c                   @   sp   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S )ÚOptionalDependenciesTestsz&
    Tests for L{_EXTRAS_REQUIRE}
    c                 C   s@   t dgd}t |d}t|ƒ}dd„ }|  ||ƒ||jƒ¡ dS )aÍ  
        Setuptools' Distribution object parses and stores its C{extras_require}
        argument as an attribute.

        Requirements for install_requires/setup_requires can specified as:
         * a single requirement as a string, such as:
           {'im_an_extra_dependency': 'thing'}
         * a series of requirements as a list, such as:
           {'im_an_extra_dependency': ['thing']}
         * a series of requirements as a multi-line string, such as:
           {'im_an_extra_dependency': '''
                                      thing
                                      '''}

        The extras need to be parsed with pkg_resources.parse_requirements(),
        which returns a generator.
        Úthing)Zim_an_extra_dependency)Úextras_requirec                 S   s*   i }|   ¡ D ]\}}tt|ƒƒ||< q|S )N)ÚitemsÚlistr   )ZmyExtrasZparsedExtrasr!   Úvalr   r   r   ÚcanonicalizeExtrasc   s    zWOptionalDependenciesTests.test_distributeTakesExtrasRequire.<locals>.canonicalizeExtrasN)Údictr   r   r-   )r   ZextrasÚattrsÚdistributionr1   r   r   r   Ú!test_distributeTakesExtrasRequireM   s    
þz;OptionalDependenciesTests.test_distributeTakesExtrasRequirec                 C   s|   |   dt¡ |   dt¡ |   dt¡ |   dt¡ |   dt¡ |   dt¡ |   dt¡ |   dt¡ |   d	t¡ |   d
t¡ dS )zÏ
        L{_EXTRAS_REQUIRE} contains options for all documented extras: C{dev},
        C{tls}, C{conch}, C{soap}, C{serial}, C{all_non_platform},
        C{macos_platform}, and C{windows_platform}.
        ÚdevÚtlsÚconchÚsoapÚserialÚall_non_platformÚmacos_platformÚosx_platformÚwindows_platformÚhttp2N©ÚassertInr   ©r   r   r   r   Ú"test_extrasRequireDictContainsKeyso   s    z<OptionalDependenciesTests.test_extrasRequireDictContainsKeysc                 C   sX   t d }|  d|¡ |  d|¡ |  d|¡ |  d|¡ tsT|  d|¡ |  d|¡ dS )	zŒ
        L{_EXTRAS_REQUIRE}'s C{dev} extra contains setuptools requirements for
        the tools required for Twisted development.
        r6   zpyflakes >= 1.0.0ztwisted-dev-tools >= 0.0.2zpython-subunitzsphinx >= 1.3.1ztwistedchecker >= 0.4.0zpydoctor >= 16.2.0N)r   rA   r   ©r   Zdepsr   r   r   Útest_extrasRequiresDevDeps   s    z4OptionalDependenciesTests.test_extrasRequiresDevDepsc                 C   s0   t d }|  d|¡ |  d|¡ |  d|¡ dS )zÒ
        L{_EXTRAS_REQUIRE}'s C{tls} extra contains setuptools requirements for
        the packages required to make Twisted's transport layer security fully
        work for both clients and servers.
        r7   úpyopenssl >= 16.0.0Úservice_identityúidna >= 0.6, != 2.3N©r   rA   rD   r   r   r   Útest_extrasRequiresTlsDeps   s    z4OptionalDependenciesTests.test_extrasRequiresTlsDepsc                 C   s0   t d }|  d|¡ |  d|¡ |  d|¡ dS )z²
        L{_EXTRAS_REQUIRE}'s C{conch} extra contains setuptools requirements
        for the packages required to make Twisted Conch's secure shell server
        work.
        r8   Úpyasn1úcryptography >= 1.5úappdirs >= 1.4.0NrI   rD   r   r   r   Útest_extrasRequiresConchDepsœ   s    z6OptionalDependenciesTests.test_extrasRequiresConchDepsc                 C   s   |   dtd ¡ dS )z§
        L{_EXTRAS_REQUIRE}' C{soap} extra contains setuptools requirements for
        the packages required to make the C{twisted.web.soap} module function.
        Úsoappyr9   Nr@   rB   r   r   r   Útest_extrasRequiresSoapDeps¨   s    þz5OptionalDependenciesTests.test_extrasRequiresSoapDepsc                 C   s   |   dtd ¡ dS )z 
        L{_EXTRAS_REQUIRE}'s C{serial} extra contains setuptools requirements
        for the packages required to make Twisted's serial support work.
        úpyserial >= 3.0r:   Nr@   rB   r   r   r   Útest_extrasRequiresSerialDeps³   s    þz7OptionalDependenciesTests.test_extrasRequiresSerialDepsc                 C   s$   t d }|  d|¡ |  d|¡ dS )z
        L{_EXTRAS_REQUIRE}'s C{http2} extra contains setuptools requirements
        for the packages required to make Twisted HTTP/2 support work.
        r?   úh2 >= 3.0, < 4.0úpriority >= 1.1.0, < 2.0NrI   rD   r   r   r   Útest_extrasRequiresHttp2Deps¾   s    z6OptionalDependenciesTests.test_extrasRequiresHttp2Depsc                 C   s„   t d }|  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d	|¡ |  d
|¡ |  d|¡ dS )zË
        L{_EXTRAS_REQUIRE}'s C{all_non_platform} extra contains setuptools
        requirements for all of Twisted's optional dependencies which work on
        all supported operating systems.
        r;   rF   rG   rH   rK   rL   rO   rQ   rM   rS   rT   NrI   rD   r   r   r   Ú%test_extrasRequiresAllNonPlatformDepsÈ   s    z?OptionalDependenciesTests.test_extrasRequiresAllNonPlatformDepsc                 C   s„   t d }|  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d	|¡ |  d
|¡ |  d|¡ dS )z¸
        L{_EXTRAS_REQUIRE}'s C{macos_platform} extra contains setuptools
        requirements for all of Twisted's optional dependencies usable on the
        macOS platform.
        r<   rF   rG   rH   rK   rL   rO   rQ   rS   rT   zpyobjc-coreNrI   rD   r   r   r   Ú$test_extrasRequiresMacosPlatformDepsÛ   s    z>OptionalDependenciesTests.test_extrasRequiresMacosPlatformDepsc                 C   s   |   td td ¡ dS )zX
        L{_EXTRAS_REQUIRE}'s C{osx_platform} is an alias to C{macos_platform}.
        r<   r=   N)r   r   rB   r   r   r   Ú$test_extrasRequireMacOSXPlatformDepsî   s    
ÿz>OptionalDependenciesTests.test_extrasRequireMacOSXPlatformDepsc                 C   s„   t d }|  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d	|¡ |  d
|¡ |  d|¡ dS )zÆ
        L{_EXTRAS_REQUIRE}'s C{windows_platform} extra contains setuptools
        requirements for all of Twisted's optional dependencies usable on the
        Microsoft Windows platform.
        r>   rF   rG   rH   rK   rL   rO   rQ   rS   rT   Zpywin32NrI   rD   r   r   r   Ú&test_extrasRequiresWindowsPlatformDepsö   s    z@OptionalDependenciesTests.test_extrasRequiresWindowsPlatformDepsN)r'   r(   r)   r*   r5   rC   rE   rJ   rN   rP   rR   rU   rV   rW   rX   rY   r   r   r   r   r+   H   s   "
r+   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú
FakeModulezF
    A fake module, suitable for dependency injection in testing.
    c                 C   s
   || _ dS )zµ
        Initializes a fake module.

        @param attrs: The attrs that will be accessible on the module.
        @type attrs: C{dict} of C{str} (Python names) to objects
        N)Ú_attrs)r   r3   r   r   r   Ú__init__  s    zFakeModule.__init__c                 C   s,   z| j | W S  tk
r&   tƒ ‚Y nX dS )z
        Gets an attribute of this fake module from its attrs.

        @raise AttributeError: When the requested attribute is missing.
        N)r[   ÚKeyErrorÚAttributeError)r   r!   r   r   r   Ú__getattr__  s    zFakeModule.__getattr__N)r'   r(   r)   r*   r\   r_   r   r   r   r   rZ   
  s   
rZ   Zpython_implementationc                   C   s   dS )NZCPythonr   r   r   r   r   r   %  r   r   c                   C   s   dS )NZlvhpyr   r   r   r   r   r   &  r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚWithPlatformTestszP
    Tests for L{_checkCPython} when used with a (fake) C{platform} module.
    c                 C   s   |   tjtd¡ dS )z€
        L{_checkCPython} returns C{True} when C{platform.python_implementation}
        says we're running on CPython.
        ©ÚplatformN)Z
assertTruer   Ú_checkCPythonÚfakeCPythonPlatformrB   r   r   r   Útest_cpython.  s    zWithPlatformTests.test_cpythonc                 C   s   |   tjtd¡ dS )z…
        L{_checkCPython} returns C{False} when C{platform.python_implementation}
        says we're not running on CPython.
        ra   N)ZassertFalser   rc   ÚfakeOtherPlatformrB   r   r   r   Ú
test_other6  s    zWithPlatformTests.test_otherN)r'   r(   r)   r*   re   rg   r   r   r   r   r`   *  s   r`   c                   @   s$   e Zd ZdZdZesdZdd„ ZdS )ÚBuildPy3Testsz 
    Tests for L{BuildPy3}.
    Nz4BuildPy3 setuptools command used with Python 3 only.c                 C   s°   t ƒ }d|_d|_t|ƒ}|  tddg¡ t tj	¡ 
¡ }| d¡ d¡}| d|j¡}|  tdd| d	¡jfdd
| d¡jfdd| d¡jfdd| d¡jfgƒt|ƒ¡ dS )zp
        Will filter the found modules excluding the modules listed in
        L{twisted.python.dist3}.
        zsetup.pyZbuild_pyZnotPortedModulesz"twisted.spread.test.test_pbfailureZspreadZtestztwisted.spread.testr\   z__init__.pyZtest_bananaztest_banana.pyZ
test_jellyztest_jelly.pyZtest_pbz
test_pb.pyN)r   Úscript_nameZscript_argsr   r$   r   r   ZFilePathÚtwistedÚ__file__ÚparentZchildZfind_package_modulesÚpathr   Úsorted)r   r4   r   ZtwistedPackageDirZ
packageDirÚresultr   r   r   Útest_find_package_modulesH  s>    ÿýÿ
ÿ
ÿ
ÿ
ÿù
öz'BuildPy3Tests.test_find_package_modules)r'   r(   r)   r*   ZmaxDiffr   Úskiprp   r   r   r   r   rh   ?  s
   rh   )r*   r%   Zpkg_resourcesr   Zsetuptools.distr   rj   Ztwisted.trial.unittestr   Ztwisted.pythonr   r   Ztwisted.python.compatr   Ztwisted.python._setupr   r	   r
   r   r   r+   ÚobjectrZ   rd   rf   r`   rh   r   r   r   r   Ú<module>   s    	, C