Ϫf1dZddlZddlmZmZmZmZmZmZm Z m Z ddl m Z ddl mZddlmZddlmZmZdd lmZmZdd lmZdd lmZdd lmZdd lmZdej>deedeeddfdZ Gddej>Z!y)z+ Test cases for L{twisted.logger._global}. N)IOAnyListOptionalTextIOTupleTypecast)Failure)unittest)textFileLogObserver)MORE_THAN_ONCE_WARNING LogBeginner) ILogObserverLogEvent)LogLevel)Logger) LogPublisher)nextLinetest actualEventsexpectedEventsreturnc.t|t|k7r|j||t|D]}t|jz dtdtffd }|Dcgc] }|| }}|j||ycc}w)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. eventrc~|j}|jD]}|vs|j||SN)copykeyspop)rrkey allMergedKeyss A/usr/lib/python3/dist-packages/twisted/logger/test/test_global.pysimplifyzcompareEvents..simplify*s=zz|::< C-'   N)len assertEqualsetr r)rrrrr%simplifiedActualr#s @r$ compareEventsr+s <C// ~6EM+UZZ\** +X6BBEBB%~6Cs.BceZdZdZddZddZddZdededdfd Z dd Z dd Z dd Z dd Z ddZddZddZddZddZddZy)LogBeginnerTestsz# Tests for L{LogBeginner}. rNc"t|_tj|_Gdd}Gdd}||_||_t|j|j|j |j |_y)Nc(eZdZeZeZy)&LogBeginnerTests.setUp..NotSysN)__name__ __module__ __qualname__objectstdoutstderrr&r$NotSysr0>sXFXFr&r8cVeZdZd dZ d dedeedededee e d eeddfd Z y) +LogBeginnerTests.setUp..NotWarningsrNcg|_yr)warnings)selfs r$__init__z4LogBeginnerTests.setUp..NotWarnings.__init__Cs   r&messagecategoryfilenamelinenofilelinecF|jj||||||fy)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)r<append)r=r?r@rArBrCrDs r$ showwarningz7LogBeginnerTests.setUp..NotWarnings.showwarningJs$2 $$gx64QU%VWr&rN)NN) r1r2r3r>strr WarningintrrrrGr7r&r$ NotWarningsr:Bsq +/&* X Xw- X  X   X r#w'  Xsm X Xr&rL) r publisherioStringIO errorStream sysModulewarningsModulerbeginner)r=r8rLs r$setUpzLogBeginnerTests.setUp:sn%;;=  ! X! XF )m# NND,,dnnd>Q>Q  r&ctdd}ggttfd}ttfd}|jj ||f|j ||j |g|j |gy)z? Test that C{beginLoggingTo()} adds observers. r foobarc&j|SrrFeevents1s r$zBLogBeginnerTests.test_beginLoggingToAddObservers..t'..*;r&c&j|Srr[r]events2s r$r_zBLogBeginnerTests.test_beginLoggingToAddObservers..ur`r&N)dictr rrSbeginLoggingTorMr(r=ro1o2r^rcs @@r$test_beginLoggingToAddObserversz0LogBeginnerTests.test_beginLoggingToAddObserversks{""$"$ , ; < , ; < $$b"X. u %'* %'*r&ctdd}ggttfd}ttfd}|j||jj ||f|j |g|j |gy)z\ Test that events are buffered until C{beginLoggingTo()} is called. rVr rWc&j|Srr[r\s r$r_zDLogBeginnerTests.test_beginLoggingToBufferedEvents..r`r&c&j|Srr[rbs r$r_zDLogBeginnerTests.test_beginLoggingToBufferedEvents..r`r&N)rdr rrMrSrer(rfs @@r$!test_beginLoggingToBufferedEventsz2LogBeginnerTests.test_beginLoggingToBufferedEvents}s{ ""$"$ , ; < , ; < u $$b"X. %'* %'*r&limitrSc (t|dzD]}|jt|g}|jt t |j g|jttd|dztd|Dy)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}. rV)countc3&K|] }|d yw)rpNr7).0rs r$ z4LogBeginnerTests._bufferLimitTest..s4Ew4sN) rangerMrdrer rrFr(list)r=rnrSrpeventss r$_bufferLimitTestz!LogBeginnerTests._bufferLimitTests}519% .E NN4e, - .!#lFMM!B CD  q%!)$ % 4V4 4 r&c\tj}|j||jy)z Up to C{LogBeginner._DEFAULT_BUFFER_SIZE} log events are buffered for replay by L{LogBeginner.beginLoggingTo}. N)r_DEFAULT_BUFFER_SIZErwrS)r=rns r$test_defaultBufferLimitz(LogBeginnerTests.test_defaultBufferLimits# 00 eT]]3r&cd}t|j|j|j|j|}|j ||y)z The size of the L{LogBeginner} event buffer can be overridden with the C{initialBufferSize} initilizer argument. )initialBufferSizeN)rrMrPrQrRrw)r=rnrSs r$test_overrideBufferLimitz)LogBeginnerTests.test_overrideBufferLimitsH  NN    NN   #   eX.r&c \g}g}tj}t|}|jt dt \}}|j jtt|j|g|jt dt \}}|j jtt|j|g|jt dt ttj||||} d|_t||t dt d| t dgt||| t dg|j!} |j#d|d|d | |j#d|d|d | y) 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. prebuffer)r postbufferpostwarn) log_format log_levelfileNowlineNowfileThenlineThenN<:>)rNrOrrMrdrrSrer rrFrrwarnmaxDiffr+getvalueassertIn) r=r^rc fileHandle textObserver firstFilename firstLinesecondFilename secondLinewarningoutputs r$test_beginLoggingToTwicez)LogBeginnerTests.test_beginLoggingToTwicesj #%"$[[] *:6  t+./#+: y $$d<&H,%WX t,/0%-Z"  $$d<&H,%WX t*-.-mm""     ;'<(:&    dGgt*/E%FG$$& -)A6? .):,a8&Ar&ct|j}|jd|jdd|j |j j dy)zP Critical messages will be written as text to the error stream. observerz ignore thisza critical {message}r?)r?za critical message N)rrMinfocriticalr(rPrr=logs r$test_criticalLoggingz%LogBeginnerTests.test_criticalLoggingsOdnn-  +Y ? ))2246LMr&ct|j}|jjd|j d|j |j jdy)z Once logging has begun with C{beginLoggingTo}, critical messages are no longer written to the output stream. rr7zanother critical messageN)rrMrSrerr(rPrrs r$test_criticalLoggingStopsz*LogBeginnerTests.test_criticalLoggingStopssP dnn- $$R( /0 ))224b9r&cg}|jjtt|jgt dtt |jjt||tddg|dd=t dtt |jjt||tddgy)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.rCr5) log_namespacelog_ioNz Error, world.r6) rSrer rrFprintrrQr5r+rdr6)r=rvs r$%test_beginLoggingToRedirectStandardIOz6LogBeginnerTests.test_beginLoggingToRedirectStandardIOs "$ $$d<&G%HI oD1F1F$GH &4hOP  1I oD1F1F$GH &4hOP r&c.|jj}|jj}|jj dd|j |jj||j |jj|y)z L{LogBeginner.beginLoggingTo} will leave the existing stdout/stderr in place if it has been told not to replace them. r7F)redirectStandardION)rQr5r6rSreassertIs)r=oldOutoldErrs r$test_beginLoggingToDontRedirectz0LogBeginnerTests.test_beginLoggingToDontRedirect si &&&& $$RE$B dnn++V4 dnn++V4r&ctjtjd}tjtjd}||j_||j_g}|j jtt|jgtt|jj}tt|jj }|j|jd|j|jd|jd|jdt||t!dt!dgy) 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-JISbig5s s u李)ru瑩N)rN TextIOWrapperBytesIOrQr5r6rSrer rrFrr(encodingwriter+rd)r=weirdweirderrrvr5r6s r$$test_beginLoggingToPreservesEncodingz5LogBeginnerTests.test_beginLoggingToPreservesEncodings  {;##BJJL&9 % (!# $$d<&G%HIfdnn334fdnn334 +6 &1 ]# ]#dFT%:Drs~ EEE*"'90$'7   7x.7N7 7>k(x((k(r&