Skip to content

Commit 25befa2

Browse files
committedOct 13, 2023
Cover error conditions in tests
PR #720
1 parent e8ebf52 commit 25befa2

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed
 

‎pkg/config/config.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,11 @@ func (c *Config) getInterfacesSection(ctx context.Context, packageName string) (
320320
if !exists {
321321
return make(map[string]any), nil
322322
}
323-
return interfaceSection.(map[string]any), nil
323+
mapConfig, ok := interfaceSection.(map[string]any)
324+
if !ok {
325+
return nil, fmt.Errorf("interfaces section has type %T, expected map[string]any", interfaceSection)
326+
}
327+
return mapConfig, nil
324328
}
325329

326330
func (c *Config) GetInterfaceConfig(ctx context.Context, packageName string, interfaceName string) ([]*Config, error) {

‎pkg/config/config_test.go

+80
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,17 @@ func TestConfig_ShouldGenerateInterface(t *testing.T) {
572572
want: false,
573573
wantErr: true,
574574
},
575+
{
576+
name: "invalid interfaces section returns error",
577+
c: &Config{
578+
Packages: map[string]interface{}{
579+
"some_package": map[string]interface{}{
580+
"interfaces": true,
581+
},
582+
},
583+
},
584+
wantErr: true,
585+
},
575586
{
576587
name: "should generate all interfaces",
577588
c: &Config{
@@ -733,6 +744,75 @@ func TestConfig_ShouldGenerateInterface(t *testing.T) {
733744
},
734745
want: true,
735746
},
747+
{
748+
name: "invalid include-regex is ignored if all is set",
749+
c: &Config{
750+
Packages: map[string]interface{}{
751+
"some_package": map[string]interface{}{
752+
"config": map[string]interface{}{
753+
"all": true,
754+
"include-regex": "[",
755+
},
756+
},
757+
},
758+
},
759+
want: true,
760+
},
761+
{
762+
name: "invalid include-regex results in error",
763+
c: &Config{
764+
Packages: map[string]interface{}{
765+
"some_package": map[string]interface{}{
766+
"config": map[string]interface{}{
767+
"include-regex": "[",
768+
},
769+
},
770+
},
771+
},
772+
wantErr: true,
773+
},
774+
{
775+
name: "invalid exclude-regex is ignored if all is set",
776+
c: &Config{
777+
Packages: map[string]interface{}{
778+
"some_package": map[string]interface{}{
779+
"config": map[string]interface{}{
780+
"all": true,
781+
"include-regex": ".*",
782+
"exclude-regex": "[",
783+
},
784+
},
785+
},
786+
},
787+
want: true,
788+
},
789+
{
790+
name: "invalid exclude-regex is ignored if include-regex is not set",
791+
c: &Config{
792+
Packages: map[string]interface{}{
793+
"some_package": map[string]interface{}{
794+
"config": map[string]interface{}{
795+
"exclude-regex": "[",
796+
},
797+
},
798+
},
799+
},
800+
want: false,
801+
},
802+
{
803+
name: "invalid exclude-regex results in error",
804+
c: &Config{
805+
Packages: map[string]interface{}{
806+
"some_package": map[string]interface{}{
807+
"config": map[string]interface{}{
808+
"include-regex": ".*",
809+
"exclude-regex": "[",
810+
},
811+
},
812+
},
813+
},
814+
wantErr: true,
815+
},
736816
}
737817
for _, tt := range tests {
738818
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)