From d24fc635b9085e47465bd198d249b7d7532bb91d Mon Sep 17 00:00:00 2001 From: Chuang Wang Date: Wed, 4 May 2022 22:52:12 -0700 Subject: [PATCH] Refactor duplicate code in task_validation_test.go Prior to this commit, many test functions in this file need to get context based on whether alpha api flag is enabled and this duplicate code chuck appears multiple times. Adding a new helper function will make this code chuck reusable when testing future features that will be developed under alpha feature. --- .../pipeline/v1beta1/task_validation_test.go | 37 +++++++------------ 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/pkg/apis/pipeline/v1beta1/task_validation_test.go b/pkg/apis/pipeline/v1beta1/task_validation_test.go index beb660a631d..bd50010f604 100644 --- a/pkg/apis/pipeline/v1beta1/task_validation_test.go +++ b/pkg/apis/pipeline/v1beta1/task_validation_test.go @@ -1041,13 +1041,7 @@ func TestStepAndSidecarWorkspaces(t *testing.T) { Sidecars: tt.fields.Sidecars, Workspaces: tt.fields.Workspaces, } - featureFlags, _ := config.NewFeatureFlagsFromMap(map[string]string{ - "enable-api-fields": "alpha", - }) - cfg := &config.Config{ - FeatureFlags: featureFlags, - } - ctx := config.ToContext(context.Background(), cfg) + ctx := getContextBasedOnFeatureFlag("alpha") ts.SetDefaults(ctx) if err := ts.Validate(ctx); err != nil { t.Errorf("TaskSpec.Validate() = %v", err) @@ -1104,14 +1098,7 @@ func TestStepAndSidecarWorkspacesErrors(t *testing.T) { Sidecars: tt.fields.Sidecars, } - featureFlags, _ := config.NewFeatureFlagsFromMap(map[string]string{ - "enable-api-fields": "alpha", - }) - cfg := &config.Config{ - FeatureFlags: featureFlags, - } - - ctx := config.ToContext(context.Background(), cfg) + ctx := getContextBasedOnFeatureFlag("alpha") ts.SetDefaults(ctx) err := ts.Validate(ctx) if err == nil { @@ -1231,14 +1218,7 @@ func TestIncompatibleAPIVersions(t *testing.T) { testName := fmt.Sprintf("(using %s) %s", version, tt.name) t.Run(testName, func(t *testing.T) { ts := tt.spec - featureFlags, _ := config.NewFeatureFlagsFromMap(map[string]string{ - "enable-api-fields": version, - }) - cfg := &config.Config{ - FeatureFlags: featureFlags, - } - - ctx := config.ToContext(context.Background(), cfg) + ctx := getContextBasedOnFeatureFlag(version) ts.SetDefaults(ctx) err := ts.Validate(ctx) @@ -1254,3 +1234,14 @@ func TestIncompatibleAPIVersions(t *testing.T) { } } } + +func getContextBasedOnFeatureFlag(featureFlag string) context.Context { + featureFlags, _ := config.NewFeatureFlagsFromMap(map[string]string{ + "enable-api-fields": featureFlag, + }) + cfg := &config.Config{ + FeatureFlags: featureFlags, + } + + return config.ToContext(context.Background(), cfg) +}