File: goversion_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 22,724 kB
  • sloc: javascript: 2,027; asm: 1,645; sh: 166; yacc: 155; makefile: 49; ansic: 8
file content (126 lines) | stat: -rw-r--r-- 3,230 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package workspace

import (
	"flag"
	"os"
	"os/exec"
	"runtime"
	"strings"
	"testing"

	. "golang.org/x/tools/gopls/internal/test/integration"
	"golang.org/x/tools/internal/testenv"
)

var go121bin = flag.String("go121bin", "", "bin directory containing go 1.21 or later")

// TODO(golang/go#65917): delete this test once we no longer support building
// gopls with older Go versions.
func TestCanHandlePatchVersions(t *testing.T) {
	// This test verifies the fixes for golang/go#66195 and golang/go#66636 --
	// that gopls does not crash when encountering a go version with a patch
	// number in the go.mod file.
	//
	// This is tricky to test, because the regression requires that gopls is
	// built with an older go version, and then the environment is upgraded to
	// have a more recent go. To set up this scenario, the test requires a path
	// to a bin directory containing go1.21 or later.
	if *go121bin == "" {
		t.Skip("-go121bin directory is not set")
	}

	if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
		t.Skip("requires linux or darwin") // for PATH separator
	}

	path := os.Getenv("PATH")
	t.Setenv("PATH", *go121bin+":"+path)

	const files = `
-- go.mod --
module example.com/bar

go 1.21.1

-- p.go --
package bar

type I interface { string }
`

	WithOptions(
		EnvVars{
			"PATH": path,
		},
	).Run(t, files, func(t *testing.T, env *Env) {
		env.OpenFile("p.go")
		env.AfterChange(
			NoDiagnostics(ForFile("p.go")),
		)
	})
}

func TestTypeCheckingFutureVersions(t *testing.T) {
	// This test checks the regression in golang/go#66677, where go/types fails
	// silently when the language version is 1.22.
	//
	// It does this by recreating the scenario of a toolchain upgrade to 1.22, as
	// reported in the issue. For this to work, the test must be able to download
	// toolchains from proxy.golang.org.
	//
	// This is really only a problem for Go 1.21, because with Go 1.23, the bug
	// is fixed, and starting with 1.23 we're going to *require* 1.23 to build
	// gopls.
	//
	// TODO(golang/go#65917): delete this test after Go 1.23 is released and
	// gopls requires the latest Go to build.
	testenv.SkipAfterGo1Point(t, 21)

	if testing.Short() {
		t.Skip("skipping with -short, as this test uses the network")
	}

	// If go 1.22.2 is already available in the module cache, reuse it rather
	// than downloading it anew.
	out, err := exec.Command("go", "env", "GOPATH").Output()
	if err != nil {
		t.Fatal(err)
	}
	gopath := strings.TrimSpace(string(out)) // use the ambient 1.22.2 toolchain if available

	const files = `
-- go.mod --
module example.com/foo

go 1.22.2

-- main.go --
package main

func main() {
	x := 1
}
`

	WithOptions(
		Modes(Default), // slow test, only run in one mode
		EnvVars{
			"GOPATH":      gopath,
			"GOTOOLCHAIN": "", // not local
			"GOPROXY":     "https://proxy.golang.org",
			"GOSUMDB":     "sum.golang.org",
		},
	).Run(t, files, func(t *testing.T, env *Env) {
		env.OpenFile("main.go")
		env.AfterChange(
			Diagnostics(
				env.AtRegexp("main.go", "x"),
				WithMessage("not used"),
			),
		)
	})
}