forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_config_test.go
156 lines (142 loc) · 3.93 KB
/
parser_config_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
package configs
import (
"io/ioutil"
"path/filepath"
"testing"
"github.com/hashicorp/hcl2/hcl"
)
// TestParseLoadConfigFileSuccess is a simple test that just verifies that
// a number of test configuration files (in test-fixtures/valid-files) can
// be parsed without raising any diagnostics.
//
// This test does not verify that reading these files produces the correct
// file element contents. More detailed assertions may be made on some subset
// of these configuration files in other tests.
func TestParserLoadConfigFileSuccess(t *testing.T) {
files, err := ioutil.ReadDir("test-fixtures/valid-files")
if err != nil {
t.Fatal(err)
}
for _, info := range files {
name := info.Name()
t.Run(name, func(t *testing.T) {
src, err := ioutil.ReadFile(filepath.Join("test-fixtures/valid-files", name))
if err != nil {
t.Fatal(err)
}
parser := testParser(map[string]string{
name: string(src),
})
_, diags := parser.LoadConfigFile(name)
if diags.HasErrors() {
t.Errorf("unexpected error diagnostics")
for _, diag := range diags {
t.Logf("- %s", diag)
}
}
})
}
}
// TestParseLoadConfigFileFailure is a simple test that just verifies that
// a number of test configuration files (in test-fixtures/invalid-files)
// produce errors as expected.
//
// This test does not verify specific error messages, so more detailed
// assertions should be made on some subset of these configuration files in
// other tests.
func TestParserLoadConfigFileFailure(t *testing.T) {
files, err := ioutil.ReadDir("test-fixtures/invalid-files")
if err != nil {
t.Fatal(err)
}
for _, info := range files {
name := info.Name()
t.Run(name, func(t *testing.T) {
src, err := ioutil.ReadFile(filepath.Join("test-fixtures/invalid-files", name))
if err != nil {
t.Fatal(err)
}
parser := testParser(map[string]string{
name: string(src),
})
_, diags := parser.LoadConfigFile(name)
if !diags.HasErrors() {
t.Errorf("LoadConfigFile succeeded; want errors")
}
for _, diag := range diags {
t.Logf("- %s", diag)
}
})
}
}
// This test uses a subset of the same fixture files as
// TestParserLoadConfigFileFailure, but additionally verifies that each
// file produces the expected diagnostic summary.
func TestParserLoadConfigFileFailureMessages(t *testing.T) {
tests := []struct {
Filename string
WantSeverity hcl.DiagnosticSeverity
WantDiag string
}{
{
"invalid-files/data-resource-lifecycle.tf",
hcl.DiagError,
"Unsupported lifecycle block",
},
{
"invalid-files/variable-type-unknown.tf",
hcl.DiagError,
"Invalid type specification",
},
{
"invalid-files/unexpected-attr.tf",
hcl.DiagError,
"Unsupported attribute",
},
{
"invalid-files/unexpected-block.tf",
hcl.DiagError,
"Unsupported block type",
},
{
"invalid-files/resource-lifecycle-badbool.tf",
hcl.DiagError,
"Unsuitable value type",
},
{
"valid-files/resources-ignorechanges-all-legacy.tf",
hcl.DiagWarning,
"Deprecated ignore_changes wildcard",
},
{
"valid-files/resources-ignorechanges-all-legacy.tf.json",
hcl.DiagWarning,
"Deprecated ignore_changes wildcard",
},
}
for _, test := range tests {
t.Run(test.Filename, func(t *testing.T) {
src, err := ioutil.ReadFile(filepath.Join("test-fixtures", test.Filename))
if err != nil {
t.Fatal(err)
}
parser := testParser(map[string]string{
test.Filename: string(src),
})
_, diags := parser.LoadConfigFile(test.Filename)
if len(diags) != 1 {
t.Errorf("Wrong number of diagnostics %d; want 1", len(diags))
for _, diag := range diags {
t.Logf("- %s", diag)
}
return
}
if diags[0].Severity != test.WantSeverity {
t.Errorf("Wrong diagnostic severity %#v; want %#v", diags[0].Severity, test.WantSeverity)
}
if diags[0].Summary != test.WantDiag {
t.Errorf("Wrong diagnostic summary\ngot: %s\nwant: %s", diags[0].Summary, test.WantDiag)
}
})
}
}