Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions pkg/env/terraform_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
59 changes: 59 additions & 0 deletions pkg/env/terraform_env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading