U
    c6{i  ã                   @   sJ   d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	 G dd„ dƒZ
dS )z\
Facilities for helping test code which interacts with Python's module system
to load code.
é    N)Ú
ModuleType)ÚIterableÚListÚTuple)ÚFilePathc                   @   sV   e Zd ZdZee ddœdd„Zeeee	f  ddœdd„Z
deee d
œdd„ZdS )ÚTwistedModulesMixinz‰
    A mixin for C{twisted.trial.unittest.SynchronousTestCase} providing useful
    methods for manipulating Python's module system.
    N)ÚsysPathÚreturnc                    s<   t jdd… ‰ ddœ‡ fdd„}|  |¡ |t jdd…< dS )zW
        Replace sys.path, for the duration of the test, with the given value.
        N©r	   c                      s   ˆ t jd d …< d S ©N)ÚsysÚpath© ©ZoriginalSysPathr   úG/tmp/pip-unpacked-wheel-hfbvu0bz/twisted/python/test/modules_helpers.pyÚcleanUpSysPath   s    z:TwistedModulesMixin.replaceSysPath.<locals>.cleanUpSysPath)r   r   Ú
addCleanup)Úselfr   r   r   r   r   ÚreplaceSysPath   s    
z"TwistedModulesMixin.replaceSysPath)Ú
sysModulesr	   c                    s@   t j ¡ ‰ ddœ‡ fdd„}|  |¡ t j ¡  t j |¡ dS )zZ
        Replace sys.modules, for the duration of the test, with the given value.
        Nr
   c                      s   t j ¡  t j ˆ ¡ d S r   )r   ÚmodulesÚclearÚupdater   ©ZoriginalSysModulesr   r   ÚcleanUpSysModules(   s    
z@TwistedModulesMixin.replaceSysModules.<locals>.cleanUpSysModules)r   r   Úcopyr   r   r   )r   r   r   r   r   r   ÚreplaceSysModules"   s
    


z%TwistedModulesMixin.replaceSysModulesÚtest_package)Úpkgnamer	   c                 C   s2   t |  ¡ ƒ}| d¡}| ¡  | d¡ d¡ |S )z‰
        Generate a L{FilePath} with one package, named C{pkgname}, on it, and
        return the L{FilePath} of the path entry.
        r   z__init__.pyó    )r   ÚmktempÚchildÚmakedirsZ
setContent)r   r   ÚentryÚpkgr   r   r   ÚpathEntryWithOnePackage0   s
    
z+TwistedModulesMixin.pathEntryWithOnePackage)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ústrr   r   r   r   r   r   r%   r   r   r   r   r      s   r   )r)   r   Útypesr   Útypingr   r   r   Ztwisted.python.filepathr   r   r   r   r   r   Ú<module>   s
   