mirror of
https://github.com/opentffoundation/opentf.git
synced 2026-05-20 12:02:01 -04:00
Fix opentf test crash on nil output (#267)
Co-authored-by: Elbaz <eran.elbaz@env0.com>
This commit is contained in:
@@ -152,6 +152,10 @@ func TestTest(t *testing.T) {
|
||||
expected: "3 passed, 0 failed.",
|
||||
code: 0,
|
||||
},
|
||||
"null_output": {
|
||||
expected: "1 passed, 0 failed.",
|
||||
code: 0,
|
||||
},
|
||||
}
|
||||
for name, tc := range tcs {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
@@ -227,6 +231,11 @@ func TestTest_Full_Output(t *testing.T) {
|
||||
expected: "Blocks of type \"check\" are not expected here.",
|
||||
code: 1,
|
||||
},
|
||||
"not_exists_output": {
|
||||
expected: "Error: Reference to undeclared output value",
|
||||
args: []string{"-no-color"},
|
||||
code: 1,
|
||||
},
|
||||
"refresh_conflicting_config": {
|
||||
expected: "Incompatible plan options",
|
||||
code: 1,
|
||||
|
||||
2
internal/command/testdata/test/not_exists_output/main.tf
vendored
Normal file
2
internal/command/testdata/test/not_exists_output/main.tf
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
resource "test_resource" "resource" {
|
||||
}
|
||||
6
internal/command/testdata/test/not_exists_output/main.tftest.hcl
vendored
Normal file
6
internal/command/testdata/test/not_exists_output/main.tftest.hcl
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
run "not_exists" {
|
||||
assert {
|
||||
condition = output.something_that_does_not_exist == null
|
||||
error_message = "Should fail for Reference to undeclared output value"
|
||||
}
|
||||
}
|
||||
3
internal/command/testdata/test/null_output/main.tf
vendored
Normal file
3
internal/command/testdata/test/null_output/main.tf
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
output "my_null_output" {
|
||||
value = null
|
||||
}
|
||||
6
internal/command/testdata/test/null_output/main.tftest.hcl
vendored
Normal file
6
internal/command/testdata/test/null_output/main.tftest.hcl
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
run "null" {
|
||||
assert {
|
||||
condition = output.my_null_output == null
|
||||
error_message = "Should work"
|
||||
}
|
||||
}
|
||||
@@ -975,17 +975,24 @@ func (d *evaluationStateData) GetOutput(addr addrs.OutputValue, rng tfdiags.Sour
|
||||
|
||||
output := d.Evaluator.State.OutputValue(addr.Absolute(d.ModulePath))
|
||||
|
||||
val := output.Value
|
||||
if val == cty.NilVal {
|
||||
// Not evaluated yet?
|
||||
val = cty.DynamicVal
|
||||
}
|
||||
// https://github.com/opentffoundation/opentf/issues/257
|
||||
// If the output is null - it does not serialize as part of the node_output state https://github.com/opentffoundation/opentf/blob/4b623c56ffe9e6c1dc345e54470b71b0f261297a/internal/opentf/node_output.go#L592-L596
|
||||
// In such a case, we should simply return a nil value because OpenTF test crash to evaluate for invalid memory address or nil pointer dereference
|
||||
if output == nil {
|
||||
return cty.NilVal, diags
|
||||
} else {
|
||||
val := output.Value
|
||||
if val == cty.NilVal {
|
||||
// Not evaluated yet?
|
||||
val = cty.DynamicVal
|
||||
}
|
||||
|
||||
if output.Sensitive {
|
||||
val = val.Mark(marks.Sensitive)
|
||||
}
|
||||
if output.Sensitive {
|
||||
val = val.Mark(marks.Sensitive)
|
||||
}
|
||||
|
||||
return val, diags
|
||||
return val, diags
|
||||
}
|
||||
}
|
||||
|
||||
func (d *evaluationStateData) GetCheckBlock(addr addrs.Check, rng tfdiags.SourceRange) (cty.Value, tfdiags.Diagnostics) {
|
||||
|
||||
Reference in New Issue
Block a user