diff --git a/config/config.go b/config/config.go index 298c0bdf5..e98aaf0a0 100644 --- a/config/config.go +++ b/config/config.go @@ -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 @@ -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 { diff --git a/config/config_test.go b/config/config_test.go index d10b0dfae..3bf854071 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/mgechev/revive/lint" + "github.com/mgechev/revive/rule" ) func TestGetConfig(t *testing.T) { @@ -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) + } + } + } + }) + } +} diff --git a/config/testdata/enable2.toml b/config/testdata/enable2.toml new file mode 100644 index 000000000..843c6bc8f --- /dev/null +++ b/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] + diff --git a/config/testdata/enable2OneSpecificSeverity.toml b/config/testdata/enable2OneSpecificSeverity.toml new file mode 100644 index 000000000..004dc83aa --- /dev/null +++ b/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" diff --git a/config/testdata/enableAll.toml b/config/testdata/enableAll.toml new file mode 100644 index 000000000..8538e3e43 --- /dev/null +++ b/config/testdata/enableAll.toml @@ -0,0 +1,7 @@ +ignoreGeneratedHeader = false +severity = "warning" +confidence = 0.8 +errorCode = 0 +warningCode = 0 + +enableAllRules = true diff --git a/config/testdata/enableAllBut2.toml b/config/testdata/enableAllBut2.toml new file mode 100644 index 000000000..adc8f7663 --- /dev/null +++ b/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 diff --git a/config/testdata/enableAllOneSpecificSeverity.toml b/config/testdata/enableAllOneSpecificSeverity.toml new file mode 100644 index 000000000..87e09f42b --- /dev/null +++ b/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" diff --git a/config/testdata/noRules.toml b/config/testdata/noRules.toml new file mode 100644 index 000000000..fad1dc79c --- /dev/null +++ b/config/testdata/noRules.toml @@ -0,0 +1,5 @@ +ignoreGeneratedHeader = false +severity = "warning" +confidence = 0.8 +errorCode = 0 +warningCode = 0