mirror of
https://github.com/python/cpython.git
synced 2025-08-09 19:38:42 +00:00
[3.12] gh-93205: When rotating logs with no namer specified, match whole extension (GH-93224) (GH-115784)
(cherry picked from commit 113687a838
)
Co-authored-by: Gabriele Catania <gabriele.ctn@gmail.com>
This commit is contained in:
parent
8e70dc8739
commit
3651c2729a
3 changed files with 62 additions and 19 deletions
|
@ -6138,6 +6138,43 @@ class TimedRotatingFileHandlerTest(BaseFileTest):
|
|||
self.assertTrue(fn.startswith(prefix + '.') and
|
||||
fn[len(prefix) + 2].isdigit())
|
||||
|
||||
def test_compute_files_to_delete_same_filename_different_extensions(self):
|
||||
# See GH-93205 for background
|
||||
wd = pathlib.Path(tempfile.mkdtemp(prefix='test_logging_'))
|
||||
self.addCleanup(shutil.rmtree, wd)
|
||||
times = []
|
||||
dt = datetime.datetime.now()
|
||||
n_files = 10
|
||||
for _ in range(n_files):
|
||||
times.append(dt.strftime('%Y-%m-%d_%H-%M-%S'))
|
||||
dt += datetime.timedelta(seconds=5)
|
||||
prefixes = ('a.log', 'a.log.b')
|
||||
files = []
|
||||
rotators = []
|
||||
for i, prefix in enumerate(prefixes):
|
||||
backupCount = i+1
|
||||
rotator = logging.handlers.TimedRotatingFileHandler(wd / prefix, when='s',
|
||||
interval=5,
|
||||
backupCount=backupCount,
|
||||
delay=True)
|
||||
rotators.append(rotator)
|
||||
for t in times:
|
||||
files.append('%s.%s' % (prefix, t))
|
||||
# Create empty files
|
||||
for f in files:
|
||||
(wd / f).touch()
|
||||
# Now the checks that only the correct files are offered up for deletion
|
||||
for i, prefix in enumerate(prefixes):
|
||||
backupCount = i+1
|
||||
rotator = rotators[i]
|
||||
candidates = rotator.getFilesToDelete()
|
||||
self.assertEqual(len(candidates), n_files - backupCount)
|
||||
matcher = re.compile(r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}(\.\w+)?$")
|
||||
for c in candidates:
|
||||
d, fn = os.path.split(c)
|
||||
self.assertTrue(fn.startswith(prefix))
|
||||
suffix = fn[(len(prefix)+1):]
|
||||
self.assertRegex(suffix, matcher)
|
||||
|
||||
def secs(**kw):
|
||||
return datetime.timedelta(**kw) // datetime.timedelta(seconds=1)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue