From e9fe47dbdcf6c8e86c591c1185423dabca556aa9 Mon Sep 17 00:00:00 2001 From: Ryan VanGundy Date: Sun, 13 Jul 2025 11:06:56 -0400 Subject: [PATCH] fix(env): Enhance generateBackendOverrideTf to accept custom directory Fixes an issue in which the `backend_override.tf` file was not being generated during a `windsor down` operation. Related to changes in which we pass explicit paths in during terraform operations. --- pkg/env/terraform_env.go | 20 +++++++----- pkg/env/terraform_env_test.go | 59 +++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 8 deletions(-) diff --git a/pkg/env/terraform_env.go b/pkg/env/terraform_env.go index a69bbe3cb..f8fcb8195 100644 --- a/pkg/env/terraform_env.go +++ b/pkg/env/terraform_env.go @@ -215,7 +215,7 @@ func (e *TerraformEnvPrinter) generateBackendOverrideTf(directory ...string) err } } - projectPath, err := e.findRelativeTerraformProjectPath() + projectPath, err := e.findRelativeTerraformProjectPath(directory...) if err != nil { return fmt.Errorf("error finding project path: %w", err) } @@ -422,15 +422,19 @@ var sanitizeForK8s = func(input string) string { } // findRelativeTerraformProjectPath locates the Terraform project path by checking the current -// directory and its ancestors for Terraform files, returning the relative path if found. -func (e *TerraformEnvPrinter) findRelativeTerraformProjectPath() (string, error) { - currentPath, err := e.shims.Getwd() - if err != nil { - return "", fmt.Errorf("error getting current directory: %w", err) +// directory (or provided directory) and its ancestors for Terraform files, returning the relative path if found. +func (e *TerraformEnvPrinter) findRelativeTerraformProjectPath(directory ...string) (string, error) { + var currentPath string + if len(directory) > 0 { + currentPath = filepath.Clean(directory[0]) + } else { + var err error + currentPath, err = e.shims.Getwd() + if err != nil { + return "", fmt.Errorf("error getting current directory: %w", err) + } } - currentPath = filepath.Clean(currentPath) - globPattern := filepath.Join(currentPath, "*.tf") matches, err := e.shims.Glob(globPattern) if err != nil { diff --git a/pkg/env/terraform_env_test.go b/pkg/env/terraform_env_test.go index d29256861..9c26bfde8 100644 --- a/pkg/env/terraform_env_test.go +++ b/pkg/env/terraform_env_test.go @@ -941,6 +941,65 @@ func TestTerraformEnv_generateBackendOverrideTf(t *testing.T) { t.Errorf("Expected error message to contain 'error removing backend_override.tf', got %v", err) } }) + + t.Run("WithSpecificDirectory", func(t *testing.T) { + // Given a TerraformEnvPrinter with mock configuration + printer, mocks := setup(t) + + // Track which directory was used for finding terraform files + var usedDirectory string + originalGlob := mocks.Shims.Glob + mocks.Shims.Glob = func(pattern string) ([]string, error) { + // Extract the directory from the pattern + if strings.Contains(pattern, "*.tf") { + usedDirectory = filepath.Dir(pattern) + // Return terraform files in the specified directory + return []string{ + filepath.Join(usedDirectory, "file1.tf"), + filepath.Join(usedDirectory, "file2.tf"), + }, nil + } + return originalGlob(pattern) + } + + // Mock WriteFile to capture the output + var writtenData []byte + var writtenPath string + mocks.Shims.WriteFile = func(filename string, data []byte, perm os.FileMode) error { + writtenData = data + writtenPath = filename + return nil + } + + // Specify a custom directory + customDir := filepath.Join("custom", "terraform", "module", "path") + + // When generateBackendOverrideTf is called with a specific directory + err := printer.generateBackendOverrideTf(customDir) + + // Then no error should occur and the custom directory should be used + if err != nil { + t.Errorf("Expected no error, got %v", err) + } + + // Verify that the custom directory was used for finding terraform files + if !strings.Contains(usedDirectory, customDir) { + t.Errorf("Expected directory %q to be used, but got %q", customDir, usedDirectory) + } + + // Verify that the backend_override.tf file is written to the custom directory + if !strings.Contains(writtenPath, customDir) { + t.Errorf("Expected backend_override.tf to be written to %q, but got %q", customDir, writtenPath) + } + + // Verify the content is correct + expectedContent := `terraform { + backend "local" {} +}` + if string(writtenData) != expectedContent { + t.Errorf("Expected backend config %q, got %q", expectedContent, string(writtenData)) + } + }) } func TestTerraformEnv_generateBackendConfigArgs(t *testing.T) {