From 37e1085f73d022fa31a87e3398aa7aca4d2b23d3 Mon Sep 17 00:00:00 2001 From: Christian Mesh Date: Wed, 2 Oct 2024 14:31:16 -0400 Subject: [PATCH] Make sure test vars are only processed during test commands (#2040) Signed-off-by: Christian Mesh --- CHANGELOG.md | 5 +++++ internal/command/meta_vars.go | 10 +++++++++- internal/command/test.go | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41958bb839..814df496ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 SECURITY: diff --git a/internal/command/meta_vars.go b/internal/command/meta_vars.go index 9811855419..b398cd482e 100644 --- a/internal/command/meta_vars.go +++ b/internal/command/meta_vars.go @@ -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. diff --git a/internal/command/test.go b/internal/command/test.go index c37caaded2..b7b826375f 100644 --- a/internal/command/test.go +++ b/internal/command/test.go @@ -125,7 +125,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)