B
    u9a%  ã               @   sð   d dl Z d dlZd dlZd dlmZmZmZ d dlmZ d dlm	Z	 yd dl
Z
W n ek
rh   dZ
Y nX yd dlZW n ek
rŽ   dZY nX e d¡G dd„ dejƒƒZG dd	„ d	ejƒZe e
d
¡G dd„ dejƒƒZedkrìe ¡  dS )é    N)ÚBytesIOÚStringIOÚTextIOWrapper)Úmock)Úsupportz
os.environc               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚGetpassGetuserTestc             C   s    d}||j _|  |t ¡ ¡ d S )NÚ	some_name)ÚgetÚreturn_valueÚassertEqualÚgetpassÚgetuser)ÚselfÚenvironÚexpected_name© r   ú"/usr/lib/python3.7/test_getpass.pyÚ%test_username_takes_username_from_env   s    z8GetpassGetuserTest.test_username_takes_username_from_envc             C   sH   d |j _yt ¡  W n tk
r(   Y nX |  |j jdd„ dD ƒ¡ d S )Nc             S   s   g | ]}t  |¡‘qS r   )r   Zcall)Ú.0Úxr   r   r   ú
<listcomp>!   s    zMGetpassGetuserTest.test_username_priorities_of_env_values.<locals>.<listcomp>)ZLOGNAMEZUSERZLNAMEZUSERNAME)r	   r
   r   r   ÚImportErrorr   Zcall_args_list)r   r   r   r   r   Ú&test_username_priorities_of_env_values   s    z9GetpassGetuserTest.test_username_priorities_of_env_valuesc          
   C   sx   d}d |j _trft d¡D}t d¡.}d|_|g|_|  |t ¡ ¡ | d¡ W d Q R X W d Q R X n|  	t
tj¡ d S )Nr   z	os.getuidzpwd.getpwuidé*   )r	   r
   Úpwdr   Úpatchr   r   r   Úassert_called_once_withÚassertRaisesr   )r   r   r   ZuidZgetpwr   r   r   Útest_username_falls_back_to_pwd#   s    
 z2GetpassGetuserTest.test_username_falls_back_to_pwdN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   r      s   
r   c               @   sP   e Zd Zdd„ Zdd„ Ze d¡dd„ ƒZe d¡dd	„ ƒZd
d„ Z	dd„ Z
dS )ÚGetpassRawinputTestc             C   s2   t jtd}tdƒ}tjd||d |j ¡  d S )N)ÚspecÚinput_stringÚsome_prompt)Úinput)r   ÚMockr   r   Ú
_raw_inputÚflushr   )r   Ústreamr&   r   r   r   Ú test_flushes_stream_after_prompt4   s    z4GetpassRawinputTest.test_flushes_stream_after_promptc          	   C   s@   t dƒ}d}t d¡ }tj||d |j |¡ W d Q R X d S )Nr$   r%   z
sys.stderr)r&   )r   r   r   r   r(   Úwriter   )r   r&   ÚpromptÚstderrr   r   r   Útest_uses_stderr_as_default;   s
    z/GetpassRawinputTest.test_uses_stderr_as_defaultz	sys.stdinc             C   s$   d|j _tjtƒ d |j  ¡  d S )Nr$   )r*   )Úreadliner
   r   r(   r   r   )r   Ú
mock_inputr   r   r   Ú test_uses_stdin_as_default_inputB   s    z4GetpassRawinputTest.test_uses_stdin_as_default_inputc             C   s2   t tƒ dd}d|j_tjd|d |j ¡  d S )NÚascii)Úencodingu   HasÃ…â€šo: )r-   r*   )r   r   r0   r
   r   r(   r   )r   r1   r*   r   r   r   Ú#test_uses_stdin_as_different_localeH   s    z7GetpassRawinputTest.test_uses_stdin_as_different_localec             C   s   t dƒ}| jttj|d d S )NÚ )r&   )r   r   ÚEOFErrorr   r(   )r   r&   r   r   r   Útest_raises_on_empty_inputP   s    z.GetpassRawinputTest.test_raises_on_empty_inputc             C   s    t dƒ}|  dtj|d¡ d S )Nztest
