Skip to content

Commit

Permalink
Merge pull request #808 from RangelReale/fix-go123-aliastype
Browse files Browse the repository at this point in the history
Fix Go 1.23 types.Alias handling
  • Loading branch information
LandonTClipp authored Sep 28, 2024
2 parents 6423255 + 720caa9 commit afe04a5
Show file tree
Hide file tree
Showing 11 changed files with 1,074 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
os: [ macos-latest, ubuntu-latest]
go_vers: ['1.21', '1.22']
go_vers: ['1.22', '1.23']
steps:
- uses: actions/checkout@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ linters:
- typecheck
- contextcheck
- durationcheck
- exportloopref
- copyloopvar
- gocheckcompilerdirectives
- gosec
- loggercheck
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module github.com/vektra/mockery/v2

go 1.22
go 1.23

toolchain go1.22.0
toolchain go1.23.1

require (
github.com/chigopher/pathlib v0.19.1
Expand Down
4 changes: 2 additions & 2 deletions go.work
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
go 1.22
go 1.23.1

toolchain go1.22.0
toolchain go1.23.1

use (
.
Expand Down
780 changes: 780 additions & 0 deletions go.work.sum

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (c *Config) GetPackages(ctx context.Context) ([]string, error) {
if !ok {
msg := "packages section is of the wrong type"
log.Error().Msg(msg)
return []string{}, fmt.Errorf(msg)
return []string{}, errors.New(msg)
}
packageList := []string{}
for key := range packageSection {
Expand Down Expand Up @@ -396,7 +396,7 @@ func (c *Config) GetInterfaceConfig(ctx context.Context, packageName string, int
return []*Config{pkgConfigCopy}, nil
}
msgString := "bad type provided for interface config"
log.Error().Msgf(msgString)
log.Error().Msg(msgString)
return nil, stackerr.NewStackErr(errors.New(msgString))
}

Expand Down Expand Up @@ -796,7 +796,7 @@ func (c *Config) mergeInConfig(ctx context.Context) error {
// wasn't defined in the yaml.
msg := "config section does not exist for package, this should never happen"
pkgLog.Error().Msg(msg)
return fmt.Errorf(msg)
return errors.New(msg)
}

pkgLog.Trace().Msg("got config section for package")
Expand Down
35 changes: 24 additions & 11 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ func (g *Generator) GeneratePrologueNote(note string) {
}
prologue += ". DO NOT EDIT.\n"

g.printf(prologue)
g.print(prologue)
if note != "" {
g.printf("\n")
for _, n := range strings.Split(note, "\\n") {
Expand Down Expand Up @@ -465,6 +465,10 @@ func (g *Generator) GenerateBuildTags(buildTags string) {
// type.
var ErrNotInterface = errors.New("expression not an interface")

func (g *Generator) print(s string) {
fmt.Fprint(&g.buf, s)
}

func (g *Generator) printf(s string, vals ...interface{}) {
fmt.Fprintf(&g.buf, s, vals...)
}
Expand Down Expand Up @@ -495,19 +499,28 @@ type namer interface {
Name() string
}

func (g *Generator) renderNamedType(ctx context.Context, t interface {
Obj() *types.TypeName
TypeArgs() *types.TypeList
}) string {
name := g.getPackageScopedType(ctx, t.Obj())
if t.TypeArgs() == nil || t.TypeArgs().Len() == 0 {
return name
}
args := make([]string, 0, t.TypeArgs().Len())
for i := 0; i < t.TypeArgs().Len(); i++ {
arg := t.TypeArgs().At(i)
args = append(args, g.renderType(ctx, arg))
}
return fmt.Sprintf("%s[%s]", name, strings.Join(args, ","))
}

func (g *Generator) renderType(ctx context.Context, typ types.Type) string {
switch t := typ.(type) {
case *types.Named:
name := g.getPackageScopedType(ctx, t.Obj())
if t.TypeArgs() == nil || t.TypeArgs().Len() == 0 {
return name
}
args := make([]string, 0, t.TypeArgs().Len())
for i := 0; i < t.TypeArgs().Len(); i++ {
arg := t.TypeArgs().At(i)
args = append(args, g.renderType(ctx, arg))
}
return fmt.Sprintf("%s[%s]", name, strings.Join(args, ","))
return g.renderNamedType(ctx, t)
case *types.Alias:
return g.renderNamedType(ctx, t)
case *types.TypeParam:
if t.Constraint() != nil {
name := t.Obj().Name()
Expand Down
59 changes: 59 additions & 0 deletions pkg/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -815,3 +815,62 @@ func TestParseReplaceType(t *testing.T) {
assert.Equal(t, test.expected, *actual)
}
}


func (s *GeneratorSuite) TestReplaceTypePackagePrologueGo123() {
expected := `package mocks
import baz "github.com/vektra/mockery/v2/pkg/fixtures/example_project/baz"
import mock "github.com/stretchr/testify/mock"
`
generator := NewGenerator(
s.ctx,
GeneratorConfig{InPackage: false},
s.getInterfaceFromFile("example_project/baz/foo.go", "Foo"),
pkg,
)

s.checkPrologueGeneration(generator, expected)
}

func (s *GeneratorSuite) TestReplaceTypePackageGo123() {
cfg := GeneratorConfig{InPackage: false}

s.checkGenerationRegexWithConfig("example_project/baz/foo.go", "Foo", cfg, []regexpExpected{
// func (_m *Foo) GetBaz() (*baz.Baz, error)
{true, regexp.MustCompile(`func \([^\)]+\) GetBaz\(\) \(\*baz\.Baz`)},
// func (_m *Foo) GetBaz() (*foo.InternalBaz, error)
{false, regexp.MustCompile(`func \([^\)]+\) GetBaz\(\) \(\*foo\.InternalBaz`)},
})
}

func (s *GeneratorSuite) TestReplaceTypePackageMultiplePrologueGo123() {
expected := `package mocks
import mock "github.com/stretchr/testify/mock"
import replace_type "github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type"
import rt1 "github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type/rti/rt1"
import rt2 "github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_type/rti/rt2"
`
generator := NewGenerator(
s.ctx,
GeneratorConfig{InPackage: false},
s.getInterfaceFromFile("example_project/replace_type/rt.go", "RType"),
pkg,
)

s.checkPrologueGeneration(generator, expected)
}

func (s *GeneratorSuite) TestReplaceTypePackageMultipleGo123() {
cfg := GeneratorConfig{InPackage: false}

s.checkGenerationRegexWithConfig("example_project/replace_type/rt.go", "RType", cfg, []regexpExpected{
// func (_m *RType) Replace1(f rt1.RType1)
{true, regexp.MustCompile(`func \([^\)]+\) Replace1\(f rt1\.RType1`)},
// func (_m *RType) Replace2(f rt2.RType2)
{true, regexp.MustCompile(`func \([^\)]+\) Replace2\(f rt2\.RType2`)},
})
}
2 changes: 1 addition & 1 deletion pkg/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func GetLogger(levelStr string) (zerolog.Logger, error) {
Out: out,
TimeFormat: time.RFC822,
}
if !term.IsTerminal(int(out.Fd())) || os.Getenv("TERM") == "dumb" {
if !term.IsTerminal(int(out.Fd())) || os.Getenv("TERM") == "dumb" { //nolint:gosec
writer.NoColor = true
}
log := zerolog.New(writer).
Expand Down
Loading

0 comments on commit afe04a5

Please sign in to comment.