1
0
mirror of https://github.com/mgechev/revive.git synced 2025-03-21 21:17:06 +02:00

Fix config initialisation

Allow setting confidence to 0
This commit is contained in:
Mihai Todor 2021-10-02 16:30:52 +01:00 committed by Minko Gechev
parent 099eeaca0c
commit c3af594461
4 changed files with 34 additions and 9 deletions

View File

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"math"
"github.com/mgechev/revive/formatter"
@ -12,6 +13,10 @@ import (
"github.com/mgechev/revive/rule"
)
const (
defaultConfidence = 0.8
)
var defaultRules = []lint.Rule{
&rule.VarDeclarationsRule{},
&rule.PackageCommentsRule{},
@ -129,7 +134,9 @@ func GetLintingRules(config *lint.Config) ([]lint.Rule, error) {
}
func parseConfig(path string) (*lint.Config, error) {
config := &lint.Config{}
config := &lint.Config{
Confidence: math.Inf(1),
}
file, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.New("cannot read the config file")
@ -142,8 +149,7 @@ func parseConfig(path string) (*lint.Config, error) {
}
func normalizeConfig(config *lint.Config) {
const defaultConfidence = 0.8
if config.Confidence == 0 {
if config.Confidence == math.Inf(1) {
config.Confidence = defaultConfidence
}
@ -210,7 +216,7 @@ func GetFormatter(formatterName string) (lint.Formatter, error) {
func defaultConfig() *lint.Config {
defaultConfig := lint.Config{
Confidence: 0.0,
Confidence: math.Inf(1),
Severity: lint.SeverityWarning,
Rules: map[string]lint.RuleConfig{},
}

View File

@ -11,9 +11,10 @@ import (
func TestGetConfig(t *testing.T) {
tt := map[string]struct {
confPath string
wantConfig *lint.Config
wantError string
confPath string
wantConfig *lint.Config
wantError string
wantConfidence float64
}{
"non-reg issue #470": {
confPath: "testdata/issue-470.toml",
@ -33,6 +34,15 @@ func TestGetConfig(t *testing.T) {
normalizeConfig(c)
return c
}(),
wantConfidence: defaultConfidence,
},
"config from file issue #585": {
confPath: "testdata/issue-585.toml",
wantConfidence: 0.0,
},
"config from file default confidence issue #585": {
confPath: "testdata/issue-585-defaultConfidence.toml",
wantConfidence: defaultConfidence,
},
}
@ -46,8 +56,9 @@ func TestGetConfig(t *testing.T) {
t.Fatalf("Expected error\n\t%q\ngot:\n\t%v", tc.wantError, err)
case tc.wantConfig != nil && !reflect.DeepEqual(cfg, tc.wantConfig):
t.Fatalf("Expected config\n\t%+v\ngot:\n\t%+v", tc.wantConfig, cfg)
case tc.wantConfig != nil && tc.wantConfidence != cfg.Confidence:
t.Fatalf("Expected confidence\n\t%+v\ngot:\n\t%+v", tc.wantConfidence, cfg.Confidence)
}
})
}
}
@ -88,7 +99,6 @@ func TestGetLintingRules(t *testing.T) {
case len(rules) != tc.wantRulesCount:
t.Fatalf("Expected %v enabled linting rules got: %v", tc.wantRulesCount, len(rules))
}
})
}
}

View File

@ -0,0 +1,4 @@
ignoreGeneratedHeader = false
severity = "warning"
errorCode = 0
warningCode = 0

5
config/testdata/issue-585.toml vendored Normal file
View File

@ -0,0 +1,5 @@
ignoreGeneratedHeader = false
severity = "warning"
confidence = 0.0
errorCode = 0
warningCode = 0