Útest)r&   )r   r   r   r(   )r   r&   r   r   r   Útest_trims_trailing_newlineT   s    z/GetpassRawinputTest.test_trims_trailing_newlineN)r   r    r!   r+   r/   r   r   r2   r5   r8   r:   r   r   r   r   r"   2   s   r"   z!tests require system with termiosc               @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚUnixGetpassTestc             C   s‚   t  d¡n}t  d¡X}t  d¡B}d |_t ¡  | dtjtjB ¡ | |jd¡ | |j¡ W d Q R X W d Q R X W d Q R X d S )Nzos.openz	io.FileIOzio.TextIOWrapperz/dev/ttyzw+)	r   r   r
   r   Úunix_getpassr   ÚosÚO_RDWRÚO_NOCTTY)r   ÚopenÚfileioÚtextior   r   r   Útest_uses_tty_directlya   s    z&UnixGetpassTest.test_uses_tty_directlyc             C   s¨   t  d¡”}t  d¡~ t  d¡h t  d¡R}t  d¡<}d|_dddddg}t|ƒ|_t ¡  | dt j|¡ W d Q R X W d Q R X W d Q R X W d Q R X W d Q R X d S )Nzos.openz	io.FileIOzio.TextIOWrapperztermios.tcgetattrztermios.tcsetattré   éÿ   )r   r   r
   Úlistr   r<   Úassert_called_withÚANY)r   r@   Z	tcgetattrÚ	tcsetattrZ
fake_attrsr   r   r   Útest_resets_termioso   s    
z#UnixGetpassTest.test_resets_termiosc             C   s´   t  d¡ }t  d¡Š}t  d¡t}t  d¡^ t  d¡H}t  d¡2}d|_tƒ |_tj|_t ¡  | 	d|j¡ W d Q R X W d Q R X W d Q R X W d Q R X W d Q R X W d Q R X d S )	Nzos.openz	io.FileIOzio.TextIOWrapperztermios.tcgetattrztermios.tcsetattrzgetpass.fallback_getpassrD   z
Password: )
r   r   r
   r   ÚtermiosÚerrorÚside_effectr   r<   r   )r   r@   rA   rB   rI   Úfallbackr   r   r   Ú-test_falls_back_to_fallback_if_termios_raises{   s    z=UnixGetpassTest.test_falls_back_to_fallback_if_termios_raisesc             C   sš   t  d¡†}t  d¡p t  d¡Z t  d¡D t  d¡. d|_t jtd}tj|d |j ¡  W d Q R X W d Q R X W d Q R X W d Q R X W d Q R X d S )	Nzos.openz	io.FileIOzio.TextIOWrapperztermios.tcgetattrztermios.tcsetattrrD   )r#   )r*   )	r   r   r
   r'   r   r   r<   r)   rG   )r   r@   Zmock_streamr   r   r   Útest_flushes_stream_after_input‰   s    z/UnixGetpassTest.test_flushes_stream_after_inputc             C   s    t  d¡Œ}t jdtdr}t|_t|j_t ¡ &}|  	t
j¡ t
 ¡  W d Q R X W d Q R X |j ¡  |  d| ¡ ¡ |  d| ¡ ¡ W d Q R X W d Q R X d S )Nzos.openz	sys.stdin)r#   ÚWarningz	Password:)r   r   r   ÚIOErrorrM   ÚAttributeErrorÚfilenor   Zcaptured_stderrZassertWarnsr   ZGetPassWarningr<   r0   r   ZassertInÚgetvalue)r   Zos_openÚstdinr.   r   r   r   Útest_falls_back_to_stdin•   s    

z(UnixGetpassTest.test_falls_back_to_stdinN)r   r    r!   rC   rJ   rO   rP   rW   r   r   r   r   r;   ^   s
   r;   Ú__main__)r   r=   ZunittestÚior   r   r   r   r9   r   rK   r   r   r   ZTestCaser   r"   Z
skipUnlessr;   r   Úmainr   r   r   r   Ú<module>   s(   


 ,
C