forked from tmccombs/hcl2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_test.go
149 lines (136 loc) · 2.92 KB
/
convert_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"encoding/json"
hcl "github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"testing"
)
const input = `
locals {
test3 = 1 + 2
test1 = "hello"
test2 = 5
arr = [1, 2, 3, 4]
hyphen-test = 3
temp = "${1 + 2} %{if local.test2 < 3}\"4\n\"%{endif}"
temp2 = "${"hi"} there"
quoted = "\"quoted\""
squoted = "'quoted'"
}
locals {
other = {
num = local.test2 + 5
thing = [for x in local.arr: x * 2]
"${local.test3}" = 4
3 = 1
"local.test1" = 89
"a.b.c[\"hi\"][3].*" = 3
loop = "This has a for loop: %{for x in local.arr}x,%{endfor}"
a.b.c = "True"
}
}
locals {
heredoc = <<-EOF
This is a heredoc template.
It references ${local.other.3}
EOF
simple = "${4 - 2}"
cond = test3 > 2 ? 1: 0
heredoc2 = <<EOF
Another heredoc, that
doesn't remove indentation
${local.other.3}
%{if true ? false : true}"gotcha"\n%{else}4%{endif}
EOF
}
data "terraform_remote_state" "remote" {
backend = "s3"
config = {
profile = var.profile
region = var.region
bucket = "mybucket"
key = "mykey"
}
}
variable "profile" {}
variable "region" {
default = "us-east-1"
}
`
const expectedJSON = `{
"data": {
"terraform_remote_state": {
"remote": {
"backend": "s3",
"config": {
"bucket": "mybucket",
"key": "mykey",
"profile": "${var.profile}",
"region": "${var.region}"
}
}
}
},
"locals": [
{
"arr": [
1,
2,
3,
4
],
"hyphen-test": 3,
"quoted": "\"quoted\"",
"squoted": "'quoted'",
"temp": "${1 + 2} %{if local.test2 \u003c 3}\"4\n\"%{endif}",
"temp2": "hi there",
"test1": "hello",
"test2": 5,
"test3": "${1 + 2}"
},
{
"other": {
"${local.test3}": 4,
"3": 1,
"a.b.c": "True",
"a.b.c[\"hi\"][3].*": 3,
"local.test1": 89,
"loop": "This has a for loop: %{for x in local.arr}x,%{endfor}",
"num": "${local.test2 + 5}",
"thing": "${[for x in local.arr: x * 2]}"
}
},
{
"cond": "${test3 \u003e 2 ? 1: 0}",
"heredoc": "This is a heredoc template.\nIt references ${local.other.3}\n",
"heredoc2": "\t\tAnother heredoc, that\n\t\tdoesn't remove indentation\n\t\t${local.other.3}\n\t\t%{if true ? false : true}\"gotcha\"\\n%{else}4%{endif}\n",
"simple": "${4 - 2}"
}
],
"variable": {
"profile": {},
"region": {
"default": "us-east-1"
}
}
}`
// Test that conversion works as expected
func TestConversion(t *testing.T) {
bytes := []byte(input)
conf, diags := hclsyntax.ParseConfig(bytes, "test", hcl.Pos{Line: 1, Column: 1})
if diags.HasErrors() {
t.Errorf("Failed to parse config: %v", diags)
}
converted, err := convertFile(conf)
if err != nil {
t.Errorf("Unable to convert from hcl: %v", err)
}
jb, err := json.MarshalIndent(converted, "", "\t")
if err != nil {
t.Errorf("Failed to serialize to json: %v", err)
}
computedJSON := string(jb)
if computedJSON != expectedJSON {
t.Errorf("Expected:\n%s\n\nGot:\n%s", expectedJSON, computedJSON)
}
}