
    gGx                        d 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mZ ddlmZmZmZmZ ddlmZmZ ddlZddlZddlmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#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/m0Z0m1Z1 dd
l2m3Z3m4Z4m5Z5m6Z6 ddl7m8Z8 ddl9m:Z:  ejv                  dejx                        Z= ejv                  dejx                        Z>d Z? G d de      Z@d ZAd ZBej                  d        ZDy)z.Automated tests for the `coloredlogs` package.    N)StringIO)ANSI_COLOR_CODESANSI_CSI
ansi_style	ansi_wrap)PatchedAttributePatchedItemTestCaseretry)formatrandom_string)CHROOT_FILESColoredFormatterNameNormalizerdecrease_verbosityfind_defined_levelsfind_handlerfind_hostnamefind_program_namefind_username	get_levelincrease_verbosityinstall
is_verboselevel_to_numbermatch_stream_handlerparse_encoded_styles	set_levelwalk_propagation_tree)demonstrate_colored_logging)SystemLoggingis_syslog_supportedmatch_syslog_handler)ColoredCronMailerEIGHT_COLOR_PALETTEcaptureconvert)CaptureOutput)VerboseLoggerz
    (?P<date> \d{4}-\d{2}-\d{2} )
    \s (?P<time> \d{2}:\d{2}:\d{2} )
    \s (?P<hostname> \S+ )
    \s (?P<logger_name> \w+ )
    \[ (?P<process_id> \d+ ) \]
    \s (?P<severity> [A-Z]+ )
    \s (?P<message> .* )
z
    (?P<date> \d{4}-\d{2}-\d{2} )
    \s (?P<time> \d{2}:\d{2}:\d{2},\d{3} )
    \s (?P<hostname> \S+ )
    \s (?P<logger_name> \w+ )
    \[ (?P<process_id> \d+ ) \]
    \s (?P<severity> [A-Z]+ )
    \s (?P<message> .* )
