1
1
package tflint
2
2
3
3
import (
4
+ "strings"
4
5
"testing"
5
6
6
7
"github.com/google/go-cmp/cmp"
7
8
"github.com/google/go-cmp/cmp/cmpopts"
8
9
hcl "github.com/hashicorp/hcl/v2"
10
+ "github.com/hashicorp/hcl/v2/hclparse"
9
11
"github.com/hashicorp/hcl/v2/hclsyntax"
10
12
)
11
13
12
14
func Test_NewAnnotations (t * testing.T ) {
13
15
tests := []struct {
14
- name string
15
- src string
16
- want Annotations
17
- diags string
16
+ name string
17
+ filename string
18
+ src string
19
+ want Annotations
20
+ diags string
18
21
}{
19
22
{
20
- name : "annotation starting with #" ,
23
+ name : "annotation starting with #" ,
24
+ filename : "resource.tf" ,
21
25
src : `
22
26
resource "aws_instance" "foo" {
23
27
# tflint-ignore: aws_instance_invalid_type
@@ -39,7 +43,8 @@ resource "aws_instance" "foo" {
39
43
},
40
44
},
41
45
{
42
- name : "annotation starting with //" ,
46
+ name : "annotation starting with //" ,
47
+ filename : "resource.tf" ,
43
48
src : `
44
49
resource "aws_instance" "foo" {
45
50
// This is also comment
@@ -61,7 +66,8 @@ resource "aws_instance" "foo" {
61
66
},
62
67
},
63
68
{
64
- name : "annotation starting with /*" ,
69
+ name : "annotation starting with /*" ,
70
+ filename : "resource.tf" ,
65
71
src : `
66
72
resource "aws_instance" "foo" {
67
73
/* tflint-ignore: aws_instance_invalid_type */
@@ -83,7 +89,8 @@ resource "aws_instance" "foo" {
83
89
},
84
90
},
85
91
{
86
- name : "ignoring multiple rules" ,
92
+ name : "ignoring multiple rules" ,
93
+ filename : "resource.tf" ,
87
94
src : `
88
95
resource "aws_instance" "foo" {
89
96
/* tflint-ignore: aws_instance_invalid_type, terraform_deprecated_syntax */
@@ -105,7 +112,8 @@ resource "aws_instance" "foo" {
105
112
},
106
113
},
107
114
{
108
- name : "with reason starting with //" ,
115
+ name : "with reason starting with //" ,
116
+ filename : "resource.tf" ,
109
117
src : `
110
118
resource "aws_instance" "foo" {
111
119
instance_type = "t2.micro" // tflint-ignore: aws_instance_invalid_type // With reason
@@ -126,7 +134,8 @@ resource "aws_instance" "foo" {
126
134
},
127
135
},
128
136
{
129
- name : "with reason starting with #" ,
137
+ name : "with reason starting with #" ,
138
+ filename : "resource.tf" ,
130
139
src : `
131
140
resource "aws_instance" "foo" {
132
141
# tflint-ignore: aws_instance_invalid_type # With reason
@@ -148,7 +157,8 @@ resource "aws_instance" "foo" {
148
157
},
149
158
},
150
159
{
151
- name : "tflint-ignore-file annotation" ,
160
+ name : "tflint-ignore-file annotation" ,
161
+ filename : "resource.tf" ,
152
162
src : `# tflint-ignore-file: aws_instance_invalid_type
153
163
resource "aws_instance" "foo" {
154
164
instance_type = "t2.micro"
@@ -169,7 +179,8 @@ resource "aws_instance" "foo" {
169
179
},
170
180
},
171
181
{
172
- name : "tflint-ignore-file annotation outside the first line" ,
182
+ name : "tflint-ignore-file annotation outside the first line" ,
183
+ filename : "resource.tf" ,
173
184
src : `
174
185
resource "aws_instance" "foo" {
175
186
# tflint-ignore-file: aws_instance_invalid_type
@@ -179,22 +190,111 @@ resource "aws_instance" "foo" {
179
190
diags : "resource.tf:3,3-4,1: tflint-ignore-file annotation must be written at the top of file; tflint-ignore-file annotation is written at line 3, column 3" ,
180
191
},
181
192
{
182
- name : "tflint-ignore-file annotation outside the first column" ,
193
+ name : "tflint-ignore-file annotation outside the first column" ,
194
+ filename : "resource.tf" ,
183
195
src : `resource "aws_instance" "foo" { # tflint-ignore-file: aws_instance_invalid_type
184
196
instance_type = "t2.micro"
185
197
}` ,
186
198
want : Annotations {},
187
199
diags : "resource.tf:1,33-2,1: tflint-ignore-file annotation must be written at the top of file; tflint-ignore-file annotation is written at line 1, column 33" ,
188
200
},
201
+ {
202
+ name : "tflint-ignore-file in JSON comment property" ,
203
+ filename : "resource.tf.json" ,
204
+ src : `{
205
+ "//": "tflint-ignore-file: aws_instance_invalid_type",
206
+ "resource": {
207
+ "aws_instance": {
208
+ "foo": {
209
+ "instance_type": "t2.micro"
210
+ }
211
+ }
212
+ }
213
+ }` ,
214
+ want : Annotations {
215
+ & FileAnnotation {
216
+ Content : "aws_instance_invalid_type" ,
217
+ Token : hclsyntax.Token {
218
+ Range : hcl.Range {
219
+ Filename : "resource.tf.json" ,
220
+ },
221
+ },
222
+ },
223
+ },
224
+ },
225
+ {
226
+ name : "tglint-ignore-file with multiple rules in JSON comment property and following comment" ,
227
+ filename : "resource.tf.json" ,
228
+ src : `{
229
+ "//": "tflint-ignore-file: aws_instance_invalid_type, terraform_deprecated_syntax # this is an extra comment",
230
+ "resource": {
231
+ "aws_instance": {
232
+ "foo": {
233
+ "instance_type": "t2.micro"
234
+ }
235
+ }
236
+ }
237
+ }` ,
238
+ want : Annotations {
239
+ & FileAnnotation {
240
+ Content : "aws_instance_invalid_type, terraform_deprecated_syntax" ,
241
+ Token : hclsyntax.Token {
242
+ Range : hcl.Range {
243
+ Filename : "resource.tf.json" ,
244
+ },
245
+ },
246
+ },
247
+ },
248
+ },
249
+ {
250
+ name : "no errors if JSON comment property is not the expected structure" ,
251
+ filename : "resource.tf.json" ,
252
+ src : `{
253
+ "//": {"foo": "bar"},
254
+ "resource": {
255
+ "aws_instance": {
256
+ "foo": {
257
+ "instance_type": "t2.micro"
258
+ }
259
+ }
260
+ }
261
+ }` ,
262
+ want : Annotations {},
263
+ },
264
+ {
265
+ name : "tflint-ignore-file annotation outside the first column of the JSON comment property" ,
266
+ filename : "resource.tf.json" ,
267
+ src : `{
268
+ "//": "blah blah # tflint-ignore-file: aws_instance_invalid_type",
269
+ "resource": {
270
+ "aws_instance": {
271
+ "foo": {
272
+ "instance_type": "t2.micro"
273
+ }
274
+ }
275
+ }
276
+ }` ,
277
+ want : Annotations {},
278
+ diags : "resource.tf.json:0,0-0: tflint-ignore-file annotation must appear at the beginning of the JSON comment property value; tflint-ignore-file annotation is written at index 12 of the comment property value" ,
279
+ },
189
280
}
190
281
191
282
for _ , test := range tests {
192
283
t .Run (test .name , func (t * testing.T ) {
193
- file , diags := hclsyntax .ParseConfig ([]byte (test .src ), "resource.tf" , hcl .InitialPos )
284
+ parser := hclparse .NewParser ()
285
+ var file * hcl.File
286
+ var diags hcl.Diagnostics
287
+ switch {
288
+ case strings .HasSuffix (test .filename , ".json" ):
289
+ file , diags = parser .ParseJSON ([]byte (test .src ), test .filename )
290
+ default :
291
+ file , diags = parser .ParseHCL ([]byte (test .src ), test .filename )
292
+ }
194
293
if diags .HasErrors () {
195
294
t .Fatal (diags )
196
295
}
197
- got , diags := NewAnnotations ("resource.tf" , file )
296
+
297
+ got , diags := NewAnnotations (test .filename , file )
198
298
if diags .HasErrors () || test .diags != "" {
199
299
if diags .Error () != test .diags {
200
300
t .Errorf ("want=%s, got=%s" , test .diags , diags .Error ())
0 commit comments