Skip to content

Commit e4dfae5

Browse files
author
Roland Sommer
committed
Add simple test for UNIX timestamp extraction
1 parent 1060e30 commit e4dfae5

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

rotate_backups/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ def match_to_datetime(self, match):
704704
except ValueError:
705705
timestamp = None
706706
if timestamp is None:
707-
logger.notice("Ignoring %s due to invalid date (%s).", value, match.group())
707+
raise ValueError("%r could not be extracted as unix timestamp")
708708
else:
709709
logger.verbose("Extracted timestamp %r from %r", timestamp, value)
710710
return timestamp

rotate_backups/tests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,21 @@ def test_argument_validation(self):
145145
returncode, output = run_cli(main, '-n', '/root')
146146
assert returncode != 0
147147

148+
def test_timestamp_dates(self):
149+
"""Make sure filenames with unix timestamps don't cause an exception."""
150+
with TemporaryDirectory(prefix='rotate-backups-', suffix='-test-suite') as root:
151+
file_with_valid_date = os.path.join(root, 'snapshot-1612396800061.tar.gz')
152+
file_with_invalid_date = os.path.join(root, 'snapshot-1807311501019237.tar.gz')
153+
for filename in file_with_valid_date, file_with_invalid_date:
154+
touch(filename)
155+
program = RotateBackups(
156+
rotation_scheme=dict(monthly='always'),
157+
timestamp_pattern=r"-(?P<unixtime>\d+)\.tar\.gz"
158+
)
159+
backups = program.collect_backups(root)
160+
assert len(backups) == 1
161+
assert backups[0].pathname == file_with_valid_date
162+
148163
def test_invalid_dates(self):
149164
"""Make sure filenames with invalid dates don't cause an exception."""
150165
with TemporaryDirectory(prefix='rotate-backups-', suffix='-test-suite') as root:

0 commit comments

Comments
 (0)