Skip to content

Commit 2415ce1

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
gopls: skip tests that load gopls packages if x/tools replacement is missing
The gopls go.mod file often contains a replace directive, but the target of the replacement (a parent directory) is not present when the test is run from the module cache or in an equivalent setting. To avoid having more configurations to test and maintain, we skip these tests if the replacement directory does not exist or does not contain a plausible x/tools go.mod file. Fixes golang/go#59841. Change-Id: Icf86af46899686c3aae410250e6d26ffd11b429a Reviewed-on: https://go-review.googlesource.com/c/tools/+/489216 Run-TryBot: Bryan Mills <[email protected]> Auto-Submit: Bryan Mills <[email protected]> Reviewed-by: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 17e5ef3 commit 2415ce1

File tree

5 files changed

+68
-6
lines changed

5 files changed

+68
-6
lines changed

gopls/doc/generate_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ import (
1111
)
1212

1313
func TestGenerated(t *testing.T) {
14+
testenv.NeedsGoPackages(t)
1415
// This test fails on 1.18 Kokoro for unknown reasons; in any case, it
1516
// suffices to run this test on any builder.
1617
testenv.NeedsGo1Point(t, 19)
17-
testenv.NeedsGoBuild(t) // This is a lie. We actually need the source code.
18+
19+
testenv.NeedsLocalXTools(t)
1820

1921
ok, err := doMain(false)
2022
if err != nil {

gopls/internal/lsp/command/interface_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import (
1414
)
1515

1616
func TestGenerated(t *testing.T) {
17-
testenv.NeedsGoBuild(t) // This is a lie. We actually need the source code.
17+
testenv.NeedsGoPackages(t)
18+
testenv.NeedsLocalXTools(t)
1819

1920
onDisk, err := ioutil.ReadFile("command_gen.go")
2021
if err != nil {

gopls/internal/lsp/safetoken/safetoken_test.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,19 @@ func TestWorkaroundIssue57490(t *testing.T) {
7272
// suggests alternatives.
7373
func TestGoplsSourceDoesNotCallTokenFileMethods(t *testing.T) {
7474
testenv.NeedsGoPackages(t)
75+
testenv.NeedsGo1Point(t, 18)
76+
testenv.NeedsLocalXTools(t)
7577

76-
pkgs, err := packages.Load(&packages.Config{
78+
cfg := &packages.Config{
7779
Mode: packages.NeedName | packages.NeedModule | packages.NeedCompiledGoFiles | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedSyntax | packages.NeedImports | packages.NeedDeps,
78-
}, "go/token", "golang.org/x/tools/gopls/...")
80+
}
81+
cfg.Env = os.Environ()
82+
cfg.Env = append(cfg.Env,
83+
"GOPACKAGESDRIVER=off",
84+
"GOFLAGS=-mod=mod",
85+
)
86+
87+
pkgs, err := packages.Load(cfg, "go/token", "golang.org/x/tools/gopls/...")
7988
if err != nil {
8089
t.Fatal(err)
8190
}

gopls/test/debug/debug_test.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ package debug_test
1313
import (
1414
"go/ast"
1515
"html/template"
16+
"os"
1617
"runtime"
1718
"sort"
1819
"strings"
@@ -44,11 +45,18 @@ var templates = map[string]struct {
4445
}
4546

4647
func TestTemplates(t *testing.T) {
47-
testenv.NeedsGoBuild(t)
48+
testenv.NeedsGoPackages(t)
49+
testenv.NeedsLocalXTools(t)
4850

4951
cfg := &packages.Config{
50-
Mode: packages.NeedTypesInfo | packages.LoadAllSyntax, // figure out what's necessary PJW
52+
Mode: packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo,
5153
}
54+
cfg.Env = os.Environ()
55+
cfg.Env = append(cfg.Env,
56+
"GOPACKAGESDRIVER=off",
57+
"GOFLAGS=-mod=mod",
58+
)
59+
5260
pkgs, err := packages.Load(cfg, "golang.org/x/tools/gopls/internal/lsp/debug")
5361
if err != nil {
5462
t.Fatal(err)
@@ -107,7 +115,9 @@ func TestTemplates(t *testing.T) {
107115
// the FuncMap is an annoyance; should not be necessary
108116
if err := templatecheck.CheckHTML(v.tmpl, v.data); err != nil {
109117
t.Errorf("%s: %v", k, err)
118+
continue
110119
}
120+
t.Logf("%s ok", k)
111121
}
112122
}
113123

internal/testenv/testenv.go

+40
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ import (
1212
"go/build"
1313
"io/ioutil"
1414
"os"
15+
"path/filepath"
1516
"runtime"
1617
"runtime/debug"
1718
"strings"
1819
"sync"
1920
"testing"
2021
"time"
2122

23+
"golang.org/x/mod/modfile"
2224
"golang.org/x/tools/internal/goroot"
2325

2426
exec "golang.org/x/sys/execabs"
@@ -400,3 +402,41 @@ func GOROOT(t testing.TB) string {
400402
}
401403
return path
402404
}
405+
406+
// NeedsLocalXTools skips t if the golang.org/x/tools module is replaced and
407+
// its replacement directory does not exist (or does not contain the module).
408+
func NeedsLocalXTools(t testing.TB) {
409+
t.Helper()
410+
411+
NeedsTool(t, "go")
412+
413+
cmd := Command(t, "go", "list", "-f", "{{with .Replace}}{{.Dir}}{{end}}", "-m", "golang.org/x/tools")
414+
out, err := cmd.Output()
415+
if err != nil {
416+
if ee, ok := err.(*exec.ExitError); ok && len(ee.Stderr) > 0 {
417+
t.Skipf("skipping test: %v: %v\n%s", cmd, err, ee.Stderr)
418+
}
419+
t.Skipf("skipping test: %v: %v", cmd, err)
420+
}
421+
422+
dir := string(bytes.TrimSpace(out))
423+
if dir == "" {
424+
// No replacement directory, and (since we didn't set -e) no error either.
425+
// Maybe x/tools isn't replaced at all (as in a gopls release, or when
426+
// using a go.work file that includes the x/tools module).
427+
return
428+
}
429+
430+
// We found the directory where x/tools would exist if we're in a clone of the
431+
// repo. Is it there? (If not, we're probably in the module cache instead.)
432+
modFilePath := filepath.Join(dir, "go.mod")
433+
b, err := os.ReadFile(modFilePath)
434+
if err != nil {
435+
t.Skipf("skipping test: x/tools replacement not found: %v", err)
436+
}
437+
modulePath := modfile.ModulePath(b)
438+
439+
if want := "golang.org/x/tools"; modulePath != want {
440+
t.Skipf("skipping test: %s module path is %q, not %q", modFilePath, modulePath, want)
441+
}
442+
}

0 commit comments

Comments
 (0)