2020-10-29 14:11:52 +01:00
package config
import (
"reflect"
"strings"
"testing"
"github.com/mgechev/revive/lint"
2021-09-30 22:37:44 +02:00
"github.com/mgechev/revive/rule"
2020-10-29 14:11:52 +01:00
)
func TestGetConfig ( t * testing . T ) {
tt := map [ string ] struct {
2021-10-02 16:30:52 +01:00
confPath string
wantConfig * lint . Config
wantError string
wantConfidence float64
2020-10-29 14:11:52 +01:00
} {
"non-reg issue #470" : {
confPath : "testdata/issue-470.toml" ,
wantError : "" ,
} ,
"unknown file" : {
confPath : "unknown" ,
wantError : "cannot read the config file" ,
} ,
"malformed file" : {
confPath : "testdata/malformed.toml" ,
wantError : "cannot parse the config file" ,
} ,
"default config" : {
2021-10-02 16:30:26 +01:00
wantConfig : func ( ) * lint . Config {
c := defaultConfig ( )
normalizeConfig ( c )
return c
} ( ) ,
2021-10-02 16:30:52 +01:00
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 ,
2020-10-29 14:11:52 +01:00
} ,
}
for name , tc := range tt {
t . Run ( name , func ( t * testing . T ) {
cfg , err := GetConfig ( tc . confPath )
switch {
case err != nil && tc . wantError == "" :
t . Fatalf ( "Unexpected error\n\t%v" , err )
case err != nil && ! strings . Contains ( err . Error ( ) , tc . wantError ) :
t . Fatalf ( "Expected error\n\t%q\ngot:\n\t%v" , tc . wantError , err )
2021-10-02 16:30:26 +01:00
case tc . wantConfig != nil && ! reflect . DeepEqual ( cfg , tc . wantConfig ) :
2020-10-29 14:11:52 +01:00
t . Fatalf ( "Expected config\n\t%+v\ngot:\n\t%+v" , tc . wantConfig , cfg )
2021-10-02 16:30:52 +01:00
case tc . wantConfig != nil && tc . wantConfidence != cfg . Confidence :
t . Fatalf ( "Expected confidence\n\t%+v\ngot:\n\t%+v" , tc . wantConfidence , cfg . Confidence )
2020-10-29 14:11:52 +01:00
}
} )
}
2023-08-12 11:21:11 +05:00
t . Run ( "rule-level file filter excludes" , func ( t * testing . T ) {
cfg , err := GetConfig ( "testdata/rule-level-exclude-850.toml" )
if err != nil {
t . Fatal ( "should be valid config" )
}
r1 := cfg . Rules [ "r1" ]
if len ( r1 . Exclude ) > 0 {
t . Fatal ( "r1 should have empty excludes" )
}
r2 := cfg . Rules [ "r2" ]
if len ( r2 . Exclude ) != 1 {
t . Fatal ( "r2 should have exclude set" )
}
if ! r2 . MustExclude ( "some/file.go" ) {
t . Fatal ( "r2 should be initialized and exclude some/file.go" )
}
if r2 . MustExclude ( "some/any-other.go" ) {
t . Fatal ( "r2 should not exclude some/any-other.go" )
}
} )
2020-10-29 14:11:52 +01:00
}
2021-09-30 22:37:44 +02:00
func TestGetLintingRules ( t * testing . T ) {
tt := map [ string ] struct {
confPath string
wantRulesCount int
2024-12-23 16:10:21 +02:00
wantErr string
2021-09-30 22:37:44 +02:00
} {
"no rules" : {
confPath : "testdata/noRules.toml" ,
wantRulesCount : 0 ,
} ,
"enableAllRules without disabled rules" : {
confPath : "testdata/enableAll.toml" ,
wantRulesCount : len ( allRules ) ,
} ,
"enableAllRules with 2 disabled rules" : {
confPath : "testdata/enableAllBut2.toml" ,
wantRulesCount : len ( allRules ) - 2 ,
} ,
"enable 2 rules" : {
confPath : "testdata/enable2.toml" ,
wantRulesCount : 2 ,
} ,
2024-12-23 16:10:21 +02:00
"var-naming configure error" : {
confPath : "testdata/varNamingConfigureError.toml" ,
wantErr : ` cannot configure rule: "var-naming": invalid argument to the var-naming rule. Expecting a allowlist of type slice with initialisms, got string ` ,
} ,
2021-09-30 22:37:44 +02:00
}
for name , tc := range tt {
t . Run ( name , func ( t * testing . T ) {
cfg , err := GetConfig ( tc . confPath )
if err != nil {
t . Fatalf ( "Unexpected error while loading conf: %v" , err )
}
2022-03-20 05:12:51 -03:00
rules , err := GetLintingRules ( cfg , [ ] lint . Rule { } )
2024-12-23 16:10:21 +02:00
if tc . wantErr != "" {
if err == nil || err . Error ( ) != tc . wantErr {
t . Fatalf ( "Expected error %q, got %q" , tc . wantErr , err )
}
return
}
2021-09-30 22:37:44 +02:00
switch {
case err != nil :
t . Fatalf ( "Unexpected error\n\t%v" , err )
case len ( rules ) != tc . wantRulesCount :
t . Fatalf ( "Expected %v enabled linting rules got: %v" , tc . wantRulesCount , len ( rules ) )
}
} )
}
}
func TestGetGlobalSeverity ( t * testing . T ) {
tt := map [ string ] struct {
confPath string
wantGlobalSeverity string
particularRule lint . Rule
wantParticularSeverity string
} {
"enable 2 rules with one specific severity" : {
confPath : "testdata/enable2OneSpecificSeverity.toml" ,
wantGlobalSeverity : "warning" ,
particularRule : & rule . CyclomaticRule { } ,
wantParticularSeverity : "error" ,
} ,
"enableAllRules with one specific severity" : {
confPath : "testdata/enableAllOneSpecificSeverity.toml" ,
wantGlobalSeverity : "error" ,
particularRule : & rule . DeepExitRule { } ,
wantParticularSeverity : "warning" ,
} ,
}
for name , tc := range tt {
t . Run ( name , func ( t * testing . T ) {
cfg , err := GetConfig ( tc . confPath )
if err != nil {
t . Fatalf ( "Unexpected error while loading conf: %v" , err )
}
2022-03-20 05:12:51 -03:00
rules , err := GetLintingRules ( cfg , [ ] lint . Rule { } )
2021-09-30 22:37:44 +02:00
if err != nil {
t . Fatalf ( "Unexpected error while loading conf: %v" , err )
}
for _ , r := range rules {
ruleName := r . Name ( )
ruleCfg := cfg . Rules [ ruleName ]
ruleSeverity := string ( ruleCfg . Severity )
switch ruleName {
case tc . particularRule . Name ( ) :
if tc . wantParticularSeverity != ruleSeverity {
t . Fatalf ( "Expected Severity %v for rule %v, got %v" , tc . wantParticularSeverity , ruleName , ruleSeverity )
}
default :
if tc . wantGlobalSeverity != ruleSeverity {
t . Fatalf ( "Expected Severity %v for rule %v, got %v" , tc . wantGlobalSeverity , ruleName , ruleSeverity )
}
}
}
} )
}
}
2024-12-13 14:57:50 +02:00
func TestGetFormatter ( t * testing . T ) {
t . Run ( "default formatter" , func ( t * testing . T ) {
formatter , err := GetFormatter ( "" )
if err != nil {
t . Fatalf ( "Unexpected error %q" , err )
}
if formatter == nil || formatter . Name ( ) != "default" {
t . Errorf ( "Expected formatter %q, got %v" , "default" , formatter )
}
} )
t . Run ( "unknown formatter" , func ( t * testing . T ) {
_ , err := GetFormatter ( "unknown" )
if err == nil || err . Error ( ) != "unknown formatter unknown" {
t . Errorf ( "Expected error %q, got: %q" , "unknown formatter unknown" , err )
}
} )
t . Run ( "checkstyle formatter" , func ( t * testing . T ) {
formatter , err := GetFormatter ( "checkstyle" )
if err != nil {
t . Fatalf ( "Unexpected error: %q" , err )
}
if formatter == nil || formatter . Name ( ) != "checkstyle" {
t . Errorf ( "Expected formatter %q, got %v" , "checkstyle" , formatter )
}
} )
}