@@ -6196,13 +6196,28 @@ def test_emit_after_closing_in_write_mode(self):
6196
6196
self .assertEqual (fp .read ().strip (), '1' )
6197
6197
6198
6198
class RotatingFileHandlerTest (BaseFileTest ):
6199
- @unittest .skipIf (support .is_wasi , "WASI does not have /dev/null." )
6200
6199
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
6202
6207
rh = logging .handlers .RotatingFileHandler (
6203
6208
self .fn , encoding = "utf-8" , maxBytes = 0 )
6204
6209
self .assertFalse (rh .shouldRollover (None ))
6205
6210
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 ):
6206
6221
# bpo-45401 - test with special file
6207
6222
# We set maxBytes to 1 so that rollover would normally happen, except
6208
6223
# for the check for regular files
@@ -6212,18 +6227,47 @@ def test_should_not_rollover(self):
6212
6227
rh .close ()
6213
6228
6214
6229
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 )
6216
6233
self .assertTrue (rh .shouldRollover (self .next_rec ()))
6217
6234
rh .close ()
6218
6235
6219
6236
def test_file_created (self ):
6220
6237
# checks that the file is created and assumes it was created
6221
6238
# by us
6239
+ os .unlink (self .fn )
6222
6240
rh = logging .handlers .RotatingFileHandler (self .fn , encoding = "utf-8" )
6223
6241
rh .emit (self .next_rec ())
6224
6242
self .assertLogFile (self .fn )
6225
6243
rh .close ()
6226
6244
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
+
6227
6271
def test_rollover_filenames (self ):
6228
6272
def namer (name ):
6229
6273
return name + ".test"
@@ -6232,11 +6276,15 @@ def namer(name):
6232
6276
rh .namer = namer
6233
6277
rh .emit (self .next_rec ())
6234
6278
self .assertLogFile (self .fn )
6279
+ self .assertFalse (os .path .exists (namer (self .fn + ".1" )))
6235
6280
rh .emit (self .next_rec ())
6236
6281
self .assertLogFile (namer (self .fn + ".1" ))
6282
+ self .assertFalse (os .path .exists (namer (self .fn + ".2" )))
6237
6283
rh .emit (self .next_rec ())
6238
6284
self .assertLogFile (namer (self .fn + ".2" ))
6239
6285
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" )))
6240
6288
rh .close ()
6241
6289
6242
6290
def test_namer_rotator_inheritance (self ):
0 commit comments