Skip to content

Commit

Permalink
plugin2host: Return sources instead of *hcl.File in GetRuleConfigCont…
Browse files Browse the repository at this point in the history
…ent (#157)
  • Loading branch information
wata727 authored May 2, 2022
1 parent 6c4029c commit 200b0a9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
4 changes: 2 additions & 2 deletions plugin/host2plugin/host2plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,8 @@ func (s *mockServer) GetFile(filename string) (*hcl.File, error) {
return nil, nil
}

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

func (s *mockServer) EvaluateExpr(expr hcl.Expression, opts tflint.EvaluateExprOption) (cty.Value, error) {
Expand Down
52 changes: 30 additions & 22 deletions plugin/plugin2host/plugin2host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type mockServerImpl struct {
getModuleContent func(*hclext.BodySchema, tflint.GetModuleContentOption) (*hclext.BodyContent, hcl.Diagnostics)
getFile func(string) (*hcl.File, error)
getFiles func() map[string][]byte
getRuleConfigContent func(string, *hclext.BodySchema) (*hclext.BodyContent, *hcl.File, error)
getRuleConfigContent func(string, *hclext.BodySchema) (*hclext.BodyContent, map[string][]byte, error)
evaluateExpr func(hcl.Expression, tflint.EvaluateExprOption) (cty.Value, error)
emitIssue func(tflint.Rule, string, hcl.Range) error
}
Expand Down Expand Up @@ -67,11 +67,11 @@ func (s *mockServer) GetFiles(tflint.ModuleCtxType) map[string][]byte {
return map[string][]byte{}
}

func (s *mockServer) GetRuleConfigContent(name string, schema *hclext.BodySchema) (*hclext.BodyContent, *hcl.File, error) {
func (s *mockServer) GetRuleConfigContent(name string, schema *hclext.BodySchema) (*hclext.BodyContent, map[string][]byte, error) {
if s.impl.getRuleConfigContent != nil {
return s.impl.getRuleConfigContent(name, schema)
}
return &hclext.BodyContent{}, &hcl.File{}, nil
return &hclext.BodyContent{}, map[string][]byte{}, nil
}

func (s *mockServer) EvaluateExpr(expr hcl.Expression, opts tflint.EvaluateExprOption) (cty.Value, error) {
Expand Down Expand Up @@ -763,35 +763,43 @@ func TestDecodeRuleConfig(t *testing.T) {
Name string
RuleName string
Target interface{}
ServerImpl func(string, *hclext.BodySchema) (*hclext.BodyContent, *hcl.File, error)
ServerImpl func(string, *hclext.BodySchema) (*hclext.BodyContent, map[string][]byte, error)
Want interface{}
ErrCheck func(error) bool
}{
{
Name: "decode to struct",
RuleName: "test_rule",
Target: &ruleConfig{},
ServerImpl: func(name string, schema *hclext.BodySchema) (*hclext.BodyContent, *hcl.File, error) {
ServerImpl: func(name string, schema *hclext.BodySchema) (*hclext.BodyContent, map[string][]byte, error) {
if name != "test_rule" {
return &hclext.BodyContent{}, &hcl.File{}, errors.New("unexpected file name")
return &hclext.BodyContent{}, map[string][]byte{}, errors.New("unexpected file name")
}

// Should return code inside of "rule" block
//
// rule "test_rule" {
// name = "foo"
// }
code := `name = "foo"`
file, diags := hclsyntax.ParseConfig([]byte(code), ".tflint.hcl", hcl.InitialPos)
sources := map[string][]byte{
".tflint.hcl": []byte(`
rule "test_rule" {
name = "foo"
}`),
}

file, diags := hclsyntax.ParseConfig(sources[".tflint.hcl"], ".tflint.hcl", hcl.InitialPos)
if diags.HasErrors() {
return &hclext.BodyContent{}, sources, diags
}

content, diags := file.Body.Content(&hcl.BodySchema{
Blocks: []hcl.BlockHeaderSchema{{Type: "rule", LabelNames: []string{"name"}}},
})
if diags.HasErrors() {
return &hclext.BodyContent{}, &hcl.File{}, diags
return &hclext.BodyContent{}, sources, diags
}

body, diags := hclext.Content(file.Body, schema)
body, diags := hclext.Content(content.Blocks[0].Body, schema)
if diags.HasErrors() {
return &hclext.BodyContent{}, &hcl.File{}, diags
return &hclext.BodyContent{}, sources, diags
}
return body, file, nil
return body, sources, nil
},
Want: &ruleConfig{Name: "foo"},
ErrCheck: neverHappend,
Expand All @@ -800,8 +808,8 @@ func TestDecodeRuleConfig(t *testing.T) {
Name: "server returns an error",
RuleName: "test_rule",
Target: &ruleConfig{},
ServerImpl: func(name string, schema *hclext.BodySchema) (*hclext.BodyContent, *hcl.File, error) {
return nil, nil, errors.New("unexpected error")
ServerImpl: func(name string, schema *hclext.BodySchema) (*hclext.BodyContent, map[string][]byte, error) {
return nil, map[string][]byte{}, errors.New("unexpected error")
},
Want: &ruleConfig{},
ErrCheck: func(err error) bool {
Expand All @@ -812,8 +820,8 @@ func TestDecodeRuleConfig(t *testing.T) {
Name: "response body is empty",
RuleName: "test_rule",
Target: &ruleConfig{},
ServerImpl: func(name string, schema *hclext.BodySchema) (*hclext.BodyContent, *hcl.File, error) {
return nil, nil, nil
ServerImpl: func(name string, schema *hclext.BodySchema) (*hclext.BodyContent, map[string][]byte, error) {
return nil, map[string][]byte{}, nil
},
Want: &ruleConfig{},
ErrCheck: func(err error) bool {
Expand All @@ -824,7 +832,7 @@ func TestDecodeRuleConfig(t *testing.T) {
Name: "config not found",
RuleName: "not_found",
Target: &ruleConfig{},
ServerImpl: func(name string, schema *hclext.BodySchema) (*hclext.BodyContent, *hcl.File, error) {
ServerImpl: func(name string, schema *hclext.BodySchema) (*hclext.BodyContent, map[string][]byte, error) {
return &hclext.BodyContent{}, nil, nil
},
Want: &ruleConfig{},
Expand Down
8 changes: 4 additions & 4 deletions plugin/plugin2host/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Server interface {
GetFile(string) (*hcl.File, error)
// For performance, GetFiles returns map[string][]bytes instead of map[string]*hcl.File.
GetFiles(tflint.ModuleCtxType) map[string][]byte
GetRuleConfigContent(string, *hclext.BodySchema) (*hclext.BodyContent, *hcl.File, error)
GetRuleConfigContent(string, *hclext.BodySchema) (*hclext.BodyContent, map[string][]byte, error)
EvaluateExpr(hcl.Expression, tflint.EvaluateExprOption) (cty.Value, error)
EmitIssue(rule tflint.Rule, message string, location hcl.Range) error
}
Expand Down Expand Up @@ -89,18 +89,18 @@ func (s *GRPCServer) GetRuleConfigContent(ctx context.Context, req *proto.GetRul
return nil, status.Error(codes.InvalidArgument, "schema should not be null")
}

body, file, err := s.Impl.GetRuleConfigContent(req.Name, fromproto.BodySchema(req.Schema))
body, sources, err := s.Impl.GetRuleConfigContent(req.Name, fromproto.BodySchema(req.Schema))
if err != nil {
return nil, toproto.Error(codes.FailedPrecondition, err)
}
if body == nil {
return nil, status.Error(codes.FailedPrecondition, "response body is empty")
}
if file == nil {
if len(sources) == 0 {
return nil, status.Error(codes.NotFound, "config file not found")
}

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

Expand Down

0 comments on commit 200b0a9

Please sign in to comment.