Parser enforces closing RBRACE [GH-88]

This commit is contained in:
Mitchell Hashimoto 2016-02-10 10:31:11 -08:00
parent 8cc8107682
commit 1c284ec98f
5 changed files with 22 additions and 2 deletions

View File

@ -238,6 +238,12 @@ func TestDecode_interface(t *testing.T) {
nil,
},
{
"unterminated_brace.hcl",
true,
nil,
},
{
"object_list.json",
false,
@ -629,7 +635,7 @@ content {
func TestDecode_NestedNode(t *testing.T) {
// given
var value struct {
Nested struct {
Nested struct {
Content ast.Node
}
}
@ -661,4 +667,3 @@ nested "content" {
t.Errorf("expected mapping to be returned")
}
}

View File

@ -246,6 +246,11 @@ func (p *Parser) objectType() (*ast.ObjectType, error) {
return nil, err
}
// If there is no error, we should be at a RBRACE to end the object
if p.tok.Type != token.RBRACE {
return nil, fmt.Errorf("object expected closing RBRACE got: %s", p.tok.Type)
}
o.List = l
o.Rbrace = p.tok.Pos // advanced via parseObjectList
return o, nil

View File

@ -156,6 +156,7 @@ func TestObjectType(t *testing.T) {
item, err := p.objectItem()
if err != nil {
t.Error(err)
continue
}
// we know that the ObjectKey name is foo for all cases, what matters
@ -163,6 +164,7 @@ func TestObjectType(t *testing.T) {
obj, ok := item.Val.(*ast.ObjectType)
if !ok {
t.Errorf("node should be of type LiteralType, got: %T", item.Val)
continue
}
// check if the total length of items are correct
@ -297,6 +299,10 @@ func TestParse(t *testing.T) {
"missing_braces.hcl",
true,
},
{
"unterminated_object.hcl",
true,
},
}
const fixtureDir = "./test-fixtures"

View File

@ -0,0 +1,2 @@
foo "baz" {
bar = "baz"

View File

@ -0,0 +1,2 @@
foo "baz" {
bar = "baz"