[BACKPORT] fix: type defaults for variables in tests (#2244) (#2247)

Signed-off-by: ollevche <ollevche@gmail.com>
This commit is contained in:
Oleksandr Levchenkov 2024-12-04 17:00:09 +02:00 committed by GitHub
parent e1daace5ea
commit 39a15ab499
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 31 additions and 0 deletions

View File

@ -5,6 +5,7 @@ BUG FIXES:
* Changes to encryption configuration now auto-apply the migration ([#2232](https://github.com/opentofu/opentofu/pull/2232)) * Changes to encryption configuration now auto-apply the migration ([#2232](https://github.com/opentofu/opentofu/pull/2232))
* Updated github.com/golang-jwt/jwt/v4 from 4.4.2 to 4.5.1 to make security scanners happy (no vulnerability, see [#2179](https://github.com/opentofu/opentofu/pull/2179)) * Updated github.com/golang-jwt/jwt/v4 from 4.4.2 to 4.5.1 to make security scanners happy (no vulnerability, see [#2179](https://github.com/opentofu/opentofu/pull/2179))
* `tofu test` is now setting `null`s for dynamic type when generating mock values. ([#2245](https://github.com/opentofu/opentofu/pull/2245)) * `tofu test` is now setting `null`s for dynamic type when generating mock values. ([#2245](https://github.com/opentofu/opentofu/pull/2245))
* Variables declared in test files are now taking into account type default values. ([#2244](https://github.com/opentofu/opentofu/pull/2244))
## 1.8.6 ## 1.8.6

View File

@ -1130,6 +1130,13 @@ func parseAndApplyDefaultValues(unparsedVariables map[string]backend.UnparsedVar
for name, variable := range unparsedVariables { for name, variable := range unparsedVariables {
value, valueDiags := variable.ParseVariableValue(configs.VariableParseLiteral) value, valueDiags := variable.ParseVariableValue(configs.VariableParseLiteral)
diags = diags.Append(valueDiags) diags = diags.Append(valueDiags)
// Even so the variable is declared, some of the fields could
// be empty and filled in via type default values.
if confVariable, ok := configVariables[name]; ok && confVariable.TypeDefaults != nil {
value.Value = confVariable.TypeDefaults.Apply(value.Value)
}
inputs[name] = value inputs[name] = value
} }

View File

@ -3,3 +3,10 @@ variable "input" {
type = string type = string
default = "Hello, world!" default = "Hello, world!"
} }
variable "another_input" {
type = object({
optional_string = optional(string, "type_default")
optional_number = optional(number, 42)
})
}

View File

@ -4,4 +4,20 @@ run "applies_defaults" {
condition = var.input == "Hello, world!" condition = var.input == "Hello, world!"
error_message = "should have applied default value" error_message = "should have applied default value"
} }
variables {
another_input = {
optional_string = "Hello, world!"
}
}
assert {
condition = var.another_input.optional_string == "Hello, world!"
error_message = "should have used custom value from test file"
}
assert {
condition = var.another_input.optional_number == 42
error_message = "should have used default type value"
}
} }