golang.org/x/tools/gopls@v0.15.3/internal/test/integration/misc/link_test.go (about) 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 "strings" 9 "testing" 10 11 . "golang.org/x/tools/gopls/internal/test/integration" 12 ) 13 14 func TestHoverAndDocumentLink(t *testing.T) { 15 const program = ` 16 -- go.mod -- 17 module mod.test 18 19 go 1.12 20 21 require import.test v1.2.3 22 -- go.sum -- 23 import.test v1.2.3 h1:Mu4N9BICLJFxwwn8YNg6T3frkFWW1O7evXvo0HiRjBc= 24 import.test v1.2.3/go.mod h1:KooCN1g237upRg7irU7F+3oADn5tVClU8YYW4I1xhMk= 25 -- main.go -- 26 package main 27 28 import "import.test/pkg" 29 30 func main() { 31 // Issue 43990: this is not a link that most users can open from an LSP 32 // client: mongodb://not.a.link.com 33 println(pkg.Hello) 34 }` 35 36 const proxy = ` 37 -- import.test@v1.2.3/go.mod -- 38 module import.test 39 40 go 1.12 41 -- import.test@v1.2.3/pkg/const.go -- 42 package pkg 43 44 const Hello = "Hello" 45 ` 46 WithOptions( 47 ProxyFiles(proxy), 48 ).Run(t, program, func(t *testing.T, env *Env) { 49 env.OpenFile("main.go") 50 env.OpenFile("go.mod") 51 52 modLink := "https://pkg.go.dev/mod/import.test@v1.2.3" 53 pkgLink := "https://pkg.go.dev/import.test@v1.2.3/pkg" 54 55 // First, check that we get the expected links via hover and documentLink. 56 content, _ := env.Hover(env.RegexpSearch("main.go", "pkg.Hello")) 57 if content == nil || !strings.Contains(content.Value, pkgLink) { 58 t.Errorf("hover: got %v in main.go, want contains %q", content, pkgLink) 59 } 60 content, _ = env.Hover(env.RegexpSearch("go.mod", "import.test")) 61 if content == nil || !strings.Contains(content.Value, pkgLink) { 62 t.Errorf("hover: got %v in go.mod, want contains %q", content, pkgLink) 63 } 64 links := env.DocumentLink("main.go") 65 if len(links) != 1 || *links[0].Target != pkgLink { 66 t.Errorf("documentLink: got links %+v for main.go, want one link with target %q", links, pkgLink) 67 } 68 links = env.DocumentLink("go.mod") 69 if len(links) != 1 || *links[0].Target != modLink { 70 t.Errorf("documentLink: got links %+v for go.mod, want one link with target %q", links, modLink) 71 } 72 73 // Then change the environment to make these links private. 74 cfg := env.Editor.Config() 75 cfg.Env = map[string]string{"GOPRIVATE": "import.test"} 76 env.ChangeConfiguration(cfg) 77 78 // Finally, verify that the links are gone. 79 content, _ = env.Hover(env.RegexpSearch("main.go", "pkg.Hello")) 80 if content == nil || strings.Contains(content.Value, pkgLink) { 81 t.Errorf("hover: got %v in main.go, want non-empty hover without %q", content, pkgLink) 82 } 83 content, _ = env.Hover(env.RegexpSearch("go.mod", "import.test")) 84 if content == nil || strings.Contains(content.Value, modLink) { 85 t.Errorf("hover: got %v in go.mod, want contains %q", content, modLink) 86 } 87 links = env.DocumentLink("main.go") 88 if len(links) != 0 { 89 t.Errorf("documentLink: got %d document links for main.go, want 0\nlinks: %v", len(links), links) 90 } 91 links = env.DocumentLink("go.mod") 92 if len(links) != 0 { 93 t.Errorf("documentLink: got %d document links for go.mod, want 0\nlinks: %v", len(links), links) 94 } 95 }) 96 }