Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dsa0x committed Dec 18, 2024
1 parent 61fd770 commit 23ce4ef
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
10 changes: 7 additions & 3 deletions internal/command/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
}
29 changes: 10 additions & 19 deletions internal/configs/mock_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 23ce4ef

Please sign in to comment.