Skip to content

Commit

Permalink
Add StrListSubsetGlob func to strutil
Browse files Browse the repository at this point in the history
  • Loading branch information
nvx committed Sep 16, 2021
1 parent 8b83eb3 commit db7038c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
11 changes: 11 additions & 0 deletions strutil/strutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ func StrListSubset(super, sub []string) bool {
return true
}

// StrListSubsetGlob checks if a given list is a subset of
// another set, allowing for globs.
func StrListSubsetGlob(super, sub []string) bool {
for _, item := range sub {
if !StrListContainsGlob(super, item) {
return false
}
}
return true
}

// ParseDedupAndSortStrings parses a comma separated list of strings
// into a slice of strings. The return slice will be sorted and will
// not contain duplicate or empty items.
Expand Down
37 changes: 37 additions & 0 deletions strutil/strutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,43 @@ func TestStrutil_ListSubset(t *testing.T) {
}
}

func TestStrutil_ListSubsetGlob(t *testing.T) {
parent := []string{
"dev",
"ops*",
"root/*",
"*-dev",
"_*_",
}
if StrListSubsetGlob(parent, []string{"tubez", "dev", "root/admin"}) {
t.Fatalf("Bad")
}
if StrListSubsetGlob(parent, []string{"devops", "ops-dev"}) {
t.Fatalf("Bad")
}
if StrListSubsetGlob(nil, parent) {
t.Fatalf("Bad")
}
if !StrListSubsetGlob(parent, []string{"root/test", "dev", "_test_"}) {
t.Fatalf("Bad")
}
if !StrListSubsetGlob(parent, []string{"ops_test", "ops", "devops-dev"}) {
t.Fatalf("Bad")
}
if !StrListSubsetGlob(parent, []string{"ops"}) {
t.Fatalf("Bad")
}
if !StrListSubsetGlob(parent, []string{"test-dev"}) {
t.Fatalf("Bad")
}
if !StrListSubsetGlob(parent, []string{"_test_"}) {
t.Fatalf("Bad")
}
if !StrListSubsetGlob(parent, nil) {
t.Fatalf("Bad")
}
}

func TestStrutil_ParseKeyValues(t *testing.T) {
actual := make(map[string]string)
expected := map[string]string{
Expand Down

0 comments on commit db7038c

Please sign in to comment.