Skip to content

Commit 33689ca

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 6b2677d commit 33689ca

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

internal/testenv/testenv.go

Lines changed: 40 additions & 0 deletions
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
"github.com/apstndb/gotoolsdiff/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 github.com/apstndb/gotoolsdiff 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", "github.com/apstndb/gotoolsdiff")
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 := "github.com/apstndb/gotoolsdiff"; modulePath != want {
440+
t.Skipf("skipping test: %s module path is %q, not %q", modFilePath, modulePath, want)
441+
}
442+
}

0 commit comments

Comments
 (0)