Skip to content

Commit 200b0a9

Browse files
authored
plugin2host: Return sources instead of *hcl.File in GetRuleConfigContent (#157)
1 parent 6c4029c commit 200b0a9

File tree

3 files changed

+36
-28
lines changed

3 files changed

+36
-28
lines changed

plugin/host2plugin/host2plugin_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,8 @@ func (s *mockServer) GetFile(filename string) (*hcl.File, error) {
474474
return nil, nil
475475
}
476476

477-
func (s *mockServer) GetRuleConfigContent(name string, schema *hclext.BodySchema) (*hclext.BodyContent, *hcl.File, error) {
478-
return &hclext.BodyContent{}, &hcl.File{}, nil
477+
func (s *mockServer) GetRuleConfigContent(name string, schema *hclext.BodySchema) (*hclext.BodyContent, map[string][]byte, error) {
478+
return &hclext.BodyContent{}, map[string][]byte{}, nil
479479
}
480480

481481
func (s *mockServer) EvaluateExpr(expr hcl.Expression, opts tflint.EvaluateExprOption) (cty.Value, error) {

plugin/plugin2host/plugin2host_test.go

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type mockServerImpl struct {
3737
getModuleContent func(*hclext.BodySchema, tflint.GetModuleContentOption) (*hclext.BodyContent, hcl.Diagnostics)
3838
getFile func(string) (*hcl.File, error)
3939
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)
4141
evaluateExpr func(hcl.Expression, tflint.EvaluateExprOption) (cty.Value, error)
4242
emitIssue func(tflint.Rule, string, hcl.Range) error
4343
}
@@ -67,11 +67,11 @@ func (s *mockServer) GetFiles(tflint.ModuleCtxType) map[string][]byte {
6767
return map[string][]byte{}
6868
}
6969

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) {
7171
if s.impl.getRuleConfigContent != nil {
7272
return s.impl.getRuleConfigContent(name, schema)
7373
}
74-
return &hclext.BodyContent{}, &hcl.File{}, nil
74+
return &hclext.BodyContent{}, map[string][]byte{}, nil
7575
}
7676

7777
func (s *mockServer) EvaluateExpr(expr hcl.Expression, opts tflint.EvaluateExprOption) (cty.Value, error) {
@@ -763,35 +763,43 @@ func TestDecodeRuleConfig(t *testing.T) {
763763
Name string
764764
RuleName string
765765
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)
767767
Want interface{}
768768
ErrCheck func(error) bool
769769
}{
770770
{
771771
Name: "decode to struct",
772772
RuleName: "test_rule",
773773
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) {
775775
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")
777777
}
778778

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+
})
786794
if diags.HasErrors() {
787-
return &hclext.BodyContent{}, &hcl.File{}, diags
795+
return &hclext.BodyContent{}, sources, diags
788796
}
789797

790-
body, diags := hclext.Content(file.Body, schema)
798+
body, diags := hclext.Content(content.Blocks[0].Body, schema)
791799
if diags.HasErrors() {
792-
return &hclext.BodyContent{}, &hcl.File{}, diags
800+
return &hclext.BodyContent{}, sources, diags
793801
}
794-
return body, file, nil
802+
return body, sources, nil
795803
},
796804
Want: &ruleConfig{Name: "foo"},
797805
ErrCheck: neverHappend,
@@ -800,8 +808,8 @@ func TestDecodeRuleConfig(t *testing.T) {
800808
Name: "server returns an error",
801809
RuleName: "test_rule",
802810
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")
805813
},
806814
Want: &ruleConfig{},
807815
ErrCheck: func(err error) bool {
@@ -812,8 +820,8 @@ func TestDecodeRuleConfig(t *testing.T) {
812820
Name: "response body is empty",
813821
RuleName: "test_rule",
814822
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
817825
},
818826
Want: &ruleConfig{},
819827
ErrCheck: func(err error) bool {
@@ -824,7 +832,7 @@ func TestDecodeRuleConfig(t *testing.T) {
824832
Name: "config not found",
825833
RuleName: "not_found",
826834
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) {
828836
return &hclext.BodyContent{}, nil, nil
829837
},
830838
Want: &ruleConfig{},

plugin/plugin2host/server.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type Server interface {
3232
GetFile(string) (*hcl.File, error)
3333
// For performance, GetFiles returns map[string][]bytes instead of map[string]*hcl.File.
3434
GetFiles(tflint.ModuleCtxType) map[string][]byte
35-
GetRuleConfigContent(string, *hclext.BodySchema) (*hclext.BodyContent, *hcl.File, error)
35+
GetRuleConfigContent(string, *hclext.BodySchema) (*hclext.BodyContent, map[string][]byte, error)
3636
EvaluateExpr(hcl.Expression, tflint.EvaluateExprOption) (cty.Value, error)
3737
EmitIssue(rule tflint.Rule, message string, location hcl.Range) error
3838
}
@@ -89,18 +89,18 @@ func (s *GRPCServer) GetRuleConfigContent(ctx context.Context, req *proto.GetRul
8989
return nil, status.Error(codes.InvalidArgument, "schema should not be null")
9090
}
9191

92-
body, file, err := s.Impl.GetRuleConfigContent(req.Name, fromproto.BodySchema(req.Schema))
92+
body, sources, err := s.Impl.GetRuleConfigContent(req.Name, fromproto.BodySchema(req.Schema))
9393
if err != nil {
9494
return nil, toproto.Error(codes.FailedPrecondition, err)
9595
}
9696
if body == nil {
9797
return nil, status.Error(codes.FailedPrecondition, "response body is empty")
9898
}
99-
if file == nil {
99+
if len(sources) == 0 {
100100
return nil, status.Error(codes.NotFound, "config file not found")
101101
}
102102

103-
content := toproto.BodyContent(body, map[string][]byte{file.Body.MissingItemRange().Filename: file.Bytes})
103+
content := toproto.BodyContent(body, sources)
104104
return &proto.GetRuleConfigContent_Response{Content: content}, nil
105105
}
106106

0 commit comments

Comments
 (0)