hcl/parser: don't allow objects with no keys
This commit is contained in:
parent
685b5f7416
commit
352bb4b5e3
@ -269,9 +269,7 @@ func TestDecode_interface(t *testing.T) {
|
|||||||
{
|
{
|
||||||
"nested_provider_bad.hcl",
|
"nested_provider_bad.hcl",
|
||||||
true,
|
true,
|
||||||
// This is not ideal but without significant rework of the decoder
|
nil,
|
||||||
// we get a partial result back as well as an error.
|
|
||||||
map[string]interface{}{},
|
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -334,6 +332,44 @@ func TestDecode_interface(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDecode_interfaceInline(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
Value string
|
||||||
|
Err bool
|
||||||
|
Out interface{}
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"t t e{{}}",
|
||||||
|
true,
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
t.Logf("Testing: %q", tc.Value)
|
||||||
|
|
||||||
|
var out interface{}
|
||||||
|
err := Decode(&out, tc.Value)
|
||||||
|
if (err != nil) != tc.Err {
|
||||||
|
t.Fatalf("Input: %q\n\nError: %s", tc.Value, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(out, tc.Out) {
|
||||||
|
t.Fatalf("Input: %q. Actual, Expected.\n\n%#v\n\n%#v", tc.Value, out, tc.Out)
|
||||||
|
}
|
||||||
|
|
||||||
|
var v interface{}
|
||||||
|
err = Unmarshal([]byte(tc.Value), &v)
|
||||||
|
if (err != nil) != tc.Err {
|
||||||
|
t.Fatalf("Input: %q\n\nError: %s", tc.Value, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(v, tc.Out) {
|
||||||
|
t.Fatalf("Input: %q. Actual, Expected.\n\n%#v\n\n%#v", tc.Value, out, tc.Out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDecode_equal(t *testing.T) {
|
func TestDecode_equal(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
One, Two string
|
One, Two string
|
||||||
|
@ -220,8 +220,19 @@ func (p *Parser) objectKey() ([]*ast.ObjectKey, error) {
|
|||||||
|
|
||||||
return keys, nil
|
return keys, nil
|
||||||
case token.LBRACE:
|
case token.LBRACE:
|
||||||
|
var err error
|
||||||
|
|
||||||
|
// If we have no keys, then it is a syntax error. i.e. {{}} is not
|
||||||
|
// allowed.
|
||||||
|
if len(keys) == 0 {
|
||||||
|
err = &PosError{
|
||||||
|
Pos: p.tok.Pos,
|
||||||
|
Err: fmt.Errorf("expected: IDENT | STRING got: %s", p.tok.Type),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// object
|
// object
|
||||||
return keys, nil
|
return keys, err
|
||||||
case token.IDENT, token.STRING:
|
case token.IDENT, token.STRING:
|
||||||
keyCount++
|
keyCount++
|
||||||
keys = append(keys, &ast.ObjectKey{Token: p.tok})
|
keys = append(keys, &ast.ObjectKey{Token: p.tok})
|
||||||
|
@ -332,6 +332,22 @@ func TestParse(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParse_inline(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
Value string
|
||||||
|
Err bool
|
||||||
|
}{
|
||||||
|
{"t t e{{}}", true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
_, err := Parse([]byte(tc.Value))
|
||||||
|
if (err != nil) != tc.Err {
|
||||||
|
t.Fatalf("Input: %q\n\nError: %s", tc.Value, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// equals fails the test if exp is not equal to act.
|
// equals fails the test if exp is not equal to act.
|
||||||
func equals(tb testing.TB, exp, act interface{}) {
|
func equals(tb testing.TB, exp, act interface{}) {
|
||||||
if !reflect.DeepEqual(exp, act) {
|
if !reflect.DeepEqual(exp, act) {
|
||||||
|
@ -332,6 +332,22 @@ func TestParse(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParse_inline(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
Value string
|
||||||
|
Err bool
|
||||||
|
}{
|
||||||
|
{"{:{", true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
_, err := Parse([]byte(tc.Value))
|
||||||
|
if (err != nil) != tc.Err {
|
||||||
|
t.Fatalf("Input: %q\n\nError: %s", tc.Value, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// equals fails the test if exp is not equal to act.
|
// equals fails the test if exp is not equal to act.
|
||||||
func equals(tb testing.TB, exp, act interface{}) {
|
func equals(tb testing.TB, exp, act interface{}) {
|
||||||
if !reflect.DeepEqual(exp, act) {
|
if !reflect.DeepEqual(exp, act) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user