parser: add official HCL tests, WIP
This commit is contained in:
parent
393af546c0
commit
9468aa324e
@ -2,6 +2,7 @@ package parser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"runtime"
|
||||
@ -213,6 +214,82 @@ func TestObjectKey(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Official HCL tests
|
||||
func TestParse(t *testing.T) {
|
||||
cases := []struct {
|
||||
Name string
|
||||
Err bool
|
||||
}{
|
||||
{
|
||||
"assign_colon.hcl",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"comment.hcl",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"comment_single.hcl",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"empty.hcl",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"list_comma.hcl",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"multiple.hcl",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"structure.hcl",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"structure_basic.hcl",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"structure_empty.hcl",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"complex.hcl",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"assign_deep.hcl",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"types.hcl",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"array_comment.hcl",
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
const fixtureDir = "./test-fixtures"
|
||||
|
||||
for _, tc := range cases {
|
||||
d, err := ioutil.ReadFile(filepath.Join(fixtureDir, tc.Name))
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
p := New(d)
|
||||
_, err = p.Parse()
|
||||
if (err != nil) != tc.Err {
|
||||
t.Fatalf("Input: %s\n\nError: %s", tc.Name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// equals fails the test if exp is not equal to act.
|
||||
func equals(tb testing.TB, exp, act interface{}) {
|
||||
if !reflect.DeepEqual(exp, act) {
|
||||
|
4
parser/test-fixtures/array_comment.hcl
Normal file
4
parser/test-fixtures/array_comment.hcl
Normal file
@ -0,0 +1,4 @@
|
||||
foo = [
|
||||
"1",
|
||||
"2", # comment
|
||||
]
|
6
parser/test-fixtures/assign_colon.hcl
Normal file
6
parser/test-fixtures/assign_colon.hcl
Normal file
@ -0,0 +1,6 @@
|
||||
resource = [{
|
||||
"foo": {
|
||||
"bar": {},
|
||||
"baz": [1, 2, "foo"],
|
||||
}
|
||||
}]
|
5
parser/test-fixtures/assign_deep.hcl
Normal file
5
parser/test-fixtures/assign_deep.hcl
Normal file
@ -0,0 +1,5 @@
|
||||
resource = [{
|
||||
foo = [{
|
||||
bar = {}
|
||||
}]
|
||||
}]
|
15
parser/test-fixtures/comment.hcl
Normal file
15
parser/test-fixtures/comment.hcl
Normal file
@ -0,0 +1,15 @@
|
||||
// Foo
|
||||
|
||||
/* Bar */
|
||||
|
||||
/*
|
||||
/*
|
||||
Baz
|
||||
*/
|
||||
|
||||
# Another
|
||||
|
||||
# Multiple
|
||||
# Lines
|
||||
|
||||
foo = "bar"
|
1
parser/test-fixtures/comment_single.hcl
Normal file
1
parser/test-fixtures/comment_single.hcl
Normal file
@ -0,0 +1 @@
|
||||
# Hello
|
42
parser/test-fixtures/complex.hcl
Normal file
42
parser/test-fixtures/complex.hcl
Normal file
@ -0,0 +1,42 @@
|
||||
// This comes from Terraform, as a test
|
||||
variable "foo" {
|
||||
default = "bar"
|
||||
description = "bar"
|
||||
}
|
||||
|
||||
provider "aws" {
|
||||
access_key = "foo"
|
||||
secret_key = "bar"
|
||||
}
|
||||
|
||||
provider "do" {
|
||||
api_key = "${var.foo}"
|
||||
}
|
||||
|
||||
resource "aws_security_group" "firewall" {
|
||||
count = 5
|
||||
}
|
||||
|
||||
resource aws_instance "web" {
|
||||
ami = "${var.foo}"
|
||||
security_groups = [
|
||||
"foo",
|
||||
"${aws_security_group.firewall.foo}"
|
||||
]
|
||||
|
||||
network_interface {
|
||||
device_index = 0
|
||||
description = "Main network interface"
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_instance" "db" {
|
||||
security_groups = "${aws_security_group.firewall.*.id}"
|
||||
VPC = "foo"
|
||||
|
||||
depends_on = ["aws_instance.web"]
|
||||
}
|
||||
|
||||
output "web_ip" {
|
||||
value = "${aws_instance.web.private_ip}"
|
||||
}
|
1
parser/test-fixtures/complex_key.hcl
Normal file
1
parser/test-fixtures/complex_key.hcl
Normal file
@ -0,0 +1 @@
|
||||
foo.bar = "baz"
|
0
parser/test-fixtures/empty.hcl
Normal file
0
parser/test-fixtures/empty.hcl
Normal file
1
parser/test-fixtures/list.hcl
Normal file
1
parser/test-fixtures/list.hcl
Normal file
@ -0,0 +1 @@
|
||||
foo = [1, 2, "foo"]
|
1
parser/test-fixtures/list_comma.hcl
Normal file
1
parser/test-fixtures/list_comma.hcl
Normal file
@ -0,0 +1 @@
|
||||
foo = [1, 2, "foo",]
|
2
parser/test-fixtures/multiple.hcl
Normal file
2
parser/test-fixtures/multiple.hcl
Normal file
@ -0,0 +1,2 @@
|
||||
foo = "bar"
|
||||
key = 7
|
3
parser/test-fixtures/old.hcl
Normal file
3
parser/test-fixtures/old.hcl
Normal file
@ -0,0 +1,3 @@
|
||||
default = {
|
||||
"eu-west-1": "ami-b1cf19c6",
|
||||
}
|
5
parser/test-fixtures/structure.hcl
Normal file
5
parser/test-fixtures/structure.hcl
Normal file
@ -0,0 +1,5 @@
|
||||
// This is a test structure for the lexer
|
||||
foo bar "baz" {
|
||||
key = 7
|
||||
foo = "bar"
|
||||
}
|
5
parser/test-fixtures/structure_basic.hcl
Normal file
5
parser/test-fixtures/structure_basic.hcl
Normal file
@ -0,0 +1,5 @@
|
||||
foo {
|
||||
value = 7
|
||||
"value" = 8
|
||||
"complex::value" = 9
|
||||
}
|
1
parser/test-fixtures/structure_empty.hcl
Normal file
1
parser/test-fixtures/structure_empty.hcl
Normal file
@ -0,0 +1 @@
|
||||
resource "foo" "bar" {}
|
7
parser/test-fixtures/types.hcl
Normal file
7
parser/test-fixtures/types.hcl
Normal file
@ -0,0 +1,7 @@
|
||||
foo = "bar"
|
||||
bar = 7
|
||||
baz = [1,2,3]
|
||||
foo = -12
|
||||
bar = 3.14159
|
||||
foo = true
|
||||
bar = false
|
Loading…
Reference in New Issue
Block a user