Skip to content

Commit e573680

Browse files
fix implementation of variable checking
1 parent df3ee21 commit e573680

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

internal/backend/local/test.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ func (runner *TestFileRunner) run(run *moduletest.Run, file *moduletest.File, st
384384

385385
vars, diag := runner.GetGlobalAndSuiteVariables(runner.Suite.Config)
386386
run.Diagnostics = run.Diagnostics.Append(diag)
387+
387388
// TODO: Merge local variables and suite-level variables
388389
// It might be necessary to change the format or add an extra parameter, not sure yet
389390
resetConfig, configDiags := configtest.TransformConfigForTest(config, run, file, vars, runner.PriorOutputs, runner.Suite.configProviders[key])
@@ -1052,17 +1053,37 @@ func (runner *TestFileRunner) GetVariables(config *configs.Config, run *modulete
10521053
values[name] = runner.getGlobalVariable(name, variable, config)
10531054
}
10541055

1056+
// We don't care if the file level variables are relevant or not
1057+
ignoreRelevance := func(name string, expr hcl.Expression) (diags tfdiags.Diagnostics) {
1058+
return diags
1059+
}
1060+
10551061
// Second, we'll check the file level variables
10561062
// This is a bit more complicated, as the file and run level variables can reference
10571063
// previously defined variables.
1058-
fileValues, fileDiags := runner.getVariablesFromConfiguration(values, relevantVariables, run.Name, runner.fileVariables)
1064+
fileValues, fileDiags := runner.getVariablesFromConfiguration(values, ignoreRelevance, runner.fileVariables)
10591065
diags = diags.Append(fileDiags)
10601066
for name, value := range fileValues {
10611067
values[name] = value
10621068
}
10631069

1070+
// We want to make sure every variable declared in the run block is actually relevant.
1071+
validateRelevance := func(name string, expr hcl.Expression) (diags tfdiags.Diagnostics) {
1072+
if !relevantVariables[name] {
1073+
// We'll add a warning for this. Since we're right in the run block
1074+
// users shouldn't be defining variables that are not relevant.
1075+
diags = diags.Append(&hcl.Diagnostic{
1076+
Severity: hcl.DiagWarning,
1077+
Summary: "Value for undeclared variable",
1078+
Detail: fmt.Sprintf("The module under test does not declare a variable named %q, but it is declared in run block %q.", name, run.Name),
1079+
Subject: expr.Range().Ptr(),
1080+
})
1081+
}
1082+
return diags
1083+
}
1084+
10641085
// Third, we'll check the run level variables.
1065-
runValues, runDiags := runner.getVariablesFromConfiguration(values, relevantVariables, run.Name, run.Config.Variables)
1086+
runValues, runDiags := runner.getVariablesFromConfiguration(values, validateRelevance, run.Config.Variables)
10661087
diags = diags.Append(runDiags)
10671088
for name, value := range runValues {
10681089
values[name] = value
@@ -1103,7 +1124,6 @@ func (runner *TestFileRunner) GetVariables(config *configs.Config, run *modulete
11031124
SourceRange: tfdiags.SourceRangeFromHCL(variable.DeclRange),
11041125
}
11051126
}
1106-
11071127
}
11081128

11091129
return values, diags
@@ -1139,7 +1159,7 @@ func (runner *TestFileRunner) getGlobalVariable(name string, variable backend.Un
11391159

11401160
// getVariablesFromConfiguration will process the variables from the configuration
11411161
// and return a map of the variables and their values.
1142-
func (runner *TestFileRunner) getVariablesFromConfiguration(knownVariables terraform.InputValues, relevantVariables map[string]bool, runName string, variableConfig map[string]hcl.Expression) (terraform.InputValues, tfdiags.Diagnostics) {
1162+
func (runner *TestFileRunner) getVariablesFromConfiguration(knownVariables terraform.InputValues, validateRelevance func(string, hcl.Expression) tfdiags.Diagnostics, variableConfig map[string]hcl.Expression) (terraform.InputValues, tfdiags.Diagnostics) {
11431163
var exprs []hcl.Expression
11441164
var diags tfdiags.Diagnostics
11451165
variableValues := make(terraform.InputValues)
@@ -1169,15 +1189,9 @@ func (runner *TestFileRunner) getVariablesFromConfiguration(knownVariables terra
11691189
}
11701190

11711191
for name, expr := range variableConfig {
1172-
if relevantVariables != nil && !relevantVariables[name] {
1173-
// We'll add a warning for this. Since we're right in the run block
1174-
// users shouldn't be defining variables that are not relevant.
1175-
diags = diags.Append(&hcl.Diagnostic{
1176-
Severity: hcl.DiagWarning,
1177-
Summary: "Value for undeclared variable",
1178-
Detail: fmt.Sprintf("The module under test does not declare a variable named %q, but it is declared in run block %q.", name, runName),
1179-
Subject: expr.Range().Ptr(),
1180-
})
1192+
relevanceDiags := validateRelevance(name, expr)
1193+
if relevanceDiags.HasWarnings() {
1194+
diags = diags.Append(relevanceDiags)
11811195
continue
11821196
}
11831197

@@ -1208,7 +1222,7 @@ func (runner *TestFileRunner) GetGlobalAndSuiteVariables(config *configs.Config)
12081222
values[name] = runner.getGlobalVariable(name, variable, config)
12091223
}
12101224

1211-
fileValues, fileDiags := runner.getVariablesFromConfiguration(values, nil, "TODO: Rewrite error to either not mention run block or wrap the error", runner.fileVariables)
1225+
fileValues, fileDiags := runner.getVariablesFromConfiguration(values, func(s string, e hcl.Expression) tfdiags.Diagnostics { return tfdiags.Diagnostics{} }, runner.fileVariables)
12121226
diags = diags.Append(fileDiags)
12131227
for name, value := range fileValues {
12141228
values[name] = value

0 commit comments

Comments
 (0)