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