Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .mockery_testify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,4 @@ packages:
configs:
- structname: FunServerWithDifferentFile
- structname: AnotherFunServerWithDifferentFile
github.com/vektra/mockery/v3/internal/fixtures/constraint_ifaces:
23 changes: 23 additions & 0 deletions internal/fixtures/constraint_ifaces/constraint_interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package skipconstraintifaces

import "golang.org/x/exp/constraints"

type Skip1 constraints.Ordered

type Skip2 interface {
~int
}

type Skip3 interface {
constraints.Float
}

type Skip4 interface {
constraints.Float | constraints.Integer
}

type Skip5 Skip1

type Skip6 interface {
Skip5
}
15 changes: 15 additions & 0 deletions internal/fixtures/constraint_ifaces/constraint_interfaces_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package skipconstraintifaces

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/vektra/mockery/v3/internal/file"
)

func TestSkipConstraintInterfaces(t *testing.T) {
exists, err := file.Exists("./mocks_testify_skipconstraintifaces_test")

require.NoError(t, err)
require.False(t, exists)
}
63 changes: 63 additions & 0 deletions internal/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ func (p *Parser) ParsePackages(ctx context.Context, packageNames []string) ([]*I
continue
}

if isConstraint(declaredInterface.typeSpec, packages) {
ifaceLog.Debug().Msg("interface is a constraint, skipping")
continue
}

interfaces = append(interfaces, NewInterface(
name,
declaredInterface.typeSpec,
Expand Down Expand Up @@ -171,3 +176,61 @@ func isAutoGenerated(pathName string) (bool, error) {
}
return false, nil
}

func isConstraint(s *ast.TypeSpec, pkgs []*packages.Package) bool {
switch typ := s.Type.(type) {
case *ast.Ident:
return isConstraintIdent(typ, pkgs)
case *ast.SelectorExpr:
return isConstraintSelector(typ, pkgs)
case *ast.InterfaceType:
for _, item := range typ.Methods.List {
switch expr := item.Type.(type) {
case *ast.UnaryExpr, *ast.BinaryExpr:
return true
case *ast.SelectorExpr:
return isConstraintSelector(expr, pkgs)
case *ast.Ident:
return isConstraintIdent(expr, pkgs)
}
}
}

return false
}

func isConstraintSelector(sel *ast.SelectorExpr, pkgs []*packages.Package) bool {
for _, pkg := range pkgs {
if tv, ok := pkg.TypesInfo.Types[sel]; ok {
if iface, ok := tv.Type.Underlying().(*types.Interface); ok {
return isConstraintInterface(iface)
}
}
}

return false
}

func isConstraintIdent(id *ast.Ident, pkgs []*packages.Package) bool {
for _, pkg := range pkgs {
if obj := pkg.TypesInfo.Uses[id]; obj != nil {
if iface, ok := obj.Type().Underlying().(*types.Interface); ok {
return isConstraintInterface(iface)
}
}
}

return false
}

func isConstraintInterface(iface *types.Interface) bool {
if iface.NumMethods() > 0 {
return false
}

if iface.NumEmbeddeds() > 0 {
return true
}

return false
}