Skip to content

Commit 965f861

Browse files
fix: make enabled optional in .deepsource.toml (#255)
* fix: make enabled optional in .deepsource.toml Signed-off-by: Sourya Vatsyayan <sourya@deepsource.io> * add more tests Signed-off-by: Sourya Vatsyayan <sourya@deepsource.io> * more tests Signed-off-by: Sourya Vatsyayan <sourya@deepsource.io> --------- Signed-off-by: Sourya Vatsyayan <sourya@deepsource.io>
1 parent 2d65beb commit 965f861

File tree

6 files changed

+60
-23
lines changed

6 files changed

+60
-23
lines changed

.deepsource.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@ version = 1
22

33
[[analyzers]]
44
name = "go"
5-
enabled = true
65

76
[analyzers.meta]
87
import_path = "github.com/deepsourcelabs/cli"
98

109
[[analyzers]]
1110
name = "secrets"
12-
enabled = true
1311

1412
[[analyzers]]
15-
name = "test-coverage"
16-
enabled = true
13+
name = "test-coverage"

configvalidator/analyzer_config_validator.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@ func (c *ConfigValidator) validateAnalyzersConfig() {
2424
c.pushError(fmt.Sprintf("Value of `analyzers` should be an array. Found: %v", analyzersType))
2525
}
2626

27-
// Enabled must be boolean. And atleast one analyzer must be enabled among all mentioned in config
27+
// Count enabled analyzers (missing enabled field defaults to true)
2828
countEnabled := 0
2929
for _, analyzer := range c.Config.Analyzers {
30-
enabledType := reflect.TypeOf(analyzer.Enabled).Kind().String()
31-
if enabledType != "bool" {
32-
c.pushError(fmt.Sprintf("The `enabled` property should be of boolean type. Found: %v", enabledType))
33-
}
30+
// If enabled is not set (nil), consider it as enabled (true)
31+
// If enabled is set, use its value
32+
isEnabled := analyzer.Enabled == nil || *analyzer.Enabled
3433

35-
if analyzer.Enabled {
34+
if isEnabled {
3635
countEnabled++
3736
}
3837
}
@@ -47,7 +46,8 @@ func (c *ConfigValidator) validateAnalyzersConfig() {
4746
if analyzer.Name == supportedAnalyzer {
4847
// Copy the meta of activated analyzer for usage in
4948
// analyzer meta validation
50-
if analyzer.Enabled {
49+
isEnabled := analyzer.Enabled == nil || *analyzer.Enabled
50+
if isEnabled {
5151
activatedAnalyzers[analyzer.Name] = analyzer.Meta
5252
}
5353
supported = true

configvalidator/config_validator.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package configvalidator
22

33
import (
44
"bytes"
5+
"fmt"
6+
"strings"
57

68
"github.com/spf13/viper"
79
)
@@ -41,7 +43,20 @@ func (c *ConfigValidator) ValidateConfig(inputConfig []byte) Result {
4143
return c.Result
4244
}
4345
// Unmarshaling the configdata into DSConfig struct
44-
viper.UnmarshalExact(&config)
46+
err = viper.UnmarshalExact(&config)
47+
if err != nil {
48+
// Check if the error is due to invalid enabled field types
49+
// match `` * cannot parse 'analyzers[0].enabled' as bool: strconv.ParseBool: parsing "falsee": invalid syntax`
50+
if strings.Contains(err.Error(), "strconv.ParseBool") {
51+
c.Result.Valid = false
52+
c.Result.Errors = append(c.Result.Errors, "The `enabled` property should be of boolean type (true/false)")
53+
return c.Result
54+
}
55+
// Other unmarshaling errors
56+
c.Result.Valid = false
57+
c.Result.Errors = append(c.Result.Errors, fmt.Sprintf("Error while parsing config: %v", err))
58+
return c.Result
59+
}
4560
c.Config = config
4661

4762
// Validate generic config which applies to all analyzers and transformers

configvalidator/config_validator_test.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package configvalidator
22

33
import (
4-
"reflect"
54
"testing"
65

76
"github.com/deepsourcelabs/cli/utils"
@@ -107,12 +106,43 @@ func TestValidateConfig(t *testing.T) {
107106
enabled = true`,
108107
valid: false,
109108
},
109+
"valid config with enabled not set (defaults to true)": {
110+
inputConfig: `
111+
version = 1
112+
113+
[[analyzers]]
114+
name = "python"
115+
116+
[[transformers]]
117+
name = "black"`,
118+
valid: true,
119+
},
120+
121+
"invalid config with enabled = \"falsee\" (non-boolean)": {
122+
inputConfig: `
123+
version = 1
124+
125+
[[analyzers]]
126+
name = "python"
127+
enabled = "falsee"`,
128+
valid: false,
129+
},
130+
"config with syntax error": {
131+
inputConfig: `
132+
version = 1
133+
134+
[[analyzers]
135+
name = "python"
136+
enabled = false`,
137+
valid: false,
138+
},
110139
}
140+
111141
for testName, tc := range tests {
112142
t.Run(testName, func(t *testing.T) {
113143
c := &ConfigValidator{}
114144
c.ValidateConfig([]byte(tc.inputConfig))
115-
if !reflect.DeepEqual(tc.valid, c.Result.Valid) {
145+
if tc.valid != c.Result.Valid {
116146
t.Errorf("%s: expected: %v, got: %v. Error: %v", testName, tc.valid, c.Result.Valid, c.Result.Errors)
117147
}
118148
})

configvalidator/transformer_config_validator.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,8 @@ func (c *ConfigValidator) validateTransformersConfig() {
2121
c.pushError(fmt.Sprintf("Value of `transformers` should be an array. Found: %v", transformersType))
2222
}
2323

24-
// Enabled property should be of boolean type
25-
for _, transformer := range c.Config.Transformers {
26-
enabledType := reflect.TypeOf(transformer.Enabled).Kind().String()
27-
if enabledType != "bool" {
28-
c.pushError(fmt.Sprintf("The `enabled` property should be of boolean type. Found: %v", enabledType))
29-
}
30-
}
24+
// Enabled property validation is handled in the main config validator
25+
// (transformers with invalid enabled types will cause unmarshaling errors)
3126

3227
// ==== Transformer shortcode validation ====
3328
supported := false

configvalidator/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ package configvalidator
44
type Analyzer struct {
55
Name string `mapstructure:"name,omitempty" json:"name,omitempty"`
66
RuntimeVersion string `mapstructure:"runtime_version,omitempty" json:"runtime_version,omitempty"`
7-
Enabled bool `mapstructure:"enabled,omitempty" json:"enabled"`
7+
Enabled *bool `mapstructure:"enabled,omitempty" json:"enabled,omitempty"`
88
DependencyFilePaths []string `mapstructure:"dependency_file_paths,omitempty" json:"dependency_file_paths,omitempty"`
99
Meta interface{} `mapstructure:"meta,omitempty" json:"meta,omitempty"`
1010
Thresholds interface{} `mapstructure:"thresholds,omitempty" json:"thresholds,omitempty"`
1111
}
1212

1313
type Transformer struct {
1414
Name string `mapstructure:"name,omitempty" json:"name,omitempty"`
15-
Enabled bool `mapstructure:"enabled,omitempty" json:"enabled,omitempty"`
15+
Enabled *bool `mapstructure:"enabled,omitempty" json:"enabled,omitempty"`
1616
}
1717

1818
type DSConfig struct {

0 commit comments

Comments
 (0)