c                  T    dt         j                  d<   dt        j                  _        y)z<Speed up the tests by disabling the demo's artificial delay.0COLOREDLOGS_DEMO_DELAYr   N)osenvironcoloredlogsdemo
DEMO_DELAY     F/var/www/openai/venv/lib/python3.12/site-packages/coloredlogs/tests.pysetUpModuler5   V   s    +.BJJ'("#Kr3   c                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d  Z"d! Z#d" Z$d# Z%d$ Z&d% Z'd& Z(y')(ColoredLogsTestCasez&Container for the `coloredlogs` tests.c                 ^   t         j                  dk(  rdndt         j                  v rdnd}|s| j                  d       yt        j                  j                  |      s| j                  d|z         yt        j                  |t        j                        s| j                  d|z         y|S )	z2Find the system log file or skip the current test.darwinz/var/log/system.loglinuxz/var/log/syslogNz$Location of system log file unknown!zSystem log file not found! (%s)z6Insufficient permissions to read system log file! (%s))sysplatformskipTestr-   pathisfileaccessR_OK)selffilenames     r4   find_system_logz#ColoredLogsTestCase.find_system_log`   s    -0\\X-E)!(CLL!8d 	 MM@A)MM;hFG8RWW-MMRU]]^Or3   c                 l   t        d      t        j                  k(  sJ t        d      t        j                  k(  sJ t        d      t        j                  k(  sJ t        d      t        j
                  k(  sJ t        d      t        j                  k(  sJ t        d      t        j                  k(  sJ y)z6Make sure :func:`level_to_number()` works as intended.debuginfowarningerrorfatalzbogus-levelN)r   loggingDEBUGINFOWARNINGERRORFATALrB   s    r4   test_level_to_numberz(ColoredLogsTestCase.test_level_to_numbern   s     w'7==888v&',,666y)W__<<<w'7==888w'7==888}-===r3   c                 v   t               sJ t        j                         \  }}	 t        |d      5 }|j	                  d       |j	                  d       ddd       t        j                  d|       t               dk(  sJ 	 t        j                  d       t        j                  |       	 t        j                  d|       t               sJ 	 t        j                  d       y# 1 sw Y   xY w# t        j                  d       t        j                  |       w xY w# t        j                  d       w xY w)z3Make sure :func:`~find_hostname()` works correctly.wzfirst line
zsecond line
Nr   z
first line)
r   tempfilemkstempopenwriter   insertpopr-   unlink)rB   fdtemporary_filehandles       r4   test_find_hostnamez&ColoredLogsTestCase.test_find_hostnamey   s    %--/N
	&nc*f^,_- + >2 ?l222 QIIn%	 >2 ?"? Q# +* QIIn% Qs.   C2 #C&-C2 -"D! &C/+C2 2,D!D8c                     t        d       t               5 }t        j                  d       |j	                         }t               |v sJ 	 ddd       y# 1 sw Y   yxY w)zSMake sure :func:`install()` integrates with :class:`~coloredlogs.HostNameFilter()`.z%(hostname)sfmt A truly insignificant message ..N)r   r(   rK   rG   get_textr   rB   captureroutputs      r4   test_host_name_filterz)ColoredLogsTestCase.test_host_name_filter   F    N#_LL;<&&(F ?f,,, __   4AAc                     t        d       t               5 }t        j                  d       |j	                         }t               |v sJ 	 ddd       y# 1 sw Y   yxY w)zVMake sure :func:`install()` integrates with :class:`~coloredlogs.ProgramNameFilter()`.z%(programname)sra   rc   N)r   r(   rK   rG   rd   r   re   s      r4   test_program_name_filterz,ColoredLogsTestCase.test_program_name_filter   sH    %&_LL;<&&(F$&&000 __rj   c                     t        d       t               5 }t        j                  d       |j	                         }t               |v sJ 	 ddd       y# 1 sw Y   yxY w)zSMake sure :func:`install()` integrates with :class:`~coloredlogs.UserNameFilter()`.z%(username)sra   rc   N)r   r(   rK   rG   rd   r   re   s      r4   test_username_filterz(ColoredLogsTestCase.test_username_filter   ri   rj   c                    | j                         t        d      t        d      5 }|s| j                  d      cddd       S t	        j
                  d       ddd       t        fd       y# 1 sw Y   xY w)zNMake sure the :class:`coloredlogs.syslog.SystemLogging` context manager works.2   zcoloredlogs-test-suite)programnamez!couldn't connect to syslog daemonN%sc                      t         d      S NTcheck_contentsexpected_messagesystem_log_files   r4   <lambda>z9ColoredLogsTestCase.test_system_logging.<locals>.<lambda>       n_6FMr3   )rD   r   r!   r=   rK   rI   r   )rB   syslogrx   ry   s     @@r4   test_system_loggingz'ColoredLogsTestCase.test_system_logging   sl    ..0(,'?@F}}%HI A@ MM$ 01 A 	MN A@s   A5A55A>c                     t        t        j                  dd      5  t               du sJ 	 ddd       t        t        j                  dd      5  t               du sJ 	 ddd       y# 1 sw Y   =xY w# 1 sw Y   yxY w)zTMake sure the :class:`coloredlogs.syslog.is_syslog_supported` respects the override.COLOREDLOGS_SYSLOGtrueTNfalseF)r	   r-   r.   r"   rQ   s    r4   test_system_logging_overridez0ColoredLogsTestCase.test_system_logging_override   sf    %96B&(D000 C%97C&(E111 DC CBCCs   A(A4(A14A=c                     | j                         t        d      t               5  t        j                  d       t        j                  d       ddd       t        fd       y# 1 sw Y   xY w)z:Make sure that ``coloredlogs.install(syslog=True)`` works.rp   Tr|   rr   Nc                      t         d      S rt   ru   rw   s   r4   rz   zAColoredLogsTestCase.test_syslog_shortcut_simple.<locals>.<lambda>   r{   r3   )rD   r   cleanup_handlersr/   r   rK   rI   r   )rB   rx   ry   s    @@r4   test_syslog_shortcut_simplez/ColoredLogsTestCase.test_syslog_shortcut_simple   sX    ..0(,t,MM$ 01  
 	MN  s   -A--A6c                 T   | j                         t        d      t        d      t               5  t        j                  d       t        j                  d       t        j                  d       ddd       t        fd       t        fd       y# 1 sw Y   (xY w)z?Make sure that ``coloredlogs.install(syslog='warning')`` works.rp   rI   r   rr   Nc                      t         d      S rt   ru   )ry   the_expected_messages   r4   rz   zCColoredLogsTestCase.test_syslog_shortcut_enhanced.<locals>.<lambda>   s    n_6JDQr3   c                      t         d      S )NFru   )not_an_expected_messagery   s   r4   rz   zCColoredLogsTestCase.test_syslog_shortcut_enhanced.<locals>.<lambda>   s    n_6MuUr3   )	rD   r   r   r/   r   rK   rH   rI   r   )rB   r   ry   r   s    @@@r4   test_syslog_shortcut_enhancedz1ColoredLogsTestCase.test_syslog_shortcut_enhanced   s{    ..0,R0"/"3w/OOD"9:MM$ 45	   	QRUV  s   ABB'c                     t               }dD ]<  }|j                  |      |k(  sJ |j                  |j                               |k(  r<J  |j                  d      dk(  sJ |j                  d      dk(  sJ y)zAMake sure :class:`~coloredlogs.NameNormalizer` works as intended.rF   rG   rH   rI   criticalwarnrH   rJ   r   N)r   normalize_nameupper)rB   nncanonical_names      r4   test_name_normalizationz+ColoredLogsTestCase.test_name_normalization   s    ON$$^4FFF$$^%9%9%;<NNN P   (I555  )Z777r3   c                     d}t        |d       }t        |j                               t        g d      k(  sJ |d   d   dk(  sJ |d   d   d	k(  sJ |d
   d   dk(  sJ |d   d   dk(  sJ |d   d   du sJ y)zHMake sure :func:`~coloredlogs.parse_encoded_styles()` works as intended.z6debug=green;warning=yellow;error=red;critical=red,boldc                 "    | j                         S N)r   )ks    r4   rz   z8ColoredLogsTestCase.test_style_parsing.<locals>.<lambda>   s    VWV]V]V_r3   )normalize_key)rF   rH   rI   r   rF   colorgreenrH   yellowrI   redr   boldTN)r   sortedkeys)rB   encoded_stylesdecoded_styless      r4   test_style_parsingz&ColoredLogsTestCase.test_style_parsing   s    Q-nL_`n))+,7`0aaaag&w/7:::i(1X===g&w/5888j)'2e;;;j)&1T999r3   c                     t        t        j                         t               rJ t        t        j                         t               sJ t        t        j
                         t               sJ y)z/Make sure is_verbose() does what it should :-).N)r   rK   rM   r   rL   VERBOSErQ   s    r4   test_is_verbosez#ColoredLogsTestCase.test_is_verbose   sE    ',,<'-- ||'//"||r3   c                    t        t        j                         t               t        j                  k(  sJ t	                t               t        j
                  k(  sJ t	                t               t        j                  k(  sJ t	                t               t        j                  k(  sJ t	                t               t        j                  k(  sJ t	                t               t        j                  k(  sJ y)zBMake sure increase_verbosity() respects default and custom levels.N)	r   rK   rM   r   r   r   rL   SPAMNOTSETrQ   s    r4   test_increase_verbosityz+ColoredLogsTestCase.test_increase_verbosity   s     	',,{gll***{goo---{gmm+++{gll***{gnn,,,{gnn,,,r3   c                 D   t        t        j                         t               t        j                  k(  sJ t	                t               t        j
                  k(  sJ t	                t               t        j                  k(  sJ t	                t               t        j                  k(  sJ t	                t               t        j                  k(  sJ t	                t               t        j                  k(  sJ t	                t               t        j                  k(  sJ y)zBMake sure decrease_verbosity() respects default and custom levels.N)
r   rK   rM   r   r   NOTICErN   SUCCESSrO   CRITICALrQ   s    r4   test_decrease_verbosityz+ColoredLogsTestCase.test_decrease_verbosity  s     	',,{gll***{gnn,,,{goo---{goo---{gmm+++{g.....{g.....r3   c                 R    t               }|j                         }dD ]  }||v rJ  y)z_Make sure find_defined_levels() always reports the levels defined in Python's standard library.)r   
         (   rp   N)r   values)rB   defined_levelslevel_valuesnumbers       r4   test_level_discoveryz(ColoredLogsTestCase.test_level_discovery&  s0    ,.%,,.-F\))) .r3   c                     | j                         \  }}}}t        t        |            }|||||gk(  sJ d|_        t        t        |            }|||gk(  sJ y)zEMake sure walk_propagation_tree() properly walks the tree of loggers.FN)get_logger_treelistr   	propagate)rB   rootparentchildgrand_childloggerss         r4   test_walk_propagation_treez.ColoredLogsTestCase.test_walk_propagation_tree-  sj    +/+?+?+A(fe[,[9:;vt<<<<,[9:;....r3   c                 .   | j                         \  }}}}t        j                         }t        j                  j	                         }|j                  |       |j                  |       t        |d       \  }}||u sJ t        |d       \  }}||u sJ y)z+Make sure find_handler() works as intended.c                 6    t        | t        j                        S r   )
