From f661d9429f0fb68b573019c9a14d0815ae0baa8a Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Thu, 25 Mar 2021 22:36:01 +0100 Subject: [PATCH] pruning/keep_last_n: correctly handle the case where `count` > matching snaps fixes #446 --- pruning/keep_last_n.go | 7 ++++++- pruning/keep_last_n_test.go | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/pruning/keep_last_n.go b/pruning/keep_last_n.go index 6bd7e2d..023568d 100644 --- a/pruning/keep_last_n.go +++ b/pruning/keep_last_n.go @@ -57,6 +57,11 @@ func (k KeepLastN) KeepRule(snaps []Snapshot) (destroyList []Snapshot) { // then lexicographically descending (e.g. b, a) return strings.Compare(matching[i].Name(), matching[j].Name()) == 1 }) - destroyList = append(destroyList, matching[k.n:]...) + + n := k.n + if n > len(matching) { + n = len(matching) + } + destroyList = append(destroyList, matching[n:]...) return destroyList } diff --git a/pruning/keep_last_n_test.go b/pruning/keep_last_n_test.go index c96bbb9..4283e3e 100644 --- a/pruning/keep_last_n_test.go +++ b/pruning/keep_last_n_test.go @@ -83,6 +83,19 @@ func TestKeepLastN(t *testing.T) { "b1": true, }, }, + "keep_more_than_matching": { + inputs: []Snapshot{ + stubSnap{"a1", false, o(10)}, + stubSnap{"b1", false, o(11)}, + stubSnap{"a2", false, o(12)}, + }, + rules: []KeepRule{ + MustKeepLastN(3, "a"), + }, + expDestroy: map[string]bool{ + "b1": true, + }, + }, } testTable(tcs, t)