Skip to content

Commit b792c29

Browse files
authored
Allow calling DecodeRuleConfig without rule config (#178)
1 parent 77ef450 commit b792c29

File tree

7 files changed

+105
-2
lines changed

7 files changed

+105
-2
lines changed

hclext/structure.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ func PartialContent(body hcl.Body, schema *BodySchema) (*BodyContent, hcl.Diagno
158158
return ret, diags
159159
}
160160

161+
// IsEmpty returns whether the body content is empty
162+
func (b *BodyContent) IsEmpty() bool {
163+
if b == nil {
164+
return true
165+
}
166+
return len(b.Attributes) == 0 && len(b.Blocks) == 0
167+
}
168+
161169
// AsNative returns self as hcl.Attributes
162170
func (as Attributes) AsNative() hcl.Attributes {
163171
ret := hcl.Attributes{}

hclext/structure_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,3 +695,44 @@ func TestContent_PartialContent(t *testing.T) {
695695
})
696696
}
697697
}
698+
699+
func Test_IsEmpty(t *testing.T) {
700+
tests := []struct {
701+
name string
702+
body *BodyContent
703+
want bool
704+
}{
705+
{
706+
name: "body is not empty",
707+
body: &BodyContent{
708+
Attributes: Attributes{
709+
"foo": &Attribute{Name: "foo"},
710+
},
711+
},
712+
want: false,
713+
},
714+
{
715+
name: "body has empty attributes and empty blocks",
716+
body: &BodyContent{Attributes: Attributes{}, Blocks: Blocks{}},
717+
want: true,
718+
},
719+
{
720+
name: "body has nil attributes and nil blocks",
721+
body: &BodyContent{},
722+
want: true,
723+
},
724+
{
725+
name: "body is nil",
726+
body: nil,
727+
want: true,
728+
},
729+
}
730+
731+
for _, test := range tests {
732+
t.Run(test.name, func(t *testing.T) {
733+
if test.body.IsEmpty() != test.want {
734+
t.Errorf("%t is expected, but got %t", test.want, test.body.IsEmpty())
735+
}
736+
})
737+
}
738+
}

helper/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (r *Runner) DecodeRuleConfig(name string, ret interface{}) error {
141141
}
142142
}
143143

144-
return fmt.Errorf("rule `%s` not found", name)
144+
return nil
145145
}
146146

147147
// EvaluateExpr returns a value of the passed expression.

helper/runner_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,46 @@ func Test_GetModuleContent_json(t *testing.T) {
280280
}
281281
}
282282

283+
func Test_DecodeRuleConfig(t *testing.T) {
284+
files := map[string]string{
285+
".tflint.hcl": `
286+
rule "test" {
287+
enabled = true
288+
foo = "bar"
289+
}`,
290+
}
291+
292+
runner := TestRunner(t, files)
293+
294+
type ruleConfig struct {
295+
Foo string `hclext:"foo"`
296+
}
297+
target := &ruleConfig{}
298+
if err := runner.DecodeRuleConfig("test", target); err != nil {
299+
t.Fatal(err)
300+
}
301+
302+
if target.Foo != "bar" {
303+
t.Errorf("target.Foo should be `bar`, but got `%s`", target.Foo)
304+
}
305+
}
306+
307+
func Test_DecodeRuleConfig_config_not_found(t *testing.T) {
308+
runner := TestRunner(t, map[string]string{})
309+
310+
type ruleConfig struct {
311+
Foo string `hclext:"foo"`
312+
}
313+
target := &ruleConfig{}
314+
if err := runner.DecodeRuleConfig("test", target); err != nil {
315+
t.Fatal(err)
316+
}
317+
318+
if target.Foo != "" {
319+
t.Errorf("target.Foo should be empty, but got `%s`", target.Foo)
320+
}
321+
}
322+
283323
func Test_EvaluateExpr(t *testing.T) {
284324
tests := []struct {
285325
Name string

plugin/plugin2host/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ func (c *GRPCClient) DecodeRuleConfig(name string, ret interface{}) error {
180180
if diags.HasErrors() {
181181
return diags
182182
}
183+
if content.IsEmpty() {
184+
return nil
185+
}
186+
183187
diags = hclext.DecodeBody(content, nil, ret)
184188
if diags.HasErrors() {
185189
return diags

plugin/plugin2host/plugin2host_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,16 @@ rule "test_rule" {
984984
ServerImpl: func(name string, schema *hclext.BodySchema) (*hclext.BodyContent, map[string][]byte, error) {
985985
return &hclext.BodyContent{}, nil, nil
986986
},
987+
Want: &ruleConfig{},
988+
ErrCheck: neverHappend,
989+
},
990+
{
991+
Name: "config not found with non-empty config",
992+
RuleName: "not_found",
993+
Target: &ruleConfig{},
994+
ServerImpl: func(name string, schema *hclext.BodySchema) (*hclext.BodyContent, map[string][]byte, error) {
995+
return &hclext.BodyContent{Attributes: hclext.Attributes{"foo": &hclext.Attribute{}}}, nil, nil
996+
},
987997
Want: &ruleConfig{},
988998
ErrCheck: func(err error) bool {
989999
return err == nil || err.Error() != "config file not found"

plugin/plugin2host/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (s *GRPCServer) GetRuleConfigContent(ctx context.Context, req *proto.GetRul
102102
if body == nil {
103103
return nil, status.Error(codes.FailedPrecondition, "response body is empty")
104104
}
105-
if len(sources) == 0 {
105+
if len(sources) == 0 && !body.IsEmpty() {
106106
return nil, status.Error(codes.NotFound, "config file not found")
107107
}
108108

0 commit comments

Comments
 (0)