Skip to content

Commit e545098

Browse files
authored
fixes issue #577 (#581)
1 parent 0bcc996 commit e545098

8 files changed

+167
-25
lines changed

config/config.go

+19-25
Original file line numberDiff line numberDiff line change
@@ -105,30 +105,6 @@ func getFormatters() map[string]lint.Formatter {
105105

106106
// GetLintingRules yields the linting rules that must be applied by the linter
107107
func GetLintingRules(config *lint.Config) ([]lint.Rule, error) {
108-
if config.EnableAllRules {
109-
return getAllRules(config)
110-
}
111-
112-
return getEnabledRules(config)
113-
}
114-
115-
// getAllRules yields the list of all available rules except those disabled by configuration
116-
func getAllRules(config *lint.Config) ([]lint.Rule, error) {
117-
lintingRules := []lint.Rule{}
118-
for _, r := range allRules {
119-
ruleConf := config.Rules[r.Name()]
120-
if ruleConf.Disabled {
121-
continue // skip disabled rules
122-
}
123-
124-
lintingRules = append(lintingRules, r)
125-
}
126-
127-
return lintingRules, nil
128-
}
129-
130-
// getEnabledRules yields the list of rules that are enabled by configuration
131-
func getEnabledRules(config *lint.Config) ([]lint.Rule, error) {
132108
rulesMap := map[string]lint.Rule{}
133109
for _, r := range allRules {
134110
rulesMap[r.Name()] = r
@@ -165,9 +141,27 @@ func parseConfig(path string) (*lint.Config, error) {
165141
}
166142

167143
func normalizeConfig(config *lint.Config) {
144+
const defaultConfidence = 0.8
168145
if config.Confidence == 0 {
169-
config.Confidence = 0.8
146+
config.Confidence = defaultConfidence
147+
}
148+
149+
if len(config.Rules) == 0 {
150+
config.Rules = map[string]lint.RuleConfig{}
170151
}
152+
if config.EnableAllRules {
153+
// Add to the configuration all rules not yet present in it
154+
for _, rule := range allRules {
155+
ruleName := rule.Name()
156+
_, alreadyInConf := config.Rules[ruleName]
157+
if alreadyInConf {
158+
continue
159+
}
160+
// Add the rule with an empty conf for
161+
config.Rules[ruleName] = lint.RuleConfig{}
162+
}
163+
}
164+
171165
severity := config.Severity
172166
if severity != "" {
173167
for k, v := range config.Rules {

config/config_test.go

+92
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/mgechev/revive/lint"
9+
"github.com/mgechev/revive/rule"
910
)
1011

1112
func TestGetConfig(t *testing.T) {
@@ -46,3 +47,94 @@ func TestGetConfig(t *testing.T) {
4647
})
4748
}
4849
}
50+
51+
func TestGetLintingRules(t *testing.T) {
52+
tt := map[string]struct {
53+
confPath string
54+
wantRulesCount int
55+
}{
56+
"no rules": {
57+
confPath: "testdata/noRules.toml",
58+
wantRulesCount: 0,
59+
},
60+
"enableAllRules without disabled rules": {
61+
confPath: "testdata/enableAll.toml",
62+
wantRulesCount: len(allRules),
63+
},
64+
"enableAllRules with 2 disabled rules": {
65+
confPath: "testdata/enableAllBut2.toml",
66+
wantRulesCount: len(allRules) - 2,
67+
},
68+
"enable 2 rules": {
69+
confPath: "testdata/enable2.toml",
70+
wantRulesCount: 2,
71+
},
72+
}
73+
74+
for name, tc := range tt {
75+
t.Run(name, func(t *testing.T) {
76+
cfg, err := GetConfig(tc.confPath)
77+
if err != nil {
78+
t.Fatalf("Unexpected error while loading conf: %v", err)
79+
}
80+
rules, err := GetLintingRules(cfg)
81+
switch {
82+
case err != nil:
83+
t.Fatalf("Unexpected error\n\t%v", err)
84+
case len(rules) != tc.wantRulesCount:
85+
t.Fatalf("Expected %v enabled linting rules got: %v", tc.wantRulesCount, len(rules))
86+
}
87+
88+
})
89+
}
90+
}
91+
92+
func TestGetGlobalSeverity(t *testing.T) {
93+
tt := map[string]struct {
94+
confPath string
95+
wantGlobalSeverity string
96+
particularRule lint.Rule
97+
wantParticularSeverity string
98+
}{
99+
"enable 2 rules with one specific severity": {
100+
confPath: "testdata/enable2OneSpecificSeverity.toml",
101+
wantGlobalSeverity: "warning",
102+
particularRule: &rule.CyclomaticRule{},
103+
wantParticularSeverity: "error",
104+
},
105+
"enableAllRules with one specific severity": {
106+
confPath: "testdata/enableAllOneSpecificSeverity.toml",
107+
wantGlobalSeverity: "error",
108+
particularRule: &rule.DeepExitRule{},
109+
wantParticularSeverity: "warning",
110+
},
111+
}
112+
113+
for name, tc := range tt {
114+
t.Run(name, func(t *testing.T) {
115+
cfg, err := GetConfig(tc.confPath)
116+
if err != nil {
117+
t.Fatalf("Unexpected error while loading conf: %v", err)
118+
}
119+
rules, err := GetLintingRules(cfg)
120+
if err != nil {
121+
t.Fatalf("Unexpected error while loading conf: %v", err)
122+
}
123+
for _, r := range rules {
124+
ruleName := r.Name()
125+
ruleCfg := cfg.Rules[ruleName]
126+
ruleSeverity := string(ruleCfg.Severity)
127+
switch ruleName {
128+
case tc.particularRule.Name():
129+
if tc.wantParticularSeverity != ruleSeverity {
130+
t.Fatalf("Expected Severity %v for rule %v, got %v", tc.wantParticularSeverity, ruleName, ruleSeverity)
131+
}
132+
default:
133+
if tc.wantGlobalSeverity != ruleSeverity {
134+
t.Fatalf("Expected Severity %v for rule %v, got %v", tc.wantGlobalSeverity, ruleName, ruleSeverity)
135+
}
136+
}
137+
}
138+
})
139+
}
140+
}

config/testdata/enable2.toml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
ignoreGeneratedHeader = false
2+
severity = "warning"
3+
confidence = 0.8
4+
errorCode = 0
5+
warningCode = 0
6+
7+
enableAllRules = false
8+
9+
[rule.exported]
10+
severity="error"
11+
[rule.cyclomatic]
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
ignoreGeneratedHeader = false
2+
severity = "warning"
3+
confidence = 0.8
4+
errorCode = 0
5+
warningCode = 0
6+
7+
[rule.deep-exit]
8+
9+
[rule.cyclomatic]
10+
severity="error"

config/testdata/enableAll.toml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ignoreGeneratedHeader = false
2+
severity = "warning"
3+
confidence = 0.8
4+
errorCode = 0
5+
warningCode = 0
6+
7+
enableAllRules = true

config/testdata/enableAllBut2.toml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
ignoreGeneratedHeader = false
2+
severity = "warning"
3+
confidence = 0.8
4+
errorCode = 0
5+
warningCode = 0
6+
7+
enableAllRules = true
8+
9+
[rule.exported]
10+
disabled=true
11+
[rule.cyclomatic]
12+
disabled=true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
ignoreGeneratedHeader = false
2+
severity = "error"
3+
confidence = 0.8
4+
errorCode = 0
5+
warningCode = 0
6+
7+
enableAllRules = true
8+
9+
[rule.deep-exit]
10+
severity="warning"

config/testdata/noRules.toml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ignoreGeneratedHeader = false
2+
severity = "warning"
3+
confidence = 0.8
4+
errorCode = 0
5+
warningCode = 0

0 commit comments

Comments
 (0)