Skip to content

Commit

Permalink
Relative path handling (#3523)
Browse files Browse the repository at this point in the history
* Relative path hanling

* Output checking simplification

* Cleanup

* abs path handling
  • Loading branch information
denis256 authored Oct 29, 2024
1 parent 09e9813 commit f32238c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ install-lint:
go install github.com/golangci/golangci-lint/cmd/[email protected]

run-lint:
golangci-lint run -v --timeout=5m ./...
golangci-lint run -v --timeout=10m ./...

run-strict-lint:
golangci-lint run -v --timeout=5m -c .strict.golangci.yml --new-from-rev origin/main ./...
golangci-lint run -v --timeout=10m -c .strict.golangci.yml --new-from-rev origin/main ./...

install-mockery:
go install github.com/vektra/mockery/[email protected]
Expand Down
34 changes: 19 additions & 15 deletions configstack/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,28 +114,32 @@ func (module *TerraformModule) planFile(terragruntOptions *options.TerragruntOpt

// outputFile - return plan file location, if output folder is set
func (module *TerraformModule) outputFile(opts *options.TerragruntOptions) string {
planFile := ""

if opts.OutputFolder != "" {
path, _ := filepath.Rel(opts.WorkingDir, module.Path)
dir := filepath.Join(opts.OutputFolder, path)
planFile = filepath.Join(dir, terraform.TerraformPlanFile)
}

return planFile
return module.getPlanFilePath(opts, opts.OutputFolder, terraform.TerraformPlanFile)
}

// outputJSONFile - return plan JSON file location, if JSON output folder is set
func (module *TerraformModule) outputJSONFile(opts *options.TerragruntOptions) string {
jsonPlanFile := ""
return module.getPlanFilePath(opts, opts.JSONOutputFolder, terraform.TerraformPlanJSONFile)
}

if opts.JSONOutputFolder != "" {
path, _ := filepath.Rel(opts.WorkingDir, module.Path)
dir := filepath.Join(opts.JSONOutputFolder, path)
jsonPlanFile = filepath.Join(dir, terraform.TerraformPlanJSONFile)
func (module *TerraformModule) getPlanFilePath(opts *options.TerragruntOptions, outputFolder, fileName string) string {
if outputFolder == "" {
return ""
}

path, _ := filepath.Rel(opts.WorkingDir, module.Path)
dir := filepath.Join(outputFolder, path)

if !filepath.IsAbs(dir) {
dir = filepath.Join(opts.WorkingDir, dir)
if absDir, err := filepath.Abs(dir); err == nil {
dir = absDir
} else {
opts.Logger.Warnf("Failed to get absolute path for %s: %v", dir, err)
}
}

return jsonPlanFile
return filepath.Join(dir, fileName)
}

// findModuleInPath returns true if a module is located under one of the target directories
Expand Down
49 changes: 49 additions & 0 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3663,6 +3663,55 @@ func TestStorePlanFilesRunAllPlanApply(t *testing.T) {
require.NoError(t, err)
}

func TestStorePlanFilesRunAllPlanApplyRelativePath(t *testing.T) {
t.Parallel()

tmpEnvPath := copyEnvironment(t, testFixtureOutDir)
cleanupTerraformFolder(t, tmpEnvPath)
testPath := util.JoinPath(tmpEnvPath, testFixtureOutDir)

// run plan with output directory
_, _, err := runTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt run-all plan --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir %s --terragrunt-out-dir %s", testPath, "test"))
require.NoError(t, err)

outDir := util.JoinPath(testPath, "test")

// verify that tfplan files are created in the tmpDir, 2 files
list, err := findFilesWithExtension(outDir, ".tfplan")
require.NoError(t, err)
assert.Len(t, list, 2)
for _, file := range list {
assert.Equal(t, "tfplan.tfplan", filepath.Base(file))
}

_, _, err = runTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt run-all apply --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir %s --terragrunt-out-dir test", testPath))
require.NoError(t, err)
}

func TestStorePlanFilesJsonRelativePath(t *testing.T) {
t.Parallel()

tmpEnvPath := copyEnvironment(t, testFixtureOutDir)
cleanupTerraformFolder(t, tmpEnvPath)
testPath := util.JoinPath(tmpEnvPath, testFixtureOutDir)

// run plan with output directory
_, _, err := runTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt run-all plan --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir %s --terragrunt-out-dir test --terragrunt-json-out-dir json", testPath))
require.NoError(t, err)

// verify that tfplan files are created in the tmpDir, 2 files
outDir := util.JoinPath(testPath, "test")
list, err := findFilesWithExtension(outDir, ".tfplan")
require.NoError(t, err)
assert.Len(t, list, 2)

// verify that json files are create
jsonDir := util.JoinPath(testPath, "json")
listJSON, err := findFilesWithExtension(jsonDir, ".json")
require.NoError(t, err)
assert.Len(t, listJSON, 2)
}

func TestPlanJsonFilesRunAll(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit f32238c

Please sign in to comment.