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
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
go:
strategy:
matrix:
go-version: [1.22.x, 1.23.x] # oldest version that can build go mock and official supported go versions
go-version: [1.23.x, 1.24.x] # oldest version that can build go mock and official supported go versions
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -71,4 +71,4 @@ jobs:

- name: Run Bazel tests
run: |
cd bazel && bazel test //...
cd bazel && bazel test //...
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module go.uber.org/mock

go 1.22
go 1.23

require (
github.com/stretchr/testify v1.9.0
Expand Down
10 changes: 5 additions & 5 deletions mockgen/internal/tests/alias/mock/interfaces.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mockgen/internal/tests/generics/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module go.uber.org/mock/mockgen/internal/tests/generics

go 1.22
go 1.23

replace go.uber.org/mock => ../../../..

Expand Down
16 changes: 8 additions & 8 deletions mockgen/internal/tests/package_mode/mock/interfaces.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mockgen/internal/tests/typed/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module go.uber.org/mock/mockgen/internal/tests/typed

go 1.22
go 1.23

replace go.uber.org/mock => ../../../..

Expand Down
109 changes: 0 additions & 109 deletions mockgen/package_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"flag"
"fmt"
"go/ast"
"go/types"
"strings"

Expand All @@ -18,20 +17,6 @@ var (

type packageModeParser struct {
pkgName string

// Mapping from underlying types to aliases used within the package source.
//
// We prefer to use aliases used in the source rather than underlying type names
// as those may be unexported or internal.
// TODO(joaks): Once mock is Go1.23+ only, we can remove this
// as the casing for types.Alias will automatically handle this
// in all cases.
aliasReplacements map[types.Type]aliasReplacement
}

type aliasReplacement struct {
name string
pkg string
}

func (p *packageModeParser) parsePackage(packageName string, ifaces []string) (*model.Package, error) {
Expand All @@ -42,8 +27,6 @@ func (p *packageModeParser) parsePackage(packageName string, ifaces []string) (*
return nil, fmt.Errorf("load package: %w", err)
}

p.buildAliasReplacements(pkg)

interfaces, err := p.extractInterfacesFromPackage(pkg, ifaces)
if err != nil {
return nil, fmt.Errorf("extract interfaces from package: %w", err)
Expand All @@ -56,90 +39,6 @@ func (p *packageModeParser) parsePackage(packageName string, ifaces []string) (*
}, nil
}

// buildAliasReplacements finds and records any references to aliases
// within the given package's source.
// These aliases will be preferred when parsing types
// over the underlying name counterparts, as those may be unexported / internal.
//
// If a type has more than one alias within the source package,
// the latest one to be inspected will be the one used for mapping.
// This is fine, since all aliases and their underlying types are interchangeable
// from a type-checking standpoint.
func (p *packageModeParser) buildAliasReplacements(pkg *packages.Package) {
p.aliasReplacements = make(map[types.Type]aliasReplacement)

// checkIdent checks if the given identifier exists
// in the given package as an alias, and adds it to
// the alias replacements map if so.
checkIdent := func(pkg *types.Package, ident string) bool {
scope := pkg.Scope()
if scope == nil {
return true
}
obj := scope.Lookup(ident)
if obj == nil {
return true
}
objTypeName, ok := obj.(*types.TypeName)
if !ok {
return true
}
if !objTypeName.IsAlias() {
return true
}
typ := objTypeName.Type()
if typ == nil {
return true
}
p.aliasReplacements[typ] = aliasReplacement{
name: objTypeName.Name(),
pkg: pkg.Path(),
}
return false

}

for _, f := range pkg.Syntax {
fileScope, ok := pkg.TypesInfo.Scopes[f]
if !ok {
continue
}
ast.Inspect(f, func(node ast.Node) bool {

// Simple identifiers: check if it is an alias
// from the source package.
if ident, ok := node.(*ast.Ident); ok {
return checkIdent(pkg.Types, ident.String())
}

// Selector expressions: check if it is an alias
// from the package represented by the qualifier.
selExpr, ok := node.(*ast.SelectorExpr)
if !ok {
return true
}

x, sel := selExpr.X, selExpr.Sel
xident, ok := x.(*ast.Ident)
if !ok {
return true
}

xObj := fileScope.Lookup(xident.String())
pkgName, ok := xObj.(*types.PkgName)
if !ok {
return true
}

xPkg := pkgName.Imported()
if xPkg == nil {
return true
}
return checkIdent(xPkg, sel.String())
})
}
}

func (p *packageModeParser) loadPackage(packageName string) (*packages.Package, error) {
var buildFlagsSet []string
if *buildFlags != "" {
Expand Down Expand Up @@ -300,14 +199,6 @@ func (p *packageModeParser) parseType(t types.Type) (model.Type, error) {
pkg = object.Obj().Pkg().Path()
}

// If there was an alias to this type used somewhere in the source,
// use that alias instead of the underlying type,
// since the underlying type might be unexported.
if alias, ok := p.aliasReplacements[t]; ok {
name = alias.name
pkg = alias.pkg
}

// TypeArgs method not available for aliases in go1.22
genericType, ok := t.(interface{ TypeArgs() *types.TypeList })
if !ok || genericType.TypeArgs() == nil {
Expand Down
14 changes: 10 additions & 4 deletions mockgen/package_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,10 @@ func Test_packageModeParser_parsePackage(t *testing.T) {
Name: "AddHumans",
In: []*model.Parameter{
{
Type: model.PredeclaredType("int"),
Type: &model.NamedType{
Package: "go.uber.org/mock/mockgen/internal/tests/package_mode",
Type: "HumansCount",
},
},
},
Out: []*model.Parameter{
Expand All @@ -333,7 +336,10 @@ func Test_packageModeParser_parsePackage(t *testing.T) {
Name: "HumanPopulation",
Out: []*model.Parameter{
{
Type: model.PredeclaredType("int"),
Type: &model.NamedType{
Package: "go.uber.org/mock/mockgen/internal/tests/package_mode",
Type: "HumansCount",
},
},
},
},
Expand Down Expand Up @@ -444,13 +450,13 @@ func TestAliases(t *testing.T) {
In: []*model.Parameter{{
Type: &model.NamedType{
Package: "go.uber.org/mock/mockgen/internal/tests/alias",
Type: "FooerAlias",
Type: "Fooer",
},
}},
Out: []*model.Parameter{{
Type: &model.NamedType{
Package: "go.uber.org/mock/mockgen/internal/tests/alias",
Type: "FooerAlias",
Type: "Fooer",
},
}},
}},
Expand Down
2 changes: 1 addition & 1 deletion tools/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/uber-go/mock/tools

go 1.22
go 1.23

require github.com/stretchr/testify v1.9.0

Expand Down