|
6 | 6 | "testing"
|
7 | 7 |
|
8 | 8 | "github.com/mgechev/revive/lint"
|
| 9 | + "github.com/mgechev/revive/rule" |
9 | 10 | )
|
10 | 11 |
|
11 | 12 | func TestGetConfig(t *testing.T) {
|
@@ -46,3 +47,94 @@ func TestGetConfig(t *testing.T) {
|
46 | 47 | })
|
47 | 48 | }
|
48 | 49 | }
|
| 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 | +} |
0 commit comments