Skip to content

Commit

Permalink
support types.Alias
Browse files Browse the repository at this point in the history
  • Loading branch information
RangelReale committed Aug 20, 2024
1 parent 42d905a commit 6f7960f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 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.21', '1.22', '1.23']
steps:
- uses: actions/checkout@v2
with:
Expand Down
54 changes: 53 additions & 1 deletion pkg/generator_go123_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@

package pkg

import "regexp"
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"
Expand All @@ -22,6 +34,12 @@ import mock "github.com/stretchr/testify/mock"
}

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{
Expand All @@ -33,6 +51,12 @@ func (s *GeneratorSuite) TestReplaceTypePackageGo123() {
}

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"
Expand All @@ -52,6 +76,12 @@ import rt2 "github.com/vektra/mockery/v2/pkg/fixtures/example_project/replace_ty
}

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{
Expand All @@ -61,3 +91,25 @@ func (s *GeneratorSuite) TestReplaceTypePackageMultipleGo123() {
{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
}

0 comments on commit 6f7960f

Please sign in to comment.