From 20bc5884eb0e1c00567787b208a53cc92b3e6faa Mon Sep 17 00:00:00 2001 From: Nolan Brubaker Date: Tue, 24 Apr 2018 11:23:40 -0400 Subject: [PATCH] Make empty excludes string more accurate Signed-off-by: Nolan Brubaker --- pkg/util/collections/includes_excludes.go | 10 +++--- .../collections/includes_excludes_test.go | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/pkg/util/collections/includes_excludes.go b/pkg/util/collections/includes_excludes.go index c6d539d4abb..fb7bc0f3aef 100644 --- a/pkg/util/collections/includes_excludes.go +++ b/pkg/util/collections/includes_excludes.go @@ -79,18 +79,18 @@ func (ie *IncludesExcludes) ShouldInclude(s string) bool { // IncludesString returns a string containing all of the includes, separated by commas, or * if the // list is empty. func (ie *IncludesExcludes) IncludesString() string { - return asString(ie.GetIncludes()) + return asString(ie.GetIncludes(), "*") } -// ExcludesString returns a string containing all of the excludes, separated by commas, or * if the +// ExcludesString returns a string containing all of the excludes, separated by commas, or none if the // list is empty. func (ie *IncludesExcludes) ExcludesString() string { - return asString(ie.GetExcludes()) + return asString(ie.GetExcludes(), "none") } -func asString(in []string) string { +func asString(in []string, empty string) string { if len(in) == 0 { - return "*" + return empty } return strings.Join(in, ", ") } diff --git a/pkg/util/collections/includes_excludes_test.go b/pkg/util/collections/includes_excludes_test.go index 1f2bf30e88a..3feaadf3c92 100644 --- a/pkg/util/collections/includes_excludes_test.go +++ b/pkg/util/collections/includes_excludes_test.go @@ -135,3 +135,36 @@ func TestValidateIncludesExcludes(t *testing.T) { }) } } + +func TestIncludeExcludeString(t *testing.T) { + tests := []struct { + name string + includes []string + excludes []string + expectedIncludes string + expectedExcludes string + }{ + { + name: "unspecified includes/excludes should return '*'/'none'", + includes: nil, + excludes: nil, + expectedIncludes: "*", + expectedExcludes: "none", + }, + { + name: "specific resources should result in sorted joined string", + includes: []string{"foo", "bar"}, + excludes: []string{"baz", "xyz"}, + expectedIncludes: "bar, foo", + expectedExcludes: "baz, xyz", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + ie := NewIncludesExcludes().Includes(test.includes...).Excludes(test.excludes...) + assert.Equal(t, test.expectedIncludes, ie.IncludesString()) + assert.Equal(t, test.expectedExcludes, ie.ExcludesString()) + }) + } +}