Skip to content

Commit

Permalink
Fix test with config initialization
Browse files Browse the repository at this point in the history
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?!
  • Loading branch information
LandonTClipp committed Nov 6, 2023
1 parent 0310201 commit 5978bc5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
28 changes: 21 additions & 7 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (c *Config) getPackageConfigMap(ctx context.Context, packageName string) (m

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

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

pkgConfig := reflect.New(reflect.ValueOf(c).Elem().Type()).Interface()
//pkgConfig := reflect.New(reflect.ValueOf(c).Elem().Type()).Interface()
pkgConfig := &Config{}
if err := copier.Copy(pkgConfig, c); err != nil {
return nil, fmt.Errorf("failed to copy config: %w", err)
}
pkgConfigTyped := pkgConfig.(*Config)
//pkgConfigTyped := pkgConfig.(*Config)
pkgConfigTyped := pkgConfig

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

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

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

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

// Don't overwrite any config that already exists
_, pkgExists := packagesSection[subPkgPath]
Expand Down Expand Up @@ -600,18 +603,25 @@ func (c *Config) subPackages(
// recursive and recurses the file tree to find all sub-packages.
func (c *Config) discoverRecursivePackages(ctx context.Context) error {
log := zerolog.Ctx(ctx)
log.Trace().Msg("discovering recursive packages")
recursivePackages := map[string]*Config{}
packageList, err := c.GetPackages(ctx)
if err != nil {
return fmt.Errorf("failed to get packages: %w", err)
}
for _, pkg := range packageList {
pkgConfig, err := c.GetPackageConfig(ctx, pkg)
pkgLog := log.With().Str("package", pkg).Logger()
pkgLog.Trace().Msg("iterating over package")
if err != nil {
return fmt.Errorf("failed to get package config: %w", err)
}
if pkgConfig.Recursive {
pkgLog.Trace().Msg("package marked as recursive")
recursivePackages[pkg] = pkgConfig
} else {
pkgLog.Trace().Msg("package not marked as recursive")
pkgLog.Trace().Msg(fmt.Sprintf("%+v", pkgConfig))
}
}
if len(recursivePackages) == 0 {
Expand Down Expand Up @@ -703,8 +713,12 @@ func (c *Config) mergeInConfig(ctx context.Context) error {

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

pkgLog.Trace().Msg("got config section for package")
Expand Down
23 changes: 14 additions & 9 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1182,22 +1182,24 @@ dir: foobar
recursive: True
all: True
packages:
github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs:
github.com/vektra/mockery/v2/pkg/fixtures/example_project/pkg_with_subpkgs/subpkg2:
`,
wantCfgMap: `dir: foobar
wantCfgMap: `all: true
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
with-expecter: false
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
recursive: true
with-expecter: false
`,
},
Expand All @@ -1209,19 +1211,22 @@ with-expecter: false
cfg := tmpdir.Join("config.yaml")
require.NoError(t, cfg.WriteFile([]byte(tt.cfgYaml)))

c := &Config{
Config: cfg.String(),
}
viperObj := viper.New()
viperObj.SetConfigFile(cfg.String())
require.NoError(t, viperObj.ReadInConfig())
c, err := NewConfigFromViper(viperObj)
require.NoError(t, err)

log, err := logging.GetLogger("TRACE")
require.NoError(t, err)

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

cfgAsMap, err := c.CfgAsMap(ctx)
require.NoError(t, err)

cfgAsStr, err := yaml.Marshal(cfgAsMap)
require.NoError(t, err)

Expand Down

0 comments on commit 5978bc5

Please sign in to comment.