Skip to content

Commit c3af594

Browse files
mihaitodormgechev
authored andcommitted
Fix config initialisation
Allow setting confidence to 0
1 parent 099eeac commit c3af594

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

config/config.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"io/ioutil"
7+
"math"
78

89
"github.com/mgechev/revive/formatter"
910

@@ -12,6 +13,10 @@ import (
1213
"github.com/mgechev/revive/rule"
1314
)
1415

16+
const (
17+
defaultConfidence = 0.8
18+
)
19+
1520
var defaultRules = []lint.Rule{
1621
&rule.VarDeclarationsRule{},
1722
&rule.PackageCommentsRule{},
@@ -129,7 +134,9 @@ func GetLintingRules(config *lint.Config) ([]lint.Rule, error) {
129134
}
130135

131136
func parseConfig(path string) (*lint.Config, error) {
132-
config := &lint.Config{}
137+
config := &lint.Config{
138+
Confidence: math.Inf(1),
139+
}
133140
file, err := ioutil.ReadFile(path)
134141
if err != nil {
135142
return nil, errors.New("cannot read the config file")
@@ -142,8 +149,7 @@ func parseConfig(path string) (*lint.Config, error) {
142149
}
143150

144151
func normalizeConfig(config *lint.Config) {
145-
const defaultConfidence = 0.8
146-
if config.Confidence == 0 {
152+
if config.Confidence == math.Inf(1) {
147153
config.Confidence = defaultConfidence
148154
}
149155

@@ -210,7 +216,7 @@ func GetFormatter(formatterName string) (lint.Formatter, error) {
210216

211217
func defaultConfig() *lint.Config {
212218
defaultConfig := lint.Config{
213-
Confidence: 0.0,
219+
Confidence: math.Inf(1),
214220
Severity: lint.SeverityWarning,
215221
Rules: map[string]lint.RuleConfig{},
216222
}

config/config_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import (
1111

1212
func TestGetConfig(t *testing.T) {
1313
tt := map[string]struct {
14-
confPath string
15-
wantConfig *lint.Config
16-
wantError string
14+
confPath string
15+
wantConfig *lint.Config
16+
wantError string
17+
wantConfidence float64
1718
}{
1819
"non-reg issue #470": {
1920
confPath: "testdata/issue-470.toml",
@@ -33,6 +34,15 @@ func TestGetConfig(t *testing.T) {
3334
normalizeConfig(c)
3435
return c
3536
}(),
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,
3646
},
3747
}
3848

@@ -46,8 +56,9 @@ func TestGetConfig(t *testing.T) {
4656
t.Fatalf("Expected error\n\t%q\ngot:\n\t%v", tc.wantError, err)
4757
case tc.wantConfig != nil && !reflect.DeepEqual(cfg, tc.wantConfig):
4858
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)
4961
}
50-
5162
})
5263
}
5364
}
@@ -88,7 +99,6 @@ func TestGetLintingRules(t *testing.T) {
8899
case len(rules) != tc.wantRulesCount:
89100
t.Fatalf("Expected %v enabled linting rules got: %v", tc.wantRulesCount, len(rules))
90101
}
91-
92102
})
93103
}
94104
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ignoreGeneratedHeader = false
2+
severity = "warning"
3+
errorCode = 0
4+
warningCode = 0

config/testdata/issue-585.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ignoreGeneratedHeader = false
2+
severity = "warning"
3+
confidence = 0.0
4+
errorCode = 0
5+
warningCode = 0

0 commit comments

Comments
 (0)