Skip to content

Commit dcccb2d

Browse files
adonovangopherbot
authored andcommitted
x/tools: make tests agnostic as to whether gotypesalias="" => 0 or 1
This is required temporarily as we flip the default. Updates golang/go#65294 Change-Id: I552e40475cc48b949e2307af347ca98a428c55ea Reviewed-on: https://go-review.googlesource.com/c/tools/+/578041 Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Alan Donovan <[email protected]>
1 parent 46a0401 commit dcccb2d

File tree

5 files changed

+33
-18
lines changed

5 files changed

+33
-18
lines changed

gopls/internal/analysis/fillreturns/fillreturns_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@
55
package fillreturns_test
66

77
import (
8-
"os"
9-
"strings"
108
"testing"
119

1210
"golang.org/x/tools/go/analysis/analysistest"
1311
"golang.org/x/tools/gopls/internal/analysis/fillreturns"
12+
"golang.org/x/tools/internal/testenv"
1413
)
1514

1615
func Test(t *testing.T) {
17-
// TODO(golang/go#65294): delete once gotypesalias=1 is the default.
18-
if strings.Contains(os.Getenv("GODEBUG"), "gotypesalias=1") {
19-
t.Skip("skipping due to gotypesalias=1, which changes (improves) the result; reenable and update the expectations once it is the default")
20-
}
16+
// TODO(golang/go#65294): delete (and update expectations)
17+
// once gotypesalias=1 is the default.
18+
testenv.SkipMaterializedAliases(t, "expectations need updating for materialized aliases")
2119

2220
testdata := analysistest.TestData()
2321
analysistest.RunWithSuggestedFixes(t, testdata, fillreturns.Analyzer, "a", "typeparams")

gopls/internal/test/integration/misc/staticcheck_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
package misc
66

77
import (
8-
"os"
9-
"strings"
108
"testing"
119

1210
"golang.org/x/tools/internal/testenv"
@@ -18,9 +16,7 @@ func TestStaticcheckGenerics(t *testing.T) {
1816
testenv.NeedsGo1Point(t, 20) // staticcheck requires go1.20+
1917

2018
// TODO(golang/go#65249): re-enable and fix this test with gotypesalias=1.
21-
if strings.Contains(os.Getenv("GODEBUG"), "gotypesalias=1") {
22-
t.Skipf("staticcheck needs updates for materialized aliases")
23-
}
19+
testenv.SkipMaterializedAliases(t, "staticcheck needs updates for materialized aliases")
2420

2521
const files = `
2622
-- go.mod --
@@ -88,9 +84,7 @@ func TestStaticcheckRelatedInfo(t *testing.T) {
8884
testenv.NeedsGo1Point(t, 20) // staticcheck is only supported at Go 1.20+
8985

9086
// TODO(golang/go#65249): re-enable and fix this test with gotypesalias=1.
91-
if strings.Contains(os.Getenv("GODEBUG"), "gotypesalias=1") {
92-
t.Skipf("staticcheck needs updates for materialized aliases")
93-
}
87+
testenv.SkipMaterializedAliases(t, "staticcheck needs updates for materialized aliases")
9488

9589
const files = `
9690
-- go.mod --

internal/aliases/aliases.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
// NewAlias creates a new TypeName in Package pkg that
1717
// is an alias for the type rhs.
1818
//
19-
// When GoVersion>=1.22 and GODEBUG=gotypesalias=1,
19+
// When GoVersion>=1.22 and GODEBUG=gotypesalias=1 (or unset),
2020
// the Type() of the return value is a *types.Alias.
2121
func NewAlias(pos token.Pos, pkg *types.Package, name string, rhs types.Type) *types.TypeName {
2222
if enabled() {

internal/aliases/aliases_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var _ func(*aliases.Alias) *types.TypeName = (*aliases.Alias).Obj
2020

2121
// TestNewAlias tests that alias.NewAlias creates an alias of a type
2222
// whose underlying and Unaliased type is *Named.
23-
// When gotypesalias=1 and GoVersion >= 1.22, the type will
23+
// When gotypesalias=1 (or unset) and GoVersion >= 1.22, the type will
2424
// be an *aliases.Alias.
2525
func TestNewAlias(t *testing.T) {
2626
const source = `
@@ -46,7 +46,10 @@ func TestNewAlias(t *testing.T) {
4646
t.Fatalf("Eval(%s) failed: %v", expr, err)
4747
}
4848

49-
for _, godebug := range []string{"", "gotypesalias=1"} {
49+
for _, godebug := range []string{
50+
// "", // The default is in transition; suppress this case for now
51+
"gotypesalias=0",
52+
"gotypesalias=1"} {
5053
t.Run(godebug, func(t *testing.T) {
5154
t.Setenv("GODEBUG", godebug)
5255

@@ -62,7 +65,7 @@ func TestNewAlias(t *testing.T) {
6265
t.Errorf("Expected Unalias(A)==%q. got %q", want, got)
6366
}
6467

65-
if testenv.Go1Point() >= 22 && godebug == "gotypesalias=1" {
68+
if testenv.Go1Point() >= 22 && godebug != "gotypesalias=0" {
6669
if _, ok := A.Type().(*aliases.Alias); !ok {
6770
t.Errorf("Expected A.Type() to be a types.Alias(). got %q", A.Type())
6871
}

internal/testenv/exec.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"reflect"
1313
"runtime"
1414
"strconv"
15+
"strings"
1516
"sync"
1617
"testing"
1718
"time"
@@ -190,3 +191,22 @@ func Command(t testing.TB, name string, args ...string) *exec.Cmd {
190191
t.Helper()
191192
return CommandContext(t, context.Background(), name, args...)
192193
}
194+
195+
// SkipMaterializedAliases skips the test if go/types would create
196+
// instances of types.Alias, which some tests do not yet handle
197+
// correctly.
198+
func SkipMaterializedAliases(t *testing.T, message string) {
199+
if hasMaterializedAliases(Go1Point()) {
200+
t.Skipf("%s", message)
201+
}
202+
}
203+
204+
func hasMaterializedAliases(minor int) bool {
205+
if minor >= 23 && !strings.Contains(os.Getenv("GODEBUG"), "gotypesalias=0") {
206+
return true // gotypesalias=1 became the default in go1.23
207+
}
208+
if minor == 22 && strings.Contains(os.Getenv("GODEBUG"), "gotypesalias=1") {
209+
return true // gotypesalias=0 was the default in go1.22
210+
}
211+
return false // types.Alias didn't exist in go1.21
212+
}

0 commit comments

Comments
 (0)