From dc6c31da0c57628af92876466a6e7b9ec3c9e508 Mon Sep 17 00:00:00 2001 From: Dominic Evans Date: Fri, 17 Jan 2025 09:51:23 +0000 Subject: [PATCH] fix: restore support and testing of Go 1.22.0 - relax go directive to 1.22.0 to match the N-1 and N support statement and permit run/build with go1.22.0 and newer - update the test fixtures go.mod similarly to make them representative - use the toolchain directive to _recommend_ the latest 1.23.x (Go treats this as advisory) - fix the CI build so that oldstable and stable are actually being tested by telling setup-go to ignore the toolchain directive - use go build tags to add a stub for types.Alias on go1.22 and (somewhat ironically!) use a simple type Alias to the builtin on go1.23 and newer Fixes maxbrunsfeld/counterfeiter#312 Signed-off-by: Dominic Evans --- .github/workflows/go.yml | 8 +++++- fixtures/dup_packages/go.mod | 4 ++- fixtures/hyphenated_package_same_name/go.mod | 4 +++ fixtures/type_aliases/go.mod | 4 ++- generator/loader.go | 12 +++++---- generator/types_alias_go122.go | 27 ++++++++++++++++++++ generator/types_alias_go123.go | 11 ++++++++ go.mod | 9 ++++--- go.sum | 10 ++++++-- 9 files changed, 75 insertions(+), 14 deletions(-) create mode 100644 generator/types_alias_go122.go create mode 100644 generator/types_alias_go123.go diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 1d6bbe0..478d111 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -6,9 +6,15 @@ on: inputs: debug_enabled: type: boolean - description: 'Run the build with tmate debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)' + description: "Run the build with tmate debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)" required: false default: false +env: + # GOOTOOLCHAIN=local tells Go to only use the installed Go version (from the + # build matrix) rather than immediately upgrading to whatever is specified by + # the `toolchain` directive in `go.mod`. This ensures that the build matrix + # versions (stable and oldstable) are actually being validated. + GOTOOLCHAIN: local jobs: build: name: Build and Test diff --git a/fixtures/dup_packages/go.mod b/fixtures/dup_packages/go.mod index b49ab2a..9e8c080 100644 --- a/fixtures/dup_packages/go.mod +++ b/fixtures/dup_packages/go.mod @@ -1,3 +1,5 @@ module github.com/maxbrunsfeld/counterfeiter/v6/fixtures/dup_packages -go 1.22 +go 1.22.0 + +toolchain go1.23.5 diff --git a/fixtures/hyphenated_package_same_name/go.mod b/fixtures/hyphenated_package_same_name/go.mod index 38a3ae5..2b3b4a8 100644 --- a/fixtures/hyphenated_package_same_name/go.mod +++ b/fixtures/hyphenated_package_same_name/go.mod @@ -1 +1,5 @@ module github.com/maxbrunsfeld/counterfeiter/v6/fixtures/hyphenated_package_same_name + +go 1.22.0 + +toolchain go1.23.5 diff --git a/fixtures/type_aliases/go.mod b/fixtures/type_aliases/go.mod index b9bd99b..c487c8f 100644 --- a/fixtures/type_aliases/go.mod +++ b/fixtures/type_aliases/go.mod @@ -1,3 +1,5 @@ module github.com/maxbrunsfeld/counterfeiter/v6/fixtures/type_aliases -go 1.22 +go 1.22.0 + +toolchain go1.23.5 diff --git a/generator/loader.go b/generator/loader.go index 106ddfb..88d0dc9 100644 --- a/generator/loader.go +++ b/generator/loader.go @@ -13,6 +13,11 @@ import ( "golang.org/x/tools/imports" ) +type namedType interface { + Obj() *types.TypeName + TypeArgs() *types.TypeList +} + func (f *Fake) loadPackages(c Cacher, workingDir string) error { log.Println("loading packages...") p, ok := c.Load(f.TargetPackage) @@ -169,7 +174,7 @@ func (f *Fake) addImportsFor(typ types.Type) { f.addImportsFor(t.Elem()) case *types.Chan: f.addImportsFor(t.Elem()) - case *types.Alias: + case *typesAlias: // note: using stub or alias from types_alias_go1.22.go or types_alias_go1.23.go as appropriate f.addImportsForNamedType(t) case *types.Named: f.addImportsForNamedType(t) @@ -190,10 +195,7 @@ func (f *Fake) addImportsFor(typ types.Type) { } } -func (f *Fake) addImportsForNamedType(t interface { - Obj() *types.TypeName - TypeArgs() *types.TypeList -}) { +func (f *Fake) addImportsForNamedType(t namedType) { if t.Obj() != nil && t.Obj().Pkg() != nil { typeArgs := t.TypeArgs() for i := 0; i < typeArgs.Len(); i++ { diff --git a/generator/types_alias_go122.go b/generator/types_alias_go122.go new file mode 100644 index 0000000..3f814ed --- /dev/null +++ b/generator/types_alias_go122.go @@ -0,0 +1,27 @@ +//go:build !go1.23 + +package generator + +import ( + "go/types" +) + +var _ namedType = &typesAlias{} + +type typesAlias struct{} + +func (a *typesAlias) Obj() *types.TypeName { + panic("counterfeiter itself must be run/built with go1.23 or newer in order to handle type aliasing") +} + +func (a *typesAlias) String() string { + panic("counterfeiter itself must be run/built with go1.23 or newer in order to handle type aliasing") +} + +func (a *typesAlias) TypeArgs() *types.TypeList { + panic("counterfeiter itself must be run/built with go1.23 or newer in order to handle type aliasing") +} + +func (a *typesAlias) Underlying() types.Type { + panic("counterfeiter itself must be run/built with go1.23 or newer in order to handle type aliasing") +} diff --git a/generator/types_alias_go123.go b/generator/types_alias_go123.go new file mode 100644 index 0000000..c9f86b8 --- /dev/null +++ b/generator/types_alias_go123.go @@ -0,0 +1,11 @@ +//go:build go1.23 + +package generator + +import ( + "go/types" +) + +var _ types.Type = &typesAlias{} + +type typesAlias = types.Alias diff --git a/go.mod b/go.mod index 397dbfa..6667124 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,9 @@ module github.com/maxbrunsfeld/counterfeiter/v6 +go 1.22.0 + +toolchain go1.23.5 + require ( github.com/onsi/gomega v1.36.2 github.com/sclevine/spec v1.4.0 @@ -9,13 +13,10 @@ require ( require ( github.com/google/go-cmp v0.6.0 // indirect + github.com/kr/pretty v0.3.1 // indirect golang.org/x/mod v0.22.0 // indirect golang.org/x/net v0.34.0 // indirect golang.org/x/sync v0.10.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) - -go 1.23 - -toolchain go1.23.1 diff --git a/go.sum b/go.sum index 33ae7a7..1788d1a 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,4 @@ +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI= @@ -6,15 +7,20 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/pprof v0.0.0-20241210010833-40e02aabc2ad h1:a6HEuzUHeKH6hwfN/ZoQgRgVIWFJljSWa/zetS2WTvg= github.com/google/pprof v0.0.0-20241210010833-40e02aabc2ad/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144= -github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/onsi/ginkgo/v2 v2.22.1 h1:QW7tbJAUDyVDVOM5dFa7qaybo+CRfR7bemlQUN6Z8aM= github.com/onsi/ginkgo/v2 v2.22.1/go.mod h1:S6aTpoRsSq2cZOd+pssHAlKW/Q/jZt6cPrPlnj4a1xM= github.com/onsi/gomega v1.36.2 h1:koNYke6TVk6ZmnyHrCXba/T/MoLBXFjeC1PtvYgw0A8= github.com/onsi/gomega v1.36.2/go.mod h1:DdwyADRjrc825LhMEkD76cHR5+pUnjhUN8GlHlRPHzY= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/sclevine/spec v1.4.0 h1:z/Q9idDcay5m5irkZ28M7PtQM4aOISzOpj4bUPkDee8= github.com/sclevine/spec v1.4.0/go.mod h1:LvpgJaFyvQzRvc1kaDs0bulYwzC70PbiYjC4QnFHkOM= golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=