Make sure test vars are only processed during test commands (#2040)

Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
This commit is contained in:
Christian Mesh 2024-10-02 14:31:16 -04:00 committed by GitHub
parent 1e61ce1bfd
commit 37e1085f73
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 2 deletions

View File

@ -1,3 +1,8 @@
## 1.8.3
BUG FIXES:
* Fixed autoloaded test tfvar files being used in non-test scenarios ([#2039](https://github.com/opentofu/opentofu/pull/2039))
## 1.8.2 ## 1.8.2
SECURITY: SECURITY:

View File

@ -25,6 +25,15 @@ import (
// for root module input variables. // for root module input variables.
const VarEnvPrefix = "TF_VAR_" const VarEnvPrefix = "TF_VAR_"
// collectVariableValuesWithTests inspects the same sources of variables as
// collectVariableValues, but also includes any autoloaded variables from the
// given tests directory.
func (m *Meta) collectVariableValuesWithTests(testDir string) (map[string]backend.UnparsedVariableValue, tfdiags.Diagnostics) {
values, diags := m.collectVariableValues()
diags = diags.Append(m.addVarsFromDir(testDir, values))
return values, diags
}
// collectVariableValues inspects the various places that root module input variable // collectVariableValues inspects the various places that root module input variable
// values can come from and constructs a map ready to be passed to the // values can come from and constructs a map ready to be passed to the
// backend as part of a backend.Operation. // backend as part of a backend.Operation.
@ -73,7 +82,6 @@ func (m *Meta) collectVariableValues() (map[string]backend.UnparsedVariableValue
// There's the original terraform.tfvars (DefaultVarsFilename) along with the later-added // There's the original terraform.tfvars (DefaultVarsFilename) along with the later-added
// search for all files ending in .auto.tfvars. // search for all files ending in .auto.tfvars.
diags = diags.Append(m.addVarsFromDir(".", ret)) diags = diags.Append(m.addVarsFromDir(".", ret))
diags = diags.Append(m.addVarsFromDir("tests", ret))
// Finally we process values given explicitly on the command line, either // Finally we process values given explicitly on the command line, either
// as individual literal settings or as additional files to read. // as individual literal settings or as additional files to read.

View File

@ -125,7 +125,7 @@ func (c *TestCommand) Run(rawArgs []string) int {
} }
c.variableArgs = rawFlags{items: &items} c.variableArgs = rawFlags{items: &items}
variables, variableDiags := c.collectVariableValues() variables, variableDiags := c.collectVariableValuesWithTests(args.TestDirectory)
diags = diags.Append(variableDiags) diags = diags.Append(variableDiags)
if variableDiags.HasErrors() { if variableDiags.HasErrors() {
view.Diagnostics(nil, nil, diags) view.Diagnostics(nil, nil, diags)