Skip to content

Commit

Permalink
gruntwork-io#1160 Specify -var-file before -var
Browse files Browse the repository at this point in the history
  • Loading branch information
aidarstg committed Oct 14, 2022
1 parent 404b916 commit 6b87aa9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion modules/terraform/format.go
Expand Up @@ -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)...)
Expand Down
21 changes: 21 additions & 0 deletions modules/terraform/format_test.go
Expand Up @@ -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"))
}
}

0 comments on commit 6b87aa9

Please sign in to comment.