nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: roboll/helmfile
Pull request #1227 (2020-04-26 11:11:14)

Enhance createNamespace error handling
- An error is raised if createNamespace is set explicitly and the
  helm version being used is not 3.2+


Patch:
diff --git a/pkg/state/state.go b/pkg/state/state.go
index b5bde67..9558b3b 100644
--- a/pkg/state/state.go
+++ b/pkg/state/state.go
@@ -1639,10 +1639,14 @@ func (st *HelmState) flagsForUpgrade(helm helmexec.Interface, release *ReleaseSp
 		flags = append(flags, "--cleanup-on-fail")
 	}
 
-	if helm.IsVersionAtLeast(3, 2) &&
-		(release.CreateNamespace != nil && *release.CreateNamespace ||
-			release.CreateNamespace == nil && (st.HelmDefaults.CreateNamespace == nil || *st.HelmDefaults.CreateNamespace)) {
-		flags = append(flags, "--create-namespace")
+	if release.CreateNamespace != nil && *release.CreateNamespace ||
+		release.CreateNamespace == nil && (st.HelmDefaults.CreateNamespace == nil || *st.HelmDefaults.CreateNamespace) {
+		if helm.IsVersionAtLeast(3, 2) {
+			flags = append(flags, "--create-namespace")
+		} else if release.CreateNamespace != nil || st.HelmDefaults.CreateNamespace != nil {
+			// createNamespace was set explicitly, but not running supported version of helm - error
+			return nil, fmt.Errorf("releases[].createNamespace requires Helm 3.2.0 or greater")
+		}
 	}
 
 	flags = st.appendConnectionFlags(flags, release)


Test patch:
diff --git a/pkg/state/state_test.go b/pkg/state/state_test.go
index 35d498a..bcb5531 100644
--- a/pkg/state/state_test.go
+++ b/pkg/state/state_test.go
@@ -167,6 +167,7 @@ func TestHelmState_flagsForUpgrade(t *testing.T) {
 		defaults HelmSpec
 		release  *ReleaseSpec
 		want     []string
+		wantErr  string
 	}{
 		{
 			name: "no-options",
@@ -669,6 +670,26 @@ func TestHelmState_flagsForUpgrade(t *testing.T) {
 				"--namespace", "test-namespace",
 			},
 		},
+		{
+			name: "create-namespace-unsupported",
+			defaults: HelmSpec{
+				Verify:          false,
+				CreateNamespace: &enable,
+			},
+			version: &helmexec.Version{
+				Major: 2,
+				Minor: 16,
+				Patch: 0,
+			},
+			release: &ReleaseSpec{
+				Chart:     "test/chart",
+				Version:   "0.1",
+				Verify:    &disable,
+				Name:      "test-charts",
+				Namespace: "test-namespace",
+			},
+			wantErr: "releases[].createNamespace requires Helm 3.2.0 or greater",
+		},
 	}
 	for i := range tests {
 		tt := tests[i]
@@ -685,9 +706,12 @@ func TestHelmState_flagsForUpgrade(t *testing.T) {
 			}
 
 			args, err := state.flagsForUpgrade(helm, tt.release, 0)
-			if err != nil {
+			if err != nil && tt.wantErr == "" {
 				t.Errorf("unexpected error flagsForUpgrade: %v", err)
 			}
+			if tt.wantErr != "" && (err == nil || err.Error() != tt.wantErr) {
+				t.Errorf("expected error '%v'; got '%v'", err, tt.wantErr)
+			}
 			if !reflect.DeepEqual(args, tt.want) {
 				t.Errorf("flagsForUpgrade returned = %v, want %v", args, tt.want)
 			}