@@ -6065,13 +6065,28 @@ def test_emit_after_closing_in_write_mode(self):
6065
6065
self .assertEqual (fp .read ().strip (), '1' )
6066
6066
6067
6067
class RotatingFileHandlerTest (BaseFileTest ):
6068
- @unittest .skipIf (support .is_wasi , "WASI does not have /dev/null." )
6069
6068
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
6071
6076
rh = logging .handlers .RotatingFileHandler (
6072
6077
self .fn , encoding = "utf-8" , maxBytes = 0 )
6073
6078
self .assertFalse (rh .shouldRollover (None ))
6074
6079
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 ):
6075
6090
# bpo-45401 - test with special file
6076
6091
# We set maxBytes to 1 so that rollover would normally happen, except
6077
6092
# for the check for regular files
@@ -6081,18 +6096,47 @@ def test_should_not_rollover(self):
6081
6096
rh .close ()
6082
6097
6083
6098
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 )
6085
6102
self .assertTrue (rh .shouldRollover (self .next_rec ()))
6086
6103
rh .close ()
6087
6104
6088
6105
def test_file_created (self ):
6089
6106
# checks that the file is created and assumes it was created
6090
6107
# by us
6108
+ os .unlink (self .fn )
6091
6109
rh = logging .handlers .RotatingFileHandler (self .fn , encoding = "utf-8" )
6092
6110
rh .emit (self .next_rec ())
6093
6111
self .assertLogFile (self .fn )
6094
6112
rh .close ()
6095
6113
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
+
6096
6140
def test_rollover_filenames (self ):
6097
6141
def namer (name ):
6098
6142
return name + ".test"
@@ -6101,11 +6145,15 @@ def namer(name):
6101
6145
rh .namer = namer
6102
6146
rh .emit (self .next_rec ())
6103
6147
self .assertLogFile (self .fn )
6148
+ self .assertFalse (os .path .exists (namer (self .fn + ".1" )))
6104
6149
rh .emit (self .next_rec ())
6105
6150
self .assertLogFile (namer (self .fn + ".1" ))
6151
+ self .assertFalse (os .path .exists (namer (self .fn + ".2" )))
6106
6152
rh .emit (self .next_rec ())
6107
6153
self .assertLogFile (namer (self .fn + ".2" ))
6108
6154
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" )))
6109
6157
rh .close ()
6110
6158
6111
6159
def test_namer_rotator_inheritance (self ):
0 commit comments