Skip to content

Commit 8e7acdb

Browse files
committed
all: replace build tags in tests with testenv helper
Many tool features, particularly modules-related, require particular Go versions. Build tags are unwieldy, requiring one-off test files which break up test organization. Add a suite of testenv functions that check what Go version is in use. Note that this is the logical Go version, as denoted by the release tags; it should be updated at the beginning of the release cycle per issue golang/go#38704. For ease of reviewing, I'll merge/delete files in a followup CL. Change-Id: Id85ce0f83387b3c45d68465161cf88447325d4f2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/234882 Run-TryBot: Heschi Kreinick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 7527cb2 commit 8e7acdb

15 files changed

+80
-107
lines changed

go/internal/gcimporter/gcimporter11_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build go1.11
6-
75
package gcimporter
86

97
import (
108
"go/types"
119
"runtime"
1210
"strings"
1311
"testing"
12+
13+
"golang.org/x/tools/internal/testenv"
1414
)
1515

1616
var importedObjectTests = []struct {
@@ -38,6 +38,7 @@ var importedObjectTests = []struct {
3838
}
3939

4040
func TestImportedTypes(t *testing.T) {
41+
testenv.NeedsGo1Point(t, 11)
4142
skipSpecialPlatforms(t)
4243

4344
// This package only handles gc export data.
@@ -112,6 +113,7 @@ func verifyInterfaceMethodRecvs(t *testing.T, named *types.Named, level int) {
112113
}
113114
}
114115
func TestIssue25301(t *testing.T) {
116+
testenv.NeedsGo1Point(t, 11)
115117
skipSpecialPlatforms(t)
116118

117119
// This package only handles gc export data.

go/packages/packages110_test.go

-11
This file was deleted.

go/packages/packages114_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build go1.14
6-
75
package packages_test
86

97
import (
@@ -13,13 +11,14 @@ import (
1311

1412
"golang.org/x/tools/go/packages"
1513
"golang.org/x/tools/go/packages/packagestest"
14+
"golang.org/x/tools/internal/testenv"
1615
)
1716

1817
// These tests check fixes that are only available in Go 1.14.
19-
// They can be moved into packages_test.go when we no longer support 1.13.
2018
// See golang/go#35973 for more information.
2119
func TestInvalidFilesInOverlay(t *testing.T) { packagestest.TestAll(t, testInvalidFilesInOverlay) }
2220
func testInvalidFilesInOverlay(t *testing.T, exporter packagestest.Exporter) {
21+
testenv.NeedsGo1Point(t, 14)
2322
exported := packagestest.Export(t, exporter, []packagestest.Module{
2423
{
2524
Name: "golang.org/fake",

go/packages/packages115_test.go

+4-9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build go1.15
6-
75
package packages_test
86

97
import (
@@ -16,10 +14,9 @@ import (
1614
)
1715

1816
// TestInvalidFilesInXTest checks the fix for golang/go#37971.
19-
func TestInvalidFilesInXTest(t *testing.T) {
20-
packagestest.TestAll(t, testInvalidFilesInXTest)
21-
}
17+
func TestInvalidFilesInXTest(t *testing.T) { packagestest.TestAll(t, testInvalidFilesInXTest) }
2218
func testInvalidFilesInXTest(t *testing.T, exporter packagestest.Exporter) {
19+
testenv.NeedsGo1Point(t, 15)
2320
exported := packagestest.Export(t, exporter, []packagestest.Module{
2421
{
2522
Name: "golang.org/fake",
@@ -44,11 +41,9 @@ func testInvalidFilesInXTest(t *testing.T, exporter packagestest.Exporter) {
4441
}
4542
}
4643

47-
func TestTypecheckCgo(t *testing.T) {
48-
packagestest.TestAll(t, testTypecheckCgo)
49-
}
50-
44+
func TestTypecheckCgo(t *testing.T) { packagestest.TestAll(t, testTypecheckCgo) }
5145
func testTypecheckCgo(t *testing.T, exporter packagestest.Exporter) {
46+
testenv.NeedsGo1Point(t, 15)
5247
testenv.NeedsTool(t, "cgo")
5348

5449
const cgo = `package cgo

go/packages/packagestest/modules_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build go1.11
6-
75
package packagestest_test
86

97
import (
108
"path/filepath"
119
"testing"
1210

1311
"golang.org/x/tools/go/packages/packagestest"
12+
"golang.org/x/tools/internal/testenv"
1413
)
1514

1615
func TestModulesExport(t *testing.T) {
16+
testenv.NeedsGo1Point(t, 11)
1717
exported := packagestest.Export(t, packagestest.Modules, testdata)
1818
defer exported.Cleanup()
1919
// Check that the cfg contains all the right bits

internal/imports/mod_112_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
// +build go1.12
2-
31
package imports
42

53
import (
64
"context"
75
"testing"
6+
7+
"golang.org/x/tools/internal/testenv"
88
)
99

1010
// Tests that we handle GO111MODULE=on with no go.mod file. See #30855.
1111
func TestNoMainModule(t *testing.T) {
12+
testenv.NeedsGo1Point(t, 12)
1213
mt := setup(t, `
1314
-- x.go --
1415
package x

internal/imports/mod_114_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
// +build go1.14
2-
31
package imports
42

53
import (
64
"testing"
5+
6+
"golang.org/x/tools/internal/testenv"
77
)
88

99
func TestModVendorAuto_114(t *testing.T) {
10+
testenv.NeedsGo1Point(t, 14)
1011
testModVendorAuto(t, true)
1112
}

internal/imports/mod_pre114_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
// +build !go1.14
2-
31
package imports
42

53
import (
64
"testing"
5+
6+
"golang.org/x/tools/internal/testenv"
77
)
88

99
func TestModVendorAuto_Pre114(t *testing.T) {
10+
testenv.SkipAfterGo1Point(t, 13)
1011
testModVendorAuto(t, false)
1112
}

internal/imports/mod_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build go1.11
2-
31
package imports
42

53
import (
@@ -21,6 +19,7 @@ import (
2119
"golang.org/x/mod/module"
2220
"golang.org/x/tools/internal/gocommand"
2321
"golang.org/x/tools/internal/gopathwalk"
22+
"golang.org/x/tools/internal/proxydir"
2423
"golang.org/x/tools/internal/testenv"
2524
"golang.org/x/tools/txtar"
2625
)
@@ -647,6 +646,7 @@ type modTest struct {
647646
// in testdata/mod, along the lines of TestScript in cmd/go.
648647
func setup(t *testing.T, main, wd string) *modTest {
649648
t.Helper()
649+
testenv.NeedsGo1Point(t, 11)
650650
testenv.NeedsTool(t, "go")
651651

652652
proxyOnce.Do(func() {
@@ -674,7 +674,7 @@ func setup(t *testing.T, main, wd string) *modTest {
674674
GOROOT: build.Default.GOROOT,
675675
GOPATH: filepath.Join(dir, "gopath"),
676676
GO111MODULE: "on",
677-
GOPROXY: proxyDirToURL(proxyDir),
677+
GOPROXY: proxydir.ToURL(proxyDir),
678678
GOSUMDB: "off",
679679
WorkingDir: filepath.Join(mainDir, wd),
680680
GocmdRunner: &gocommand.Runner{},
@@ -834,6 +834,7 @@ import _ "rsc.io/quote"
834834

835835
// Tests that crud in the module cache is ignored.
836836
func TestInvalidModCache(t *testing.T) {
837+
testenv.NeedsGo1Point(t, 11)
837838
dir, err := ioutil.TempDir("", t.Name())
838839
if err != nil {
839840
t.Fatal(err)
@@ -920,6 +921,7 @@ import _ "rsc.io/quote"
920921
}
921922

922923
func BenchmarkScanModCache(b *testing.B) {
924+
testenv.NeedsGo1Point(b, 11)
923925
env := &ProcessEnv{
924926
GOPATH: build.Default.GOPATH,
925927
GOROOT: build.Default.GOROOT,

internal/imports/proxy_112_test.go

-20
This file was deleted.

internal/imports/proxy_113_test.go

-24
This file was deleted.

internal/lsp/regtest/diagnostics_114_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//+build go1.14
6-
75
package regtest
86

97
import (
@@ -12,6 +10,7 @@ import (
1210
"golang.org/x/tools/internal/lsp"
1311
"golang.org/x/tools/internal/lsp/fake"
1412
"golang.org/x/tools/internal/lsp/protocol"
13+
"golang.org/x/tools/internal/testenv"
1514
)
1615

1716
const ardanLabsProxy = `
@@ -28,6 +27,7 @@ var ErrHelpWanted error
2827
// -modfile flag that is used to provide modfile diagnostics is only available
2928
// with 1.14.
3029
func Test_Issue38211(t *testing.T) {
30+
testenv.NeedsGo1Point(t, 14)
3131
const ardanLabs = `
3232
-- go.mod --
3333
module mod.com
@@ -91,6 +91,7 @@ func main() {
9191

9292
// Test for golang/go#38207.
9393
func TestNewModule_Issue38207(t *testing.T) {
94+
testenv.NeedsGo1Point(t, 14)
9495
const emptyFile = `
9596
-- go.mod --
9697
module mod.com
@@ -125,6 +126,7 @@ func main() {
125126
}
126127

127128
func TestNewFileBadImports_Issue36960(t *testing.T) {
129+
testenv.NeedsGo1Point(t, 14)
128130
const simplePackage = `
129131
-- go.mod --
130132
module mod.com

internal/proxydir/proxydir_112.go

-18
This file was deleted.

internal/proxydir/proxydir_113.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build go1.13
2-
31
// Copyright 2020 The Go Authors. All rights reserved.
42
// Use of this source code is governed by a BSD-style
53
// license that can be found in the LICENSE file.
@@ -9,14 +7,24 @@ package proxydir
97
import (
108
"path/filepath"
119
"strings"
10+
11+
"golang.org/x/tools/internal/testenv"
1212
)
1313

1414
// ToURL returns the file uri for a proxy directory.
1515
func ToURL(dir string) string {
16-
// file URLs on Windows must start with file:///. See golang.org/issue/6027.
17-
path := filepath.ToSlash(dir)
18-
if !strings.HasPrefix(path, "/") {
19-
path = "/" + path
16+
if testenv.Go1Point() >= 13 {
17+
// file URLs on Windows must start with file:///. See golang.org/issue/6027.
18+
path := filepath.ToSlash(dir)
19+
if !strings.HasPrefix(path, "/") {
20+
path = "/" + path
21+
}
22+
return "file://" + path
23+
} else {
24+
// Prior to go1.13, the Go command on Windows only accepted GOPROXY file URLs
25+
// of the form file://C:/path/to/proxy. This was incorrect: when parsed, "C:"
26+
// is interpreted as the host. See golang.org/issue/6027. This has been
27+
// fixed in go1.13, but we emit the old format for old releases.
28+
return "file://" + filepath.ToSlash(dir)
2029
}
21-
return "file://" + path
2230
}

0 commit comments

Comments
 (0)