U
    ¸~Hj·  ã                   @   s`   d Z ddlmZ ddlmZ ddlmZ e ¡ s<ddlm	Z	 G dd„ deƒZ
eƒ  e
 ¡ ¡ dS )	z&
Tests for L{twisted.internet.stdio}.
é    )ÚProtocol)ÚReactorBuilder)Úplatform)Ú
StandardIOc                   @   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e ¡ rXdZdS )ÚStdioFilesTestszI
    L{StandardIO} supports reading and writing to filesystem files.
    c                 C   s4   |   ¡ }t|dƒ ¡  t|dƒ| _|  | jj¡ d S )NÚwbzrb+)ÚmktempÚopenÚcloseÚ	extraFileZ
addCleanup)ÚselfÚpath© r   úD/tmp/pip-unpacked-wheel-ewod1tgb/twisted/internet/test/test_stdio.pyÚsetUp   s    zStdioFilesTests.setUpc              	      s–   |   ¡ ‰ G ‡ fdd„dtƒ}|  ¡ }t|dƒ�}| d¡ W 5 Q R X t|dƒ�0}|ƒ }t|| ¡ | j ¡ ˆ d� |  ˆ ¡ W 5 Q R X |  	|j
d¡ dS )zc
        Adding a filesystem file reader to a reactor will make sure it is
        polled.
        c                       s   e Zd ZdZ‡ fdd„ZdS )z4StdioFilesTests.test_addReader.<locals>.DataProtocoló    c                    s$   |  j |7  _ | j dkr ˆ  ¡  d S )Nó   hello!)ÚdataÚstop)r   r   ©Úreactorr   r   ÚdataReceived&   s    
zAStdioFilesTests.test_addReader.<locals>.DataProtocol.dataReceivedN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   ÚDataProtocol#   s   r   r   r   Úrb©ÚstdinÚstdoutr   N)ÚbuildReactorr   r   r	   Úwriter   Úfilenor   Ú
runReactorÚassertEqualr   )r   r   r   ÚfÚprotocolr   r   r   Útest_addReader   s    üzStdioFilesTests.test_addReaderc              	      s°   |   ¡ ‰ G ‡ fdd„dtƒ}|  ¡ }t|dƒ�R}|ƒ }t|| ¡ | j ¡ ˆ d� |j d¡ |j d¡ |j 	¡  |  
ˆ ¡ W 5 Q R X t|dƒ�}|  | ¡ d¡ W 5 Q R X d	S )
zc
        Adding a filesystem file writer to a reactor will make sure it is
        polled.
        c                       s   e Zd Z‡ fdd„ZdS )z:StdioFilesTests.test_addWriter.<locals>.DisconnectProtocolc                    s   ˆ   ¡  d S )N)r   )r   Úreasonr   r   r   ÚconnectionLostH   s    zIStdioFilesTests.test_addWriter.<locals>.DisconnectProtocol.connectionLostN)r   r   r   r)   r   r   r   r   ÚDisconnectProtocolG   s   r*   r   ©r   r   r   ó   hellos   , worldr   s   hello, worldN)r    r   r   r	   r   r"   r   Ú	transportr!   ZloseConnectionr#   r$   Úread)r   r*   r   r%   r&   r   r   r   Útest_addWriter@   s"    ü
zStdioFilesTests.test_addWriterc              	   C   s‚   |   ¡ }|  ¡ }t|dƒ ¡  t|dƒ�P}ttƒ | ¡ | j ¡ |d�}|  |j	| 
¡ ¡ |j	 ¡  |  |j	| 
¡ ¡ W 5 Q R X dS )zq
        Removing a filesystem file reader from a reactor will make sure it is
        no longer polled.
        r   r   r   N)r    r   r	   r
   r   r   r"   r   ÚassertInÚ_readerÚ
getReadersZstopReadingÚassertNotIn©r   r   r   r%   Ústdior   r   r   Útest_removeReader`   s    ü
z!StdioFilesTests.test_removeReaderc                 C   sz   |   ¡ }t|  ¡ dƒ | _}tƒ }t|| ¡ | j ¡ |d�}|j 	d¡ |  
|j| ¡ ¡ |j ¡  |  |j| ¡ ¡ dS )zq
        Removing a filesystem file writer from a reactor will make sure it is
        no longer polled.
        r   r+   r,   N)r    r	   r   r%   r   r   r"   r   r-   r!   r0   Ú_writerÚ
getWritersZstopWritingr3   )r   r   r%   r&   r5   r   r   r   Útest_removeWriterv   s       ÿ
z!StdioFilesTests.test_removeWriterc                 C   sv   |   ¡ }|  ¡ }t|dƒ ¡  t|dƒ | _}ttƒ | ¡ | j ¡ |d�}| 	¡ }|  
|j|¡ |  |j| ¡ ¡ dS )zk
        Calling C{removeAll} on a reactor includes descriptors that are
        filesystem files.
        r   r   r   N)r    r   r	   r
   r%   r   r   r"   r   Z	removeAllr0   r1   r3   r2   )r   r   r   r%   r5   Úremovedr   r   r   Útest_removeAllŠ   s    üzStdioFilesTests.test_removeAllc              	   C   sf   |   ¡ }|  ¡ }t|dƒ ¡  t|dƒ�4}ttƒ | ¡ | j ¡ |d�}|  |j	| 
¡ ¡ W 5 Q R X dS )zW
        C{reactor.getReaders} includes descriptors that are filesystem files.
        r   r   r   N)r    r   r	   r
   r   r   r"   r   r0   r1   r2   r4   r   r   r   Útest_getReaders£   s    üzStdioFilesTests.test_getReadersc                 C   sj   |   ¡ }t|  ¡ dƒ | _}ttƒ | ¡ | j ¡ |d�}|  |j	| 
¡ ¡ |j	 ¡  |  |j	| 
¡ ¡ dS )zW
        C{reactor.getWriters} includes descriptors that are filesystem files.
        r   r+   N)r    r	   r   r%   r   r   r"   r   r3   r7   r8   ZstartWritingr0   )r   r   r%   r5   r   r   r   Útest_getWriters·   s    ü
zStdioFilesTests.test_getWriterszoStandardIO does not accept stdout as an argument to Windows. Testing redirection to a file is therefore harder.N)r   r   r   Ú__doc__r   r'   r/   r6   r9   r;   r<   r=   r   Ú	isWindowsÚskipr   r   r   r   r      s   $ ÿr   N)r>   Ztwisted.internet.protocolr   Z#twisted.internet.test.reactormixinsr   Ztwisted.python.runtimer   r?   Ztwisted.internet.stdior   r   ÚglobalsÚupdateZmakeTestCaseClassesr   r   r   r   Ú<module>   s    B