U
    
W[E  ã                   @   s<   d Z ddlmZmZ ddlZddlmZ G dd„ deƒZdS )z\
Facilities for helping test code which interacts with Python's module system
to load code.
é    )ÚdivisionÚabsolute_importN)ÚFilePathc                   @   s*   e Zd ZdZdd„ Zdd„ Zd
dd„Zd	S )ÚTwistedModulesMixinz‰
    A mixin for C{twisted.trial.unittest.SynchronousTestCase} providing useful
    methods for manipulating Python's module system.
    c                    s6   t j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.
        Nc                      s   ˆ t jd d …< d S ©N)ÚsysÚpath© ©ZoriginalSysPathr	   úE/usr/lib/python3/dist-packages/twisted/python/test/modules_helpers.pyÚcleanUpSysPath   s    z:TwistedModulesMixin.replaceSysPath.<locals>.cleanUpSysPath)r   r   Ú
addCleanup)ÚselfZsysPathr   r	   r
   r   ÚreplaceSysPath   s    
z"TwistedModulesMixin.replaceSysPathc                    s:   t j ¡ ‰ ‡ fdd„}|  |¡ t j ¡  t j |¡ dS )zZ
        Replace sys.modules, for the duration of the test, with the given value.
        c                      s   t j ¡  t j ˆ ¡ d S r   )r   ÚmodulesÚclearÚupdater	   ©ZoriginalSysModulesr	   r   ÚcleanUpSysModules&   s    
z@TwistedModulesMixin.replaceSysModules.<locals>.cleanUpSysModulesN)r   r   Úcopyr   r   r   )r   Z
sysModulesr   r	   r   r   ÚreplaceSysModules!   s
    


z%TwistedModulesMixin.replaceSysModulesÚtest_packagec                 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   ZmktempZchildÚmakedirsZ
setContent)r   ZpkgnameÚentryÚpkgr	   r	   r   ÚpathEntryWithOnePackage.   s
    
z+TwistedModulesMixin.pathEntryWithOnePackageN)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r	   r	   r	   r   r      s   r   )	r    Z
__future__r   r   r   Ztwisted.python.filepathr   Úobjectr   r	   r	   r	   r   Ú<module>   s   