Skip to content

Commit

Permalink
Fix config bug where mockery crashes when package map is nil
Browse files Browse the repository at this point in the history
Fixes issue #726.

We needed an additional bit of logic to ensure that if the `config` section
is nil that we default it to being an empty map.
  • Loading branch information
LandonTClipp committed Nov 5, 2023
1 parent 726d76c commit 77064ad
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
21 changes: 14 additions & 7 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ func (c *Config) getPackageConfigMap(ctx context.Context, packageName string) (m
if isMap {
return configAsMap, nil
}

return map[string]any{}, nil

}
Expand Down Expand Up @@ -430,21 +431,21 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP
Str("parent-package", parentPkgPath).
Str("sub-package", subPkgPath).Logger()

log.Trace().Msg("adding sub-package to config map")
log.Debug().Msg("adding sub-package to config map")
parentPkgConfig, err := c.getPackageConfigMap(ctx, parentPkgPath)
if err != nil {
log.Err(err).
Msg("failed to get package config for parent package")
return fmt.Errorf("failed to get package config: %w", err)
}

log.Trace().Msg("getting config")
log.Debug().Msg("getting config")
cfg, err := c.CfgAsMap(ctx)
if err != nil {
return fmt.Errorf("failed to get configuration map: %w", err)
}

log.Trace().Msg("getting packages section")
log.Debug().Msg("getting packages section")
packagesSection := cfg["packages"].(map[string]any)

// Don't overwrite any config that already exists
Expand Down Expand Up @@ -688,15 +689,21 @@ func (c *Config) mergeInConfig(ctx context.Context) error {
packageConfig["config"] = defaultCfg
continue
}
packageConfigSection := configSectionUntyped.(map[string]any)
// Sometimes the config section may be provided, but it's nil.
// We need to account for this fact.
if configSectionUntyped == nil {
configSectionUntyped = map[string]any{}
}

configSectionTyped := configSectionUntyped.(map[string]any)

for key, value := range defaultCfg {
if contains([]string{"packages", "config"}, key) {
continue
}
_, keyExists := packageConfigSection[key]
_, keyExists := configSectionTyped[key]
if !keyExists {
packageConfigSection[key] = value
configSectionTyped[key] = value
}
}
interfaces, err := c.getInterfacesForPackage(ctx, pkgPath)
Expand Down Expand Up @@ -728,7 +735,7 @@ func (c *Config) mergeInConfig(ctx context.Context) error {
// Assume this interface's value in the map is nil. Just skip it.
continue
}
for key, value := range packageConfigSection {
for key, value := range configSectionTyped {
if key == "packages" {
continue
}
Expand Down
27 changes: 27 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,33 @@ packages:
recursive: true
with-expecter: true
with-expecter: false
`,
},
{
name: "empty map for recursive package",
cfgYaml: `
with-expecter: False
dir: foobar
recursive: True
all: True
packages:
github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs:
`,
wantCfgMap: `dir: foobar
packages:
github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs/subpkg2:
config:
all: true
dir: foobar
recursive: true
with-expecter: true
github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs/subpkg2/subpkg3:
config:
all: true
dir: foobar
recursive: true
with-expecter: true
with-expecter: false
`,
},
}
Expand Down

0 comments on commit 77064ad

Please sign in to comment.