Skip to content

Commit 802c30c

Browse files
authored
Merge branch 'main' into fix/json-log-attribution-1871
2 parents 58d655a + 74bb0d6 commit 802c30c

7 files changed

Lines changed: 136 additions & 132 deletions

File tree

modules/aws/region.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/aws/aws-sdk-go-v2/aws"
99
"github.com/aws/aws-sdk-go-v2/service/ec2"
1010
"github.com/aws/aws-sdk-go-v2/service/ssm"
11-
"github.com/gruntwork-io/terratest/modules/core/v2/collections"
1211
"github.com/gruntwork-io/terratest/modules/core/v2/logger"
1312
"github.com/gruntwork-io/terratest/modules/core/v2/random"
1413
"github.com/gruntwork-io/terratest/modules/core/v2/testing"
@@ -53,11 +52,11 @@ func GetRandomStableRegionContextE(t testing.TestingT, ctx context.Context, appr
5352
regionsToPickFrom := stableRegions
5453

5554
if len(approvedRegions) > 0 {
56-
regionsToPickFrom = collections.Intersection(regionsToPickFrom, approvedRegions)
55+
regionsToPickFrom = intersection(regionsToPickFrom, approvedRegions)
5756
}
5857

5958
if len(forbiddenRegions) > 0 {
60-
regionsToPickFrom = collections.Subtract(regionsToPickFrom, forbiddenRegions)
59+
regionsToPickFrom = subtract(regionsToPickFrom, forbiddenRegions)
6160
}
6261

6362
return GetRandomRegionContextE(t, ctx, regionsToPickFrom, nil)
@@ -100,7 +99,7 @@ func GetRandomRegionContextE(t testing.TestingT, ctx context.Context, approvedRe
10099
regionsToPickFrom = allRegions
101100
}
102101

103-
regionsToPickFrom = collections.Subtract(regionsToPickFrom, forbiddenRegions)
102+
regionsToPickFrom = subtract(regionsToPickFrom, forbiddenRegions)
104103
region := random.RandomString(regionsToPickFrom)
105104

106105
logger.Default.Logf(t, "Using region %s", region)
@@ -255,3 +254,39 @@ func GetRandomRegionForServiceContext(t testing.TestingT, ctx context.Context, s
255254

256255
return region
257256
}
257+
258+
// intersection returns the items present in both lists, de-duplicated, in the
259+
// order they appear in list1.
260+
func intersection[T comparable](list1, list2 []T) []T {
261+
lookups := make(map[T]struct{}, len(list2))
262+
for _, item := range list2 {
263+
lookups[item] = struct{}{}
264+
}
265+
266+
out := make([]T, 0, min(len(list1), len(list2)))
267+
for _, item := range list1 {
268+
if _, found := lookups[item]; found {
269+
out = append(out, item)
270+
delete(lookups, item) // delete so a repeated list1 item isn't emitted twice
271+
}
272+
}
273+
274+
return out
275+
}
276+
277+
// subtract returns the items in list1 that are not in list2.
278+
func subtract[T comparable](list1, list2 []T) []T {
279+
lookups := make(map[T]struct{}, len(list2))
280+
for _, item := range list2 {
281+
lookups[item] = struct{}{}
282+
}
283+
284+
out := make([]T, 0, len(list1))
285+
for _, item := range list1 {
286+
if _, found := lookups[item]; !found {
287+
out = append(out, item)
288+
}
289+
}
290+
291+
return out
292+
}

modules/azure/region.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package azure
33
import (
44
"context"
55

6-
"github.com/gruntwork-io/terratest/modules/core/v2/collections"
76
"github.com/gruntwork-io/terratest/modules/core/v2/random"
87
"github.com/gruntwork-io/terratest/modules/core/v2/testing"
98
"github.com/stretchr/testify/require"
@@ -67,11 +66,11 @@ func GetRandomStableRegionContext(t testing.TestingT, ctx context.Context, appro
6766
regionsToPickFrom := stableRegions
6867

6968
if len(approvedRegions) > 0 {
70-
regionsToPickFrom = collections.Intersection(regionsToPickFrom, approvedRegions)
69+
regionsToPickFrom = intersection(regionsToPickFrom, approvedRegions)
7170
}
7271

7372
if len(forbiddenRegions) > 0 {
74-
regionsToPickFrom = collections.Subtract(regionsToPickFrom, forbiddenRegions)
73+
regionsToPickFrom = subtract(regionsToPickFrom, forbiddenRegions)
7574
}
7675

7776
return GetRandomRegionContext(t, ctx, regionsToPickFrom, nil, subscriptionID)
@@ -115,7 +114,7 @@ func GetRandomRegionContextE(t testing.TestingT, ctx context.Context, approvedRe
115114
regionsToPickFrom = allRegions
116115
}
117116

118-
regionsToPickFrom = collections.Subtract(regionsToPickFrom, forbiddenRegions)
117+
regionsToPickFrom = subtract(regionsToPickFrom, forbiddenRegions)
119118
region := random.RandomString(regionsToPickFrom)
120119

121120
return region, nil
@@ -168,3 +167,39 @@ func GetAllAzureRegionsContextE(t testing.TestingT, ctx context.Context, subscri
168167

169168
return regions, nil
170169
}
170+
171+
// intersection returns the items present in both lists, de-duplicated, in the
172+
// order they appear in list1.
173+
func intersection[T comparable](list1, list2 []T) []T {
174+
lookups := make(map[T]struct{}, len(list2))
175+
for _, item := range list2 {
176+
lookups[item] = struct{}{}
177+
}
178+
179+
out := make([]T, 0, min(len(list1), len(list2)))
180+
for _, item := range list1 {
181+
if _, found := lookups[item]; found {
182+
out = append(out, item)
183+
delete(lookups, item) // delete so a repeated list1 item isn't emitted twice
184+
}
185+
}
186+
187+
return out
188+
}
189+
190+
// subtract returns the items in list1 that are not in list2.
191+
func subtract[T comparable](list1, list2 []T) []T {
192+
lookups := make(map[T]struct{}, len(list2))
193+
for _, item := range list2 {
194+
lookups[item] = struct{}{}
195+
}
196+
197+
out := make([]T, 0, len(list1))
198+
for _, item := range list1 {
199+
if _, found := lookups[item]; !found {
200+
out = append(out, item)
201+
}
202+
}
203+
204+
return out
205+
}

modules/core/collections/collections.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

modules/core/collections/collections_test.go

Lines changed: 0 additions & 74 deletions
This file was deleted.

modules/gcp/region.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"os"
66
"strings"
77

8-
"github.com/gruntwork-io/terratest/modules/core/v2/collections"
98
"github.com/gruntwork-io/terratest/modules/core/v2/logger"
109
"github.com/gruntwork-io/terratest/modules/core/v2/random"
1110
"github.com/gruntwork-io/terratest/modules/core/v2/testing"
@@ -56,7 +55,7 @@ func GetRandomRegionContextE(t testing.TestingT, ctx context.Context, projectID
5655
regionsToPickFrom = allRegions
5756
}
5857

59-
regionsToPickFrom = collections.Subtract(regionsToPickFrom, forbiddenRegions)
58+
regionsToPickFrom = subtract(regionsToPickFrom, forbiddenRegions)
6059
region := random.RandomString(regionsToPickFrom)
6160

6261
logger.Default.Logf(t, "Using Region %s", region)
@@ -99,7 +98,7 @@ func GetRandomZoneContextE(t testing.TestingT, ctx context.Context, projectID st
9998
zonesToPickFrom = allZones
10099
}
101100

102-
zonesToPickFrom = collections.Subtract(zonesToPickFrom, forbiddenZones)
101+
zonesToPickFrom = subtract(zonesToPickFrom, forbiddenZones)
103102

104103
var zonesToPickFromFiltered []string
105104

@@ -274,3 +273,20 @@ func isInRegions(zone string, regions []string) bool {
274273
func isInRegion(zone string, region string) bool {
275274
return strings.Contains(zone, region)
276275
}
276+
277+
// subtract returns the items in list1 that are not in list2.
278+
func subtract[T comparable](list1, list2 []T) []T {
279+
lookups := make(map[T]struct{}, len(list2))
280+
for _, item := range list2 {
281+
lookups[item] = struct{}{}
282+
}
283+
284+
out := make([]T, 0, len(list1))
285+
for _, item := range list1 {
286+
if _, found := lookups[item]; !found {
287+
out = append(out, item)
288+
}
289+
}
290+
291+
return out
292+
}

modules/teststructure/validate_struct.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"path/filepath"
77

88
go_commons_collections "github.com/gruntwork-io/go-commons/collections"
9-
"github.com/gruntwork-io/terratest/modules/core/v2/collections"
109
"github.com/gruntwork-io/terratest/modules/core/v2/files"
1110
"github.com/mattn/go-zglob"
1211
)
@@ -178,11 +177,11 @@ func FindTerraformModulePathsInRootE(opts *ValidationOptions) ([]string, error)
178177
terraformDirs := go_commons_collections.Keys(terraformDirSet)
179178

180179
if len(opts.IncludeDirs) > 0 {
181-
terraformDirs = collections.Intersection(terraformDirs, opts.IncludeDirs)
180+
terraformDirs = intersection(terraformDirs, opts.IncludeDirs)
182181
}
183182

184183
if len(opts.ExcludeDirs) > 0 {
185-
terraformDirs = collections.Subtract(terraformDirs, opts.ExcludeDirs)
184+
terraformDirs = subtract(terraformDirs, opts.ExcludeDirs)
186185
}
187186

188187
// Filter out any filepaths that were explicitly included in opts.ExcludeDirs
@@ -207,3 +206,39 @@ type ValidationUndefinedRootDirErr struct{}
207206
func (e ValidationUndefinedRootDirErr) Error() string {
208207
return "RootDir must be defined in ValidationOptions passed to ValidateAllTerraformModules"
209208
}
209+
210+
// intersection returns the items present in both lists, de-duplicated, in the
211+
// order they appear in list1.
212+
func intersection[T comparable](list1, list2 []T) []T {
213+
lookups := make(map[T]struct{}, len(list2))
214+
for _, item := range list2 {
215+
lookups[item] = struct{}{}
216+
}
217+
218+
out := make([]T, 0, min(len(list1), len(list2)))
219+
for _, item := range list1 {
220+
if _, found := lookups[item]; found {
221+
out = append(out, item)
222+
delete(lookups, item) // delete so a repeated list1 item isn't emitted twice
223+
}
224+
}
225+
226+
return out
227+
}
228+
229+
// subtract returns the items in list1 that are not in list2.
230+
func subtract[T comparable](list1, list2 []T) []T {
231+
lookups := make(map[T]struct{}, len(list2))
232+
for _, item := range list2 {
233+
lookups[item] = struct{}{}
234+
}
235+
236+
out := make([]T, 0, len(list1))
237+
for _, item := range list1 {
238+
if _, found := lookups[item]; !found {
239+
out = append(out, item)
240+
}
241+
}
242+
243+
return out
244+
}

scripts/check-release-mode.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ consumer_imports() {
3636
for s in $ORDER; do
3737
if [ "$s" = core ]; then
3838
# Keep in sync with core/v2's public leaf packages.
39-
for pkg in random files collections formatting logger shell retry testing; do
39+
for pkg in random files formatting logger shell retry testing; do
4040
echo " _ \"$MODULE_BASE/core/v2/$pkg\""
4141
done
4242
else

0 commit comments

Comments
 (0)