U
    ‰d%  ã                   @   sà   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
mZ d dlmZmZmZ d dlmZ d dlmZ e jdd„ ƒZG dd	„ d	ejƒZG d
d„ deƒZG dd„ dejƒZG dd„ dejƒZdS )é    N)Úutf8)ÚLogFormatterÚdefine_logging_optionsÚenable_pretty_logging)ÚOptionParser)Úbasestring_typec                	   c   s,   t  ¡  t jdtd d V  W 5 Q R X d S )NÚignore)Úcategory)ÚwarningsÚcatch_warningsÚsimplefilterÚBytesWarning© r   r   ú9/tmp/pip-unpacked-wheel-fekwu36z/tornado/test/log_test.pyÚignore_bytes_warning    s    
r   c                   @   sV   e Zd Ze 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dS )ÚLogFormatterTestsD   (?s)\[E [0-9]{6} [0-9]{2}:[0-9]{2}:[0-9]{2} log_test:[0-9]+\] (.*)c                 C   s€   t dd| _tjdi| j_d| j_t d¡| _d| j_t	 
¡ | _tj | jd¡| _|  | j¡| _| j | j¡ | j | j¡ d S )NF)Úcolorúúr   zlog.out)r   Ú	formatterÚloggingÚERRORZ_colorsZ_normalÚLoggerÚloggerÚ	propagateÚtempfileÚmkdtempÚtempdirÚosÚpathÚjoinÚfilenameÚmake_handlerÚhandlerÚsetFormatterÚ
addHandler©Úselfr   r   r   ÚsetUp.   s    
zLogFormatterTest.setUpc                 C   s&   | j  ¡  t | j¡ t | j¡ d S ©N)r#   Úcloser   Úunlinkr!   Úrmdirr   r&   r   r   r   ÚtearDown?   s    
zLogFormatterTest.tearDownc                 C   s
   t  |¡S r)   ©r   ÚFileHandler©r'   r!   r   r   r   r"   D   s    zLogFormatterTest.make_handlerc              
   C   sZ   t | jdƒD}| ¡  ¡ }tj |¡}|r@| d¡W  5 Q R £ S td| ƒ‚W 5 Q R X d S )NÚrbé   zoutput didn't match regex: %r)	Úopenr!   ÚreadÚstripr   ÚLINE_REÚmatchÚgroupÚ	Exception)r'   ÚfÚlineÚmr   r   r   Ú
get_outputK   s    zLogFormatterTest.get_outputc                 C   s    | j  d¡ |  |  ¡ d¡ d S )NZfoos   foo)r   ÚerrorÚassertEqualr=   r&   r   r   r   Útest_basic_loggingT   s    z#LogFormatterTest.test_basic_loggingc              	   C   s:   t ƒ * | j d¡ |  |  ¡ ttdƒƒ¡ W 5 Q R X d S )Nó   é)r   r   r>   r?   r=   r   Úreprr&   r   r   r   Útest_bytes_loggingX   s    z#LogFormatterTest.test_bytes_loggingc              	   C   sd   t ƒ  | j d d¡¡ W 5 Q R X tttƒrD|  |  ¡ t	dƒ¡ n|  |  ¡ t	t
t	dƒƒƒ¡ d S )Nõ   Ã©r   )r   r   r>   ÚencodeÚ
issubclassÚbytesr   r?   r=   r   rB   r&   r   r   r   Útest_utf8_logging^   s
    
z"LogFormatterTest.test_utf8_loggingc                 C   sR   zt dƒ‚W n  t k
r,   | j d¡ Y nX |  ¡ }|  |d¡ |  d|¡ d S )NrA   zcaught exceptions   Exception.*\\xe9s   \n)r9   r   Ú	exceptionr=   ÚassertRegexZassertNotIn)r'   Úoutputr   r   r   Útest_bytes_exception_loggingk   s    z-LogFormatterTest.test_bytes_exception_loggingN)Ú__name__Ú
__module__Ú__qualname__ÚreÚcompiler6   r(   r-   r"   r=   r@   rC   rH   rL   r   r   r   r   r   '   s   ÿ	r   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚUnicodeLogFormatterTestc                 C   s   t j|ddS )Nr   )Úencodingr.   r0   r   r   r   r"   y   s    z$UnicodeLogFormatterTest.make_handlerc                 C   s$   | j  d¡ |  |  ¡ tdƒ¡ d S )NrD   )r   r>   r?   r=   r   r&   r   r   r   Útest_unicode_logging   s    z,UnicodeLogFormatterTest.test_unicode_loggingN)rM   rN   rO   r"   rT   r   r   r   r   rR   x   s   rR   c                       s4   e Zd Z‡ fdd„Zdd„ Zdd„ Zdd„ Z‡  ZS )	ÚEnablePrettyLoggingTestc                    s4   t ƒ  ¡  tƒ | _t| jƒ t d¡| _d| j_d S )Nz-tornado.test.log_test.EnablePrettyLoggingTestF)	Úsuperr(   r   Úoptionsr   r   r   r   r   r&   ©Ú	__class__r   r   r(   …   s
    

zEnablePrettyLoggingTest.setUpc              
   C   sì   t  ¡ }z–|d | j
_t| j
| jd |  dt| jjƒ¡ | j d¡ | jjd  ¡  t |d ¡}|  dt|ƒ¡ t|d ƒ}|  | ¡ d¡ W 5 Q R X W 5 | jjD ]}| ¡  | ¡  q¨t |d ¡D ]}t |¡ qÌt 	|¡ X d S )Nú
/test_log*ú	/test_log©rW   r   r2   Úhellor   ú^\[E [^]]*\] hello$)r   r   r   ÚhandlersÚflushr*   Úglobr   r+   r,   rW   Úlog_file_prefixr   r?   Úlenr>   r3   rJ   r4   ©r'   Ztmpdirr#   r!   Ú	filenamesr:   r   r   r   Útest_log_fileŒ   s"    
z%EnablePrettyLoggingTest.test_log_filec              
   C   sà   t  ¡ }zŠ|d | j
_d| j
_t| j
| jd | j d¡ | jjd  ¡  t |d ¡}|  dt|ƒ¡ t|d ƒ}|  | ¡ d¡ W 5 Q R X W 5 | jjD ]}| ¡  | ¡  qœt |d ¡D ]}t |¡ qÀt 	|¡ X d S )	NrZ   r[   Útimer\   r]   r   r2   r^   )r   r   r   r_   r`   r*   ra   r   r+   r,   rW   rb   Úlog_rotate_moder   r>   r?   rc   r3   rJ   r4   rd   r   r   r   Ú!test_log_file_with_timed_rotating    s"    
z9EnablePrettyLoggingTest.test_log_file_with_timed_rotatingc              	   C   sP   z*d| j_d| j_| jtt	| j| j d W 5 | j jD ]}| ¡  | ¡  q4X d S )NZ	some_pathZ
wrong_moder\   )
r   r_   r`   r*   rW   rb   rh   ÚassertRaisesÚ
ValueErrorr   )r'   r#   r   r   r   Útest_wrong_rotate_mode_value´   s    ü
z4EnablePrettyLoggingTest.test_wrong_rotate_mode_value)rM   rN   rO   r(   rf   ri   rl   Ú__classcell__r   r   rX   r   rU   „   s   rU   c                   @   sR   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚLoggingOptionTestz?Test the ability to enable and disable Tornado's logging hooks.Nc           	      C   sd   d}d}d  |||g¡}tjtjd|g|p,g  tjtjd}| ¡ \}}|  |j	dd| ¡ d|kS )	Nz7from tornado.options import options, parse_command_linez%import logging; logging.info("hello")ú;z-c)ÚstdoutÚstderrr   zprocess failed: %rs   hello)
r    Ú
subprocessÚPopenÚsysÚ
executableÚPIPEÚSTDOUTÚcommunicater?   Ú
returncode)	r'   Z	statementÚargsZIMPORTÚLOG_INFOÚprogramÚprocrp   rq   r   r   r   Úlogs_presentÇ   s    ýzLoggingOptionTest.logs_presentc                 C   s   |   |  d¡¡ d S )NÚpass©ZassertFalser~   r&   r   r   r   Útest_defaultÙ   s    zLoggingOptionTest.test_defaultc                 C   s   |   |  d¡¡ d S )Núparse_command_line()©Ú
assertTruer~   r&   r   r   r   Útest_tornado_defaultÜ   s    z&LoggingOptionTest.test_tornado_defaultc                 C   s   |   |  ddg¡¡ d S )Nr‚   z--logging=noner€   r&   r   r   r   Útest_disable_command_lineß   s    z+LoggingOptionTest.test_disable_command_linec                 C   s   |   |  ddg¡¡ d S )Nr‚   z--logging=Noner€   r&   r   r   r   Ú*test_disable_command_line_case_insensitiveâ   s    z<LoggingOptionTest.test_disable_command_line_case_insensitivec                 C   s   |   |  d¡¡ d S )Nz.options.logging = "none"; parse_command_line()r€   r&   r   r   r   Útest_disable_code_stringå   s    ÿz*LoggingOptionTest.test_disable_code_stringc                 C   s   |   |  d¡¡ d S )Nú,options.logging = None; parse_command_line()r€   r&   r   r   r   Útest_disable_code_noneê   s    ÿz(LoggingOptionTest.test_disable_code_nonec                 C   s   |   |  ddg¡¡ d S )Nr‰   z--logging=inforƒ   r&   r   r   r   Útest_disable_overrideï   s     ÿÿz'LoggingOptionTest.test_disable_override)N)rM   rN   rO   Ú__doc__r~   r   r…   r†   r‡   rˆ   rŠ   r‹   r   r   r   r   rn   Ä   s   
rn   )Ú
contextlibra   r   r   rP   rr   rt   r   Zunittestr
   Ztornado.escaper   Ztornado.logr   r   r   Ztornado.optionsr   Ztornado.utilr   Úcontextmanagerr   ZTestCaser   rR   rU   rn   r   r   r   r   Ú<module>   s&   
Q@