Skip to content

Commit

Permalink
fixes issue #577 (#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
chavacava committed Sep 30, 2021
1 parent 0bcc996 commit e545098
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 25 deletions.
44 changes: 19 additions & 25 deletions config/config.go
Expand Up @@ -105,30 +105,6 @@ func getFormatters() map[string]lint.Formatter {

// GetLintingRules yields the linting rules that must be applied by the linter
func GetLintingRules(config *lint.Config) ([]lint.Rule, error) {
if config.EnableAllRules {
return getAllRules(config)
}

return getEnabledRules(config)
}

// getAllRules yields the list of all available rules except those disabled by configuration
func getAllRules(config *lint.Config) ([]lint.Rule, error) {
lintingRules := []lint.Rule{}
for _, r := range allRules {
ruleConf := config.Rules[r.Name()]
if ruleConf.Disabled {
continue // skip disabled rules
}

lintingRules = append(lintingRules, r)
}

return lintingRules, nil
}

// getEnabledRules yields the list of rules that are enabled by configuration
func getEnabledRules(config *lint.Config) ([]lint.Rule, error) {
rulesMap := map[string]lint.Rule{}
for _, r := range allRules {
rulesMap[r.Name()] = r
Expand Down Expand Up @@ -165,9 +141,27 @@ func parseConfig(path string) (*lint.Config, error) {
}

func normalizeConfig(config *lint.Config) {
const defaultConfidence = 0.8
if config.Confidence == 0 {
config.Confidence = 0.8
config.Confidence = defaultConfidence
}

if len(config.Rules) == 0 {
config.Rules = map[string]lint.RuleConfig{}
}
if config.EnableAllRules {
// Add to the configuration all rules not yet present in it
for _, rule := range allRules {
ruleName := rule.Name()
_, alreadyInConf := config.Rules[ruleName]
if alreadyInConf {
continue
}
// Add the rule with an empty conf for
config.Rules[ruleName] = lint.RuleConfig{}
}
}

severity := config.Severity
if severity != "" {
for k, v := range config.Rules {
Expand Down
92 changes: 92 additions & 0 deletions config/config_test.go
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/mgechev/revive/lint"
"github.com/mgechev/revive/rule"
)

func TestGetConfig(t *testing.T) {
Expand Down Expand Up @@ -46,3 +47,94 @@ func TestGetConfig(t *testing.T) {
})
}
}

func TestGetLintingRules(t *testing.T) {
tt := map[string]struct {
confPath string
wantRulesCount int
}{
"no rules": {
confPath: "testdata/noRules.toml",
wantRulesCount: 0,
},
"enableAllRules without disabled rules": {
confPath: "testdata/enableAll.toml",
wantRulesCount: len(allRules),
},
"enableAllRules with 2 disabled rules": {
confPath: "testdata/enableAllBut2.toml",
wantRulesCount: len(allRules) - 2,
},
"enable 2 rules": {
confPath: "testdata/enable2.toml",
wantRulesCount: 2,
},
}

for name, tc := range tt {
t.Run(name, func(t *testing.T) {
cfg, err := GetConfig(tc.confPath)
if err != nil {
t.Fatalf("Unexpected error while loading conf: %v", err)
}
rules, err := GetLintingRules(cfg)
switch {
case err != nil:
t.Fatalf("Unexpected error\n\t%v", err)
case len(rules) != tc.wantRulesCount:
t.Fatalf("Expected %v enabled linting rules got: %v", tc.wantRulesCount, len(rules))
}

})
}
}

func TestGetGlobalSeverity(t *testing.T) {
tt := map[string]struct {
confPath string
wantGlobalSeverity string
particularRule lint.Rule
wantParticularSeverity string
}{
"enable 2 rules with one specific severity": {
confPath: "testdata/enable2OneSpecificSeverity.toml",
wantGlobalSeverity: "warning",
particularRule: &rule.CyclomaticRule{},
wantParticularSeverity: "error",
},
"enableAllRules with one specific severity": {
confPath: "testdata/enableAllOneSpecificSeverity.toml",
wantGlobalSeverity: "error",
particularRule: &rule.DeepExitRule{},
wantParticularSeverity: "warning",
},
}

for name, tc := range tt {
t.Run(name, func(t *testing.T) {
cfg, err := GetConfig(tc.confPath)
if err != nil {
t.Fatalf("Unexpected error while loading conf: %v", err)
}
rules, err := GetLintingRules(cfg)
if err != nil {
t.Fatalf("Unexpected error while loading conf: %v", err)
}
for _, r := range rules {
ruleName := r.Name()
ruleCfg := cfg.Rules[ruleName]
ruleSeverity := string(ruleCfg.Severity)
switch ruleName {
case tc.particularRule.Name():
if tc.wantParticularSeverity != ruleSeverity {
t.Fatalf("Expected Severity %v for rule %v, got %v", tc.wantParticularSeverity, ruleName, ruleSeverity)
}
default:
if tc.wantGlobalSeverity != ruleSeverity {
t.Fatalf("Expected Severity %v for rule %v, got %v", tc.wantGlobalSeverity, ruleName, ruleSeverity)
}
}
}
})
}
}
12 changes: 12 additions & 0 deletions config/testdata/enable2.toml
@@ -0,0 +1,12 @@
ignoreGeneratedHeader = false
severity = "warning"
confidence = 0.8
errorCode = 0
warningCode = 0

enableAllRules = false

[rule.exported]
severity="error"
[rule.cyclomatic]

10 changes: 10 additions & 0 deletions config/testdata/enable2OneSpecificSeverity.toml
@@ -0,0 +1,10 @@
ignoreGeneratedHeader = false
severity = "warning"
confidence = 0.8
errorCode = 0
warningCode = 0

[rule.deep-exit]

[rule.cyclomatic]
severity="error"
7 changes: 7 additions & 0 deletions config/testdata/enableAll.toml
@@ -0,0 +1,7 @@
ignoreGeneratedHeader = false
severity = "warning"
confidence = 0.8
errorCode = 0
warningCode = 0

enableAllRules = true
12 changes: 12 additions & 0 deletions config/testdata/enableAllBut2.toml
@@ -0,0 +1,12 @@
ignoreGeneratedHeader = false
severity = "warning"
confidence = 0.8
errorCode = 0
warningCode = 0

enableAllRules = true

[rule.exported]
disabled=true
[rule.cyclomatic]
disabled=true
10 changes: 10 additions & 0 deletions config/testdata/enableAllOneSpecificSeverity.toml
@@ -0,0 +1,10 @@
ignoreGeneratedHeader = false
severity = "error"
confidence = 0.8
errorCode = 0
warningCode = 0

enableAllRules = true

[rule.deep-exit]
severity="warning"
5 changes: 5 additions & 0 deletions config/testdata/noRules.toml
@@ -0,0 +1,5 @@
ignoreGeneratedHeader = false
severity = "warning"
confidence = 0.8
errorCode = 0
warningCode = 0

0 comments on commit e545098

Please sign in to comment.