<s> import py <EOL> import sys <EOL> callcapture = py . io . StdCapture . call <EOL> def setup_module ( mod ) : <EOL> mod . tempdir = py . test . ensuretemp ( "<STR_LIT>" ) <EOL> mod . logstate = py . log . _getstate ( ) <EOL> def teardown_module ( mod ) : <EOL> py . log . _setstate ( mod . logstate ) <EOL> class TestLogProducer : <EOL> def setup_method ( self , meth ) : <EOL> self . state = py . log . _getstate ( ) <EOL> def teardown_method ( self , meth ) : <EOL> py . log . _setstate ( self . state ) <EOL> def test_producer_repr ( self ) : <EOL> d = py . log . default <EOL> assert repr ( d ) . find ( '<STR_LIT:default>' ) != - <NUM_LIT:1> <EOL> def test_produce_one_keyword ( self ) : <EOL> l = [ ] <EOL> py . log . setconsumer ( '<STR_LIT>' , l . append ) <EOL> py . log . Producer ( '<STR_LIT>' ) ( "<STR_LIT>" ) <EOL> assert len ( l ) == <NUM_LIT:1> <EOL> msg = l [ <NUM_LIT:0> ] <EOL> assert msg . content ( ) . startswith ( '<STR_LIT>' ) <EOL> assert msg . prefix ( ) == '<STR_LIT>' <EOL> assert str ( msg ) == "<STR_LIT>" <EOL> def test_producer_class ( self ) : <EOL> p = py . log . Producer ( '<STR_LIT>' ) <EOL> l = [ ] <EOL> py . log . setconsumer ( p . keywords , l . append ) <EOL> p ( "<STR_LIT:hello>" ) <EOL> assert len ( l ) == <NUM_LIT:1> <EOL> assert len ( l [ <NUM_LIT:0> ] . keywords ) == <NUM_LIT:1> <EOL> assert '<STR_LIT>' == l [ <NUM_LIT:0> ] . keywords [ <NUM_LIT:0> ] <EOL> def test_producer_caching ( self ) : <EOL> p = py . log . Producer ( '<STR_LIT>' ) <EOL> x2 = p . x2 <EOL> assert x2 is p . x2 <EOL> class TestLogConsumer : <EOL> def setup_method ( self , meth ) : <EOL> self . state = py . log . _getstate ( ) <EOL> def teardown_method ( self , meth ) : <EOL> py . log . _setstate ( self . state ) <EOL> def test_log_none ( self ) : <EOL> log = py . log . Producer ( "<STR_LIT>" ) <EOL> l = [ ] <EOL> py . log . setconsumer ( '<STR_LIT>' , l . append ) <EOL> log ( "<STR_LIT:1>" ) <EOL> assert l <EOL> l [ : ] = [ ] <EOL> py . log . setconsumer ( '<STR_LIT>' , None ) <EOL> log ( "<STR_LIT:2>" ) <EOL> assert not l <EOL> def test_log_default_stdout ( self ) : <EOL> res , out , err = callcapture ( py . log . default , "<STR_LIT:hello>" ) <EOL> assert out . strip ( ) == "<STR_LIT>" <EOL> def test_simple_consumer_match ( self ) : <EOL> l = [ ] <EOL> py . log . setconsumer ( "<STR_LIT>" , l . append ) <EOL> p = py . log . Producer ( "<STR_LIT>" ) <EOL> p ( "<STR_LIT:hello>" ) <EOL> assert l <EOL> assert l [ <NUM_LIT:0> ] . content ( ) == "<STR_LIT:hello>" <EOL> def test_simple_consumer_match_2 ( self ) : <EOL> l = [ ] <EOL> p = py . log . Producer ( "<STR_LIT>" ) <EOL> p . set_consumer ( l . append ) <EOL> p ( "<STR_LIT>" ) <EOL> assert l <EOL> assert l [ <NUM_LIT:0> ] . content ( ) == "<STR_LIT>" <EOL> def test_no_auto_producer ( self ) : <EOL> p = py . log . Producer ( '<STR_LIT:x>' ) <EOL> py . test . raises ( AttributeError , "<STR_LIT>" ) <EOL> py . test . raises ( AttributeError , "<STR_LIT>" ) <EOL> def test_setconsumer_with_producer ( self ) : <EOL> l = [ ] <EOL> p = py . log . Producer ( "<STR_LIT:hello>" ) <EOL> py . log . setconsumer ( p , l . append ) <EOL> p ( "<STR_LIT>" ) <EOL> assert str ( l [ <NUM_LIT:0> ] ) == "<STR_LIT>" <EOL> def test_multi_consumer ( self ) : <EOL> l = [ ] <EOL> py . log . setconsumer ( "<STR_LIT>" , l . append ) <EOL> py . log . setconsumer ( "<STR_LIT>" , None ) <EOL> p = py . log . Producer ( "<STR_LIT>" ) <EOL> p ( "<STR_LIT:hello>" ) <EOL> assert not l <EOL> py . log . Producer ( "<STR_LIT>" ) ( "<STR_LIT:hello>" ) <EOL> assert l <EOL> assert l [ <NUM_LIT:0> ] . content ( ) == "<STR_LIT:hello>" <EOL> def test_log_stderr ( self ) : <EOL> py . log . setconsumer ( "<STR_LIT:default>" , py . log . STDERR ) <EOL> res , out , err = callcapture ( py . log . default , "<STR_LIT:hello>" ) <EOL> assert not out <EOL> assert err . strip ( ) == '<STR_LIT>' <EOL> def test_log_file ( self ) : <EOL> custom_log = tempdir . join ( '<STR_LIT>' ) <EOL> py . log . setconsumer ( "<STR_LIT:default>" , open ( str ( custom_log ) , '<STR_LIT:w>' , buffering = <NUM_LIT:0> ) ) <EOL> py . log . default ( "<STR_LIT>" ) <EOL> assert custom_log . readlines ( ) == [ '<STR_LIT>' ] <EOL> py . log . setconsumer ( "<STR_LIT:default>" , py . log . Path ( custom_log , buffering = <NUM_LIT:0> ) ) <EOL> py . log . default ( "<STR_LIT>" ) <EOL> assert custom_log . readlines ( ) == [ '<STR_LIT>' ] <EOL> def test_log_file_append_mode ( self ) : <EOL> logfilefn = tempdir . join ( '<STR_LIT>' ) <EOL> py . log . setconsumer ( "<STR_LIT:default>" , py . log . Path ( logfilefn , append = True , <EOL> buffering = <NUM_LIT:0> ) ) <EOL> assert logfilefn . check ( ) <EOL> py . log . default ( "<STR_LIT>" ) <EOL> lines = logfilefn . readlines ( ) <EOL> assert lines == [ '<STR_LIT>' ] <EOL> py . log . setconsumer ( "<STR_LIT:default>" , py . log . Path ( logfilefn , append = True , <EOL> buffering = <NUM_LIT:0> ) ) <EOL> py . log . default ( "<STR_LIT>" ) <EOL> lines = logfilefn . readlines ( ) <EOL> assert lines == [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> def test_log_file_delayed_create ( self ) : <EOL> logfilefn = tempdir . join ( '<STR_LIT>' ) <EOL> py . log . setconsumer ( "<STR_LIT:default>" , py . log . Path ( logfilefn , <EOL> delayed_create = True , buffering = <NUM_LIT:0> ) ) <EOL> assert not logfilefn . check ( ) <EOL> py . log . default ( "<STR_LIT>" ) <EOL> lines = logfilefn . readlines ( ) <EOL> assert lines == [ '<STR_LIT>' ] <EOL> def test_keyword_based_log_files ( self ) : <EOL> logfiles = [ ] <EOL> keywords = '<STR_LIT>' . split ( ) <EOL> for key in keywords : <EOL> path = tempdir . join ( key ) <EOL> py . log . setconsumer ( key , py . log . Path ( path , buffering = <NUM_LIT:0> ) ) <EOL> py . log . Producer ( '<STR_LIT>' ) ( '<STR_LIT:1>' ) <EOL> py . log . Producer ( '<STR_LIT>' ) ( '<STR_LIT:2>' ) <EOL> py . log . Producer ( '<STR_LIT>' ) ( '<STR_LIT:3>' ) <EOL> for key in keywords : <EOL> path = tempdir . join ( key ) <EOL> assert path . read ( ) . strip ( ) == '<STR_LIT>' % ( key , key [ - <NUM_LIT:1> ] ) <EOL> def no_test_log_syslog ( self ) : <EOL> py . log . setconsumer ( "<STR_LIT:default>" , py . log . Syslog ( ) ) <EOL> py . log . default ( "<STR_LIT>" ) <EOL> def no_test_log_winevent ( self ) : <EOL> py . log . setconsumer ( "<STR_LIT:default>" , py . log . WinEvent ( ) ) <EOL> py . log . default ( "<STR_LIT>" ) <EOL> def no_test_log_email ( self ) : <EOL> py . log . setconsumer ( "<STR_LIT:default>" , py . log . Email ( mailhost = "<STR_LIT>" , <EOL> fromaddr = "<STR_LIT>" , <EOL> toaddrs = "<STR_LIT>" , <EOL> subject = "<STR_LIT>" ) ) <EOL> py . log . default ( "<STR_LIT>" ) </s>
