From 5b050bd01c2364e230ffaec7c15a2e8d39513331 Mon Sep 17 00:00:00 2001 From: Alf Mikula Date: Wed, 27 Oct 2021 15:52:45 -0700 Subject: [PATCH 1/2] Add test for prune_split when no archives exist --- src/borg/testsuite/helpers.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/borg/testsuite/helpers.py b/src/borg/testsuite/helpers.py index caf50ec79..df4e0caa2 100644 --- a/src/borg/testsuite/helpers.py +++ b/src/borg/testsuite/helpers.py @@ -439,6 +439,19 @@ def test_prune_split_keep_oldest(): assert kept_because[4][0] == "yearly" +def test_prune_split_no_archives(): + def subset(lst, ids): + return {i for i in lst if i.id in ids} + + archives = [] + + kept_because = {} + keep = prune_split(archives, "yearly", 3, kept_because) + + assert keep == [] + assert kept_because == {} + + class IntervalTestCase(BaseTestCase): def test_interval(self): self.assert_equal(interval('1H'), 1) From 663abc7c906ff92c9751eb3bf9b9e33542a588ac Mon Sep 17 00:00:00 2001 From: Alf Mikula Date: Wed, 27 Oct 2021 16:01:07 -0700 Subject: [PATCH 2/2] Handle case of calling prune_split when there are no archives --- src/borg/helpers/misc.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/borg/helpers/misc.py b/src/borg/helpers/misc.py index ec592eec8..bf7ae8065 100644 --- a/src/borg/helpers/misc.py +++ b/src/borg/helpers/misc.py @@ -49,6 +49,8 @@ def prune_split(archives, rule, n, kept_because=None): kept_because = {} if n == 0: return keep + + a = None for a in sorted(archives, key=attrgetter('ts'), reverse=True): period = to_localtime(a.ts).strftime(pattern) if period != last: @@ -59,7 +61,7 @@ def prune_split(archives, rule, n, kept_because=None): if len(keep) == n: break # Keep oldest archive if we didn't reach the target retention count - if len(keep) < n and a.id not in kept_because: + if a is not None and len(keep) < n and a.id not in kept_because: keep.append(a) kept_because[a.id] = (rule+"[oldest]", len(keep)) return keep