isinstancerK   Handlerhs    r4   rz   z7ColoredLogsTestCase.test_find_handler.<locals>.<lambda>A  s    jYZ\c\k\kNlr3   c                 J    t        | t        j                  j                        S r   )r   rK   handlersSysLogHandlerr   s    r4   rz   z7ColoredLogsTestCase.test_find_handler.<locals>.<lambda>D  s    
STV]VfVfVtVtHur3   N)r   rK   StreamHandlerr   r   
addHandlerr   )	rB   r   r   r   r   stream_handlersyslog_handlermatched_handlermatched_loggers	            r4   test_find_handlerz%ColoredLogsTestCase.test_find_handler8  s    +/+?+?+A(fe[ ..0 ))779(.)*6{Dl*m'.000*6u>u*v'.000r3   c                    t        j                         }t               }t        j                  |      }|dt               }t        j                  |      }|dt               }t        j                  |      }||||fS )z$Create and return a tree of loggers..)rK   	getLoggerr   )rB   r   parent_namer   
child_namer   grand_child_namer   s           r4   r   z#ColoredLogsTestCase.get_logger_treeG  sr       "#o"";/ +]_=
!!*-&0-/B''(89VUK//r3   c                    t               }t        d|       t        j                  d       t	        t        t        j                  |j                         j                                     sJ t               }t        dd|       t        j                  d       t	        t        t        j                  |j                         j                                     sJ y)zGMake sure milliseconds are hidden by default but can be easily enabled.T)reconfigurestreamz%This should not include milliseconds.)millisecondsr   r   z!This should include milliseconds.N)r   r   rK   rG   allmapPLAIN_TEXT_PATTERNmatchgetvalue
splitlinesPATTERN_INCLUDING_MILLISECONDSrB   r   s     r4   test_support_for_millisecondsz1ColoredLogsTestCase.test_support_for_millisecondsV  s     D0<=3)//1B1M1M1OPQQQTtFC8935;;V__=N=Y=Y=[\]]]r3   c                     t               }t        d|d       t        j                  d       t	        j
                  d|j                               sJ y)z@Make sure milliseconds using the ``%f`` directive are supported.Tz%Y-%m-%dT%H:%M:%S.%f%z)r   r   datefmtz,This should be timestamped according to #45.z6^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}[+-]\d{4}\sN)r   r   rK   rG   rer   r   r   s     r4   'test_support_for_milliseconds_directivez;ColoredLogsTestCase.test_support_for_milliseconds_directivec  sB    D9QRCDxxQSYSbSbSdeeer3   c                 8   t        t        d            }t               }t        t        j
                  ||       |j                  t        j                         |j                  d       t        |j                         j                               dk(  sJ |j                  t        j
                         |j                  df|j                  df|j                  df|j                  df|j                  d	f|j                   d
ffD ]  \  }}	 |j"                  j%                          d|j)                         z  } ||       |j                         }|j+                         }|d   }||v sJ ||v sJ t,        j/                  |      rJ  y# t&        $ r Y qw xY w)z-Inspect the plain text output of coloredlogs.   )levelloggerr   zNo one should see this message.r   rL   rM   r   rN   rO   r   z#This is a message with severity %r.N)r)   r   r   r   rK   r   setLevelrM   rF   lenr   striprG   verboserH   rI   r   _cacheclearAttributeErrorlowerr   r   r   )	rB   r   r   methodseveritytextrg   lines	last_lines	            r4   test_plain_text_output_formatz1ColoredLogsTestCase.test_plain_text_output_formatj  si   }R01gnnVFC%676??$**,-222'"(,,!8"(++v!6"(..)!<"(..)!<"(,,!8"(//:!>!@FH##% 98>>;KKD4L__&F%%'Eb	I9$$$y(((%++I6661!@ " s   F	FFc                    t        j                          t               }d}t        t        d|      5  t        j                  |       ddd       ||j                         v sJ t               }d}t        t        d|      5  t        j                  |       ddd       ||j                         v sJ y# 1 sw Y   mxY w# 1 sw Y   *xY w)zHMake sure coloredlogs.install() uses StandardErrorHandler when possible.z$Which stream will receive this text?stderrNz.And which stream will receive this other text?)r/   r   r   r   r;   rK   rG   r   )rB   initial_streaminitial_textsubsequent_streamsubsequent_texts        r4   test_dynamic_stderr_lookupz.ColoredLogsTestCase.test_dynamic_stderr_lookup  s    !=c8^<LL& =~668888$JJc8->?LL) @"3"<"<">>>> =< @?s   B4C 4B= C	c                     t        j                  t        j                  ddj	                  g d      gt         j
                        }|j                         \  }}t        |j                  d      v sJ y)zIMake sure ANSI escape sequences can be forced (bypassing auto-detection).-c;)import coloredlogs, loggingz coloredlogs.install(isatty=True)logging.info('Hello world')r  UTF-8N)	
subprocessPopenr;   
executablejoinPIPEcommunicater   decoderB   interpreterstdoutr  s       r4   test_force_enablez%ColoredLogsTestCase.test_force_enable  sd     &&NND#(( , #(
 //# %0026==1111r3   c           	      *   t               5  t        j                  t        j                  ddj                  g d      gt        j                        }|j                         \  }}t        |j                  d      vsJ 	 ddd       y# 1 sw Y   yxY w)a  
        Make sure ANSI escape sequences are not emitted when logging output is being redirected.

        This is a regression test for https://github.com/xolox/python-coloredlogs/issues/100.

        It works as follows:

        1. We mock an interactive terminal using 'capturer' to ensure that this
           test works inside test drivers that capture output (like pytest).

        2. We launch a subprocess (to ensure a clean process state) where
           stderr is captured but stdout is not, emulating issue #100.

        3. The output captured on stderr contained ANSI escape sequences after
           this test was written and before the issue was fixed, so now this
           serves as a regression test for issue #100.
        r  r	  r
  zcoloredlogs.install()r  r  r  N)
r(   r  r  r;   r  r  r  r  r   r  r  s       r4   test_auto_disablez%ColoredLogsTestCase.test_auto_disable  su    $ _$**chh 0 ', !oo'K )446NFF6==#9999 __s   A4B		Bc           
      F   t        t        j                  dd      5  t               5 }t	        j
                  t        j                  ddj                  g d      g       |j                         }t        |vsJ 	 ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)zDMake sure ANSI escape sequences can be disabled using ``$NO_COLOR``.NO_COLORzI like monochromer  r	  r  N)r	   r-   r.   r(   r  
check_callr;   r  r  rd   r   re   s      r4   test_env_disablez$ColoredLogsTestCase.test_env_disable  s    Z1DEH%%NND#(( 4 +'  "**,v--- ! FE  FEs#   BAB:BB	BB c           
         t        j                         D ]E  \  }}dt        ||      z  }t        dt        |   |      }| j                  |t        |             G d}| j                  |t        t        ddd	                   d
}| j                  |t        t        dd                   d}| j                  |t        t        ddd                   d}| j                  |t        t        dd                   d}| j                  |t        t        dd                   d}| j                  |t        d             d}| j                  |t        t        dd                   d}| j                  |t        t        dd                   d}| j                  |t        t        d d!                   d"}| j                  |t        t        d#dd$                   d%D ]1  }d&|v r|nd'|z   }d(|d)|d*}| j                  |t        |             3 d+}t        d,d-      }d.|d/|d0}d1}| j                  |t        |             y2)3z8Check the conversion from ANSI escape sequences to HTML.zplain text followed by %s text)r   zP<code>plain text followed by <span style="color:{css}">{name}</span> text</code>)cssnamez:<code><span style="color:#FF0">bright yellow</span></code>zbright yellowr   T)r   brightzI<code><span style="background-color:#DE382B">red background</span></code>zred backgroundr   )
backgroundzM<code><span style="background-color:#F00">bright red background</span></code>zbright red background)r$  r#  zI<code><span style="color:#FFAF00">256 color mode foreground</span></code>z256 color mode foreground   zT<code><span style="background-color:#AF0000">256 color mode background</span></code>z256 color mode background|   z <code>plain text expected</code>z"[38;5;256mplain text expected[0mz<<code><span style="font-weight:bold">bold text</span></code>z	bold text)r   zK<code><span style="text-decoration:underline">underlined text</span></code>zunderlined text)	underlinezR<code><span style="text-decoration:line-through">strike-through text</span></code>zstrike-through text)strike_throughzM<code><span style="background-color:#FFC706;color:#000">inverse</span></code>inverse)r   r)  )zwww.python.orgzhttp://coloredlogs.rtfd.orgzhttps://coloredlogs.rtfd.orgz://zhttp://z<code><a href="z" style="color:inherit">z</a></code>z[0mblue)r   r'  <z"https://coloredlogs.readthedocs.io>z<code>&lt;<span style="color:#006FB8;text-decoration:underline"><a href="https://coloredlogs.readthedocs.io" style="color:inherit">https://coloredlogs.readthedocs.io</a></span>&gt;</code>N)r   itemsr   r   r%   assertEqualr'   r   )	rB   
color_name	ansi_codeansi_encoded_textexpected_htmlsample_text
sample_urlreset_short_handblue_underlineds	            r4   test_html_conversionz(ColoredLogsTestCase.test_html_conversion  s;    &6%;%;%=!J	 @9Z_iCj j"b'	2M ]G4E,FG &> U	/QYbf0g(hic	:JW\0](^_g	:Q^clp0q(rsc	:U]`0a(bcn	:Ube0f(gh:0Z([\V	+D0Q(RSe	:KW[0\(]^l	:O`d0e(fgg	)8]a0b(cdjK(-(<9{CZJXbdopM]GK,@A k %$6TBJY[kl% 	 	0A(BCr3   c                 h    d}t        d|g      }|j                         |j                         k(  sJ y)z0Test capturing of output from external commands.ztesting, 1, 2, 3 ..echoN)r&   r   )rB   expected_outputactual_outputs      r4   test_output_interceptionz,ColoredLogsTestCase.test_output_interception  s6    / 9:""$(=(=(????r3   c                     t        t        j                  dd      5  t               5 }|j                  sJ 	 ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)zWTest that automatic ANSI to HTML conversion when running under ``cron`` can be enabled.CONTENT_TYPEz	text/htmlNr	   r-   r.   r$   
is_enabledrB   mailers     r4   test_enable_colored_cron_mailerz3ColoredLogsTestCase.test_enable_colored_cron_mailer  sH    ^[A"$(((( % BA$$ BA!   AAAA	AAc                     t        t        j                  dd      5  t               5 }|j                  rJ 	 ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)zXTest that automatic ANSI to HTML conversion when running under ``cron`` can be disabled.r>  z
text/plainNr?  rA  s     r4    test_disable_colored_cron_mailerz4ColoredLogsTestCase.test_disable_colored_cron_mailer  sK    ^\B"$!,,,,, % CB$$ CBrD  c                    t               }t        j                  dd|z  g}t               5 }dt        j
                  d<   t        j                  |       |j                         }ddd       |vsJ t               5 }dt        j
                  d<   t        j                  |       |j                         }ddd       ||v sJ y# 1 sw Y   axY w# 1 sw Y   xY w)z(Test :func:`coloredlogs.auto_install()`.r  z import logging; logging.info(%r)r   COLOREDLOGS_AUTO_INSTALLNr   )	r   r;   r  r(   r-   r.   r  r  rd   )rB   needlecommand_linerf   rg   s        r4   test_auto_installz%ColoredLogsTestCase.test_auto_install$  s    .PSY.YZ_5<BJJ12!!,/&&(F  V### _5;BJJ12!!,/&&(F   _ _s   9C<9CCCc                     t               5 }t        dd       |j                         }ddd       dD ]  }|j                         v rJ  y# 1 sw Y   %xY w)z4Test the command line colored logging demonstration.r/   --demoNr   )r(   mainrd   r   )rB   rf   rg   r"  s       r4   test_cli_demoz!ColoredLogsTestCase.test_cli_demo6  sP    _)&&(F  DD::<6))) D	 _s   AAc                 0    t        ddddd      }d|v sJ y)z&Test the command line HTML conversion.r/   	--convertrM  Tr&   z<spanNrN  rB   rg   s     r4   test_cli_conversionz'ColoredLogsTestCase.test_cli_conversion?  s#    m[-SWX&   r3   c                 F    t        dddd      }|j                         rJ y)a  
        Test that conversion of empty output produces no HTML.

        This test was added because I found that ``coloredlogs --convert`` when
        used in a cron job could cause cron to send out what appeared to be
        empty emails. On more careful inspection the body of those emails was
        ``<code></code>``. By not emitting the wrapper element when no other
        HTML is generated, cron will not send out an email.
        r/   rQ  r   TrR  N)rN  r   rT  s     r4   test_empty_conversionz)ColoredLogsTestCase.test_empty_conversionE  s%     m[&$G<<>!!>r3   c                 &    dt        dd      v sJ y)z?Test that the usage message is shown when no actions are given.Usage:r/   TrR  NrS  rQ   s    r4   test_implicit_usage_messagez/ColoredLogsTestCase.test_implicit_usage_messageR  s    4t<<<<r3   c                 (    dt        ddd      v sJ y)z>Test that the usage message is shown when ``--help`` is given.rY  r/   z--helpTrR  NrS  rQ   s    r4   test_explicit_usage_messagez/ColoredLogsTestCase.test_explicit_usage_messageV  s    4xFFFFr3   c                    t        t        d      s| j                  d      S g t        j                  t        j
                         fd}fd}t        t        d|      5  t        j                  |       	 t                t        j                         	 ddd       rJ y# t        j                         w xY w# 1 sw Y   (xY w)ah  
        Test that custom LogRecord factories are supported.

        This test is a bit convoluted because the logging module suppresses
        exceptions. We monkey patch the method suspected of encountering
        exceptions so that we can tell after it was called whether any
        exceptions occurred (despite the exceptions not propagating).
        getLogRecordFactoryz this test requires Python >= 3.2c                  &     | i |}d|_         |S )Nl   {= )custom_attribute)argskwargsrecordoriginal_factorys      r4   custom_factoryzFColoredLogsTestCase.test_custom_record_factory.<locals>.custom_factoryj  s    %t6v6F&0F#Mr3   c                  \    	  | i |S # t         $ r}j                  |        d }~ww xY wr   )	Exceptionappend)ra  kwe
