Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
LandonTClipp committed Jan 6, 2025
1 parent a72ae3c commit 13d1afa
Show file tree
Hide file tree
Showing 5 changed files with 2,481 additions and 20 deletions.
8 changes: 7 additions & 1 deletion .mockery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ template: "mockery"
tags: "custom2"
formatter: "goimports"
all: True
dir: "{{.InterfaceDir}}"
dir: "{{.InterfaceDirRelative}}"
mockname: "Mock{{.InterfaceName}}"
pkgname: "{{.SrcPackageName}}"
filename: "mocks_test.go"
Expand Down Expand Up @@ -42,4 +42,10 @@ packages:
github.com/vektra/mockery/v3/internal/fixtures/index_list_expr:
github.com/vektra/mockery/v3/internal/fixtures/iface_new_type:
github.com/vektra/mockery/v3/internal/fixtures/type_alias:
io:
config:
all: True
dir: internal/fixtures/
filename: mocks_io_test.go
pkgname: test

3 changes: 2 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ All configuration is specified in a `.mockery.yml` file. An example config file

```yaml
all: False
boilerplate-file: ./path/to/boilerplate.txt
template-data:
boilerplate-file: ./path/to/boilerplate.txt
template: mockery
packages:
github.com/vektra/example:
Expand Down
45 changes: 33 additions & 12 deletions internal/cmd/mockery.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,31 @@ func NewInterfaceCollection(
}

func (i *InterfaceCollection) Append(ctx context.Context, iface *pkg.Interface) error {
collectionFilepath := i.outFilePath.String()
interfaceFilepath := iface.Config.FilePath().String()
log := zerolog.Ctx(ctx).With().
Str(logging.LogKeyInterface, iface.Name).
Str("source-pkgname", iface.Pkg.Name).
Str(logging.LogKeyPackagePath, iface.Pkg.PkgPath).
Str("expected-package-path", i.srcPkgPath).Logger()
if i.outFilePath.String() != pathlib.NewPath(*iface.Config.Dir).Join(*iface.Config.FileName).String() {
Str("collection-pkgname", i.outPkgName).
Str("interface-pkgname", *iface.Config.PkgName).
Str("collection-pkgpath", i.srcPkgPath).
Str("interface-pkgpath", iface.Pkg.PkgPath).
Str("collection-filepath", collectionFilepath).
Str("interface-filepath", interfaceFilepath).
Logger()

if collectionFilepath != interfaceFilepath {
msg := "all mocks in an InterfaceCollection must have the same output file path"
log.Error().Msg(msg)
return errors.New(msg)
}
if i.outPkgName != *iface.Config.PkgName {
msg := "all mocks in an output file must have the same pkgname"
log.Error().Str("output-pkgname", i.outPkgName).Str("interface-pkgname", *iface.Config.PkgName).Msg(msg)
log.Error().Str("interface-pkgname", *iface.Config.PkgName).Msg(msg)
return errors.New(msg)
}
if i.srcPkgPath != iface.Pkg.PkgPath {
msg := "all mocks in an output file must come from the same source package"
log.Error().Msg(msg)
return errors.New(msg)
}
if i.template != *iface.Config.Template {
Expand Down Expand Up @@ -222,7 +234,6 @@ func (r *RootApp) Run() error {
ifaceLog.Debug().Msg("config doesn't specify to generate this interface, skipping")
continue
}
ifaceLog.Info().Msg("adding interface")
if pkgConfig.Interfaces == nil {
ifaceLog.Debug().Msg("interfaces is nil")
}
Expand All @@ -232,20 +243,20 @@ func (r *RootApp) Run() error {
log.Err(err).Msg("Can't parse config templates for interface")
return err
}
filePath := ifaceConfig.FilePath().String()
ifaceLog.Debug().Str("collection", filePath).Msg("adding interface to collection")
filePath := ifaceConfig.FilePath().Clean()
ifaceLog.Info().Str("collection", filePath.String()).Msg("adding interface to collection")

_, ok := mockFileToInterfaces[filePath]
_, ok := mockFileToInterfaces[filePath.String()]
if !ok {
mockFileToInterfaces[filePath] = NewInterfaceCollection(
mockFileToInterfaces[filePath.String()] = NewInterfaceCollection(
iface.Pkg.PkgPath,
pathlib.NewPath(*ifaceConfig.Dir).Join(*ifaceConfig.FileName),
filePath,
iface.Pkg,
*ifaceConfig.PkgName,
*ifaceConfig.Template,
)
}
if err := mockFileToInterfaces[filePath].Append(
if err := mockFileToInterfaces[filePath.String()].Append(
ctx,
pkg.NewInterface(
iface.Name,
Expand Down Expand Up @@ -297,6 +308,16 @@ func (r *RootApp) Run() error {
return stackerr.NewStackErr(err)
}
fileLog.Info().Msg("Writing template to file")
outFileExists, err := outFile.Exists()
if err != nil {
fileLog.Err(err).Msg("can't determine if outfile exists")
return fmt.Errorf("determining if outfile exists: %w", err)
}
if outFileExists {
fileLog.Error().Msg("output file exists, can't write mocks")
return fmt.Errorf("outfile exists")
}

if err := outFile.WriteFile(templateBytes); err != nil {
return stackerr.NewStackErr(err)
}
Expand Down
14 changes: 8 additions & 6 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,10 @@ func (c *RootConfig) subPackages(pkgPath string) ([]string, error) {
return convertPkgPath(pkgs), nil
}

func (c *RootConfig) GetPackageConfig(ctx context.Context, packageName string) (*PackageConfig, error) {
pkgConfig, ok := c.Packages[packageName]
func (c *RootConfig) GetPackageConfig(ctx context.Context, pkgPath string) (*PackageConfig, error) {
pkgConfig, ok := c.Packages[pkgPath]
if !ok {
return nil, fmt.Errorf("package %s does not exist in the config", packageName)
return nil, stackerr.NewStackErr(fmt.Errorf("package %s does not exist in the config", pkgPath))
}
return pkgConfig, nil
}
Expand Down Expand Up @@ -447,7 +447,7 @@ func findConfig() (*pathlib.Path, error) {
}

func (c *Config) FilePath() *pathlib.Path {
return pathlib.NewPath(*c.Dir).Join(*c.FileName)
return pathlib.NewPath(*c.Dir).Join(*c.FileName).Clean()
}

func (c *Config) ShouldExcludeSubpkg(pkgPath string) bool {
Expand Down Expand Up @@ -519,9 +519,11 @@ func (c *Config) ParseTemplates(ctx context.Context, iface *Interface, srcPkg *p
interfaceDir = interfaceDirPath.String()
interfaceDirRelativePath, err := interfaceDirPath.RelativeToStr(workingDir)
if err != nil {
return stackerr.NewStackErr(err)
log.Debug().Err(err).Msg("can't make path relative to working dir, setting to './'")
interfaceDirRelative = "."
} else {
interfaceDirRelative = interfaceDirRelativePath.String()
}
interfaceDirRelative = interfaceDirRelativePath.String()
}
// data is the struct sent to the template parser
data := mockeryTemplate.ConfigData{
Expand Down
Loading

0 comments on commit 13d1afa

Please sign in to comment.