From 5d43adebe48b145aa1a0ecfc762c2ab75ca13905 Mon Sep 17 00:00:00 2001 From: Sarah French Date: Sat, 21 Dec 2024 14:50:33 +0000 Subject: [PATCH 1/4] Make `filterSweepers` use regular expressions --- helper/resource/testing.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/helper/resource/testing.go b/helper/resource/testing.go index 9e1961a46..db9f1ff9c 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -200,10 +200,15 @@ func filterSweepers(f string, source map[string]*Sweeper) map[string]*Sweeper { return source } + filterRegexes := make([]regexp.Regexp, len(filterSlice)) + for i, filter := range filterSlice { + filterRegexes[i] = *regexp.MustCompile(filter) + } + sweepers := make(map[string]*Sweeper) for name := range source { - for _, s := range filterSlice { - if strings.Contains(strings.ToLower(name), s) { + for _, r := range filterRegexes { + if r.MatchString(strings.ToLower(name)) { for foundName, foundSweeper := range filterSweeperWithDependencies(name, source) { sweepers[foundName] = foundSweeper } From 29690f0e5c053b2e57d55d497c530f296d5dcb57 Mon Sep 17 00:00:00 2001 From: Sarah French Date: Sat, 21 Dec 2024 14:52:10 +0000 Subject: [PATCH 2/4] Make downcasing of filters more explicit --- helper/resource/testing.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/helper/resource/testing.go b/helper/resource/testing.go index db9f1ff9c..ee97133e2 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -193,15 +193,17 @@ func runSweepers(regions []string, sweepers map[string]*Sweeper, allowFailures b // to be ran, and returns a filtered set from the list of all of sweepers to // run based on the names given. func filterSweepers(f string, source map[string]*Sweeper) map[string]*Sweeper { - filterSlice := strings.Split(strings.ToLower(f), ",") + filterSlice := strings.Split(f, ",") if len(filterSlice) == 1 && filterSlice[0] == "" { // if the filter slice is a single element of "" then no sweeper list was // given, so just return the full list return source } + // Downcase filter elements and create regular expressions filterRegexes := make([]regexp.Regexp, len(filterSlice)) for i, filter := range filterSlice { + filter = strings.ToLower(filter) filterRegexes[i] = *regexp.MustCompile(filter) } From 5e59588adf0ae2170c4a821f70ba621bfe9fa042 Mon Sep 17 00:00:00 2001 From: Sarah French Date: Sat, 21 Dec 2024 14:54:19 +0000 Subject: [PATCH 3/4] Add test cases to show use of regex filters --- helper/resource/testing_test.go | 60 ++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/helper/resource/testing_test.go b/helper/resource/testing_test.go index 00137efc9..0bdfd2b23 100644 --- a/helper/resource/testing_test.go +++ b/helper/resource/testing_test.go @@ -250,6 +250,25 @@ func TestFilterSweepers(t *testing.T) { ExpectedSweepers: []string{"aws_dummy"}, Filter: "aws_dummy", }, + { + Name: "with regex filter", + Sweepers: map[string]*Sweeper{ + "aws_dummy": { + Name: "aws_dummy", + F: mockSweeperFunc, + }, + "aws_top": { + Name: "aws_top", + F: mockSweeperFunc, + }, + "aws_sub": { + Name: "aws_sub", + F: mockSweeperFunc, + }, + }, + ExpectedSweepers: []string{"aws_dummy", "aws_sub", "aws_top"}, + Filter: "^aws_", + }, { Name: "with two filters", Sweepers: map[string]*Sweeper{ @@ -310,7 +329,46 @@ func TestFilterSweepers(t *testing.T) { Filter: "none", }, { - Name: "with nested depenencies and top level filter", + Name: "with non-matching regex filter", + Sweepers: map[string]*Sweeper{ + "aws_dummy": { + Name: "aws_dummy", + F: mockSweeperFunc, + }, + "aws_top": { + Name: "aws_top", + Dependencies: []string{"aws_sub"}, + F: mockSweeperFunc, + }, + "aws_sub": { + Name: "aws_sub", + F: mockSweeperFunc, + }, + }, + Filter: "^aws_$", + }, + { + Name: "with redundant filter", + Sweepers: map[string]*Sweeper{ + "aws_dummy": { + Name: "aws_dummy", + F: mockSweeperFunc, + }, + "aws_top": { + Name: "aws_top", + Dependencies: []string{"aws_sub"}, + F: mockSweeperFunc, + }, + "aws_sub": { + Name: "aws_sub", + F: mockSweeperFunc, + }, + }, + ExpectedSweepers: []string{"aws_sub", "aws_top", "aws_dummy"}, + Filter: "aws_,aws_dummy", + }, + { + Name: "with nested dependencies and top level filter", Sweepers: map[string]*Sweeper{ "not_matching": { Name: "not_matching", From 27ff3b98b175562770b0225f896437d05e044a55 Mon Sep 17 00:00:00 2001 From: Sarah French Date: Sat, 21 Dec 2024 14:54:47 +0000 Subject: [PATCH 4/4] Fix typos and abbreviations --- helper/resource/testing_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/helper/resource/testing_test.go b/helper/resource/testing_test.go index 0bdfd2b23..c79a8a930 100644 --- a/helper/resource/testing_test.go +++ b/helper/resource/testing_test.go @@ -212,7 +212,7 @@ func TestFilterSweepers(t *testing.T) { ExpectedSweepers: []string{"aws_dummy"}, }, { - Name: "with dep", + Name: "with dependencies", Sweepers: map[string]*Sweeper{ "aws_dummy": { Name: "aws_dummy", @@ -290,7 +290,7 @@ func TestFilterSweepers(t *testing.T) { Filter: "aws_dummy,aws_sub", }, { - Name: "with dep and filter", + Name: "with dependency and filter", Sweepers: map[string]*Sweeper{ "aws_dummy": { Name: "aws_dummy", @@ -393,7 +393,7 @@ func TestFilterSweepers(t *testing.T) { Filter: "matching_level1", }, { - Name: "with nested depenencies and middle level filter", + Name: "with nested dependencies and middle level filter", Sweepers: map[string]*Sweeper{ "not_matching": { Name: "not_matching", @@ -418,7 +418,7 @@ func TestFilterSweepers(t *testing.T) { Filter: "matching_level2", }, { - Name: "with nested depenencies and bottom level filter", + Name: "with nested dependencies and bottom level filter", Sweepers: map[string]*Sweeper{ "not_matching": { Name: "not_matching",