Skip to content

Commit

Permalink
Force all interfaces to have the same pkgname
Browse files Browse the repository at this point in the history
  • Loading branch information
LandonTClipp committed Jan 3, 2025
1 parent 9171d97 commit 8e90c1e
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion internal/cmd/mockery.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/vektra/mockery/v3/internal"
pkg "github.com/vektra/mockery/v3/internal"
"github.com/vektra/mockery/v3/internal/logging"
"github.com/vektra/mockery/v3/internal/stackerr"
"golang.org/x/tools/go/packages"
Expand Down Expand Up @@ -157,16 +157,22 @@ func GetRootAppFromViper(v *viper.Viper) (*RootApp, error) {
// asserts that various properties of the interfaces added to the collection are
// uniform.
type InterfaceCollection struct {
// Mockery needs to assert that certain properties of the added interfaces
// are uniform for all members of the collection. This includes things like
// 1. Package name of the output mock file
// 2. Source package path (only one package per output file is allowed)
srcPkgPath string
outFilePath *pathlib.Path
srcPkg *packages.Package
pkgName string
interfaces []*pkg.Interface
}

func NewInterfaceCollection(
srcPkgPath string,
outFilePath *pathlib.Path,
srcPkg *packages.Package,
pkgName string,
) *InterfaceCollection {
return &InterfaceCollection{
srcPkgPath: srcPkgPath,
Expand All @@ -192,6 +198,11 @@ func (i *InterfaceCollection) Append(ctx context.Context, iface *pkg.Interface)
log.Error().Msg(msg)
return errors.New(msg)
}
if i.pkgName != 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)
return errors.New(msg)
}
i.interfaces = append(i.interfaces, iface)
return nil
}
Expand Down Expand Up @@ -276,6 +287,7 @@ func (r *RootApp) Run() error {
iface.Pkg.PkgPath,
pathlib.NewPath(ifaceConfig.Dir).Join(ifaceConfig.FileName),
iface.Pkg,
iface.Config.PkgName,
)
}
mockFileToInterfaces[filePath].Append(
Expand Down

0 comments on commit 8e90c1e

Please sign in to comment.