pruning: add 'Negate' option to KeepRegex and expose it in config
This commit is contained in:
+10
-5
@@ -6,20 +6,21 @@ import (
|
||||
|
||||
type KeepRegex struct {
|
||||
expr *regexp.Regexp
|
||||
negate bool
|
||||
}
|
||||
|
||||
var _ KeepRule = &KeepRegex{}
|
||||
|
||||
func NewKeepRegex(expr string) (*KeepRegex, error) {
|
||||
func NewKeepRegex(expr string, negate bool) (*KeepRegex, error) {
|
||||
re, err := regexp.Compile(expr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &KeepRegex{re}, nil
|
||||
return &KeepRegex{re, negate}, nil
|
||||
}
|
||||
|
||||
func MustKeepRegex(expr string) *KeepRegex {
|
||||
k, err := NewKeepRegex(expr)
|
||||
func MustKeepRegex(expr string, negate bool) *KeepRegex {
|
||||
k, err := NewKeepRegex(expr, negate)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -28,6 +29,10 @@ func MustKeepRegex(expr string) *KeepRegex {
|
||||
|
||||
func (k *KeepRegex) KeepRule(snaps []Snapshot) []Snapshot {
|
||||
return filterSnapList(snaps, func(s Snapshot) bool {
|
||||
return k.expr.FindStringIndex(s.Name()) == nil
|
||||
if k.negate {
|
||||
return k.expr.FindStringIndex(s.Name()) != nil
|
||||
} else {
|
||||
return k.expr.FindStringIndex(s.Name()) == nil
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package pruning
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestKeepRegexNegation(t *testing.T) {
|
||||
|
||||
noneg := MustKeepRegex("^zrepl_", false)
|
||||
neg := MustKeepRegex("^zrepl_", true)
|
||||
|
||||
snaps := []Snapshot{
|
||||
stubSnap{name: "zrepl_foobar"},
|
||||
stubSnap{name: "zrepl"},
|
||||
stubSnap{name: "barfoo"},
|
||||
}
|
||||
|
||||
destroyNonNeg := snapshotList(noneg.KeepRule(snaps))
|
||||
t.Logf("non-negated rule destroys: %#v", destroyNonNeg.NameList())
|
||||
assert.True(t, destroyNonNeg.ContainsName("zrepl"))
|
||||
assert.True(t, destroyNonNeg.ContainsName("barfoo"))
|
||||
assert.False(t, destroyNonNeg.ContainsName("zrepl_foobar"))
|
||||
|
||||
destroyNeg := snapshotList(neg.KeepRule(snaps))
|
||||
t.Logf("negated rule destroys: %#v", destroyNeg.NameList())
|
||||
assert.False(t, destroyNeg.ContainsName("zrepl"))
|
||||
assert.False(t, destroyNeg.ContainsName("barfoo"))
|
||||
assert.True(t, destroyNeg.ContainsName("zrepl_foobar"))
|
||||
|
||||
}
|
||||
+1
-1
@@ -60,7 +60,7 @@ func RuleFromConfig(in config.PruningEnum) (KeepRule, error) {
|
||||
case *config.PruneKeepLastN:
|
||||
return NewKeepLastN(v.Count)
|
||||
case *config.PruneKeepRegex:
|
||||
return NewKeepRegex(v.Regex)
|
||||
return NewKeepRegex(v.Regex, v.Negate)
|
||||
case *config.PruneGrid:
|
||||
return NewKeepGrid(v)
|
||||
default:
|
||||
|
||||
+25
-6
@@ -24,6 +24,25 @@ type testCase struct {
|
||||
expDestroyAlternatives []map[string]bool
|
||||
}
|
||||
|
||||
type snapshotList []Snapshot
|
||||
|
||||
func (l snapshotList) ContainsName(n string) bool {
|
||||
for _, s := range l {
|
||||
if s.Name() == n {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (l snapshotList) NameList() []string {
|
||||
res := make([]string, len(l))
|
||||
for i, s := range l {
|
||||
res[i] = s.Name()
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func testTable(tcs map[string]testCase, t *testing.T) {
|
||||
mapEqual := func(a, b map[string]bool) bool {
|
||||
if len(a) != len(b) {
|
||||
@@ -79,7 +98,7 @@ func TestPruneSnapshots(t *testing.T) {
|
||||
"simple": {
|
||||
inputs: inputs["s1"],
|
||||
rules: []KeepRule{
|
||||
MustKeepRegex("foo_"),
|
||||
MustKeepRegex("foo_", false),
|
||||
},
|
||||
expDestroy: map[string]bool{
|
||||
"bar_123": true,
|
||||
@@ -88,16 +107,16 @@ func TestPruneSnapshots(t *testing.T) {
|
||||
"multipleRules": {
|
||||
inputs: inputs["s1"],
|
||||
rules: []KeepRule{
|
||||
MustKeepRegex("foo_"),
|
||||
MustKeepRegex("bar_"),
|
||||
MustKeepRegex("foo_", false),
|
||||
MustKeepRegex("bar_", false),
|
||||
},
|
||||
expDestroy: map[string]bool{},
|
||||
},
|
||||
"onlyThoseRemovedByAllAreRemoved": {
|
||||
inputs: inputs["s1"],
|
||||
rules: []KeepRule{
|
||||
MustKeepRegex("notInS1"), // would remove all
|
||||
MustKeepRegex("bar_"), // would remove all but bar_, i.e. foo_.*
|
||||
MustKeepRegex("notInS1", false), // would remove all
|
||||
MustKeepRegex("bar_", false), // would remove all but bar_, i.e. foo_.*
|
||||
},
|
||||
expDestroy: map[string]bool{
|
||||
"foo_123": true,
|
||||
@@ -117,7 +136,7 @@ func TestPruneSnapshots(t *testing.T) {
|
||||
"noSnaps": {
|
||||
inputs: []Snapshot{},
|
||||
rules: []KeepRule{
|
||||
MustKeepRegex("foo_"),
|
||||
MustKeepRegex("foo_", false),
|
||||
},
|
||||
expDestroy: map[string]bool{},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user