Debian Codegraph @

gopls/internal/test/integration/misc/link_test.go

raw symbols SCIP
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.
4
5 package misc
6
7 import (
8 "path/filepath"
9 "slices"
10 "strings"
11 "testing"
12
13 "golang.org/x/tools/gopls/internal/protocol"
14 . "golang.org/x/tools/gopls/internal/test/integration"
15 )
16
17 func TestHoverAndDocumentLink(t *testing.T) {
18 const program = `
19 -- go.mod --
20 module mod.test
21
22 go 1.12
23
24 require import.test v1.2.3
25
26 require replace.test v1.2.3
27 replace replace.test => replace.test v1.2.4
28
29 require replace.fixed.test v1.2.3
30 replace replace.fixed.test v1.2.3 => replace.fixed.test v1.2.4
31
32 require replace.another.test v1.2.3
33 replace replace.another.test => another.test v1.2.3
34
35
36 replace example.com/non-exist => ./
37 replace example.com/non-exist1 => ../work/
38
39 -- main.go --
40 package main
41
42 import "import.test/pkg"
43 import "replace.test/replace"
44 import "replace.fixed.test/fixed"
45 import "replace.another.test/another"
46
47 func main() {
48 // Issue 43990: this is not a link that most users can open from an LSP
49 // client: mongodb://not.a.link.com
50 println(pkg.Hello)
51 println(replace.Hello)
52 println(fixed.Hello)
53 println(another.Hello)
54 }`
55
56 const proxy = `
57 -- [email protected]/go.mod --
58 module import.test
59
60 go 1.12
61 -- [email protected]/pkg/const.go --
62 // package documentation
63 package pkg
64
65
66 -- [email protected]/go.mod --
67 module replace.test
68
69 go 1.12
70 -- [email protected]/replace/const.go --
71 package replace
72
73 const Hello = "Hello"
74
75 -- [email protected]/go.mod --
76 module replace.fixed.test
77
78 go 1.12
79 -- [email protected]/fixed/const.go --
80 package fixed
81
82 const Hello = "Hello"
83
84 -- [email protected]/go.mod --
85 module another.test
86
87 go 1.12
88 -- [email protected]/another/const.go --
89 package another
90
91 const Hello = "Hello"
92 `
93 WithOptions(
94 ProxyFiles(proxy),
95 WriteGoSum("."),
96 ).Run(t, program, func(t *testing.T, env *Env) {
97 env.OpenFile("main.go")
98 env.OpenFile("go.mod")
99
100 const (
101 modImportLink = "https://pkg.go.dev/mod/[email protected]"
102 modReplaceLink = "https://pkg.go.dev/mod/[email protected]"
103 modReplaceFixedeLink = "https://pkg.go.dev/mod/[email protected]"
104 modAnotherLink = "https://pkg.go.dev/mod/[email protected]"
105
106 pkgImportLink = "https://pkg.go.dev/[email protected]/pkg"
107 pkgReplaceLink = "https://pkg.go.dev/[email protected]/replace"
108 pkgReplaceFixedLink = "https://pkg.go.dev/[email protected]/fixed"
109 pkgAnotherLink = "https://pkg.go.dev/[email protected]/another"
110 pkgDoc = "package documentation"
111 )
112
113 // First, check that we get the expected links via hover and documentLink.
114 content, _ := env.Hover(env.RegexpSearch("main.go", "()pkg.Hello"))
115 if content == nil || !strings.Contains(content.Value, pkgImportLink) {
116 t.Errorf("hover: got %v in main.go, want contains %q", content, pkgImportLink)
117 }
118 content, _ = env.Hover(env.RegexpSearch("main.go", "()replace.Hello"))
119 if content == nil || !strings.Contains(content.Value, pkgReplaceLink) {
120 t.Errorf("hover: got %v in main.go, want contains %q", content, pkgReplaceLink)
121 }
122 content, _ = env.Hover(env.RegexpSearch("main.go", "()fixed.Hello"))
123 if content == nil || !strings.Contains(content.Value, pkgReplaceFixedLink) {
124 t.Errorf("hover: got %v in main.go, want contains %q", content, pkgReplaceFixedLink)
125 }
126 content, _ = env.Hover(env.RegexpSearch("main.go", "()another.Hello"))
127 if content == nil || !strings.Contains(content.Value, pkgAnotherLink) {
128 t.Errorf("hover: got %v in main.go, want contains %q", content, pkgAnotherLink)
129 }
130
131 content, _ = env.Hover(env.RegexpSearch("go.mod", "()import.test"))
132 if content == nil || !strings.Contains(content.Value, pkgImportLink) {
133 t.Errorf("hover: got %v in main.go, want contains %q", content, pkgImportLink)
134 }
135 content, _ = env.Hover(env.RegexpSearch("go.mod", "()replace.test"))
136 if content == nil || !strings.Contains(content.Value, pkgReplaceLink) {
137 t.Errorf("hover: got %v in main.go, want contains %q", content, pkgReplaceLink)
138 }
139 content, _ = env.Hover(env.RegexpSearch("go.mod", "()replace.fixed.test"))
140 if content == nil || !strings.Contains(content.Value, pkgReplaceFixedLink) {
141 t.Errorf("hover: got %v in main.go, want contains %q", content, pkgReplaceFixedLink)
142 }
143 content, _ = env.Hover(env.RegexpSearch("go.mod", "()replace.another.test"))
144 if content == nil || !strings.Contains(content.Value, pkgAnotherLink) {
145 t.Errorf("hover: got %v in main.go, want contains %q", content, pkgAnotherLink)
146 }
147
148 getLinks := func(links []protocol.DocumentLink) []string {
149 var got []string
150 for i := range links {
151 got = append(got, *links[i].Target)
152 }
153 return got
154 }
155 links := env.DocumentLink("main.go")
156 got, want := getLinks(links), []string{
157 pkgImportLink,
158 pkgReplaceLink,
159 pkgReplaceFixedLink,
160 pkgAnotherLink,
161 }
162 if !slices.Equal(got, want) {
163 t.Errorf("documentLink: got links %v for main.go, want links %v", got, want)
164 }
165
166 links = env.DocumentLink("go.mod")
167 localReplacePath := filepath.Join(env.Sandbox.Workdir.RootURI().Path(), "go.mod")
168 got, want = getLinks(links), []string{
169 localReplacePath, localReplacePath,
170 modImportLink,
171 modReplaceLink,
172 modReplaceFixedeLink,
173 modAnotherLink,
174 }
175 if !slices.Equal(got, want) {
176 t.Errorf("documentLink: got links %v for go.mod, want links %v", got, want)
177 }
178
179 // Then change the environment to make these links private.
180 cfg := env.Editor.Config()
181 cfg.Env = map[string]string{"GOPRIVATE": "import.test"}
182 env.ChangeConfiguration(cfg)
183
184 // Finally, verify that the links are gone.
185 content, _ = env.Hover(env.RegexpSearch("main.go", "()pkg.Hello"))
186 if content == nil || strings.Contains(content.Value, pkgImportLink) {
187 t.Errorf("hover: got %v in main.go, want non-empty hover without %q", content, pkgImportLink)
188 }
189 content, _ = env.Hover(env.RegexpSearch("go.mod", "()import.test"))
190 if content == nil || strings.Contains(content.Value, modImportLink) {
191 t.Errorf("hover: got %v in go.mod, want contains %q", content, modImportLink)
192 }
193
194 links = env.DocumentLink("main.go")
195 got, want = getLinks(links), []string{
196 pkgReplaceLink,
197 pkgReplaceFixedLink,
198 pkgAnotherLink,
199 }
200 if !slices.Equal(got, want) {
201 t.Errorf("documentLink: got links %v for main.go, want links %v", got, want)
202 }
203
204 links = env.DocumentLink("go.mod")
205 got, want = getLinks(links), []string{
206 localReplacePath, localReplacePath,
207 modReplaceLink,
208 modReplaceFixedeLink,
209 modAnotherLink,
210 }
211 if !slices.Equal(got, want) {
212 t.Errorf("documentLink: got links %v for go.mod, want links %v", got, want)
213 }
214 })
215 }