a
    )(b1                     @   s   d Z ddlZddlmZmZmZmZmZmZm	Z	m
Z
 ddlmZ ddlmZ ddlmZ ddlmZmZ dd	lmZmZ dd
lmZ ddlmZ ddlmZ ddlmZ ejee ee ddddZ G dd dejZ!dS )z+
Test cases for L{twisted.logger._global}.
    N)IOAnyListOptionalTextIOTupleTypecast)Failure)unittest   )textFileLogObserver)MORE_THAN_ONCE_WARNINGLogBeginner)ILogObserverLogEvent)LogLevel)Logger)LogPublisher)nextLine)testactualEventsexpectedEventsreturnc                    sr   t |t |kr| || t  |D ]} t| O  q&ttd fddfdd|D }| || dS )a7  
    Compare two sequences of log events, examining only the the keys which are
    present in both.

    @param test: a test case doing the comparison
    @param actualEvents: A list of log events that were emitted by a logger.
    @param expectedEvents: A list of log events that were expected by a test.
    )eventr   c                    s,   |   }|  D ]}| vr|| q|S N)copykeyspop)r   r   key)allMergedKeys >lib/python3.9/site-packages/twisted/logger/test/test_global.pysimplify*   s
    zcompareEvents.<locals>.simplifyc                    s   g | ]} |qS r!   r!   .0r   )r#   r!   r"   
<listcomp>1       z!compareEvents.<locals>.<listcomp>N)lenassertEqualsetr   r   )r   r   r   r   ZsimplifiedActualr!   )r    r#   r"   compareEvents   s    r+   c                   @   s   e Zd ZdZddddZddddZdddd	Zeedd
ddZ	ddddZ
ddddZddddZddddZddddZddddZddddZddddZddddZdddd ZdS )!LogBeginnerTestsz#
    Tests for L{LogBeginner}.
    Nr   c                 C   sZ   t  | _t | _G dd d}G dd d}| | _| | _t| j| j| j| j| _d S )Nc                   @   s   e Zd Ze Ze ZdS )z&LogBeginnerTests.setUp.<locals>.NotSysN)__name__
__module____qualname__objectstdoutstderrr!   r!   r!   r"   NotSys>   s   r4   c                	   @   sF   e Zd ZddddZdeee eeee	e
  ee ddddZdS )	z+LogBeginnerTests.setUp.<locals>.NotWarningsNr-   c                 S   s
   g | _ d S r   )warnings)selfr!   r!   r"   __init__C   s    z4LogBeginnerTests.setUp.<locals>.NotWarnings.__init__)messagecategoryfilenamelinenofileliner   c                 S   s   | j ||||||f dS )a  
                Emulate warnings.showwarning.

                @param message: A warning message to emit.
                @param category: A warning category to associate with
                    C{message}.
                @param filename: A file name for the source code file issuing
                    the warning.
                @param lineno: A line number in the source file where the
                    warning was issued.
                @param file: A file to write the warning message to.  If
                    L{None}, write to L{sys.stderr}.
                @param line: A line of source code to include with the warning
                    message. If L{None}, attempt to read the line from
                    C{filename} and C{lineno}.
                N)r5   append)r6   r8   r9   r:   r;   r<   r=   r!   r!   r"   showwarningJ   s    z7LogBeginnerTests.setUp.<locals>.NotWarnings.showwarning)NN)r.   r/   r0   r7   strr   Warningintr   r   r   r?   r!   r!   r!   r"   NotWarningsB   s     
rC   )	r   	publisherioStringIOerrorStream	sysModulewarningsModuler   beginner)r6   r4   rC   r!   r!   r"   setUp:   s    
#zLogBeginnerTests.setUpc                    sr   t ddd}g  g tt fdd}ttfdd}| j||f | | | |g  | |g dS )z?
        Test that C{beginLoggingTo()} adds observers.
           r   ZfooZbarc                    s
     | S r   r>   eevents1r!   r"   <lambda>t   r'   zBLogBeginnerTests.test_beginLoggingToAddObservers.<locals>.<lambda>c                    s
     | S r   rN   rO   events2r!   r"   rS   u   r'   N)dictr	   r   rJ   beginLoggingTorD   r)   r6   r   Zo1Zo2r!   rR   rU   r"   test_beginLoggingToAddObserversk   s    
