1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
11 . "golang.org/x/tools/gopls/internal/regtest"
13 "golang.org/x/tools/internal/testenv"
16 func TestHoverAndDocumentLink(t *testing.T) {
17 testenv.NeedsGo1Point(t, 13)
24 require import.test v1.2.3
26 import.test v1.2.3 h1:Mu4N9BICLJFxwwn8YNg6T3frkFWW1O7evXvo0HiRjBc=
27 import.test v1.2.3/go.mod h1:KooCN1g237upRg7irU7F+3oADn5tVClU8YYW4I1xhMk=
31 import "import.test/pkg"
38 -- import.test@v1.2.3/go.mod --
42 -- import.test@v1.2.3/pkg/const.go --
49 ).Run(t, program, func(t *testing.T, env *Env) {
50 env.OpenFile("main.go")
51 env.OpenFile("go.mod")
53 modLink := "https://pkg.go.dev/mod/import.test@v1.2.3?utm_source=gopls"
54 pkgLink := "https://pkg.go.dev/import.test@v1.2.3/pkg?utm_source=gopls"
56 // First, check that we get the expected links via hover and documentLink.
57 content, _ := env.Hover("main.go", env.RegexpSearch("main.go", "pkg.Hello"))
58 if content == nil || !strings.Contains(content.Value, pkgLink) {
59 t.Errorf("hover: got %v in main.go, want contains %q", content, pkgLink)
61 content, _ = env.Hover("go.mod", env.RegexpSearch("go.mod", "import.test"))
62 if content == nil || !strings.Contains(content.Value, pkgLink) {
63 t.Errorf("hover: got %v in go.mod, want contains %q", content, pkgLink)
65 links := env.DocumentLink("main.go")
66 if len(links) != 1 || links[0].Target != pkgLink {
67 t.Errorf("documentLink: got %v for main.go, want link to %q", links, pkgLink)
69 links = env.DocumentLink("go.mod")
70 if len(links) != 1 || links[0].Target != modLink {
71 t.Errorf("documentLink: got %v for go.mod, want link to %q", links, modLink)
74 // Then change the environment to make these links private.
75 env.ChangeEnv(map[string]string{"GOPRIVATE": "import.test"})
77 // Finally, verify that the links are gone.
78 content, _ = env.Hover("main.go", env.RegexpSearch("main.go", "pkg.Hello"))
79 if content == nil || strings.Contains(content.Value, pkgLink) {
80 t.Errorf("hover: got %v in main.go, want non-empty hover without %q", content, pkgLink)
82 content, _ = env.Hover("go.mod", env.RegexpSearch("go.mod", "import.test"))
83 if content == nil || strings.Contains(content.Value, modLink) {
84 t.Errorf("hover: got %v in go.mod, want contains %q", content, modLink)
86 links = env.DocumentLink("main.go")
88 t.Errorf("documentLink: got %d document links for main.go, want 0\nlinks: %v", len(links), links)
90 links = env.DocumentLink("go.mod")
92 t.Errorf("documentLink: got %d document links for go.mod, want 0\nlinks: %v", len(links), links)