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/internal/testenv"
14 func TestHoverAndDocumentLink(t *testing.T) {
15 testenv.NeedsGo1Point(t, 13)
22 require import.test v1.2.3
26 import "import.test/pkg"
33 -- import.test@v1.2.3/go.mod --
37 -- import.test@v1.2.3/pkg/const.go --
42 runner.Run(t, program, func(t *testing.T, env *Env) {
43 env.OpenFile("main.go")
44 env.OpenFile("go.mod")
46 modLink := "https://pkg.go.dev/mod/import.test@v1.2.3"
47 pkgLink := "https://pkg.go.dev/import.test@v1.2.3/pkg"
49 // First, check that we get the expected links via hover and documentLink.
50 content, _ := env.Hover("main.go", env.RegexpSearch("main.go", "pkg.Hello"))
51 if content == nil || !strings.Contains(content.Value, pkgLink) {
52 t.Errorf("hover: got %v in main.go, want contains %q", content, pkgLink)
54 content, _ = env.Hover("go.mod", env.RegexpSearch("go.mod", "import.test"))
55 if content == nil || !strings.Contains(content.Value, pkgLink) {
56 t.Errorf("hover: got %v in go.mod, want contains %q", content, pkgLink)
58 links := env.DocumentLink("main.go")
59 if len(links) != 1 || links[0].Target != pkgLink {
60 t.Errorf("documentLink: got %v for main.go, want link to %q", links, pkgLink)
62 links = env.DocumentLink("go.mod")
63 if len(links) != 1 || links[0].Target != modLink {
64 t.Errorf("documentLink: got %v for go.mod, want link to %q", links, modLink)
67 // Then change the environment to make these links private.
68 env.ChangeEnv(map[string]string{"GOPRIVATE": "import.test"})
70 // Finally, verify that the links are gone.
71 content, _ = env.Hover("main.go", env.RegexpSearch("main.go", "pkg.Hello"))
72 if content == nil || strings.Contains(content.Value, pkgLink) {
73 t.Errorf("hover: got %v in main.go, want non-empty hover without %q", content, pkgLink)
75 content, _ = env.Hover("go.mod", env.RegexpSearch("go.mod", "import.test"))
76 if content == nil || strings.Contains(content.Value, modLink) {
77 t.Errorf("hover: got %v in go.mod, want contains %q", content, modLink)
79 links = env.DocumentLink("main.go")
81 t.Errorf("documentLink: got %d document links for main.go, want 0\nlinks: %v", len(links), links)
83 links = env.DocumentLink("go.mod")
85 t.Errorf("documentLink: got %d document links for go.mod, want 0\nlinks: %v", len(links), links)
87 }, WithProxyFiles(proxy))