From e6a5cbb663fe27ab9c9faac5e1ad9479c7d884f1 Mon Sep 17 00:00:00 2001 From: Ryan VanGundy Date: Sun, 12 Oct 2025 12:38:55 -0400 Subject: [PATCH] fix(config): Set initial values when --set is passed A bug prevented the `windsor init --set ...` command/flag from setting initial values and creating the initial `values.yaml` file. --- pkg/config/config_handler.go | 2 +- pkg/config/config_handler_public_test.go | 45 +++++++++++++++++++----- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/pkg/config/config_handler.go b/pkg/config/config_handler.go index b8f4a1be0..71f211b76 100644 --- a/pkg/config/config_handler.go +++ b/pkg/config/config_handler.go @@ -317,7 +317,7 @@ func (c *configHandler) SaveConfig(overwrite ...bool) error { } } - if c.loaded && c.contextValues != nil && len(c.contextValues) > 0 { + if len(c.contextValues) > 0 { if err := c.saveContextValues(); err != nil { return fmt.Errorf("error saving values.yaml: %w", err) } diff --git a/pkg/config/config_handler_public_test.go b/pkg/config/config_handler_public_test.go index cebda999c..eac8a07ca 100644 --- a/pkg/config/config_handler_public_test.go +++ b/pkg/config/config_handler_public_test.go @@ -1224,8 +1224,7 @@ contexts: } }) - t.Run("SkipsSavingContextValuesWhenNotLoaded", func(t *testing.T) { - // Given a configHandler with contextValues but not loaded + t.Run("SavesContextValuesEvenWhenNotLoaded", func(t *testing.T) { handler, mocks := setup(t) tempDir := t.TempDir() @@ -1233,24 +1232,54 @@ contexts: return tempDir, nil } - handler.(*configHandler).context = "test-context" + contextValue := "test-context" + handler.(*configHandler).shims.WriteFile = os.WriteFile + handler.(*configHandler).shims.MkdirAll = os.MkdirAll + handler.(*configHandler).shims.Stat = os.Stat + handler.(*configHandler).shims.Getenv = func(key string) string { + if key == "WINDSOR_CONTEXT" { + return contextValue + } + return "" + } + handler.(*configHandler).shims.Setenv = func(key, value string) error { + if key == "WINDSOR_CONTEXT" { + contextValue = value + } + return nil + } + + if err := handler.SetContext("test-context"); err != nil { + t.Fatalf("Failed to set context: %v", err) + } + handler.(*configHandler).loaded = false handler.(*configHandler).contextValues = map[string]any{ "test_key": "test_value", } - // When SaveConfig is called err := handler.SaveConfig() - // Then no error should be returned if err != nil { t.Errorf("Expected no error, got %v", err) } - // And values.yaml should NOT be created valuesPath := filepath.Join(tempDir, "contexts", "test-context", "values.yaml") - if _, err := os.Stat(valuesPath); !os.IsNotExist(err) { - t.Errorf("values.yaml should not have been created when not loaded") + + if _, err := os.Stat(valuesPath); os.IsNotExist(err) { + contextDir := filepath.Join(tempDir, "contexts", "test-context") + files, _ := os.ReadDir(contextDir) + t.Logf("Files in context directory: %v", files) + t.Errorf("values.yaml should have been created even when not loaded") + } + + content, err := os.ReadFile(valuesPath) + if err != nil { + t.Fatalf("Failed to read values.yaml: %v", err) + } + + if !strings.Contains(string(content), "test_value") { + t.Errorf("values.yaml should contain 'test_value', got: %s", string(content)) } })