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

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

View File

@ -23,6 +23,7 @@ BUG FIXES:
* Ensured that using a sensitive path for templatefile that it doesn't panic([#1801](https://github.com/opentofu/opentofu/issues/1801))
* Fixed a crash when module source is not present ([#1888](https://github.com/opentofu/opentofu/pull/1888))
* Fixed a crash when importing an empty optional sensitive string ([#1986](https://github.com/opentofu/opentofu/pull/1986))
* Fixed autoloaded test tfvar files being used in non-test scenarios ([#2039](https://github.com/opentofu/opentofu/pull/2039))
## Previous Releases

View File

@ -25,6 +25,15 @@ import (
// for root module input variables.
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
// values can come from and constructs a map ready to be passed to the
// 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
// search for all files ending in .auto.tfvars.
diags = diags.Append(m.addVarsFromDir(".", ret))
diags = diags.Append(m.addVarsFromDir("tests", ret))
// Finally we process values given explicitly on the command line, either
// as individual literal settings or as additional files to read.

View File

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