Skip to content

Commit

Permalink
Cover error conditions in tests
Browse files Browse the repository at this point in the history
PR #720
  • Loading branch information
kbolino committed Oct 13, 2023
1 parent e8ebf52 commit 25befa2
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,11 @@ func (c *Config) getInterfacesSection(ctx context.Context, packageName string) (
if !exists {
return make(map[string]any), nil
}
return interfaceSection.(map[string]any), nil
mapConfig, ok := interfaceSection.(map[string]any)
if !ok {
return nil, fmt.Errorf("interfaces section has type %T, expected map[string]any", interfaceSection)
}
return mapConfig, nil
}

func (c *Config) GetInterfaceConfig(ctx context.Context, packageName string, interfaceName string) ([]*Config, error) {
Expand Down
80 changes: 80 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,17 @@ func TestConfig_ShouldGenerateInterface(t *testing.T) {
want: false,
wantErr: true,
},
{
name: "invalid interfaces section returns error",
c: &Config{
Packages: map[string]interface{}{
"some_package": map[string]interface{}{
"interfaces": true,
},
},
},
wantErr: true,
},
{
name: "should generate all interfaces",
c: &Config{
Expand Down Expand Up @@ -733,6 +744,75 @@ func TestConfig_ShouldGenerateInterface(t *testing.T) {
},
want: true,
},
{
name: "invalid include-regex is ignored if all is set",
c: &Config{
Packages: map[string]interface{}{
"some_package": map[string]interface{}{
"config": map[string]interface{}{
"all": true,
"include-regex": "[",
},
},
},
},
want: true,
},
{
name: "invalid include-regex results in error",
c: &Config{
Packages: map[string]interface{}{
"some_package": map[string]interface{}{
"config": map[string]interface{}{
"include-regex": "[",
},
},
},
},
wantErr: true,
},
{
name: "invalid exclude-regex is ignored if all is set",
c: &Config{
Packages: map[string]interface{}{
"some_package": map[string]interface{}{
"config": map[string]interface{}{
"all": true,
"include-regex": ".*",
"exclude-regex": "[",
},
},
},
},
want: true,
},
{
name: "invalid exclude-regex is ignored if include-regex is not set",
c: &Config{
Packages: map[string]interface{}{
"some_package": map[string]interface{}{
"config": map[string]interface{}{
"exclude-regex": "[",
},
},
},
},
want: false,
},
{
name: "invalid exclude-regex results in error",
c: &Config{
Packages: map[string]interface{}{
"some_package": map[string]interface{}{
"config": map[string]interface{}{
"include-regex": ".*",
"exclude-regex": "[",
},
},
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 25befa2

Please sign in to comment.