z0LogBeginnerTests.test_beginLoggingToAddObserversc                    sr   t ddd}g  g tt fdd}ttfdd}| | | j||f | |g  | |g dS )z\
        Test that events are buffered until C{beginLoggingTo()} is
        called.
        rL   r   rM   c                    s
     | S r   rN   rO   rQ   r!   r"   rS      r'   zDLogBeginnerTests.test_beginLoggingToBufferedEvents.<locals>.<lambda>c                    s
     | S r   rN   rO   rT   r!   r"   rS      r'   N)rV   r	   r   rD   rJ   rW   r)   rX   r!   rY   r"   !test_beginLoggingToBufferedEvents}   s    
z2LogBeginnerTests.test_beginLoggingToBufferedEvents)limitrJ   r   c                 C   sf   t |d D ]}| t|d qg }|tt|jg | tt d|d tdd |D  dS )a  
        Verify that when more than C{limit} events are logged to L{LogBeginner},
        only the last C{limit} are replayed by L{LogBeginner.beginLoggingTo}.

        @param limit: The maximum number of events the log beginner should
            buffer.
        @param beginner: The L{LogBeginner} against which to verify.

        @raise: C{self.failureException} if the wrong events are replayed by
            C{beginner}.
        rL   )countc                 s   s   | ]}|d  V  qdS )r]   Nr!   r$   r!   r!   r"   	<genexpr>   r'   z4LogBeginnerTests._bufferLimitTest.<locals>.<genexpr>N)	rangerD   rV   rW   r	   r   r>   r)   list)r6   r\   rJ   r]   eventsr!   r!   r"   _bufferLimitTest   s    z!LogBeginnerTests._bufferLimitTestc                 C   s   t j}| || j dS )z
        Up to C{LogBeginner._DEFAULT_BUFFER_SIZE} log events are buffered for
        replay by L{LogBeginner.beginLoggingTo}.
        N)r   Z_DEFAULT_BUFFER_SIZErb   rJ   )r6   r\   r!   r!   r"   test_defaultBufferLimit   s    z(LogBeginnerTests.test_defaultBufferLimitc                 C   s.   d}t | j| j| j| j|d}| || dS )z
        The size of the L{LogBeginner} event buffer can be overridden with the
        C{initialBufferSize} initilizer argument.
           )ZinitialBufferSizeN)r   rD   rG   rH   rI   rb   )r6   r\   rJ   r!   r!   r"   test_overrideBufferLimit   s    z)LogBeginnerTests.test_overrideBufferLimitc              	   C   s$  g }g }t  }t|}| tdd t \}}| jtt	|j
|g | tdd t \}}| jtt	|j
|g | tdd tttj||||d}	d| _t| |tddtdd|	tddg t| ||	tddg | }
| d| d| d	|
 | d| d| d	|
 dS )
z
        When invoked twice, L{LogBeginner.beginLoggingTo} will emit a log
        message warning the user that they previously began logging, and add
        the new log observers.
        Z	prebuffer)r   Z
postbufferZpostwarn)Z
log_formatZ	log_levelZfileNowZlineNowZfileThenZlineThenN<:>)rE   rF   r   rD   rV   r   rJ   rW   r	   r   r>   r   r   warnZmaxDiffr+   getvalueassertIn)r6   rR   rU   Z
fileHandleZtextObserverZfirstFilenameZ	firstLineZsecondFilenameZ
secondLinewarningoutputr!   r!   r"   test_beginLoggingToTwice   sB    

	
z)LogBeginnerTests.test_beginLoggingToTwicec                 C   s:   t | jd}|d |jddd | | j d dS )zP
        Critical messages will be written as text to the error stream.
        Zobserverzignore thisza critical {message}r8   )r8   za critical message