exceptionsoriginal_methods      r4   custom_methodzEColoredLogsTestCase.test_custom_record_factory.<locals>.custom_methodo  s9    &333 !!!$s    	+&+r   N)	hasattrrK   r=   r   r   r^  r   setLogRecordFactoryr    )rB   re  rm  rk  rd  rl  s      @@@r4   test_custom_record_factoryz.ColoredLogsTestCase.test_custom_record_factoryZ  s     w 56==!CDD
*11"668	
	 .-H''7>+-++,<= I ~: ++,<= IHs$   'C>
B+C+CCCN))__name__
__module____qualname____doc__rD   rR   r_   rh   rl   rn   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r7  r<  rC  rF  rK  rO  rU  rW  rZ  r\  rp  r2   r3   r4   r7   r7   \   s    0	> 2-1-O$2	OW8	:-*/0*	/10^f#7J? 
2::.:Dx@)- $*!"=G$r3   r7   c                 z    t        |       5 }t        fd|D              |k(  sJ 	 ddd       y# 1 sw Y   yxY w)z6Check if a line in a file contains an expected string.c              3   &   K   | ]  }|v  
 y wr   r2   ).0linecontentss     r4   	<genexpr>z!check_contents.<locals>.<genexpr>  s     78t#s   N)rW   any)rC   ry  r   r^   s    `  r4   rv   rv     s/    	h67775@@@ 
