diff --git a/rotate_backups/__init__.py b/rotate_backups/__init__.py index d144b6b..2c1b469 100644 --- a/rotate_backups/__init__.py +++ b/rotate_backups/__init__.py @@ -986,7 +986,12 @@ def timestamp(self): @property def week(self): """The ISO week number of :attr:`timestamp` (a number).""" - return self.timestamp.isocalendar()[1] + # for some days close to January 1, isocalendar()[1] may return the week number as 52 or 53 + # eg: date(2022, 1, 1).isocalendar() returns (2021, 52, 6) + if self.year == self.timestamp.isocalendar()[0] + 1: + return 0 + else: + return self.timestamp.isocalendar()[1] def __getattr__(self, name): """Defer attribute access to :attr:`timestamp`.""" diff --git a/rotate_backups/tests.py b/rotate_backups/tests.py index c3b8078..1681514 100644 --- a/rotate_backups/tests.py +++ b/rotate_backups/tests.py @@ -387,6 +387,23 @@ def test_minutely_rotation(self): assert os.path.exists(os.path.join(root, 'backup-2016-01-10_21-30-00')) assert os.path.exists(os.path.join(root, 'backup-2016-01-10_21-45-00')) + def test_weekly_rotation(self): + """Test weekly rotation.""" + with TemporaryDirectory(prefix='rotate-backups-', suffix='-test-suite') as root: + os.mkdir(os.path.join(root, 'galera_backup_db4.sl.example.lab_2020-12-26_10-00')) + os.mkdir(os.path.join(root, 'galera_backup_db4.sl.example.lab_2021-01-02_10-00')) + os.mkdir(os.path.join(root, 'galera_backup_db4.sl.example.lab_2021-01-09_10-00')) + os.mkdir(os.path.join(root, 'galera_backup_db4.sl.example.lab_2021-01-16_10-00')) + os.mkdir(os.path.join(root, 'galera_backup_db4.sl.example.lab_2021-01-23_10-00')) + os.mkdir(os.path.join(root, 'galera_backup_db4.sl.example.lab_2021-01-30_10-00')) + run_cli(main, '--weekly=4', root) + assert os.path.exists(os.path.join(root, 'galera_backup_db4.sl.example.lab_2020-12-26_10-00')) is False + assert os.path.exists(os.path.join(root, 'galera_backup_db4.sl.example.lab_2021-01-02_10-00')) is False + assert os.path.exists(os.path.join(root, 'galera_backup_db4.sl.example.lab_2021-01-09_10-00')) + assert os.path.exists(os.path.join(root, 'galera_backup_db4.sl.example.lab_2021-01-16_10-00')) + assert os.path.exists(os.path.join(root, 'galera_backup_db4.sl.example.lab_2021-01-23_10-00')) + assert os.path.exists(os.path.join(root, 'galera_backup_db4.sl.example.lab_2021-01-30_10-00')) + def test_removal_command(self): """Test that the removal command can be customized.""" with TemporaryDirectory(prefix='rotate-backups-', suffix='-test-suite') as root: