diff --git a/builder/azure/common/strings_contains.go b/builder/azure/common/strings_contains.go new file mode 100644 index 000000000..2a8fae416 --- /dev/null +++ b/builder/azure/common/strings_contains.go @@ -0,0 +1,13 @@ +package common + +import "strings" + +// StringsContains returns true if the `haystack` contains the `needle`. Search is case insensitive. +func StringsContains(haystack []string, needle string) bool { + for _, s := range haystack { + if strings.EqualFold(s, needle) { + return true + } + } + return false +} diff --git a/builder/azure/common/strings_contains_test.go b/builder/azure/common/strings_contains_test.go new file mode 100644 index 000000000..51a24ff70 --- /dev/null +++ b/builder/azure/common/strings_contains_test.go @@ -0,0 +1,39 @@ +package common + +import "testing" + +func TestStringsContains(t *testing.T) { + + tests := []struct { + name string + haystack []string + needle string + want bool + }{ + { + name: "found", + haystack: []string{"a", "b", "c"}, + needle: "b", + want: true, + }, + { + name: "missing", + haystack: []string{"a", "b", "c"}, + needle: "D", + want: false, + }, + { + name: "case insensitive", + haystack: []string{"a", "b", "c"}, + needle: "B", + want: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := StringsContains(tt.haystack, tt.needle); got != tt.want { + t.Errorf("StringsContains() = %v, want %v", got, tt.want) + } + }) + } +}