s   1:c                     |j                  dd      }t        j                  }t        j                  }	 | t        _        |rt	               t        _        t
        j                  j                          |r4t        j                  j                         |t        _        |t        _        S 	 |t        _        |t        _        y# |t        _        |t        _        w xY w)z:Wrap the command line interface to make it easier to test.r&   FN)	getr;   argvr  r   r/   clirN  r   )	argumentsoptionsr&   
saved_argvsaved_stdouts        r4   rN  rN    s    kk)U+GJ::L	"!CJ::&&(!
	  !
 !
s   AC   Cc               #      K   d t         t        fD ]:  } t        t        j                         |       \  }}|s'|s*|j                  |       < yw)z+Context manager to cleanup output handlers.N)r   r#   r   rK   r   removeHandler)
match_funchandlerr   s      r4   r   r     sH      
*,@@
&w'8'8':JGv  ) As   7AAA)Ert  
contextlibrK   logging.handlersr-   r   r  r;   rU   humanfriendly.compatr   humanfriendly.terminalr   r   r   r   humanfriendly.testingr   r	   r
   r   humanfriendly.textr   r   r/   coloredlogs.clir   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   coloredlogs.demor    coloredlogs.syslogr!   r"   r#   coloredlogs.converterr$   r%   r&   r'   rf   r(   verboselogsr)   compiler   r   r   r5   r7   rv   rN  contextmanagerr   r2   r3   r4   <module>r     s    5    	 	  
  * T T P P 4      ( 9 W W  # %  RZZ ! ZZ  ", - ZZ" $b( bJA"" * *r3   