@@ -6195,13 +6195,28 @@ def test_emit_after_closing_in_write_mode(self):
61956195 self .assertEqual (fp .read ().strip (), '1' )
61966196
61976197class RotatingFileHandlerTest (BaseFileTest ):
6198- @unittest .skipIf (support .is_wasi , "WASI does not have /dev/null." )
61996198 def test_should_not_rollover (self ):
6200- # If maxbytes is zero rollover never occurs
6199+ # If file is empty rollover never occurs
6200+ rh = logging .handlers .RotatingFileHandler (
6201+ self .fn , encoding = "utf-8" , maxBytes = 1 )
6202+ self .assertFalse (rh .shouldRollover (None ))
6203+ rh .close ()
6204+
6205+ # If maxBytes is zero rollover never occurs
62016206 rh = logging .handlers .RotatingFileHandler (
62026207 self .fn , encoding = "utf-8" , maxBytes = 0 )
62036208 self .assertFalse (rh .shouldRollover (None ))
62046209 rh .close ()
6210+
6211+ with open (self .fn , 'wb' ) as f :
6212+ f .write (b'\n ' )
6213+ rh = logging .handlers .RotatingFileHandler (
6214+ self .fn , encoding = "utf-8" , maxBytes = 0 )
6215+ self .assertFalse (rh .shouldRollover (None ))
6216+ rh .close ()
6217+
6218+ @unittest .skipIf (support .is_wasi , "WASI does not have /dev/null." )
6219+ def test_should_not_rollover_non_file (self ):
62056220 # bpo-45401 - test with special file
62066221 # We set maxBytes to 1 so that rollover would normally happen, except
62076222 # for the check for regular files
@@ -6211,18 +6226,47 @@ def test_should_not_rollover(self):
62116226 rh .close ()
62126227
62136228 def test_should_rollover (self ):
6214- rh = logging .handlers .RotatingFileHandler (self .fn , encoding = "utf-8" , maxBytes = 1 )
6229+ with open (self .fn , 'wb' ) as f :
6230+ f .write (b'\n ' )
6231+ rh = logging .handlers .RotatingFileHandler (self .fn , encoding = "utf-8" , maxBytes = 2 )
62156232 self .assertTrue (rh .shouldRollover (self .next_rec ()))
62166233 rh .close ()
62176234
62186235 def test_file_created (self ):
62196236 # checks that the file is created and assumes it was created
62206237 # by us
6238+ os .unlink (self .fn )
62216239 rh = logging .handlers .RotatingFileHandler (self .fn , encoding = "utf-8" )
62226240 rh .emit (self .next_rec ())
62236241 self .assertLogFile (self .fn )
62246242 rh .close ()
62256243
6244+ def test_max_bytes (self , delay = False ):
6245+ kwargs = {'delay' : delay } if delay else {}
6246+ os .unlink (self .fn )
6247+ rh = logging .handlers .RotatingFileHandler (
6248+ self .fn , encoding = "utf-8" , backupCount = 2 , maxBytes = 100 , ** kwargs )
6249+ self .assertIs (os .path .exists (self .fn ), not delay )
6250+ small = logging .makeLogRecord ({'msg' : 'a' })
6251+ large = logging .makeLogRecord ({'msg' : 'b' * 100 })
6252+ self .assertFalse (rh .shouldRollover (small ))
6253+ self .assertFalse (rh .shouldRollover (large ))
6254+ rh .emit (small )
6255+ self .assertLogFile (self .fn )
6256+ self .assertFalse (os .path .exists (self .fn + ".1" ))
6257+ self .assertFalse (rh .shouldRollover (small ))
6258+ self .assertTrue (rh .shouldRollover (large ))
6259+ rh .emit (large )
6260+ self .assertTrue (os .path .exists (self .fn ))
6261+ self .assertLogFile (self .fn + ".1" )
6262+ self .assertFalse (os .path .exists (self .fn + ".2" ))
6263+ self .assertTrue (rh .shouldRollover (small ))
6264+ self .assertTrue (rh .shouldRollover (large ))
6265+ rh .close ()
6266+
6267+ def test_max_bytes_delay (self ):
6268+ self .test_max_bytes (delay = True )
6269+
62266270 def test_rollover_filenames (self ):
62276271 def namer (name ):
62286272 return name + ".test"
@@ -6231,11 +6275,15 @@ def namer(name):
62316275 rh .namer = namer
62326276 rh .emit (self .next_rec ())
62336277 self .assertLogFile (self .fn )
6278+ self .assertFalse (os .path .exists (namer (self .fn + ".1" )))
62346279 rh .emit (self .next_rec ())
62356280 self .assertLogFile (namer (self .fn + ".1" ))
6281+ self .assertFalse (os .path .exists (namer (self .fn + ".2" )))
62366282 rh .emit (self .next_rec ())
62376283 self .assertLogFile (namer (self .fn + ".2" ))
62386284 self .assertFalse (os .path .exists (namer (self .fn + ".3" )))
6285+ rh .emit (self .next_rec ())
6286+ self .assertFalse (os .path .exists (namer (self .fn + ".3" )))
62396287 rh .close ()
62406288
62416289 def test_namer_rotator_inheritance (self ):
0 commit comments