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