Skip to content

Commit 77064ad

Browse files
committedNov 5, 2023
Fix config bug where mockery crashes when package map is nil
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.
1 parent 726d76c commit 77064ad

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed
 

‎pkg/config/config.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ func (c *Config) getPackageConfigMap(ctx context.Context, packageName string) (m
209209
if isMap {
210210
return configAsMap, nil
211211
}
212+
212213
return map[string]any{}, nil
213214

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

433-
log.Trace().Msg("adding sub-package to config map")
434+
log.Debug().Msg("adding sub-package to config map")
434435
parentPkgConfig, err := c.getPackageConfigMap(ctx, parentPkgPath)
435436
if err != nil {
436437
log.Err(err).
437438
Msg("failed to get package config for parent package")
438439
return fmt.Errorf("failed to get package config: %w", err)
439440
}
440441

441-
log.Trace().Msg("getting config")
442+
log.Debug().Msg("getting config")
442443
cfg, err := c.CfgAsMap(ctx)
443444
if err != nil {
444445
return fmt.Errorf("failed to get configuration map: %w", err)
445446
}
446447

447-
log.Trace().Msg("getting packages section")
448+
log.Debug().Msg("getting packages section")
448449
packagesSection := cfg["packages"].(map[string]any)
449450

450451
// Don't overwrite any config that already exists
@@ -688,15 +689,21 @@ func (c *Config) mergeInConfig(ctx context.Context) error {
688689
packageConfig["config"] = defaultCfg
689690
continue
690691
}
691-
packageConfigSection := configSectionUntyped.(map[string]any)
692+
// Sometimes the config section may be provided, but it's nil.
693+
// We need to account for this fact.
694+
if configSectionUntyped == nil {
695+
configSectionUntyped = map[string]any{}
696+
}
697+
698+
configSectionTyped := configSectionUntyped.(map[string]any)
692699

693700
for key, value := range defaultCfg {
694701
if contains([]string{"packages", "config"}, key) {
695702
continue
696703
}
697-
_, keyExists := packageConfigSection[key]
704+
_, keyExists := configSectionTyped[key]
698705
if !keyExists {
699-
packageConfigSection[key] = value
706+
configSectionTyped[key] = value
700707
}
701708
}
702709
interfaces, err := c.getInterfacesForPackage(ctx, pkgPath)
@@ -728,7 +735,7 @@ func (c *Config) mergeInConfig(ctx context.Context) error {
728735
// Assume this interface's value in the map is nil. Just skip it.
729736
continue
730737
}
731-
for key, value := range packageConfigSection {
738+
for key, value := range configSectionTyped {
732739
if key == "packages" {
733740
continue
734741
}

‎pkg/config/config_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,33 @@ packages:
11721172
recursive: true
11731173
with-expecter: true
11741174
with-expecter: false
1175+
`,
1176+
},
1177+
{
1178+
name: "empty map for recursive package",
1179+
cfgYaml: `
1180+
with-expecter: False
1181+
dir: foobar
1182+
recursive: True
1183+
all: True
1184+
packages:
1185+
github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs:
1186+
`,
1187+
wantCfgMap: `dir: foobar
1188+
packages:
1189+
github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs/subpkg2:
1190+
config:
1191+
all: true
1192+
dir: foobar
1193+
recursive: true
1194+
with-expecter: true
1195+
github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs/subpkg2/subpkg3:
1196+
config:
1197+
all: true
1198+
dir: foobar
1199+
recursive: true
1200+
with-expecter: true
1201+
with-expecter: false
11751202
`,
11761203
},
11771204
}

0 commit comments

Comments
 (0)
Please sign in to comment.