Skip to content

Commit 89e345c

Browse files
committed
Fix plan output relative
1 parent 75ffd55 commit 89e345c

File tree

8 files changed

+78
-1
lines changed

8 files changed

+78
-1
lines changed

configstack/module.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,12 @@ func (module *TerraformModule) getPlanFilePath(l log.Logger, opts *options.Terra
134134
return ""
135135
}
136136

137-
path, _ := filepath.Rel(opts.WorkingDir, module.Path)
137+
// module path will be an absolute path so working dir should absolute too so that Rel() can work
138+
wdir, err := filepath.Abs(opts.WorkingDir)
139+
if err != nil {
140+
wdir = opts.WorkingDir
141+
}
142+
path, _ := filepath.Rel(wdir, module.Path)
138143
dir := filepath.Join(outputFolder, path)
139144

140145
if !filepath.IsAbs(dir) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
variable "terragrunt_dir" {}
2+
3+
output "terragrunt_dir" {
4+
value = var.terragrunt_dir
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
terraform {
2+
source = "."
3+
}
4+
5+
inputs = {
6+
terragrunt_dir = "."
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
variable "terragrunt_dir" {}
2+
3+
output "terragrunt_dir" {
4+
value = var.terragrunt_dir
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
terraform {
2+
source = "."
3+
}
4+
5+
inputs = {
6+
terragrunt_dir = "."
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
variable "terragrunt_dir" {}
2+
3+
output "terragrunt_dir" {
4+
value = var.terragrunt_dir
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
terraform {
2+
source = "."
3+
}
4+
5+
inputs = {
6+
terragrunt_dir = "."
7+
}

test/integration_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ const (
9292
testFixtureParallelism = "fixtures/parallelism"
9393
testFixturePath = "fixtures/terragrunt/"
9494
testFixturePlanfileOrder = "fixtures/planfile-order-test"
95+
testFixturePlanOutput = "fixtures/plan-output"
9596
testFixtureProviderCacheDirect = "fixtures/provider-cache/direct"
9697
testFixtureProviderCacheFilesystemMirror = "fixtures/provider-cache/filesystem-mirror"
9798
testFixtureProviderCacheMultiplePlatforms = "fixtures/provider-cache/multiple-platforms"
@@ -4186,3 +4187,38 @@ func TestVersionIsInvokedInDifferentDirectory(t *testing.T) {
41864187
assert.Len(t, matches, 2, "Expected exactly one occurrence of '-version' command, found %d", len(matches))
41874188
assert.Contains(t, stderr, "prefix=dependency-with-custom-version msg=Running command: "+wrappedBinary()+" -version")
41884189
}
4190+
4191+
func TestTerragruntPlanAllOutput(t *testing.T) {
4192+
t.Parallel()
4193+
4194+
helpers.CleanupTerraformFolder(t, testFixturePlanOutput)
4195+
tmpEnvPath := helpers.CopyEnvironment(t, testFixturePlanOutput)
4196+
4197+
outDir := filepath.Join(tmpEnvPath, "plans")
4198+
pwd := filepath.Join(tmpEnvPath, testFixturePlanOutput)
4199+
{
4200+
originalDir, err := os.Getwd()
4201+
require.NoError(t, err)
4202+
t.Cleanup(func() {
4203+
require.NoError(t, os.Chdir(originalDir))
4204+
})
4205+
require.NoError(t, os.Chdir(pwd))
4206+
}
4207+
4208+
cmd := fmt.Sprintf("terragrunt plan --all --non-interactive --out-dir %s --working-dir %s ", outDir, ".")
4209+
var (
4210+
stdout bytes.Buffer
4211+
stderr bytes.Buffer
4212+
)
4213+
// Call helpers.RunTerragruntCommand directly because this command contains failures (which causes helpers.RunTerragruntRedirectOutput to abort) but we don't care.
4214+
err := helpers.RunTerragruntCommand(t, cmd, &stdout, &stderr)
4215+
require.NoError(t, err)
4216+
4217+
output := stdout.String()
4218+
errOutput := stderr.String()
4219+
fmt.Printf("STDERR is %s.\n STDOUT is %s", errOutput, output)
4220+
4221+
assert.FileExists(t, filepath.Join(outDir, "vnet", "tfplan.tfplan"))
4222+
assert.FileExists(t, filepath.Join(outDir, "resource-group", "tfplan.tfplan"))
4223+
assert.FileExists(t, filepath.Join(outDir, "private-dns-zone", "tfplan.tfplan"))
4224+
}

0 commit comments

Comments
 (0)