o
    E¾±hÐ"  ã                   @   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jƒZG dd„ dejƒZdS )é    N)Úutf8)ÚLogFormatterÚdefine_logging_optionsÚenable_pretty_logging)ÚOptionParser)Úbasestring_typec                   c   sD    t  ¡  t jdtd d V  W d   ƒ d S 1 sw   Y  d S )NÚignore)Úcategory)ÚwarningsÚcatch_warningsÚsimplefilterÚBytesWarning© r   r   úV/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/tornado/test/log_test.pyÚignore_bytes_warning    s
   €
"þr   c                   @   s^   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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ÚERRORÚ_colorsÚ_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 j|ddS )Núutf-8©Úencoding)r   ÚFileHandler)r)   r#   r   r   r   r$   D   s   zLogFormatterTest.make_handlerc                 C   sb   t | jdƒ!}| ¡  ¡ }tj |¡}|r!| d¡W  d   ƒ S td| ƒ‚1 s*w   Y  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_outputG   s   üúzLogFormatterTest.get_outputc                 C   s    | j  d¡ |  |  ¡ d¡ d S )NÚfoos   foo)r   ÚerrorÚassertEqualr@   r(   r   r   r   Útest_basic_loggingP   s   z#LogFormatterTest.test_basic_loggingc                 C   sP   t ƒ  | j d¡ |  |  ¡ ttdƒƒ¡ W d   ƒ d S 1 s!w   Y  d S )Nó   é)r   r   rB   rC   r@   r   Úreprr(   r   r   r   Útest_bytes_loggingT   s   "ýz#LogFormatterTest.test_bytes_loggingc                 C   sx   t ƒ  | j d ¡ ¡ W d   ƒ n1 sw   Y  tttƒr,|  |  ¡ t	dƒ¡ d S |  |  ¡ t	t
t	dƒƒƒ¡ d S ©Nõ   Ã©)r   r   rB   ÚencodeÚ
issubclassÚbytesr   rC   r@   r   rF   r(   r   r   r   Útest_utf8_loggingZ   s   ÿ
 z"LogFormatterTest.test_utf8_loggingc                 C   sL   zt dƒ‚ t y   | j d¡ Y nw |  ¡ }|  |d¡ |  d|¡ d S )NrE   zcaught exceptions   Exception.*\\xe9s   \n)r<   r   Ú	exceptionr@   ÚassertRegexÚassertNotIn)r)   Úoutputr   r   r   Útest_bytes_exception_loggingg   s   ÿz-LogFormatterTest.test_bytes_exception_loggingc                 C   s$   | j  d¡ |  |  ¡ tdƒ¡ d S rH   )r   rB   rC   r@   r   r(   r   r   r   Útest_unicode_loggings   s   z%LogFormatterTest.test_unicode_loggingN)Ú__name__Ú
__module__Ú__qualname__ÚreÚcompiler9   r*   r/   r$   r@   rD   rG   rM   rR   rS   r   r   r   r   r   '   s    ÿ	r   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*   y   s
   

zEnablePrettyLoggingTest.setUpc              
   C   sH  t  ¡ }z{|d | j_t| j| jd |  dt| jjƒ¡ | j 	d¡ | jjd  
¡  t |d ¡}|  dt|ƒ¡ t|d dd}|  | ¡ d	¡ W d   ƒ n1 sUw   Y  W | jjD ]
}| 
¡  | ¡  q_t |d ¡D ]}t |¡ qqt |¡ d S | jjD ]
}| 
¡  | ¡  q„t |d ¡D ]}t |¡ q–t |¡ w )
Nú	/test_log©r[   r   r5   Úhellor   ú
/test_log*r0   r1   ú^\[E [^]]*\] hello$)r   r   r[   Úlog_file_prefixr   r   rC   ÚlenÚhandlersrB   ÚflushÚglobr6   rO   r7   r,   r    r-   r.   ©r)   ÚtmpdirÚ	filenamesr=   r%   r#   r   r   r   Útest_log_file€   s2   ÿ€
û
z%EnablePrettyLoggingTest.test_log_filec              
   C   s<  t  ¡ }zu|d | j_d| j_t| j| jd | j d¡ | jjd  	¡  t
 
|d ¡}|  dt|ƒ¡ t|d dd	}|  | ¡ d
¡ W d   ƒ n1 sOw   Y  W | jjD ]
}| 	¡  | ¡  qYt
 
|d ¡D ]}t |¡ qkt |¡ d S | jjD ]
}| 	¡  | ¡  q~t
 
|d ¡D ]}t |¡ qt |¡ w )Nr^   Útimer_   r`   r   ra   r5   r0   r1   rb   )r   r   r[   rc   Úlog_rotate_moder   r   rB   re   rf   rg   rC   rd   r6   rO   r7   r,   r    r-   r.   rh   r   r   r   Ú!test_log_file_with_timed_rotating”   s2   ÿ€
û
z9EnablePrettyLoggingTest.test_log_file_with_timed_rotatingc              	   C   sl   z%d| j _d| j _| jtt| j | jd W | jjD ]
}| ¡  | 	¡  qd S | jjD ]
}| ¡  | 	¡  q*w )NÚ	some_pathÚ
wrong_moder_   )
r[   rc   rm   ÚassertRaisesÚ
ValueErrorr   r   re   rf   r,   )r)   r%   r   r   r   Útest_wrong_rotate_mode_value¨   s    ü
þ
þz4EnablePrettyLoggingTest.test_wrong_rotate_mode_value)rT   rU   rV   r*   rk   rn   rs   Ú__classcell__r   r   r\   r   rY   x   s
    rY   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|v 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ÚcommunicaterC   Ú
returncode)	r)   Ú	statementÚargsÚIMPORTÚLOG_INFOÚprogramÚprocrw   rx   r   r   r   Úlogs_present»   s   ýzLoggingOptionTest.logs_presentc                 C   ó   |   |  d¡¡ d S )NÚpass©ÚassertFalser‡   r(   r   r   r   Útest_defaultÍ   ó   zLoggingOptionTest.test_defaultc                 C   rˆ   )Núparse_command_line()©Ú
assertTruer‡   r(   r   r   r   Útest_tornado_defaultÐ   r   z&LoggingOptionTest.test_tornado_defaultc                 C   ó   |   |  ddg¡¡ d S )NrŽ   z--logging=nonerŠ   r(   r   r   r   Útest_disable_command_lineÓ   ó   z+LoggingOptionTest.test_disable_command_linec                 C   r’   )NrŽ   z--logging=NonerŠ   r(   r   r   r   Ú*test_disable_command_line_case_insensitiveÖ   r”   z<LoggingOptionTest.test_disable_command_line_case_insensitivec                 C   rˆ   )Nz.options.logging = "none"; parse_command_line()rŠ   r(   r   r   r   Útest_disable_code_stringÙ   ó   ÿz*LoggingOptionTest.test_disable_code_stringc                 C   rˆ   )Nú,options.logging = None; parse_command_line()rŠ   r(   r   r   r   Útest_disable_code_noneÞ   r—   z(LoggingOptionTest.test_disable_code_nonec                 C   r’   )Nr˜   z--logging=infor   r(   r   r   r   Útest_disable_overrideã   s
   ÿÿz'LoggingOptionTest.test_disable_overrider+   )rT   rU   rV   Ú__doc__r‡   rŒ   r‘   r“   r•   r–   r™   rš   r   r   r   r   ru   ¸   s    
ru   )Ú
contextlibrg   r   r    rW   ry   r{   r   Úunittestr
   Útornado.escaper   Útornado.logr   r   r   Útornado.optionsr   Útornado.utilr   Úcontextmanagerr   ÚTestCaser   rY   ru   r   r   r   r   Ú<module>   s&   
Q@