Skip to content

Commit

Permalink
Refactor duplicate code in task_validation_test.go
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
chuangw6 authored and tekton-robot committed May 5, 2022
1 parent a3caabf commit 30bed9f
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions pkg/apis/pipeline/v1beta1/task_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}

0 comments on commit 30bed9f

Please sign in to comment.