tests: add tests for invalid syntax cases

This commit is contained in:
Mahmood Ali
2021-02-01 12:51:51 -05:00
parent 4b25371842
commit 2ae4761356
2 changed files with 49 additions and 1 deletions

View File

@@ -382,7 +382,7 @@ func decodeAsAttribute(body hcl.Body, ctx *hcl.EvalContext, name string) (map[st
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("Duplicate %v block", name),
Detail: fmt.Sprintf("Only one %v block is allowed. Another was defined at %s.",
Detail: fmt.Sprintf("%v may not be defined more than once. Another definition is defined at %s.",
name, attr.Range.String()),
Subject: &bb.Blocks[0].DefRange,
})

View File

@@ -729,6 +729,54 @@ job "example" {
require.Contains(t, err.Error(), "Duplicate env block")
}
func Test_TaskEnvs_Invalid(t *testing.T) {
cases := []struct {
name string
envSnippet string
expectedErr string
}{
{
"attr: invalid expression",
`env = { key = local.undefined_local }`,
`does not have an attribute named "undefined_local"`,
},
{
"block: invalid block expression",
`env {
for k in ["a", "b"]: k => k
}`,
"Invalid block definition",
},
{
"attr: not make sense",
`env = [ "a" ]`,
"Unsuitable value: map of string required",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
hcl := `
job "example" {
group "group" {
task "task" {
driver = "docker"
config {}
` + c.envSnippet + `
}
}
}`
_, err := ParseWithConfig(&ParseConfig{
Path: "input.hcl",
Body: []byte(hcl),
})
require.Error(t, err)
require.Contains(t, err.Error(), c.expectedErr)
})
}
}
func TestParse_Meta_Alternatives(t *testing.T) {
hcl := ` job "example" {
group "group" {