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)