Skip to content

Commit

Permalink
Merge pull request #1217 from chilledornaments/master
Browse files Browse the repository at this point in the history
Add ability to reorder var and var-file options
  • Loading branch information
denis256 committed Dec 19, 2022
2 parents 5d6baeb + cee5e91 commit e63181e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
9 changes: 7 additions & 2 deletions modules/terraform/format.go
Expand Up @@ -56,8 +56,13 @@ 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)...)
if options.SetVarsAfterVarFiles {
terraformArgs = append(terraformArgs, FormatTerraformArgs("-var-file", options.VarFiles)...)
terraformArgs = append(terraformArgs, FormatTerraformVarsAsArgs(options.Vars)...)
} else {
terraformArgs = append(terraformArgs, FormatTerraformVarsAsArgs(options.Vars)...)
terraformArgs = append(terraformArgs, FormatTerraformArgs("-var-file", options.VarFiles)...)
}
}

terraformArgs = append(terraformArgs, FormatTerraformArgs("-target", options.Targets)...)
Expand Down
34 changes: 34 additions & 0 deletions modules/terraform/format_test.go
Expand Up @@ -278,3 +278,37 @@ func TestFormatArgsAppliesLockCorrectly(t *testing.T) {
assert.Equal(t, testCase.expected, FormatArgs(&Options{}, testCase.command...))
}
}

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

testCases := []struct {
command []string
vars map[string]interface{}
varFiles []string
setVarsAfterVarFiles bool
expected []string
}{
{[]string{"plan"}, map[string]interface{}{"foo": "bar"}, []string{"test.tfvars"}, true, []string{"plan", "-var-file", "test.tfvars", "-var", "foo=bar", "-lock=false"}},
{[]string{"plan"}, map[string]interface{}{"foo": "bar", "hello": "world"}, []string{"test.tfvars"}, true, []string{"plan", "-var-file", "test.tfvars", "-var", "foo=bar", "-var", "hello=world", "-lock=false"}},
{[]string{"plan"}, map[string]interface{}{"foo": "bar", "hello": "world"}, []string{"test.tfvars"}, false, []string{"plan", "-var", "foo=bar", "-var", "hello=world", "-var-file", "test.tfvars", "-lock=false"}},
{[]string{"plan"}, map[string]interface{}{"foo": "bar"}, []string{"test.tfvars"}, false, []string{"plan", "-var", "foo=bar", "-var-file", "test.tfvars", "-lock=false"}},
}

for _, testCase := range testCases {
result := FormatArgs(&Options{SetVarsAfterVarFiles: testCase.setVarsAfterVarFiles, Vars: testCase.vars, VarFiles: testCase.varFiles}, testCase.command...)

// Make sure that -var and -var-file options are in the expected order relative to each other
// Note that the order of the different -var and -var-file options may change
// See this comment for more info: https://github.com/gruntwork-io/terratest/blob/6fb86056797e3e62ebdd9011ba26605e0976a6f8/modules/terraform/format_test.go#L123-L142
for idx, arg := range result {
if arg == "-var-file" || arg == "-var" {
assert.Equal(t, testCase.expected[idx], arg)
}
}

// Make sure that the order of other arguments hasn't been incorrectly modified
assert.Equal(t, testCase.expected[0], result[0])
assert.Equal(t, testCase.expected[len(testCase.expected)-1], result[len(result)-1])
}
}
1 change: 1 addition & 0 deletions modules/terraform/options.go
Expand Up @@ -70,6 +70,7 @@ type Options struct {
Parallelism int // Set the parallelism setting for Terraform
PlanFilePath string // The path to output a plan file to (for the plan command) or read one from (for the apply command)
PluginDir string // The path of downloaded plugins to pass to the terraform init command (-plugin-dir)
SetVarsAfterVarFiles bool // Pass -var options after -var-file options to Terraform commands
}

// Clone makes a deep copy of most fields on the Options object and returns it.
Expand Down

0 comments on commit e63181e

Please sign in to comment.