mirror of
https://github.com/mgechev/revive.git
synced 2025-03-25 21:29:16 +02:00
Fix config initialisation
Allow setting confidence to 0
This commit is contained in:
parent
099eeaca0c
commit
c3af594461
@ -4,6 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"math"
|
||||||
|
|
||||||
"github.com/mgechev/revive/formatter"
|
"github.com/mgechev/revive/formatter"
|
||||||
|
|
||||||
@ -12,6 +13,10 @@ import (
|
|||||||
"github.com/mgechev/revive/rule"
|
"github.com/mgechev/revive/rule"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultConfidence = 0.8
|
||||||
|
)
|
||||||
|
|
||||||
var defaultRules = []lint.Rule{
|
var defaultRules = []lint.Rule{
|
||||||
&rule.VarDeclarationsRule{},
|
&rule.VarDeclarationsRule{},
|
||||||
&rule.PackageCommentsRule{},
|
&rule.PackageCommentsRule{},
|
||||||
@ -129,7 +134,9 @@ func GetLintingRules(config *lint.Config) ([]lint.Rule, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(path string) (*lint.Config, error) {
|
func parseConfig(path string) (*lint.Config, error) {
|
||||||
config := &lint.Config{}
|
config := &lint.Config{
|
||||||
|
Confidence: math.Inf(1),
|
||||||
|
}
|
||||||
file, err := ioutil.ReadFile(path)
|
file, err := ioutil.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New("cannot read the config file")
|
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) {
|
func normalizeConfig(config *lint.Config) {
|
||||||
const defaultConfidence = 0.8
|
if config.Confidence == math.Inf(1) {
|
||||||
if config.Confidence == 0 {
|
|
||||||
config.Confidence = defaultConfidence
|
config.Confidence = defaultConfidence
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -210,7 +216,7 @@ func GetFormatter(formatterName string) (lint.Formatter, error) {
|
|||||||
|
|
||||||
func defaultConfig() *lint.Config {
|
func defaultConfig() *lint.Config {
|
||||||
defaultConfig := lint.Config{
|
defaultConfig := lint.Config{
|
||||||
Confidence: 0.0,
|
Confidence: math.Inf(1),
|
||||||
Severity: lint.SeverityWarning,
|
Severity: lint.SeverityWarning,
|
||||||
Rules: map[string]lint.RuleConfig{},
|
Rules: map[string]lint.RuleConfig{},
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,10 @@ import (
|
|||||||
|
|
||||||
func TestGetConfig(t *testing.T) {
|
func TestGetConfig(t *testing.T) {
|
||||||
tt := map[string]struct {
|
tt := map[string]struct {
|
||||||
confPath string
|
confPath string
|
||||||
wantConfig *lint.Config
|
wantConfig *lint.Config
|
||||||
wantError string
|
wantError string
|
||||||
|
wantConfidence float64
|
||||||
}{
|
}{
|
||||||
"non-reg issue #470": {
|
"non-reg issue #470": {
|
||||||
confPath: "testdata/issue-470.toml",
|
confPath: "testdata/issue-470.toml",
|
||||||
@ -33,6 +34,15 @@ func TestGetConfig(t *testing.T) {
|
|||||||
normalizeConfig(c)
|
normalizeConfig(c)
|
||||||
return 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)
|
t.Fatalf("Expected error\n\t%q\ngot:\n\t%v", tc.wantError, err)
|
||||||
case tc.wantConfig != nil && !reflect.DeepEqual(cfg, tc.wantConfig):
|
case tc.wantConfig != nil && !reflect.DeepEqual(cfg, tc.wantConfig):
|
||||||
t.Fatalf("Expected config\n\t%+v\ngot:\n\t%+v", tc.wantConfig, cfg)
|
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:
|
case len(rules) != tc.wantRulesCount:
|
||||||
t.Fatalf("Expected %v enabled linting rules got: %v", tc.wantRulesCount, len(rules))
|
t.Fatalf("Expected %v enabled linting rules got: %v", tc.wantRulesCount, len(rules))
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
4
config/testdata/issue-585-defaultConfidence.toml
vendored
Normal file
4
config/testdata/issue-585-defaultConfidence.toml
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
ignoreGeneratedHeader = false
|
||||||
|
severity = "warning"
|
||||||
|
errorCode = 0
|
||||||
|
warningCode = 0
|
5
config/testdata/issue-585.toml
vendored
Normal file
5
config/testdata/issue-585.toml
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
ignoreGeneratedHeader = false
|
||||||
|
severity = "warning"
|
||||||
|
confidence = 0.0
|
||||||
|
errorCode = 0
|
||||||
|
warningCode = 0
|
Loading…
x
Reference in New Issue
Block a user