@@ -36,7 +36,7 @@ type mockServer struct {
3636type mockServerImpl struct {
3737 getModuleContent func (* hclext.BodySchema , tflint.GetModuleContentOption ) (* hclext.BodyContent , hcl.Diagnostics )
3838 getFile func (string ) (* hcl.File , error )
39- getFiles func () map [string ]* hcl. File
39+ getFiles func () map [string ][] byte
4040 getRuleConfigContent func (string , * hclext.BodySchema ) (* hclext.BodyContent , * hcl.File , error )
4141 evaluateExpr func (hcl.Expression , tflint.EvaluateExprOption ) (cty.Value , error )
4242 emitIssue func (tflint.Rule , string , hcl.Range ) error
@@ -60,11 +60,11 @@ func (s *mockServer) GetFile(filename string) (*hcl.File, error) {
6060 return nil , nil
6161}
6262
63- func (s * mockServer ) GetFiles (tflint.ModuleCtxType ) map [string ]* hcl. File {
63+ func (s * mockServer ) GetFiles (tflint.ModuleCtxType ) map [string ][] byte {
6464 if s .impl .getFiles != nil {
6565 return s .impl .getFiles ()
6666 }
67- return map [string ]* hcl. File {}
67+ return map [string ][] byte {}
6868}
6969
7070func (s * mockServer ) GetRuleConfigContent (name string , schema * hclext.BodySchema ) (* hclext.BodyContent , * hcl.File , error ) {
@@ -96,8 +96,8 @@ func TestGetResourceContent(t *testing.T) {
9696 neverHappend := func (err error ) bool { return err != nil }
9797
9898 // 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 {
101101 return files
102102 }
103103
@@ -107,15 +107,15 @@ func TestGetResourceContent(t *testing.T) {
107107 if diags .HasErrors () {
108108 panic (diags )
109109 }
110- files [filename ] = file
110+ files [filename ] = file . Bytes
111111 return file
112112 }
113113 jsonFile := func (filename string , code string ) * hcl.File {
114114 file , diags := json .Parse ([]byte (code ), filename )
115115 if diags .HasErrors () {
116116 panic (diags )
117117 }
118- files [filename ] = file
118+ files [filename ] = file . Bytes
119119 return file
120120 }
121121
@@ -215,6 +215,34 @@ resource "aws_instance" "foo" {
215215 },
216216 ErrCheck : neverHappend ,
217217 },
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+ },
218246 }
219247
220248 for _ , test := range tests {
@@ -252,8 +280,8 @@ func TestGetModuleContent(t *testing.T) {
252280 neverHappend := func (err error ) bool { return err != nil }
253281
254282 // 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 {
257285 return files
258286 }
259287
@@ -263,15 +291,15 @@ func TestGetModuleContent(t *testing.T) {
263291 if diags .HasErrors () {
264292 panic (diags )
265293 }
266- files [filename ] = file
294+ files [filename ] = file . Bytes
267295 return file
268296 }
269297 jsonFile := func (filename string , code string ) * hcl.File {
270298 file , diags := json .Parse ([]byte (code ), filename )
271299 if diags .HasErrors () {
272300 panic (diags )
273301 }
274- files [filename ] = file
302+ files [filename ] = file . Bytes
275303 return file
276304 }
277305
@@ -359,12 +387,20 @@ resource "aws_instance" "foo" {
359387 {
360388 Name : "get content with options" ,
361389 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+ }
363394 },
364395 ServerImpl : func (schema * hclext.BodySchema , opts tflint.GetModuleContentOption ) (* hclext.BodyContent , hcl.Diagnostics ) {
365396 if opts .ModuleCtx != tflint .RootModuleCtxType {
366397 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" },
368404 }
369405 }
370406 return & hclext.BodyContent {}, hcl.Diagnostics {}
@@ -608,19 +644,19 @@ func TestGetFiles(t *testing.T) {
608644
609645 tests := []struct {
610646 Name string
611- ServerImpl func () map [string ]* hcl. File
647+ ServerImpl func () map [string ][] byte
612648 Want map [string ]* hcl.File
613649 ErrCheck func (error ) bool
614650 }{
615651 {
616652 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 ( `
620656resource "aws_instance" "foo" {
621657 instance_type = "t2.micro"
622658}` ),
623- "test2.tf" : hclFile ( "test2.tf" , `
659+ "test2.tf" : [] byte ( `
624660resource "aws_s3_bucket" "bar" {
625661 bucket = "baz"
626662}` ),
@@ -640,9 +676,9 @@ resource "aws_s3_bucket" "bar" {
640676 },
641677 {
642678 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 ( `
646682{
647683 "resource": {
648684 "aws_instance": {
@@ -652,7 +688,7 @@ resource "aws_s3_bucket" "bar" {
652688 }
653689 }
654690}` ),
655- "test2.tf.json" : jsonFile ( "test2.tf.json" , `
691+ "test2.tf.json" : [] byte ( `
656692{
657693 "resource": {
658694 "aws_s3_bucket": {
0 commit comments