internal/lsp/regtest: add a regtest for hover handling of GOPRIVATE

Add a regtest to verify that GOPRIVATE identifiers are not given a link
to pkg.go.dev. For efficiency, as well as to exercise dynamic
configuration, do all this in a single regtest.

Updates golang/go#36998

Change-Id: I9102a11312db5c334fdbd30cce9ca2d2e19e9ac2
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237938
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/internal/lsp/regtest/hover_test.go b/internal/lsp/regtest/hover_test.go
new file mode 100644
index 0000000..1281f4d
--- /dev/null
+++ b/internal/lsp/regtest/hover_test.go
@@ -0,0 +1,54 @@
+// Copyright 2020 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 regtest
+
+import (
+	"strings"
+	"testing"
+
+	"golang.org/x/tools/internal/lsp/fake"
+)
+
+const simpleProgram = `
+-- go.mod --
+module gopls.test
+
+go 1.12
+-- lib/lib.go --
+package lib
+
+const Hello = "Hello"
+-- main.go --
+package main
+
+import (
+	"fmt"
+	"gopls.test/lib"
+)
+
+func main() {
+	fmt.Println(lib.Hello)
+}`
+
+func TestHover(t *testing.T) {
+	runner.Run(t, simpleProgram, func(t *testing.T, env *Env) {
+		// Hover on an empty line.
+		env.OpenFile("main.go")
+		content, pos := env.Hover("main.go", fake.Pos{Line: 3, Column: 0})
+		if content != nil {
+			t.Errorf("got non-empty response for empty hover: %v: %v", pos, *content)
+		}
+		content, pos = env.Hover("main.go", env.RegexpSearch("main.go", "lib.Hello"))
+		link := "pkg.go.dev/gopls.test/lib"
+		if content == nil || !strings.Contains(content.Value, link) {
+			t.Errorf("got hover: %v, want contains %q", content, link)
+		}
+		env.ChangeEnv("GOPRIVATE=gopls.test")
+		content, pos = env.Hover("main.go", env.RegexpSearch("main.go", "lib.Hello"))
+		if content == nil || strings.Contains(content.Value, link) {
+			t.Errorf("got hover: %v, want non-empty hover without %q", content, link)
+		}
+	})
+}
diff --git a/internal/lsp/regtest/serialization_test.go b/internal/lsp/regtest/serialization_test.go
deleted file mode 100644
index 7d4eb02..0000000
--- a/internal/lsp/regtest/serialization_test.go
+++ /dev/null
@@ -1,36 +0,0 @@
-// Copyright 2020 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 regtest
-
-import (
-	"testing"
-
-	"golang.org/x/tools/internal/lsp/fake"
-)
-
-const simpleProgram = `
--- go.mod --
-module mod.com
-
-go 1.12
--- main.go --
-package main
-
-import "fmt"
-
-func main() {
-	fmt.Println("Hello World.")
-}`
-
-func TestHoverSerialization(t *testing.T) {
-	runner.Run(t, simpleProgram, func(t *testing.T, env *Env) {
-		// Hover on an empty line.
-		env.OpenFile("main.go")
-		content, pos := env.Hover("main.go", fake.Pos{Line: 3, Column: 0})
-		if content != nil {
-			t.Errorf("got non-empty response for empty hover: %v: %v", pos, *content)
-		}
-	})
-}
diff --git a/internal/lsp/regtest/wrappers.go b/internal/lsp/regtest/wrappers.go
index 276f5b1..d25b039 100644
--- a/internal/lsp/regtest/wrappers.go
+++ b/internal/lsp/regtest/wrappers.go
@@ -214,3 +214,18 @@
 	}
 	return actions
 }
+
+// ChangeEnv modifies the editor environment and reconfigures the LSP client.
+// TODO: extend this to "ChangeConfiguration", once we refactor the way editor
+// configuration is defined.
+func (e *Env) ChangeEnv(envvars ...string) {
+	e.T.Helper()
+	// TODO: to be correct, this should probably be synchronized, but right now
+	// configuration is only ever modified synchronously in a regtest, so this
+	// correctness can wait for the previously mentioned refactoring.
+	e.Editor.Config.Env = append(e.Editor.Config.Env, envvars...)
+	var params protocol.DidChangeConfigurationParams
+	if err := e.Editor.Server.DidChangeConfiguration(e.Ctx, &params); err != nil {
+		e.T.Fatal(err)
+	}
+}