@@ -36,7 +36,7 @@ type mockServer struct {
36
36
type mockServerImpl struct {
37
37
getModuleContent func (* hclext.BodySchema , tflint.GetModuleContentOption ) (* hclext.BodyContent , hcl.Diagnostics )
38
38
getFile func (string ) (* hcl.File , error )
39
- getFiles func () map [string ]* hcl. File
39
+ getFiles func () map [string ][] byte
40
40
getRuleConfigContent func (string , * hclext.BodySchema ) (* hclext.BodyContent , * hcl.File , error )
41
41
evaluateExpr func (hcl.Expression , tflint.EvaluateExprOption ) (cty.Value , error )
42
42
emitIssue func (tflint.Rule , string , hcl.Range ) error
@@ -60,11 +60,11 @@ func (s *mockServer) GetFile(filename string) (*hcl.File, error) {
60
60
return nil , nil
61
61
}
62
62
63
- func (s * mockServer ) GetFiles (tflint.ModuleCtxType ) map [string ]* hcl. File {
63
+ func (s * mockServer ) GetFiles (tflint.ModuleCtxType ) map [string ][] byte {
64
64
if s .impl .getFiles != nil {
65
65
return s .impl .getFiles ()
66
66
}
67
- return map [string ]* hcl. File {}
67
+ return map [string ][] byte {}
68
68
}
69
69
70
70
func (s * mockServer ) GetRuleConfigContent (name string , schema * hclext.BodySchema ) (* hclext.BodyContent , * hcl.File , error ) {
@@ -96,8 +96,8 @@ func TestGetResourceContent(t *testing.T) {
96
96
neverHappend := func (err error ) bool { return err != nil }
97
97
98
98
// default getFileImpl function
99
- files := map [string ]* hcl. File {}
100
- fileExists := func () map [string ]* hcl. File {
99
+ files := map [string ][] byte {}
100
+ fileExists := func () map [string ][] byte {
101
101
return files
102
102
}
103
103
@@ -107,15 +107,15 @@ func TestGetResourceContent(t *testing.T) {
107
107
if diags .HasErrors () {
108
108
panic (diags )
109
109
}
110
- files [filename ] = file
110
+ files [filename ] = file . Bytes
111
111
return file
112
112
}
113
113
jsonFile := func (filename string , code string ) * hcl.File {
114
114
file , diags := json .Parse ([]byte (code ), filename )
115
115
if diags .HasErrors () {
116
116
panic (diags )
117
117
}
118
- files [filename ] = file
118
+ files [filename ] = file . Bytes
119
119
return file
120
120
}
121
121
@@ -215,6 +215,34 @@ resource "aws_instance" "foo" {
215
215
},
216
216
ErrCheck : neverHappend ,
217
217
},
218
+ {
219
+ Name : "get content with options" ,
220
+ Args : func () (string , * hclext.BodySchema , * tflint.GetModuleContentOption ) {
221
+ return "aws_instance" , & hclext.BodySchema {}, & tflint.GetModuleContentOption {
222
+ ModuleCtx : tflint .RootModuleCtxType ,
223
+ }
224
+ },
225
+ ServerImpl : func (schema * hclext.BodySchema , opts tflint.GetModuleContentOption ) (* hclext.BodyContent , hcl.Diagnostics ) {
226
+ if opts .ModuleCtx != tflint .RootModuleCtxType {
227
+ return & hclext.BodyContent {}, hcl.Diagnostics {
228
+ & hcl.Diagnostic {Severity : hcl .DiagError , Summary : "unexpected moduleCtx options" },
229
+ }
230
+ }
231
+ if opts .Hint .ResourceType != "aws_instance" {
232
+ return & hclext.BodyContent {}, hcl.Diagnostics {
233
+ & hcl.Diagnostic {Severity : hcl .DiagError , Summary : "unexpected hint options" },
234
+ }
235
+ }
236
+ return & hclext.BodyContent {}, hcl.Diagnostics {}
237
+ },
238
+ Want : func (resource string , schema * hclext.BodySchema , opts * tflint.GetModuleContentOption ) (* hclext.BodyContent , hcl.Diagnostics ) {
239
+ return & hclext.BodyContent {
240
+ Attributes : hclext.Attributes {},
241
+ Blocks : hclext.Blocks {},
242
+ }, hcl.Diagnostics {}
243
+ },
244
+ ErrCheck : neverHappend ,
245
+ },
218
246
}
219
247
220
248
for _ , test := range tests {
@@ -252,8 +280,8 @@ func TestGetModuleContent(t *testing.T) {
252
280
neverHappend := func (err error ) bool { return err != nil }
253
281
254
282
// default getFileImpl function
255
- files := map [string ]* hcl. File {}
256
- fileExists := func () map [string ]* hcl. File {
283
+ files := map [string ][] byte {}
284
+ fileExists := func () map [string ][] byte {
257
285
return files
258
286
}
259
287
@@ -263,15 +291,15 @@ func TestGetModuleContent(t *testing.T) {
263
291
if diags .HasErrors () {
264
292
panic (diags )
265
293
}
266
- files [filename ] = file
294
+ files [filename ] = file . Bytes
267
295
return file
268
296
}
269
297
jsonFile := func (filename string , code string ) * hcl.File {
270
298
file , diags := json .Parse ([]byte (code ), filename )
271
299
if diags .HasErrors () {
272
300
panic (diags )
273
301
}
274
- files [filename ] = file
302
+ files [filename ] = file . Bytes
275
303
return file
276
304
}
277
305
@@ -359,12 +387,20 @@ resource "aws_instance" "foo" {
359
387
{
360
388
Name : "get content with options" ,
361
389
Args : func () (* hclext.BodySchema , * tflint.GetModuleContentOption ) {
362
- return & hclext.BodySchema {}, & tflint.GetModuleContentOption {ModuleCtx : tflint .RootModuleCtxType }
390
+ return & hclext.BodySchema {}, & tflint.GetModuleContentOption {
391
+ ModuleCtx : tflint .RootModuleCtxType ,
392
+ Hint : tflint.GetModuleContentHint {ResourceType : "aws_instance" },
393
+ }
363
394
},
364
395
ServerImpl : func (schema * hclext.BodySchema , opts tflint.GetModuleContentOption ) (* hclext.BodyContent , hcl.Diagnostics ) {
365
396
if opts .ModuleCtx != tflint .RootModuleCtxType {
366
397
return & hclext.BodyContent {}, hcl.Diagnostics {
367
- & hcl.Diagnostic {Severity : hcl .DiagError , Summary : "unexpected options" },
398
+ & hcl.Diagnostic {Severity : hcl .DiagError , Summary : "unexpected moduleCtx options" },
399
+ }
400
+ }
401
+ if opts .Hint .ResourceType != "aws_instance" {
402
+ return & hclext.BodyContent {}, hcl.Diagnostics {
403
+ & hcl.Diagnostic {Severity : hcl .DiagError , Summary : "unexpected hint options" },
368
404
}
369
405
}
370
406
return & hclext.BodyContent {}, hcl.Diagnostics {}
@@ -608,19 +644,19 @@ func TestGetFiles(t *testing.T) {
608
644
609
645
tests := []struct {
610
646
Name string
611
- ServerImpl func () map [string ]* hcl. File
647
+ ServerImpl func () map [string ][] byte
612
648
Want map [string ]* hcl.File
613
649
ErrCheck func (error ) bool
614
650
}{
615
651
{
616
652
Name : "HCL files" ,
617
- ServerImpl : func () map [string ]* hcl. File {
618
- return map [string ]* hcl. File {
619
- "test1.tf" : hclFile ( "test1.tf" , `
653
+ ServerImpl : func () map [string ][] byte {
654
+ return map [string ][] byte {
655
+ "test1.tf" : [] byte ( `
620
656
resource "aws_instance" "foo" {
621
657
instance_type = "t2.micro"
622
658
}` ),
623
- "test2.tf" : hclFile ( "test2.tf" , `
659
+ "test2.tf" : [] byte ( `
624
660
resource "aws_s3_bucket" "bar" {
625
661
bucket = "baz"
626
662
}` ),
@@ -640,9 +676,9 @@ resource "aws_s3_bucket" "bar" {
640
676
},
641
677
{
642
678
Name : "JSON files" ,
643
- ServerImpl : func () map [string ]* hcl. File {
644
- return map [string ]* hcl. File {
645
- "test1.tf.json" : jsonFile ( "test1.tf.json" , `
679
+ ServerImpl : func () map [string ][] byte {
680
+ return map [string ][] byte {
681
+ "test1.tf.json" : [] byte ( `
646
682
{
647
683
"resource": {
648
684
"aws_instance": {
@@ -652,7 +688,7 @@ resource "aws_s3_bucket" "bar" {
652
688
}
653
689
}
654
690
}` ),
655
- "test2.tf.json" : jsonFile ( "test2.tf.json" , `
691
+ "test2.tf.json" : [] byte ( `
656
692
{
657
693
"resource": {
658
694
"aws_s3_bucket": {
0 commit comments