@@ -37,7 +37,7 @@ type mockServerImpl struct {
37
37
getModuleContent func (* hclext.BodySchema , tflint.GetModuleContentOption ) (* hclext.BodyContent , hcl.Diagnostics )
38
38
getFile func (string ) (* hcl.File , error )
39
39
getFiles func () map [string ][]byte
40
- getRuleConfigContent func (string , * hclext.BodySchema ) (* hclext.BodyContent , * hcl. File , error )
40
+ getRuleConfigContent func (string , * hclext.BodySchema ) (* hclext.BodyContent , map [ string ][] byte , error )
41
41
evaluateExpr func (hcl.Expression , tflint.EvaluateExprOption ) (cty.Value , error )
42
42
emitIssue func (tflint.Rule , string , hcl.Range ) error
43
43
}
@@ -67,11 +67,11 @@ func (s *mockServer) GetFiles(tflint.ModuleCtxType) map[string][]byte {
67
67
return map [string ][]byte {}
68
68
}
69
69
70
- func (s * mockServer ) GetRuleConfigContent (name string , schema * hclext.BodySchema ) (* hclext.BodyContent , * hcl. File , error ) {
70
+ func (s * mockServer ) GetRuleConfigContent (name string , schema * hclext.BodySchema ) (* hclext.BodyContent , map [ string ][] byte , error ) {
71
71
if s .impl .getRuleConfigContent != nil {
72
72
return s .impl .getRuleConfigContent (name , schema )
73
73
}
74
- return & hclext.BodyContent {}, & hcl. File {}, nil
74
+ return & hclext.BodyContent {}, map [ string ][] byte {}, nil
75
75
}
76
76
77
77
func (s * mockServer ) EvaluateExpr (expr hcl.Expression , opts tflint.EvaluateExprOption ) (cty.Value , error ) {
@@ -763,35 +763,43 @@ func TestDecodeRuleConfig(t *testing.T) {
763
763
Name string
764
764
RuleName string
765
765
Target interface {}
766
- ServerImpl func (string , * hclext.BodySchema ) (* hclext.BodyContent , * hcl. File , error )
766
+ ServerImpl func (string , * hclext.BodySchema ) (* hclext.BodyContent , map [ string ][] byte , error )
767
767
Want interface {}
768
768
ErrCheck func (error ) bool
769
769
}{
770
770
{
771
771
Name : "decode to struct" ,
772
772
RuleName : "test_rule" ,
773
773
Target : & ruleConfig {},
774
- ServerImpl : func (name string , schema * hclext.BodySchema ) (* hclext.BodyContent , * hcl. File , error ) {
774
+ ServerImpl : func (name string , schema * hclext.BodySchema ) (* hclext.BodyContent , map [ string ][] byte , error ) {
775
775
if name != "test_rule" {
776
- return & hclext.BodyContent {}, & hcl. File {}, errors .New ("unexpected file name" )
776
+ return & hclext.BodyContent {}, map [ string ][] byte {}, errors .New ("unexpected file name" )
777
777
}
778
778
779
- // Should return code inside of "rule" block
780
- //
781
- // rule "test_rule" {
782
- // name = "foo"
783
- // }
784
- code := `name = "foo"`
785
- file , diags := hclsyntax .ParseConfig ([]byte (code ), ".tflint.hcl" , hcl .InitialPos )
779
+ sources := map [string ][]byte {
780
+ ".tflint.hcl" : []byte (`
781
+ rule "test_rule" {
782
+ name = "foo"
783
+ }` ),
784
+ }
785
+
786
+ file , diags := hclsyntax .ParseConfig (sources [".tflint.hcl" ], ".tflint.hcl" , hcl .InitialPos )
787
+ if diags .HasErrors () {
788
+ return & hclext.BodyContent {}, sources , diags
789
+ }
790
+
791
+ content , diags := file .Body .Content (& hcl.BodySchema {
792
+ Blocks : []hcl.BlockHeaderSchema {{Type : "rule" , LabelNames : []string {"name" }}},
793
+ })
786
794
if diags .HasErrors () {
787
- return & hclext.BodyContent {}, & hcl. File {} , diags
795
+ return & hclext.BodyContent {}, sources , diags
788
796
}
789
797
790
- body , diags := hclext .Content (file .Body , schema )
798
+ body , diags := hclext .Content (content . Blocks [ 0 ] .Body , schema )
791
799
if diags .HasErrors () {
792
- return & hclext.BodyContent {}, & hcl. File {} , diags
800
+ return & hclext.BodyContent {}, sources , diags
793
801
}
794
- return body , file , nil
802
+ return body , sources , nil
795
803
},
796
804
Want : & ruleConfig {Name : "foo" },
797
805
ErrCheck : neverHappend ,
@@ -800,8 +808,8 @@ func TestDecodeRuleConfig(t *testing.T) {
800
808
Name : "server returns an error" ,
801
809
RuleName : "test_rule" ,
802
810
Target : & ruleConfig {},
803
- ServerImpl : func (name string , schema * hclext.BodySchema ) (* hclext.BodyContent , * hcl. File , error ) {
804
- return nil , nil , errors .New ("unexpected error" )
811
+ ServerImpl : func (name string , schema * hclext.BodySchema ) (* hclext.BodyContent , map [ string ][] byte , error ) {
812
+ return nil , map [ string ][] byte {} , errors .New ("unexpected error" )
805
813
},
806
814
Want : & ruleConfig {},
807
815
ErrCheck : func (err error ) bool {
@@ -812,8 +820,8 @@ func TestDecodeRuleConfig(t *testing.T) {
812
820
Name : "response body is empty" ,
813
821
RuleName : "test_rule" ,
814
822
Target : & ruleConfig {},
815
- ServerImpl : func (name string , schema * hclext.BodySchema ) (* hclext.BodyContent , * hcl. File , error ) {
816
- return nil , nil , nil
823
+ ServerImpl : func (name string , schema * hclext.BodySchema ) (* hclext.BodyContent , map [ string ][] byte , error ) {
824
+ return nil , map [ string ][] byte {} , nil
817
825
},
818
826
Want : & ruleConfig {},
819
827
ErrCheck : func (err error ) bool {
@@ -824,7 +832,7 @@ func TestDecodeRuleConfig(t *testing.T) {
824
832
Name : "config not found" ,
825
833
RuleName : "not_found" ,
826
834
Target : & ruleConfig {},
827
- ServerImpl : func (name string , schema * hclext.BodySchema ) (* hclext.BodyContent , * hcl. File , error ) {
835
+ ServerImpl : func (name string , schema * hclext.BodySchema ) (* hclext.BodyContent , map [ string ][] byte , error ) {
828
836
return & hclext.BodyContent {}, nil , nil
829
837
},
830
838
Want : & ruleConfig {},
0 commit comments