diff --git a/internal/command/test_test.go b/internal/command/test_test.go index 313f20b72050..94af10732550 100644 --- a/internal/command/test_test.go +++ b/internal/command/test_test.go @@ -223,11 +223,15 @@ func TestTest_Runs(t *testing.T) { code: 0, }, "mocking-invalid": { - expectedErr: []string{"Invalid outputs attribute"}, - initCode: 1, + expectedErr: []string{ + "Invalid outputs attribute", + "The override_computed attribute must be a boolean.", + }, + initCode: 1, }, "mocking-error": { - expectedErr: []string{"Unknown condition value", + expectedErr: []string{ + "Unknown condition value", "test_resource.primary[0].id", }, code: 1, diff --git a/internal/command/testdata/test/mocking-invalid/tests/override_computed_invalid_boolean.tftest.hcl b/internal/command/testdata/test/mocking-invalid/tests/override_computed_invalid_boolean.tftest.hcl new file mode 100644 index 000000000000..59ce59f3f3e8 --- /dev/null +++ b/internal/command/testdata/test/mocking-invalid/tests/override_computed_invalid_boolean.tftest.hcl @@ -0,0 +1,30 @@ +mock_provider "test" { + alias = "primary" + override_computed = foo // This should be a boolean value, therefore this test should fail + + mock_resource "test_resource" { + defaults = { + id = "aaaa" + } + } + + override_resource { + target = test_resource.primary + values = { + id = "bbbb" + } + } +} + +variables { + instances = 1 + child_instances = 1 +} + +run "test" { + + assert { + condition = test_resource.primary[0].id == "bbbb" + error_message = "mock not applied" + } +} diff --git a/internal/configs/mock_provider.go b/internal/configs/mock_provider.go index 00aabce77613..7b916d86118d 100644 --- a/internal/configs/mock_provider.go +++ b/internal/configs/mock_provider.go @@ -83,26 +83,17 @@ func extractOverrideComputed(content *hcl.BodyContent) (*bool, hcl.Diagnostics) if attr, exists := content.Attributes[overrideComputed]; exists { val, valueDiags := attr.Expr.Value(nil) diags = append(diags, valueDiags...) - if val.Type().Equals(cty.Bool) { - var overrideComputedBool bool - err := gocty.FromCtyValue(val, &overrideComputedBool) - if err != nil { - // should not happen as we already checked the type - diags = diags.Append(&hcl.Diagnostic{ - Severity: hcl.DiagError, - Summary: fmt.Sprintf("Invalid %s value", overrideComputed), - Detail: fmt.Sprintf("The %s attribute must be a boolean.", overrideComputed), - Subject: attr.Range.Ptr(), - }) - } - return &overrideComputedBool, diags + var overrideComputedBool bool + err := gocty.FromCtyValue(val, &overrideComputedBool) + if err != nil { + diags = diags.Append(&hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: fmt.Sprintf("Invalid %s value", overrideComputed), + Detail: fmt.Sprintf("The %s attribute must be a boolean.", overrideComputed), + Subject: attr.Range.Ptr(), + }) } - diags = diags.Append(&hcl.Diagnostic{ - Severity: hcl.DiagError, - Summary: fmt.Sprintf("Invalid %s value", overrideComputed), - Detail: fmt.Sprintf("The %s attribute must be a boolean.", overrideComputed), - Subject: attr.Range.Ptr(), - }) + return &overrideComputedBool, diags } return nil, diags