github.com/songshiyun/revive@v1.1.5-0.20220323112655-f8433a19b3c5/config/config_test.go (about) 1 package config 2 3 import ( 4 "reflect" 5 "strings" 6 "testing" 7 8 "github.com/songshiyun/revive/lint" 9 "github.com/songshiyun/revive/rule" 10 ) 11 12 func TestGetConfig(t *testing.T) { 13 tt := map[string]struct { 14 confPath string 15 wantConfig *lint.Config 16 wantError string 17 wantConfidence float64 18 }{ 19 "non-reg issue #470": { 20 confPath: "testdata/issue-470.toml", 21 wantError: "", 22 }, 23 "unknown file": { 24 confPath: "unknown", 25 wantError: "cannot read the config file", 26 }, 27 "malformed file": { 28 confPath: "testdata/malformed.toml", 29 wantError: "cannot parse the config file", 30 }, 31 "default config": { 32 wantConfig: func() *lint.Config { 33 c := defaultConfig() 34 normalizeConfig(c) 35 return c 36 }(), 37 wantConfidence: defaultConfidence, 38 }, 39 "config from file issue #585": { 40 confPath: "testdata/issue-585.toml", 41 wantConfidence: 0.0, 42 }, 43 "config from file default confidence issue #585": { 44 confPath: "testdata/issue-585-defaultConfidence.toml", 45 wantConfidence: defaultConfidence, 46 }, 47 } 48 49 for name, tc := range tt { 50 t.Run(name, func(t *testing.T) { 51 cfg, err := GetConfig(tc.confPath) 52 switch { 53 case err != nil && tc.wantError == "": 54 t.Fatalf("Unexpected error\n\t%v", err) 55 case err != nil && !strings.Contains(err.Error(), tc.wantError): 56 t.Fatalf("Expected error\n\t%q\ngot:\n\t%v", tc.wantError, err) 57 case tc.wantConfig != nil && !reflect.DeepEqual(cfg, tc.wantConfig): 58 t.Fatalf("Expected config\n\t%+v\ngot:\n\t%+v", tc.wantConfig, cfg) 59 case tc.wantConfig != nil && tc.wantConfidence != cfg.Confidence: 60 t.Fatalf("Expected confidence\n\t%+v\ngot:\n\t%+v", tc.wantConfidence, cfg.Confidence) 61 } 62 }) 63 } 64 } 65 66 func TestGetLintingRules(t *testing.T) { 67 tt := map[string]struct { 68 confPath string 69 wantRulesCount int 70 }{ 71 "no rules": { 72 confPath: "testdata/noRules.toml", 73 wantRulesCount: 0, 74 }, 75 "enableAllRules without disabled rules": { 76 confPath: "testdata/enableAll.toml", 77 wantRulesCount: len(allRules), 78 }, 79 "enableAllRules with 2 disabled rules": { 80 confPath: "testdata/enableAllBut2.toml", 81 wantRulesCount: len(allRules) - 2, 82 }, 83 "enable 2 rules": { 84 confPath: "testdata/enable2.toml", 85 wantRulesCount: 2, 86 }, 87 } 88 89 for name, tc := range tt { 90 t.Run(name, func(t *testing.T) { 91 cfg, err := GetConfig(tc.confPath) 92 if err != nil { 93 t.Fatalf("Unexpected error while loading conf: %v", err) 94 } 95 rules, err := GetLintingRules(cfg, []lint.Rule{}) 96 switch { 97 case err != nil: 98 t.Fatalf("Unexpected error\n\t%v", err) 99 case len(rules) != tc.wantRulesCount: 100 t.Fatalf("Expected %v enabled linting rules got: %v", tc.wantRulesCount, len(rules)) 101 } 102 }) 103 } 104 } 105 106 func TestGetGlobalSeverity(t *testing.T) { 107 tt := map[string]struct { 108 confPath string 109 wantGlobalSeverity string 110 particularRule lint.Rule 111 wantParticularSeverity string 112 }{ 113 "enable 2 rules with one specific severity": { 114 confPath: "testdata/enable2OneSpecificSeverity.toml", 115 wantGlobalSeverity: "warning", 116 particularRule: &rule.CyclomaticRule{}, 117 wantParticularSeverity: "error", 118 }, 119 "enableAllRules with one specific severity": { 120 confPath: "testdata/enableAllOneSpecificSeverity.toml", 121 wantGlobalSeverity: "error", 122 particularRule: &rule.DeepExitRule{}, 123 wantParticularSeverity: "warning", 124 }, 125 } 126 127 for name, tc := range tt { 128 t.Run(name, func(t *testing.T) { 129 cfg, err := GetConfig(tc.confPath) 130 if err != nil { 131 t.Fatalf("Unexpected error while loading conf: %v", err) 132 } 133 rules, err := GetLintingRules(cfg, []lint.Rule{}) 134 if err != nil { 135 t.Fatalf("Unexpected error while loading conf: %v", err) 136 } 137 for _, r := range rules { 138 ruleName := r.Name() 139 ruleCfg := cfg.Rules[ruleName] 140 ruleSeverity := string(ruleCfg.Severity) 141 switch ruleName { 142 case tc.particularRule.Name(): 143 if tc.wantParticularSeverity != ruleSeverity { 144 t.Fatalf("Expected Severity %v for rule %v, got %v", tc.wantParticularSeverity, ruleName, ruleSeverity) 145 } 146 default: 147 if tc.wantGlobalSeverity != ruleSeverity { 148 t.Fatalf("Expected Severity %v for rule %v, got %v", tc.wantGlobalSeverity, ruleName, ruleSeverity) 149 } 150 } 151 } 152 }) 153 } 154 }