Repository: mgechev/revive
Pull request #586 (2021-09-28 23:41:02)

Update module golang.org/x/tools to v0.1.7
[![WhiteSource Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [golang.org/x/tools](https://togithub.com/golang/tools) | require | patch | `v0.1.6` -> `v0.1.7` |

---

### Release Notes

<details>
<summary>golang/tools</summary>

### [`v0.1.7`](https://togithub.com/golang/tools/compare/v0.1.6...v0.1.7)

[Compare Source](https://togithub.com/golang/tools/compare/v0.1.6...v0.1.7)

</details>

---

### Configuration

📅 **Schedule**: At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box.

---

This PR has been generated by [WhiteSource Renovate](https://renovate.whitesourcesoftware.com). View repository job log [here](https://app.renovatebot.com/dashboard#github/mgechev/revive).


Patch:
diff --git a/config/config.go b/config/config.go
index 298c0bd..e98aaf0 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/go.mod b/go.mod
index ac49ede..62633f8 100644
--- a/go.mod
+++ b/go.mod
@@ -11,5 +11,5 @@ require (
 	github.com/mitchellh/go-homedir v1.1.0
 	github.com/olekukonko/tablewriter v0.0.5
 	github.com/pkg/errors v0.9.1
-	golang.org/x/tools v0.1.6
+	golang.org/x/tools v0.1.7
 )
diff --git a/go.sum b/go.sum
index 6f29791..3cc14c4 100644
--- a/go.sum
+++ b/go.sum
@@ -96,6 +96,8 @@ golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA=
 golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
 golang.org/x/tools v0.1.6 h1:SIasE1FVIQOWz2GEAHFOmoW7xchJcqlucjSULTL0Ag4=
 golang.org/x/tools v0.1.6/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
+golang.org/x/tools v0.1.7 h1:6j8CgantCy3yc8JGBqkDLMKWqZ0RDU2g1HVgacojGWQ=
+golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
 golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=


Test patch:
diff --git a/config/config_test.go b/config/config_test.go
index d10b0df..3bf8540 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 0000000..843c6bc
--- /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 0000000..004dc83
--- /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 0000000..8538e3e
--- /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 0000000..adc8f76
--- /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 0000000..87e09f4
--- /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 0000000..fad1dc7
--- /dev/null
+++ b/config/testdata/noRules.toml
@@ -0,0 +1,5 @@
+ignoreGeneratedHeader = false
+severity = "warning"
+confidence = 0.8
+errorCode = 0
+warningCode = 0