-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
configs: Include context when variable default has nested problem
Previously this error message was including only the main error message and ignoring any context about what part of a potential nested data structure it arose from. Now we'll use our usual path formatting utility to introduce additional context whenever a problem arises with a nested value.
- Loading branch information
1 parent
125cf53
commit 130b356
Showing
2 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package configs | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hclsyntax" | ||
) | ||
|
||
func TestVariableInvalidDefault(t *testing.T) { | ||
src := ` | ||
variable foo { | ||
type = map(object({ | ||
foo = bool | ||
})) | ||
default = { | ||
"thingy" = { | ||
foo = "string where bool is expected" | ||
} | ||
} | ||
} | ||
` | ||
|
||
hclF, diags := hclsyntax.ParseConfig([]byte(src), "test.tf", hcl.InitialPos) | ||
if diags.HasErrors() { | ||
t.Fatal(diags.Error()) | ||
} | ||
|
||
_, diags = parseConfigFile(hclF.Body, nil, false, false) | ||
if !diags.HasErrors() { | ||
t.Fatal("unexpected success; want error") | ||
} | ||
|
||
for _, diag := range diags { | ||
if diag.Severity != hcl.DiagError { | ||
continue | ||
} | ||
if diag.Summary != "Invalid default value for variable" { | ||
t.Errorf("unexpected diagnostic summary: %q", diag.Summary) | ||
continue | ||
} | ||
if got, want := diag.Detail, `This default value is not compatible with the variable's type constraint: ["thingy"].foo: a bool is required.`; got != want { | ||
t.Errorf("wrong diagnostic detault\ngot: %s\nwant: %s", got, want) | ||
} | ||
} | ||
} |