Skip to content

Commit

Permalink
Add simple test for UNIX timestamp extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Roland Sommer committed Feb 8, 2021
1 parent 1060e30 commit e4dfae5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rotate_backups/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ def match_to_datetime(self, match):
except ValueError:
timestamp = None
if timestamp is None:
logger.notice("Ignoring %s due to invalid date (%s).", value, match.group())
raise ValueError("%r could not be extracted as unix timestamp")
else:
logger.verbose("Extracted timestamp %r from %r", timestamp, value)
return timestamp
Expand Down
15 changes: 15 additions & 0 deletions rotate_backups/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,21 @@ def test_argument_validation(self):
returncode, output = run_cli(main, '-n', '/root')
assert returncode != 0

def test_timestamp_dates(self):
"""Make sure filenames with unix timestamps don't cause an exception."""
with TemporaryDirectory(prefix='rotate-backups-', suffix='-test-suite') as root:
file_with_valid_date = os.path.join(root, 'snapshot-1612396800061.tar.gz')
file_with_invalid_date = os.path.join(root, 'snapshot-1807311501019237.tar.gz')
for filename in file_with_valid_date, file_with_invalid_date:
touch(filename)
program = RotateBackups(
rotation_scheme=dict(monthly='always'),
timestamp_pattern=r"-(?P<unixtime>\d+)\.tar\.gz"
)
backups = program.collect_backups(root)
assert len(backups) == 1
assert backups[0].pathname == file_with_valid_date

def test_invalid_dates(self):
"""Make sure filenames with invalid dates don't cause an exception."""
with TemporaryDirectory(prefix='rotate-backups-', suffix='-test-suite') as root:
Expand Down

0 comments on commit e4dfae5

Please sign in to comment.