Skip to content

Commit

Permalink
Fix confusion around pkgname
Browse files Browse the repository at this point in the history
  • Loading branch information
LandonTClipp committed Jan 3, 2025
1 parent e29f842 commit ed75463
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 642 deletions.
1 change: 0 additions & 1 deletion .mockery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ packages:
interfaces:
TypesPackage:
github.com/vektra/mockery/v3/internal/fixtures:
interfaces:
RequesterVariadic:
configs:
- mockname: MockRequesterVariadicOneArgument
Expand Down
26 changes: 15 additions & 11 deletions internal/cmd/mockery.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,32 +164,33 @@ type InterfaceCollection struct {
srcPkgPath string
outFilePath *pathlib.Path
srcPkg *packages.Package
pkgName string
outPkgName string
interfaces []*pkg.Interface
}

func NewInterfaceCollection(
srcPkgPath string,
outFilePath *pathlib.Path,
srcPkg *packages.Package,
pkgName string,
outPkgName string,
) *InterfaceCollection {
return &InterfaceCollection{
srcPkgPath: srcPkgPath,
outFilePath: outFilePath,
srcPkg: srcPkg,
outPkgName: outPkgName,
interfaces: make([]*pkg.Interface, 0),
}
}

func (i *InterfaceCollection) Append(ctx context.Context, iface *pkg.Interface) error {
log := zerolog.Ctx(ctx).With().
Str(logging.LogKeyInterface, iface.Name).
Str(logging.LogKeyPackageName, iface.Pkg.Name).
Str("source-pkgname", iface.Pkg.Name).
Str(logging.LogKeyPackagePath, iface.Pkg.PkgPath).
Str("expected-package-path", i.srcPkgPath).Logger()
if iface.Pkg.PkgPath != i.srcPkgPath {
msg := "cannot mix interfaces from different packages in the same file."
msg := "cannot mix interfaces from different packages in the same file"
log.Error().Msg(msg)
return errors.New(msg)
}
Expand All @@ -198,9 +199,9 @@ func (i *InterfaceCollection) Append(ctx context.Context, iface *pkg.Interface)
log.Error().Msg(msg)
return errors.New(msg)
}
if i.pkgName != iface.Config.PkgName {
if i.outPkgName != iface.Config.PkgName {
msg := "all mocks in an output file must have the same pkgname"
log.Error().Str("expected-pkgname", i.pkgName).Str("interface-pkgname", iface.Config.PkgName).Msg(msg)
log.Error().Str("output-pkgname", i.outPkgName).Str("interface-pkgname", iface.Config.PkgName).Msg(msg)
return errors.New(msg)
}
i.interfaces = append(i.interfaces, iface)
Expand Down Expand Up @@ -256,7 +257,7 @@ func (r *RootApp) Run() error {
ifaceLog := log.
With().
Str(logging.LogKeyInterface, iface.Name).
Str(logging.LogKeyQualifiedName, iface.Pkg.Types.Path()).
Str(logging.LogKeyPackagePath, iface.Pkg.Types.Path()).
Logger()

ifaceCtx := ifaceLog.WithContext(ctx)
Expand Down Expand Up @@ -287,18 +288,20 @@ func (r *RootApp) Run() error {
iface.Pkg.PkgPath,
pathlib.NewPath(ifaceConfig.Dir).Join(ifaceConfig.FileName),
iface.Pkg,
iface.Config.PkgName,
ifaceConfig.PkgName,
)
}
mockFileToInterfaces[filePath].Append(
if err := mockFileToInterfaces[filePath].Append(
ctx,
pkg.NewInterface(
iface.Name,
iface.FileName,
iface.File,
iface.Pkg,
ifaceConfig),
)
); err != nil {
return err
}
}
}

Expand All @@ -320,7 +323,8 @@ func (r *RootApp) Run() error {
interfacesInFile.outFilePath.Parent(),
packageConfig.Template,
pkg.Formatter(r.Config.Formatter),
interfacesInFile.pkgName,
packageConfig,
interfacesInFile.outPkgName,
)
if err != nil {
return err
Expand Down
Loading

0 comments on commit ed75463

Please sign in to comment.