@@ -6065,13 +6065,28 @@ def test_emit_after_closing_in_write_mode(self):
60656065 self .assertEqual (fp .read ().strip (), '1' )
60666066
60676067class RotatingFileHandlerTest (BaseFileTest ):
6068- @unittest .skipIf (support .is_wasi , "WASI does not have /dev/null." )
60696068 def test_should_not_rollover (self ):
6070- # If maxbytes is zero rollover never occurs
6069+ # If file is empty rollover never occurs
6070+ rh = logging .handlers .RotatingFileHandler (
6071+ self .fn , encoding = "utf-8" , maxBytes = 1 )
6072+ self .assertFalse (rh .shouldRollover (None ))
6073+ rh .close ()
6074+
6075+ # If maxBytes is zero rollover never occurs
60716076 rh = logging .handlers .RotatingFileHandler (
60726077 self .fn , encoding = "utf-8" , maxBytes = 0 )
60736078 self .assertFalse (rh .shouldRollover (None ))
60746079 rh .close ()
6080+
6081+ with open (self .fn , 'wb' ) as f :
6082+ f .write (b'\n ' )
6083+ rh = logging .handlers .RotatingFileHandler (
6084+ self .fn , encoding = "utf-8" , maxBytes = 0 )
6085+ self .assertFalse (rh .shouldRollover (None ))
6086+ rh .close ()
6087+
6088+ @unittest .skipIf (support .is_wasi , "WASI does not have /dev/null." )
6089+ def test_should_not_rollover_non_file (self ):
60756090 # bpo-45401 - test with special file
60766091 # We set maxBytes to 1 so that rollover would normally happen, except
60776092 # for the check for regular files
@@ -6081,18 +6096,47 @@ def test_should_not_rollover(self):
60816096 rh .close ()
60826097
60836098 def test_should_rollover (self ):
6084- rh = logging .handlers .RotatingFileHandler (self .fn , encoding = "utf-8" , maxBytes = 1 )
6099+ with open (self .fn , 'wb' ) as f :
6100+ f .write (b'\n ' )
6101+ rh = logging .handlers .RotatingFileHandler (self .fn , encoding = "utf-8" , maxBytes = 2 )
60856102 self .assertTrue (rh .shouldRollover (self .next_rec ()))
60866103 rh .close ()
60876104
60886105 def test_file_created (self ):
60896106 # checks that the file is created and assumes it was created
60906107 # by us
6108+ os .unlink (self .fn )
60916109 rh = logging .handlers .RotatingFileHandler (self .fn , encoding = "utf-8" )
60926110 rh .emit (self .next_rec ())
60936111 self .assertLogFile (self .fn )
60946112 rh .close ()
60956113
6114+ def test_max_bytes (self , delay = False ):
6115+ kwargs = {'delay' : delay } if delay else {}
6116+ os .unlink (self .fn )
6117+ rh = logging .handlers .RotatingFileHandler (
6118+ self .fn , encoding = "utf-8" , backupCount = 2 , maxBytes = 100 , ** kwargs )
6119+ self .assertIs (os .path .exists (self .fn ), not delay )
6120+ small = logging .makeLogRecord ({'msg' : 'a' })
6121+ large = logging .makeLogRecord ({'msg' : 'b' * 100 })
6122+ self .assertFalse (rh .shouldRollover (small ))
6123+ self .assertFalse (rh .shouldRollover (large ))
6124+ rh .emit (small )
6125+ self .assertLogFile (self .fn )
6126+ self .assertFalse (os .path .exists (self .fn + ".1" ))
6127+ self .assertFalse (rh .shouldRollover (small ))
6128+ self .assertTrue (rh .shouldRollover (large ))
6129+ rh .emit (large )
6130+ self .assertTrue (os .path .exists (self .fn ))
6131+ self .assertLogFile (self .fn + ".1" )
6132+ self .assertFalse (os .path .exists (self .fn + ".2" ))
6133+ self .assertTrue (rh .shouldRollover (small ))
6134+ self .assertTrue (rh .shouldRollover (large ))
6135+ rh .close ()
6136+
6137+ def test_max_bytes_delay (self ):
6138+ self .test_max_bytes (delay = True )
6139+
60966140 def test_rollover_filenames (self ):
60976141 def namer (name ):
60986142 return name + ".test"
@@ -6101,11 +6145,15 @@ def namer(name):
61016145 rh .namer = namer
61026146 rh .emit (self .next_rec ())
61036147 self .assertLogFile (self .fn )
6148+ self .assertFalse (os .path .exists (namer (self .fn + ".1" )))
61046149 rh .emit (self .next_rec ())
61056150 self .assertLogFile (namer (self .fn + ".1" ))
6151+ self .assertFalse (os .path .exists (namer (self .fn + ".2" )))
61066152 rh .emit (self .next_rec ())
61076153 self .assertLogFile (namer (self .fn + ".2" ))
61086154 self .assertFalse (os .path .exists (namer (self .fn + ".3" )))
6155+ rh .emit (self .next_rec ())
6156+ self .assertFalse (os .path .exists (namer (self .fn + ".3" )))
61096157 rh .close ()
61106158
61116159 def test_namer_rotator_inheritance (self ):
0 commit comments