Skip to content

Commit

Permalink
Allow ruleset to accept Only option
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed Sep 17, 2022
1 parent 6997918 commit 39364ff
Show file tree
Hide file tree
Showing 8 changed files with 459 additions and 282 deletions.
6 changes: 5 additions & 1 deletion plugin/fromproto/fromproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,11 @@ func Config(config *proto.ApplyGlobalConfig_Config) *tflint.Config {
for name, rule := range config.Rules {
rules[name] = &tflint.RuleConfig{Name: rule.Name, Enabled: rule.Enabled}
}
return &tflint.Config{Rules: rules, DisabledByDefault: config.DisabledByDefault}
return &tflint.Config{
Rules: rules,
DisabledByDefault: config.DisabledByDefault,
Only: config.Only,
}
}

// GetModuleContentOption converts proto.GetModuleContent_Option to tflint.GetModuleContentOption
Expand Down
2 changes: 2 additions & 0 deletions plugin/host2plugin/host2plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ func TestApplyGlobalConfig(t *testing.T) {
"test2": {Name: "test2", Enabled: false},
},
DisabledByDefault: true,
Only: []string{"test_rule1", "test_rule2"},
},
ServerImpl: func(config *tflint.Config) error {
want := &tflint.Config{
Expand All @@ -307,6 +308,7 @@ func TestApplyGlobalConfig(t *testing.T) {
"test2": {Name: "test2", Enabled: false},
},
DisabledByDefault: true,
Only: []string{"test_rule1", "test_rule2"},
}

if diff := cmp.Diff(config, want); diff != "" {
Expand Down
561 changes: 285 additions & 276 deletions plugin/proto/tflint.pb.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions plugin/proto/tflint.proto
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ message ApplyGlobalConfig {
message Config {
map<string, RuleConfig> rules = 1;
bool disabled_by_default = 2;
repeated string only = 3;
}
message RuleConfig {
string name = 1;
Expand Down
6 changes: 5 additions & 1 deletion plugin/toproto/toproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ func Config(config *tflint.Config) *proto.ApplyGlobalConfig_Config {
for name, rule := range config.Rules {
rules[name] = &proto.ApplyGlobalConfig_RuleConfig{Name: rule.Name, Enabled: rule.Enabled}
}
return &proto.ApplyGlobalConfig_Config{Rules: rules, DisabledByDefault: config.DisabledByDefault}
return &proto.ApplyGlobalConfig_Config{
Rules: rules,
DisabledByDefault: config.DisabledByDefault,
Only: config.Only,
}
}

// GetModuleContentOption converts tflint.GetModuleContentOption to proto.GetModuleContent_Option
Expand Down
1 change: 1 addition & 0 deletions tflint/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package tflint
type Config struct {
Rules map[string]*RuleConfig
DisabledByDefault bool
Only []string
}

// RuleConfig is a TFLint's rule configuration.
Expand Down
22 changes: 18 additions & 4 deletions tflint/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package tflint

import (
"fmt"
"log"

"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint-plugin-sdk/logger"
)

var _ RuleSet = &BuiltinRuleSet{}
Expand Down Expand Up @@ -42,16 +42,30 @@ func (r *BuiltinRuleSet) RuleNames() []string {
// ApplyGlobalConfig applies the common config to the ruleset.
// This is not supposed to be overridden from custom rulesets.
// Override the ApplyConfig if you want to apply the plugin's own configuration.
//
// The priority of rule configs is as follows:
//
// 1. --only option
// 2. Rule config declared in each "rule" block
// 3. The `disabled_by_default` declared in global "config" block
func (r *BuiltinRuleSet) ApplyGlobalConfig(config *Config) error {
r.EnabledRules = []Rule{}
only := map[string]bool{}

if config.DisabledByDefault {
log.Printf("[DEBUG] Only mode is enabled. Ignoring default plugin rules")
if len(config.Only) > 0 {
logger.Debug("Only mode is enabled. Ignoring default plugin rules")
for _, rule := range config.Only {
only[rule] = true
}
} else if config.DisabledByDefault {
logger.Debug("Default plugin rules are disabled by default")
}

for _, rule := range r.Rules {
enabled := rule.Enabled()
if cfg := config.Rules[rule.Name()]; cfg != nil {
if len(only) > 0 {
enabled = only[rule.Name()]
} else if cfg := config.Rules[rule.Name()]; cfg != nil {
enabled = cfg.Enabled
} else if config.DisabledByDefault {
enabled = false
Expand Down
142 changes: 142 additions & 0 deletions tflint/ruleset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package tflint

import (
"testing"

"github.com/google/go-cmp/cmp"
)

type testRule struct {
DefaultRule
name string
}

func (r *testRule) Name() string { return r.name }
func (r *testRule) Enabled() bool { return true }
func (r *testRule) Severity() Severity { return ERROR }
func (r *testRule) Check(Runner) error { return nil }

type testRule1 struct {
testRule
}

type testRule2 struct {
testRule
}

type testRule3 struct {
testRule
}

func TestApplyGlobalConfig(t *testing.T) {
tests := []struct {
name string
config *Config
want []string
}{
{
name: "default",
config: &Config{},
want: []string{"test_rule1", "test_rule2", "test_rule3"},
},
{
name: "disabled by default",
config: &Config{DisabledByDefault: true},
want: []string{},
},
{
name: "rule config",
config: &Config{
Rules: map[string]*RuleConfig{
"test_rule1": {
Name: "test_rule1",
Enabled: false,
},
},
},
want: []string{"test_rule2", "test_rule3"},
},
{
name: "only",
config: &Config{Only: []string{"test_rule1"}},
want: []string{"test_rule1"},
},
{
name: "disabled by default + rule config",
config: &Config{
Rules: map[string]*RuleConfig{
"test_rule2": {
Name: "test_rule2",
Enabled: true,
},
},
DisabledByDefault: true,
},
want: []string{"test_rule2"},
},
{
name: "only + rule config",
config: &Config{
Rules: map[string]*RuleConfig{
"test_rule1": {
Name: "test_rule1",
Enabled: false,
},
},
Only: []string{"test_rule1", "test_rule2"},
},
want: []string{"test_rule1", "test_rule2"},
},
{
name: "disabled by default + only",
config: &Config{
DisabledByDefault: true,
Only: []string{"test_rule1", "test_rule2"},
},
want: []string{"test_rule1", "test_rule2"},
},
{
name: "disabled by default + only + rule config",
config: &Config{
Rules: map[string]*RuleConfig{
"test_rule2": {
Name: "test_rule2",
Enabled: true,
},
"test_rule3": {
Name: "test_rule3",
Enabled: false,
},
},
DisabledByDefault: true,
Only: []string{"test_rule1", "test_rule3"},
},
want: []string{"test_rule1", "test_rule3"},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ruleset := &BuiltinRuleSet{
Rules: []Rule{
&testRule1{testRule: testRule{name: "test_rule1"}},
&testRule2{testRule: testRule{name: "test_rule2"}},
&testRule3{testRule: testRule{name: "test_rule3"}},
},
}

if err := ruleset.ApplyGlobalConfig(test.config); err != nil {
t.Fatal(err)
}

got := make([]string, len(ruleset.EnabledRules))
for i, r := range ruleset.EnabledRules {
got[i] = r.Name()
}

if diff := cmp.Diff(got, test.want); diff != "" {
t.Errorf(diff)
}
})
}
}

0 comments on commit 39364ff

Please sign in to comment.