Skip to content

Commit 5978bc5

Browse files
committedNov 6, 2023
Fix test with config initialization
The test wasn't properly initializing the viper object so the config wasn't getting decoded into the struct properly. Also adding more comments to various places to better explain what the code was doing, as even I, the person who wrote it, was confused as to what I was doing. How can I expect other people to understand it if I can't?!
1 parent 0310201 commit 5978bc5

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed
 

‎pkg/config/config.go

+21-7
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func (c *Config) getPackageConfigMap(ctx context.Context, packageName string) (m
218218

219219
}
220220
func (c *Config) GetPackageConfig(ctx context.Context, packageName string) (*Config, error) {
221-
log := zerolog.Ctx(ctx)
221+
log := zerolog.Ctx(ctx).With().Str("package-path", packageName).Logger()
222222

223223
if c.pkgConfigCache == nil {
224224
log.Debug().Msg("package cache is nil")
@@ -228,11 +228,13 @@ func (c *Config) GetPackageConfig(ctx context.Context, packageName string) (*Con
228228
return pkgConf, nil
229229
}
230230

231-
pkgConfig := reflect.New(reflect.ValueOf(c).Elem().Type()).Interface()
231+
//pkgConfig := reflect.New(reflect.ValueOf(c).Elem().Type()).Interface()
232+
pkgConfig := &Config{}
232233
if err := copier.Copy(pkgConfig, c); err != nil {
233234
return nil, fmt.Errorf("failed to copy config: %w", err)
234235
}
235-
pkgConfigTyped := pkgConfig.(*Config)
236+
//pkgConfigTyped := pkgConfig.(*Config)
237+
pkgConfigTyped := pkgConfig
236238

237239
configMap, err := c.getPackageConfigMap(ctx, packageName)
238240
if err != nil {
@@ -242,6 +244,7 @@ func (c *Config) GetPackageConfig(ctx context.Context, packageName string) (*Con
242244
configSection, ok := configMap["config"]
243245
if !ok {
244246
log.Debug().Msg("config section not provided for package")
247+
configMap["config"] = deepCopyConfigMap(c._cfgAsMap)
245248
return pkgConfigTyped, nil
246249
}
247250

@@ -444,13 +447,13 @@ func (c *Config) addSubPkgConfig(ctx context.Context, subPkgPath string, parentP
444447
}
445448

446449
log.Debug().Msg("getting config")
447-
cfg, err := c.CfgAsMap(ctx)
450+
topLevelConfig, err := c.CfgAsMap(ctx)
448451
if err != nil {
449452
return fmt.Errorf("failed to get configuration map: %w", err)
450453
}
451454

452455
log.Debug().Msg("getting packages section")
453-
packagesSection := cfg["packages"].(map[string]any)
456+
packagesSection := topLevelConfig["packages"].(map[string]any)
454457

455458
// Don't overwrite any config that already exists
456459
_, pkgExists := packagesSection[subPkgPath]
@@ -600,18 +603,25 @@ func (c *Config) subPackages(
600603
// recursive and recurses the file tree to find all sub-packages.
601604
func (c *Config) discoverRecursivePackages(ctx context.Context) error {
602605
log := zerolog.Ctx(ctx)
606+
log.Trace().Msg("discovering recursive packages")
603607
recursivePackages := map[string]*Config{}
604608
packageList, err := c.GetPackages(ctx)
605609
if err != nil {
606610
return fmt.Errorf("failed to get packages: %w", err)
607611
}
608612
for _, pkg := range packageList {
609613
pkgConfig, err := c.GetPackageConfig(ctx, pkg)
614+
pkgLog := log.With().Str("package", pkg).Logger()
615+
pkgLog.Trace().Msg("iterating over package")
610616
if err != nil {
611617
return fmt.Errorf("failed to get package config: %w", err)
612618
}
613619
if pkgConfig.Recursive {
620+
pkgLog.Trace().Msg("package marked as recursive")
614621
recursivePackages[pkg] = pkgConfig
622+
} else {
623+
pkgLog.Trace().Msg("package not marked as recursive")
624+
pkgLog.Trace().Msg(fmt.Sprintf("%+v", pkgConfig))
615625
}
616626
}
617627
if len(recursivePackages) == 0 {
@@ -703,8 +713,12 @@ func (c *Config) mergeInConfig(ctx context.Context) error {
703713

704714
configSectionUntyped, configExists := packageConfig["config"]
705715
if !configExists {
706-
pkgLog.Trace().Msg("config section doesn't exist, setting it to a deepcopy of the top-level config")
707-
packageConfig["config"] = deepCopyConfigMap(defaultCfg)
716+
// The reason why this should never happen is because getPackageConfigMap
717+
// should be populating the config section with the top-level config if it
718+
// wasn't defined in the yaml.
719+
msg := "config section does not exist for package, this should never happen"
720+
pkgLog.Error().Msg(msg)
721+
return fmt.Errorf(msg)
708722
}
709723

710724
pkgLog.Trace().Msg("got config section for package")

‎pkg/config/config_test.go

+14-9
Original file line numberDiff line numberDiff line change
@@ -1182,22 +1182,24 @@ dir: foobar
11821182
recursive: True
11831183
all: True
11841184
packages:
1185-
github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs:
1185+
github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs/subpkg2:
11861186
`,
1187-
wantCfgMap: `dir: foobar
1187+
wantCfgMap: `all: true
1188+
dir: foobar
11881189
packages:
11891190
github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs/subpkg2:
11901191
config:
11911192
all: true
11921193
dir: foobar
11931194
recursive: true
1194-
with-expecter: true
1195+
with-expecter: false
11951196
github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs/subpkg2/subpkg3:
11961197
config:
11971198
all: true
11981199
dir: foobar
11991200
recursive: true
1200-
with-expecter: true
1201+
with-expecter: false
1202+
recursive: true
12011203
with-expecter: false
12021204
`,
12031205
},
@@ -1209,19 +1211,22 @@ with-expecter: false
12091211
cfg := tmpdir.Join("config.yaml")
12101212
require.NoError(t, cfg.WriteFile([]byte(tt.cfgYaml)))
12111213

1212-
c := &Config{
1213-
Config: cfg.String(),
1214-
}
1214+
viperObj := viper.New()
1215+
viperObj.SetConfigFile(cfg.String())
1216+
require.NoError(t, viperObj.ReadInConfig())
1217+
c, err := NewConfigFromViper(viperObj)
1218+
require.NoError(t, err)
1219+
12151220
log, err := logging.GetLogger("TRACE")
12161221
require.NoError(t, err)
12171222

1218-
if err := c.Initialize(log.WithContext(context.Background())); !errors.Is(err, tt.wantErr) {
1223+
if err := c.Initialize(log.WithContext(ctx)); !errors.Is(err, tt.wantErr) {
12191224
t.Errorf("Config.Initialize() error = %v, wantErr %v", err, tt.wantErr)
12201225
}
12211226

12221227
cfgAsMap, err := c.CfgAsMap(ctx)
12231228
require.NoError(t, err)
1224-
1229+
12251230
cfgAsStr, err := yaml.Marshal(cfgAsMap)
12261231
require.NoError(t, err)
12271232

0 commit comments

Comments
 (0)
Please sign in to comment.