N)r   rD   infocriticalr)   rG   rj   r6   logr!   r!   r"   test_criticalLogging   s    
z%LogBeginnerTests.test_criticalLoggingc                 C   s8   t | jd}| jd |d | | j d dS )z
        Once logging has begun with C{beginLoggingTo}, critical messages are no
        longer written to the output stream.
        ro   r!   zanother critical message N)r   rD   rJ   rW   rq   r)   rG   rj   rr   r!   r!   r"   test_criticalLoggingStops   s    
z*LogBeginnerTests.test_criticalLoggingStopsc                 C   s   g }| j tt|jg tdtt| jjd t	| |t
dddg |dd= tdtt| jjd t	| |t
dddg dS )z
        L{LogBeginner.beginLoggingTo} will re-direct the standard output and
        error streams by setting the C{stdio} and C{stderr} attributes on its
        sys module object.
        zHello, world.r<   r2   )Zlog_namespacelog_ioNzError, world.r3   )rJ   rW   r	   r   r>   printr   rH   r2   r+   rV   r3   )r6   ra   r!   r!   r"   %test_beginLoggingToRedirectStandardIO   s    
z6LogBeginnerTests.test_beginLoggingToRedirectStandardIOc                 C   sD   | j j}| j j}| jjddd | | j j| | | j j| dS )z
        L{LogBeginner.beginLoggingTo} will leave the existing stdout/stderr in
        place if it has been told not to replace them.
        r!   F)ZredirectStandardION)rH   r2   r3   rJ   rW   ZassertIs)r6   ZoldOutZoldErrr!   r!   r"   test_beginLoggingToDontRedirect  s
    z0LogBeginnerTests.test_beginLoggingToDontRedirectc                 C   s   t t  d}t t  d}|| j_|| j_g }| jtt	|j
g tt| jj}tt| jj}| |jd | |jd |d |d t| |tddtddg dS )	z
        When L{LogBeginner.beginLoggingTo} redirects stdout/stderr streams, the
        replacement streams will preserve the encoding of the replaced streams,
        to minimally disrupt any application relying on a specific encoding.
        z	shift-JISbig5s   
s   
u   李)rx   u   瑩N)rE   TextIOWrapperBytesIOrH   r2   r3   rJ   rW   r	   r   r>   r   r)   encodingwriter+   rV   )r6   ZweirdZweirderrra   r2   r3   r!   r!   r"   $test_beginLoggingToPreservesEncoding  s    

z5LogBeginnerTests.test_beginLoggingToPreservesEncodingc              
   C   s   | j dttd g }| jtt|jg | j dttd t	
 }| j jdttd|d | | j jdttdddfdttd|dfg t| |tdtjd	 tj tdd
g dS )z
        L{LogBeginner.beginLoggingTo} will redirect the warnings of its
        warnings module into the logging system.
        z	a messagerL   zanother messager   zyet anotherrd   rw   N.)rl   r9   r:   r;   )rI   r?   DeprecationWarning__file__rJ   rW   r	   r   r>   rE   rF   r)   r5   r+   rV   r/   r.   )r6   ra   fr!   r!   r"   test_warningsModule/  s>    
z$LogBeginnerTests.test_warningsModulec                 C   sX   t td}t| jd}|jd|d | j }| d| | d| | d| dS )zR
        The string resulting from a logged failure contains a traceback.
        z,this is not the behavior you are looking forro   z	a failure)failureZ	TracebackN)r
   	Exceptionr   rD   r   rG   rj   rk   )r6   r   rs   msgr!   r!   r"   test_failuresAppendTracebacksV  s    
z.LogBeginnerTests.test_failuresAppendTracebacks)r.   r/   r0   __doc__rK   rZ   r[   rB   r   rb   rc   re   rn   rt   rv   rz   r{   r   r   r   r!   r!   r!   r"   r,   5   s   1+	
'r,   )"r   rE   typingr   r   r   r   r   r   r   r	   Ztwisted.python.failurer
   Ztwisted.trialr   Z_filer   Z_globalr   r   Z_interfacesr   r   Z_levelsr   Z_loggerr   Z	_observerr   Ztest.test_stdlibr   ZTestCaser+   r,   r!   r!   r!   r"   <module>   s"   (