@@ -17,8 +17,8 @@ func TestLoadConfig_NonExistent(t *testing.T) {
1717 if cfg .Linters == nil {
1818 t .Error ("cfg.Linters is nil, want non-nil" )
1919 }
20- if cfg .Linters .Default != "all" {
21- t .Errorf ("cfg.Linters.Default = %q, want %q" , cfg .Linters .Default , "all" )
20+ if cfg .Linters .Default != defaultLinterDefault {
21+ t .Errorf ("cfg.Linters.Default = %q, want %q" , cfg .Linters .Default , defaultLinterDefault )
2222 }
2323 if cfg .Upgrade == nil {
2424 t .Error ("cfg.Upgrade is nil, want non-nil" )
@@ -39,7 +39,7 @@ linters:
3939 enable:
4040 - permissions
4141 disable:
42- - security
42+ - secrets
4343 settings:
4444 format:
4545 indent-width: 4
@@ -65,8 +65,8 @@ upgrade:
6565 if len (cfg .Linters .Enable ) != 1 || cfg .Linters .Enable [0 ] != "permissions" {
6666 t .Errorf ("cfg.Linters.Enable = %v, want [permissions]" , cfg .Linters .Enable )
6767 }
68- if len (cfg .Linters .Disable ) != 1 || cfg .Linters .Disable [0 ] != "security " {
69- t .Errorf ("cfg.Linters.Disable = %v, want [security ]" , cfg .Linters .Disable )
68+ if len (cfg .Linters .Disable ) != 1 || cfg .Linters .Disable [0 ] != "secrets " {
69+ t .Errorf ("cfg.Linters.Disable = %v, want [secrets ]" , cfg .Linters .Disable )
7070 }
7171
7272 // Check upgrade config
@@ -630,8 +630,8 @@ func TestShouldUpdate(t *testing.T) {
630630func TestFullDefaultLinterConfig (t * testing.T ) {
631631 cfg := FullDefaultLinterConfig ()
632632
633- if cfg .Default != "all" {
634- t .Errorf ("Default = %q, want %q" , cfg .Default , "all" )
633+ if cfg .Default != defaultLinterDefault {
634+ t .Errorf ("Default = %q, want %q" , cfg .Default , defaultLinterDefault )
635635 }
636636
637637 // Should have all linters enabled
@@ -693,3 +693,129 @@ func TestUpgradeConfig_EnsureDefaults(t *testing.T) {
693693 })
694694 }
695695}
696+
697+ func TestConfig_Validate (t * testing.T ) {
698+ tests := []struct {
699+ name string
700+ config * Config
701+ wantErr bool
702+ }{
703+ {
704+ name : "valid empty config" ,
705+ config : & Config {},
706+ wantErr : false ,
707+ },
708+ {
709+ name : "valid full config" ,
710+ config : & Config {
711+ Run : & RunConfig {Timeout : "5m" , IssuesExitCode : 2 },
712+ Linters : & LinterConfig {Default : "all" , Enable : []string {"versions" }},
713+ Upgrade : & UpgradeConfig {Version : "tag" },
714+ },
715+ wantErr : false ,
716+ },
717+ {
718+ name : "invalid timeout" ,
719+ config : & Config {Run : & RunConfig {Timeout : "invalid" }},
720+ wantErr : true ,
721+ },
722+ {
723+ name : "invalid exit code too low" ,
724+ config : & Config {Run : & RunConfig {IssuesExitCode : - 1 }},
725+ wantErr : true ,
726+ },
727+ {
728+ name : "invalid exit code too high" ,
729+ config : & Config {Run : & RunConfig {IssuesExitCode : 300 }},
730+ wantErr : true ,
731+ },
732+ {
733+ name : "invalid linter default" ,
734+ config : & Config {Linters : & LinterConfig {Default : "invalid" }},
735+ wantErr : true ,
736+ },
737+ {
738+ name : "unknown linter in enable" ,
739+ config : & Config {Linters : & LinterConfig {Enable : []string {"unknown" }}},
740+ wantErr : true ,
741+ },
742+ {
743+ name : "unknown linter in disable" ,
744+ config : & Config {Linters : & LinterConfig {Disable : []string {"unknown" }}},
745+ wantErr : true ,
746+ },
747+ {
748+ name : "invalid upgrade version format" ,
749+ config : & Config {Upgrade : & UpgradeConfig {Version : "invalid" }},
750+ wantErr : true ,
751+ },
752+ {
753+ name : "invalid format indent-width" ,
754+ config : & Config {Linters : & LinterConfig {
755+ Settings : & LinterSettings {Format : & FormatSettings {IndentWidth : - 1 }},
756+ }},
757+ wantErr : true ,
758+ },
759+ {
760+ name : "invalid format max-line-length" ,
761+ config : & Config {Linters : & LinterConfig {
762+ Settings : & LinterSettings {Format : & FormatSettings {MaxLineLength : - 1 }},
763+ }},
764+ wantErr : true ,
765+ },
766+ {
767+ name : "invalid style min-name-length negative" ,
768+ config : & Config {Linters : & LinterConfig {
769+ Settings : & LinterSettings {Style : & StyleSettings {MinNameLength : - 1 }},
770+ }},
771+ wantErr : true ,
772+ },
773+ {
774+ name : "invalid style max-name-length negative" ,
775+ config : & Config {Linters : & LinterConfig {
776+ Settings : & LinterSettings {Style : & StyleSettings {MaxNameLength : - 1 }},
777+ }},
778+ wantErr : true ,
779+ },
780+ {
781+ name : "invalid style max-run-lines negative" ,
782+ config : & Config {Linters : & LinterConfig {
783+ Settings : & LinterSettings {Style : & StyleSettings {MaxRunLines : - 1 }},
784+ }},
785+ wantErr : true ,
786+ },
787+ {
788+ name : "invalid style min > max name length" ,
789+ config : & Config {Linters : & LinterConfig {
790+ Settings : & LinterSettings {Style : & StyleSettings {MinNameLength : 10 , MaxNameLength : 5 }},
791+ }},
792+ wantErr : true ,
793+ },
794+ {
795+ name : "invalid naming convention" ,
796+ config : & Config {Linters : & LinterConfig {
797+ Settings : & LinterSettings {Style : & StyleSettings {NamingConvention : "invalid" }},
798+ }},
799+ wantErr : true ,
800+ },
801+ {
802+ name : "valid settings" ,
803+ config : & Config {Linters : & LinterConfig {
804+ Settings : & LinterSettings {
805+ Format : & FormatSettings {IndentWidth : 4 , MaxLineLength : 100 },
806+ Style : & StyleSettings {MinNameLength : 3 , MaxNameLength : 50 , NamingConvention : "title" },
807+ },
808+ }},
809+ wantErr : false ,
810+ },
811+ }
812+
813+ for _ , tt := range tests {
814+ t .Run (tt .name , func (t * testing.T ) {
815+ err := tt .config .Validate ()
816+ if (err != nil ) != tt .wantErr {
817+ t .Errorf ("Validate() error = %v, wantErr %v" , err , tt .wantErr )
818+ }
819+ })
820+ }
821+ }
0 commit comments