From 6b87aa93a3b4b2118a86bbb2150893ae9032bb57 Mon Sep 17 00:00:00 2001 From: Aidar Samerkhanov Date: Fri, 14 Oct 2022 23:14:50 +0300 Subject: [PATCH] #1160 Specify -var-file before -var --- modules/terraform/format.go | 2 +- modules/terraform/format_test.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/modules/terraform/format.go b/modules/terraform/format.go index f50072d7c..40c2d1f95 100644 --- a/modules/terraform/format.go +++ b/modules/terraform/format.go @@ -56,8 +56,8 @@ func FormatArgs(options *Options, args ...string) []string { terraformArgs = append(terraformArgs, args...) if includeVars { - terraformArgs = append(terraformArgs, FormatTerraformVarsAsArgs(options.Vars)...) terraformArgs = append(terraformArgs, FormatTerraformArgs("-var-file", options.VarFiles)...) + terraformArgs = append(terraformArgs, FormatTerraformVarsAsArgs(options.Vars)...) } terraformArgs = append(terraformArgs, FormatTerraformArgs("-target", options.Targets)...) diff --git a/modules/terraform/format_test.go b/modules/terraform/format_test.go index acc3c1f06..50417f5e2 100644 --- a/modules/terraform/format_test.go +++ b/modules/terraform/format_test.go @@ -278,3 +278,24 @@ func TestFormatArgsAppliesLockCorrectly(t *testing.T) { assert.Equal(t, testCase.expected, FormatArgs(&Options{}, testCase.command...)) } } + +func TestFormatArgsAppliesVarsFilesAndVarsCorrectly(t *testing.T) { + t.Parallel() + + testCases := []struct { + varFile []string + vars map[string]interface{} + expected []string + }{ + {[]string{}, map[string]interface{}{"foo": "bar"}, []string{"apply", "-var", "foo=bar", "-lock=false"}}, + {[]string{"var_file.tfvars"}, map[string]interface{}{}, []string{"apply", "-var-file", "var_file.tfvars", "-lock=false"}}, + {[]string{"var_file.tfvars"}, map[string]interface{}{"foo": "bar"}, []string{"apply", "-var-file", "var_file.tfvars", "-var", "foo=bar", "-lock=false"}}, + } + + for _, testCase := range testCases { + assert.Equal(t, testCase.expected, FormatArgs(&Options{ + VarFiles: testCase.varFile, + Vars: testCase.vars, + }, "apply")) + } +}