Skip to content

Commit

Permalink
Fix config initialisation
Browse files Browse the repository at this point in the history
Allow setting confidence to 0
  • Loading branch information
mihaitodor authored and mgechev committed Oct 4, 2021
1 parent 099eeac commit c3af594
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
14 changes: 10 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"math"

"github.com/mgechev/revive/formatter"

Expand All @@ -12,6 +13,10 @@ import (
"github.com/mgechev/revive/rule"
)

const (
defaultConfidence = 0.8
)

var defaultRules = []lint.Rule{
&rule.VarDeclarationsRule{},
&rule.PackageCommentsRule{},
Expand Down Expand Up @@ -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")
Expand All @@ -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
}

Expand Down Expand Up @@ -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{},
}
Expand Down
20 changes: 15 additions & 5 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
},
}

Expand All @@ -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)
}

})
}
}
Expand Down Expand Up @@ -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))
}

})
}
}
Expand Down
4 changes: 4 additions & 0 deletions config/testdata/issue-585-defaultConfidence.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ignoreGeneratedHeader = false
severity = "warning"
errorCode = 0
warningCode = 0
5 changes: 5 additions & 0 deletions config/testdata/issue-585.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ignoreGeneratedHeader = false
severity = "warning"
confidence = 0.0
errorCode = 0
warningCode = 0

0 comments on commit c3af594

Please sign in to comment.