diff --git a/modules/terraform/apply_test.go b/modules/terraform/apply_test.go index 56c12c95d..2cabcfe8a 100644 --- a/modules/terraform/apply_test.go +++ b/modules/terraform/apply_test.go @@ -1,6 +1,7 @@ package terraform import ( + "path/filepath" "testing" "time" @@ -201,3 +202,28 @@ func TestTgApplyUseLockNoError(t *testing.T) { // make sure -lock CLI option is passed down correctly require.Contains(t, out, "-lock=true") } + +func TestApplyWithPlanFile(t *testing.T) { + t.Parallel() + + testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-basic-configuration", t.Name()) + require.NoError(t, err) + planFilePath := filepath.Join(testFolder, "plan.out") + + options := &Options{ + TerraformDir: testFolder, + Vars: map[string]interface{}{ + "cnt": 1, + }, + NoColor: true, + PlanFilePath: planFilePath, + } + _, err = InitAndPlanE(t, options) + require.NoError(t, err) + require.FileExists(t, planFilePath, "Plan file was not saved to expected location:", planFilePath) + + out, err := ApplyE(t, options) + require.NoError(t, err) + require.Contains(t, out, "1 added, 0 changed, 0 destroyed.") + require.NotRegexp(t, `\[\d*m`, out, "Output should not contain color escape codes") +} diff --git a/modules/terraform/cmd.go b/modules/terraform/cmd.go index b4725927c..3591c083a 100644 --- a/modules/terraform/cmd.go +++ b/modules/terraform/cmd.go @@ -32,10 +32,6 @@ var commandsWithParallelism = []string{ // GetCommonOptions extracts commons terraform options func GetCommonOptions(options *Options, args ...string) (*Options, []string) { - if options.NoColor && !collections.ListContains(args, "-no-color") { - args = append(args, "-no-color") - } - if options.TerraformBinary == "" { options.TerraformBinary = "terraform" } diff --git a/modules/terraform/format.go b/modules/terraform/format.go index a04b1df50..4edd1b16d 100644 --- a/modules/terraform/format.go +++ b/modules/terraform/format.go @@ -50,11 +50,22 @@ func FormatArgs(options *Options, args ...string) []string { lockSupported := collections.ListContains(TerraformCommandsWithLockSupport, commandType) planFileSupported := collections.ListContains(TerraformCommandsWithPlanFileSupport, commandType) + // Include -var and -var-file flags unless we're running 'apply' with a plan file + includeVars := !(commandType == "apply" && len(options.PlanFilePath) > 0) + terraformArgs = append(terraformArgs, args...) - terraformArgs = append(terraformArgs, FormatTerraformVarsAsArgs(options.Vars)...) - terraformArgs = append(terraformArgs, FormatTerraformArgs("-var-file", options.VarFiles)...) + + if includeVars { + terraformArgs = append(terraformArgs, FormatTerraformVarsAsArgs(options.Vars)...) + terraformArgs = append(terraformArgs, FormatTerraformArgs("-var-file", options.VarFiles)...) + } + terraformArgs = append(terraformArgs, FormatTerraformArgs("-target", options.Targets)...) + if options.NoColor { + terraformArgs = append(terraformArgs, "-no-color") + } + if lockSupported { // If command supports locking, handle lock arguments terraformArgs = append(terraformArgs, FormatTerraformLockAsArgs(options.Lock, options.LockTimeout)...)