From 003569e49af3d43a980f0b1f97d772a976b897e7 Mon Sep 17 00:00:00 2001 From: piotrekfilip <43957913+piotrekfilip@users.noreply.github.com> Date: Tue, 15 Mar 2022 17:51:51 +0100 Subject: [PATCH 1/2] test: test_weekly_rotation --- rotate_backups/tests.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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: From e59dd1c778bfad1de31550b3973f9f3168a5fa05 Mon Sep 17 00:00:00 2001 From: piotrekfilip <43957913+piotrekfilip@users.noreply.github.com> Date: Tue, 15 Mar 2022 18:06:23 +0100 Subject: [PATCH 2/2] fix: weekly not working correctly at start of year --- rotate_backups/__init__.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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`."""