o
    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   I/var/www/vscode/kcb/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   sz   t   | jd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   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 )NrH   )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   rL   rQ   rR   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 ]
}|
  |  qt|d D ]}t| qt| w )
N	/test_logrZ   r   r5   hellor   
/test_log*r0   r1   ^\[E [^]]*\] hello$)r   r   rZ   log_file_prefixr   r   rC   lenhandlersrB   flushglobr6   rN   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   r`   r5   r0   r1   ra   )r   r   rZ   rb   log_rotate_moder   r   rB   rd   re   rf   rC   rc   r6   rN   r7   r,   r    r-   r.   rg   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^   )
rZ   rb   rl   assertRaises
ValueErrorr   r   rd   re   r,   )r)   r%   r   r   r   test_wrong_rotate_mode_value   s    

z4EnablePrettyLoggingTest.test_wrong_rotate_mode_value)rS   rT   rU   r*   rj   rm   rr   __classcell__r   r   r[   r   rX   x   s
    rX   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procrv   rw   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+   )rS   rT   rU   __doc__r   r   r   r   r   r   r   r   r   r   r   r   rt      s    
rt   )
contextlibrf   r   r    rV   rx   rz   r   unittestr
   tornado.escaper   tornado.logr   r   r   tornado.optionsr   tornado.utilr   contextmanagerr   TestCaser   rX   rt   r   r   r   r   <module>   s&   
Q@