o
    ¯b  ã                   @   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                   @   sX   e Zd ZdZdee ddfdd„Zdeeee	f  ddfdd	„Z
ddedefdd„ZdS )ÚTwistedModulesMixinz‰
    A mixin for C{twisted.trial.unittest.SynchronousTestCase} providing useful
    methods for manipulating Python's module system.
    ÚsysPathÚreturnNc                    s8   t j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.
        Nr	   c                      s   ˆ t jd d …< d S ©N)ÚsysÚpath© ©ÚoriginalSysPathr   úE/usr/lib/python3/dist-packages/twisted/python/test/modules_helpers.pyÚcleanUpSysPath   s   z:TwistedModulesMixin.replaceSysPath.<locals>.cleanUpSysPath©r	   N)r   r   Ú
addCleanup)Úselfr   r   r   r   r   ÚreplaceSysPath   s   
z"TwistedModulesMixin.replaceSysPathÚ
sysModulesc                    s<   t j ¡ ‰ d‡ fdd„}|  |¡ t j ¡  t j |¡ dS )zZ
        Replace sys.modules, for the duration of the test, with the given value.
        r	   Nc                      s   t j ¡  t j ˆ ¡ d S r
   )r   ÚmodulesÚclearÚupdater   ©ÚoriginalSysModulesr   r   ÚcleanUpSysModules(   s   
z@TwistedModulesMixin.replaceSysModules.<locals>.cleanUpSysModulesr   )r   r   Úcopyr   r   r   )r   r   r   r   r   r   ÚreplaceSysModules"   s
   


z%TwistedModulesMixin.replaceSysModulesÚtest_packageÚpkgnamec                 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ÚmakedirsÚ
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   Útwisted.python.filepathr   r   r   r   r   r   Ú<module>   s   