forked from hashicorp/hcl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathround_trip_test.go
172 lines (148 loc) · 3.93 KB
/
round_trip_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package hclwrite
import (
"bytes"
"testing"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
"github.com/zclconf/go-cty/cty/function/stdlib"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
)
func TestRoundTripVerbatim(t *testing.T) {
tests := []string{
``,
`foo = 1
`,
`
foobar = 1
baz = 1
`,
`
# this file is awesome
# tossed salads and scrambled eggs
foobar = 1
baz = 1
block {
a = "a"
b = "b"
c = "c"
d = "d"
subblock {
}
subblock {
e = "e"
}
}
# and they all lived happily ever after
`,
}
for _, test := range tests {
t.Run(test, func(t *testing.T) {
src := []byte(test)
file, diags := parse(src, "", hcl.Pos{Line: 1, Column: 1})
if len(diags) != 0 {
for _, diag := range diags {
t.Logf(" - %s", diag.Error())
}
t.Fatalf("unexpected diagnostics")
}
wr := &bytes.Buffer{}
n, err := file.WriteTo(wr)
if n != int64(len(test)) {
t.Errorf("wrong number of bytes %d; want %d", n, len(test))
}
if err != nil {
t.Fatalf("error from WriteTo")
}
result := wr.Bytes()
if !bytes.Equal(result, src) {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(string(src), string(result), false)
//t.Errorf("wrong result\nresult:\n%s\ninput:\n%s", result, src)
t.Errorf("wrong result\ndiff: (red indicates missing lines, and green indicates unexpected lines)\n%s", dmp.DiffPrettyText(diffs))
}
})
}
}
func TestRoundTripFormat(t *testing.T) {
// The goal of this test is to verify that the formatter doesn't change
// the semantics of any expressions when it adds and removes whitespace.
// String templates are the primary area of concern here, but we also
// test some other things for completeness sake.
//
// The tests here must define zero or more attributes, which will be
// extract with JustAttributes and evaluated both before and after
// formatting.
tests := []string{
"",
"\n\n\n",
"a=1\n",
"a=\"hello\"\n",
"a=\"${hello} world\"\n",
"a=upper(\"hello\")\n",
"a=upper(hello)\n",
"a=[1,2,3,4,five]\n",
"a={greeting=hello}\n",
"a={\ngreeting=hello\n}\n",
"a={\ngreeting=hello}\n",
"a={greeting=hello\n}\n",
"a={greeting=hello,number=five,sarcastic=\"${upper(hello)}\"\n}\n",
"a={\ngreeting=hello\nnumber=five\nsarcastic=\"${upper(hello)}\"\n}\n",
"a=<<EOT\nhello\nEOT\n\n",
"a=[<<EOT\nhello\nEOT\n]\n",
"a=[\n<<EOT\nhello\nEOT\n]\n",
"a=[\n]\n",
"a=1\nb=2\nc=3\n",
"a=\"${\n5\n}\"\n",
}
ctx := &hcl.EvalContext{
Variables: map[string]cty.Value{
"hello": cty.StringVal("hello"),
"five": cty.NumberIntVal(5),
},
Functions: map[string]function.Function{
"upper": stdlib.UpperFunc,
},
}
for _, test := range tests {
t.Run(test, func(t *testing.T) {
attrsAsObj := func(src []byte, phase string) cty.Value {
t.Logf("source %s:\n%s", phase, src)
f, diags := hclsyntax.ParseConfig(src, "", hcl.Pos{Line: 1, Column: 1})
if len(diags) != 0 {
for _, diag := range diags {
t.Logf(" - %s", diag.Error())
}
t.Fatalf("unexpected diagnostics in parse %s", phase)
}
attrs, diags := f.Body.JustAttributes()
if len(diags) != 0 {
for _, diag := range diags {
t.Logf(" - %s", diag.Error())
}
t.Fatalf("unexpected diagnostics in JustAttributes %s", phase)
}
vals := map[string]cty.Value{}
for k, attr := range attrs {
val, diags := attr.Expr.Value(ctx)
if len(diags) != 0 {
for _, diag := range diags {
t.Logf(" - %s", diag.Error())
}
t.Fatalf("unexpected diagnostics evaluating %s", phase)
}
vals[k] = val
}
return cty.ObjectVal(vals)
}
src := []byte(test)
before := attrsAsObj(src, "before")
formatted := Format(src)
after := attrsAsObj(formatted, "after")
if !after.RawEquals(before) {
t.Errorf("mismatching after format\nbefore: %#v\nafter: %#v", before, after)
}
})
}
}