U
    
W[1  ã                   @   sp   d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	 e 
¡ sLddlmZ G dd„ deƒZeƒ  e ¡ ¡ d	S )
z&
Tests for L{twisted.internet.stdio}.
é    )Úabsolute_importÚdivision)Úplatform)ÚReactorBuilder)ÚProtocol)Ú
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Ú	extraFileÚ
addCleanup)ÚselfÚpath© r   úB/usr/lib/python3/dist-packages/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   |   ¡ }|  | j|¡ |  ¡ }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   ÚunbuildReactorr
   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   sˆ   |   ¡ }|  | j|¡ 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   r3   r   r
   r(   r   r   r%   r   r0   r$   r4   Ú_writerÚ
getWritersZstopWritingr7   )r   r   r(   r)   r9   r   r   r   Útest_removeWriterr   s    
þ
z!StdioFilesTests.test_removeWriterc                 C   s„   |   ¡ }|  | j|¡ |  ¡ }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   r3   r
   r   r   r(   r   r   r%   r   Z	removeAllr4   r5   r7   r6   )r   r   r   r(   r9   Zremovedr   r   r   Útest_removeAllˆ   s     ÿzStdioFilesTests.test_removeAllc              	   C   st   |   ¡ }|  | j|¡ |  ¡ }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   r3   r
   r   r   r   r   r%   r   r4   r5   r6   r8   r   r   r   Útest_getReadersŸ   s     ÿzStdioFilesTests.test_getReadersc                 C   sx   |   ¡ }|  | j|¡ 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   r3   r   r
   r(   r   r   r%   r   r7   r;   r<   ZstartWritingr4   )r   r   r(   r9   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*   r2   r:   r=   r>   r?   r@   r   Ú	isWindowsÚskipr   r   r   r   r      s   !r   N)rA   Z
__future__r   r   Ztwisted.python.runtimer   Z#twisted.internet.test.reactormixinsr   Ztwisted.internet.protocolr   rB   Ztwisted.internet.stdior   r   ÚglobalsÚupdateZmakeTestCaseClassesr   r   r   r   Ú<module>   s    5