From c3af594461b6ef6cfe84c748f2d3b868df9cce4a Mon Sep 17 00:00:00 2001 From: Mihai Todor Date: Sat, 2 Oct 2021 16:30:52 +0100 Subject: [PATCH] Fix config initialisation Allow setting confidence to 0 --- config/config.go | 14 +++++++++---- config/config_test.go | 20 ++++++++++++++----- .../testdata/issue-585-defaultConfidence.toml | 4 ++++ config/testdata/issue-585.toml | 5 +++++ 4 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 config/testdata/issue-585-defaultConfidence.toml create mode 100644 config/testdata/issue-585.toml diff --git a/config/config.go b/config/config.go index 8666cf4..c1a84cd 100644 --- a/config/config.go +++ b/config/config.go @@ -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{}, } diff --git a/config/config_test.go b/config/config_test.go index ebad320..53bff5f 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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)) } - }) } } diff --git a/config/testdata/issue-585-defaultConfidence.toml b/config/testdata/issue-585-defaultConfidence.toml new file mode 100644 index 0000000..443ee4c --- /dev/null +++ b/config/testdata/issue-585-defaultConfidence.toml @@ -0,0 +1,4 @@ +ignoreGeneratedHeader = false +severity = "warning" +errorCode = 0 +warningCode = 0 diff --git a/config/testdata/issue-585.toml b/config/testdata/issue-585.toml new file mode 100644 index 0000000..d66cd8b --- /dev/null +++ b/config/testdata/issue-585.toml @@ -0,0 +1,5 @@ +ignoreGeneratedHeader = false +severity = "warning" +confidence = 0.0 +errorCode = 0 +warningCode = 0