From 720caa9a0b949bca858e0338e87b7d05fd340926 Mon Sep 17 00:00:00 2001 From: LandonTClipp <11232769+LandonTClipp@users.noreply.github.com> Date: Fri, 27 Sep 2024 21:03:48 -0500 Subject: [PATCH] Undo go:build statements, make code a little DRY --- pkg/generator.go | 29 +++++---- pkg/generator_alias.go | 12 ---- pkg/generator_alias_go123.go | 23 ------- pkg/generator_go123_test.go | 115 ----------------------------------- pkg/generator_test.go | 59 ++++++++++++++++++ 5 files changed, 77 insertions(+), 161 deletions(-) delete mode 100644 pkg/generator_alias.go delete mode 100644 pkg/generator_alias_go123.go delete mode 100644 pkg/generator_go123_test.go diff --git a/pkg/generator.go b/pkg/generator.go index 527bc146..abf12516 100644 --- a/pkg/generator.go +++ b/pkg/generator.go @@ -499,21 +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.renderTypeAlias(ctx, t) + return g.renderNamedType(ctx, t) case *types.TypeParam: if t.Constraint() != nil { name := t.Obj().Name() diff --git a/pkg/generator_alias.go b/pkg/generator_alias.go deleted file mode 100644 index aef17f27..00000000 --- a/pkg/generator_alias.go +++ /dev/null @@ -1,12 +0,0 @@ -//go:build !go1.23 - -package pkg - -import ( - "context" - "go/types" -) - -func (g *Generator) renderTypeAlias(ctx context.Context, t *types.Alias) string { - return g.getPackageScopedType(ctx, t.Obj()) -} diff --git a/pkg/generator_alias_go123.go b/pkg/generator_alias_go123.go deleted file mode 100644 index a83b7e20..00000000 --- a/pkg/generator_alias_go123.go +++ /dev/null @@ -1,23 +0,0 @@ -//go:build go1.23 - -package pkg - -import ( - "context" - "fmt" - "go/types" - "strings" -) - -func (g *Generator) renderTypeAlias(ctx context.Context, t *types.Alias) 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, ",")) -} diff --git a/pkg/generator_go123_test.go b/pkg/generator_go123_test.go deleted file mode 100644 index e2c64883..00000000 --- a/pkg/generator_go123_test.go +++ /dev/null @@ -1,115 +0,0 @@ -//go:build go1.23 - -package pkg - -import ( - "go/ast" - "go/parser" - "go/token" - "go/types" - "regexp" -) - -func (s *GeneratorSuite) TestReplaceTypePackagePrologueGo123() { - if !isTypeAliasEnabled() { - // "go 1.22" in go.mod makes gotypesalias=0 even when compiling with Go 1.23. - // Remove this when upgrading to Go 1.23 in go.mod. - return - } - - 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() { - if !isTypeAliasEnabled() { - // "go 1.22" in go.mod makes gotypesalias=0 even when compiling with Go 1.23. - // Remove this when upgrading to Go 1.23 in go.mod. - return - } - - 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() { - if !isTypeAliasEnabled() { - // "go 1.22" in go.mod makes gotypesalias=0 even when compiling with Go 1.23. - // Remove this when upgrading to Go 1.23 in go.mod. - return - } - - 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() { - if !isTypeAliasEnabled() { - // "go 1.22" in go.mod makes gotypesalias=0 even when compiling with Go 1.23. - // Remove this when upgrading to Go 1.23 in go.mod. - return - } - - 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`)}, - }) -} - -// isTypeAliasEnabled reports whether [NewAlias] should create [types.Alias] types. -// -// This function is expensive! Call it sparingly. -// source: /go/1.23.0/libexec/src/cmd/vendor/golang.org/x/tools/internal/aliases/aliases_go122.go -func isTypeAliasEnabled() bool { - // The only reliable way to compute the answer is to invoke go/types. - // We don't parse the GODEBUG environment variable, because - // (a) it's tricky to do so in a manner that is consistent - // with the godebug package; in particular, a simple - // substring check is not good enough. The value is a - // rightmost-wins list of options. But more importantly: - // (b) it is impossible to detect changes to the effective - // setting caused by os.Setenv("GODEBUG"), as happens in - // many tests. Therefore any attempt to cache the result - // is just incorrect. - fset := token.NewFileSet() - f, _ := parser.ParseFile(fset, "a.go", "package p; type A = int", 0) - pkg, _ := new(types.Config).Check("p", fset, []*ast.File{f}, nil) - _, enabled := pkg.Scope().Lookup("A").Type().(*types.Alias) - return enabled -} diff --git a/pkg/generator_test.go b/pkg/generator_test.go index d99a27df..71d11760 100644 --- a/pkg/generator_test.go +++ b/pkg/generator_test.go @@ -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`)}, + }) +}