diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-11-02 16:18:57 +0300 |
---|---|---|
committer | Paul Okstad <pokstad@gitlab.com> | 2020-11-04 08:14:04 +0300 |
commit | b0b1d34d41cb01c4cf46266d31ba195a10f3a681 (patch) | |
tree | 3852f7a352c33bf8616bb958e6d2bd824662e82b | |
parent | e28343e56bc5b32d42ce6cfd0795d86d963e77a8 (diff) |
testhelper: Extract feature set
The featureset implementation is currently part of the catch-all
"testhelper.go" file. Let's extract it into its own file to make it more
contained and easier to maintain.
-rw-r--r-- | internal/testhelper/featureset.go | 77 | ||||
-rw-r--r-- | internal/testhelper/testhelper.go | 69 |
2 files changed, 77 insertions, 69 deletions
diff --git a/internal/testhelper/featureset.go b/internal/testhelper/featureset.go new file mode 100644 index 000000000..8c2d3acb3 --- /dev/null +++ b/internal/testhelper/featureset.go @@ -0,0 +1,77 @@ +package testhelper + +import ( + "context" + "sort" + "strings" + + "gitlab.com/gitlab-org/gitaly/internal/metadata/featureflag" +) + +// FeatureSet is a representation of a set of features that should be disabled. +// This is useful in situations where a test needs to test any combination of features toggled on and off. +// It is designed to disable features as all features are enabled by default, please see: testhelper.Context() +type FeatureSet struct { + features map[featureflag.FeatureFlag]struct{} + rubyFeatures map[featureflag.FeatureFlag]struct{} +} + +func (f FeatureSet) IsDisabled(flag featureflag.FeatureFlag) bool { + _, ok := f.features[flag] + return ok +} + +func (f FeatureSet) String() string { + features := make([]string, 0, len(f.features)) + for feature := range f.features { + features = append(features, feature.Name) + } + + if len(features) == 0 { + return "none" + } + + sort.Strings(features) + + return strings.Join(features, ",") +} + +func (f FeatureSet) Disable(ctx context.Context) context.Context { + for feature := range f.features { + if _, ok := f.rubyFeatures[feature]; ok { + ctx = featureflag.OutgoingCtxWithRubyFeatureFlagValue(ctx, feature, "false") + continue + } + ctx = featureflag.OutgoingCtxWithFeatureFlagValue(ctx, feature, "false") + } + + return ctx +} + +// FeatureSets is a slice containing many FeatureSets +type FeatureSets []FeatureSet + +// NewFeatureSets takes a slice of go feature flags, and an optional variadic set of ruby feature flags +// and returns a FeatureSets slice +func NewFeatureSets(goFeatures []featureflag.FeatureFlag, rubyFeatures ...featureflag.FeatureFlag) (FeatureSets, error) { + rubyFeatureMap := make(map[featureflag.FeatureFlag]struct{}) + for _, rubyFeature := range rubyFeatures { + rubyFeatureMap[rubyFeature] = struct{}{} + } + + // start with an empty feature set + f := []FeatureSet{{features: make(map[featureflag.FeatureFlag]struct{}), rubyFeatures: rubyFeatureMap}} + + allFeatures := append(goFeatures, rubyFeatures...) + + for i := range allFeatures { + featureMap := make(map[featureflag.FeatureFlag]struct{}) + for j := 0; j <= i; j++ { + featureMap[allFeatures[j]] = struct{}{} + } + + f = append(f, FeatureSet{features: featureMap, rubyFeatures: rubyFeatureMap}) + } + + return f, nil +} diff --git a/internal/testhelper/testhelper.go b/internal/testhelper/testhelper.go index fe6b94030..531dd2248 100644 --- a/internal/testhelper/testhelper.go +++ b/internal/testhelper/testhelper.go @@ -20,7 +20,6 @@ import ( "os/exec" "path/filepath" "runtime" - "sort" "strconv" "strings" "sync" @@ -923,74 +922,6 @@ func WriteBlobs(t testing.TB, testRepoPath string, n int) []string { return blobIDs } -// FeatureSet is a representation of a set of features that should be disabled. -// This is useful in situations where a test needs to test any combination of features toggled on and off. -// It is designed to disable features as all features are enabled by default, please see: testhelper.Context() -type FeatureSet struct { - features map[featureflag.FeatureFlag]struct{} - rubyFeatures map[featureflag.FeatureFlag]struct{} -} - -func (f FeatureSet) IsDisabled(flag featureflag.FeatureFlag) bool { - _, ok := f.features[flag] - return ok -} - -func (f FeatureSet) String() string { - features := make([]string, 0, len(f.features)) - for feature := range f.features { - features = append(features, feature.Name) - } - - if len(features) == 0 { - return "none" - } - - sort.Strings(features) - - return strings.Join(features, ",") -} - -func (f FeatureSet) Disable(ctx context.Context) context.Context { - for feature := range f.features { - if _, ok := f.rubyFeatures[feature]; ok { - ctx = featureflag.OutgoingCtxWithRubyFeatureFlagValue(ctx, feature, "false") - continue - } - ctx = featureflag.OutgoingCtxWithFeatureFlagValue(ctx, feature, "false") - } - - return ctx -} - -// FeatureSets is a slice containing many FeatureSets -type FeatureSets []FeatureSet - -// NewFeatureSets takes a slice of go feature flags, and an optional variadic set of ruby feature flags -// and returns a FeatureSets slice -func NewFeatureSets(goFeatures []featureflag.FeatureFlag, rubyFeatures ...featureflag.FeatureFlag) (FeatureSets, error) { - rubyFeatureMap := make(map[featureflag.FeatureFlag]struct{}) - for _, rubyFeature := range rubyFeatures { - rubyFeatureMap[rubyFeature] = struct{}{} - } - - // start with an empty feature set - f := []FeatureSet{{features: make(map[featureflag.FeatureFlag]struct{}), rubyFeatures: rubyFeatureMap}} - - allFeatures := append(goFeatures, rubyFeatures...) - - for i := range allFeatures { - featureMap := make(map[featureflag.FeatureFlag]struct{}) - for j := 0; j <= i; j++ { - featureMap[allFeatures[j]] = struct{}{} - } - - f = append(f, FeatureSet{features: featureMap, rubyFeatures: rubyFeatureMap}) - } - - return f, nil -} - // ModifyEnvironment will change an environment variable and return a func suitable // for `defer` to change the value back. func ModifyEnvironment(t testing.TB, key string, value string) func() { |