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.
14 . "golang.org/x/tools/gopls/internal/regtest"
16 "golang.org/x/tools/internal/lsp/protocol"
17 "golang.org/x/tools/internal/testenv"
20 // Tests golang/go#38815.
21 func TestIssue38815(t *testing.T) {
31 const ntest = `package main
32 func TestZ(t *testing.T) {
36 const want = `package main
40 func TestZ(t *testing.T) {
46 // "package main\nimport \"testing\"\npackage main..."
47 Run(t, needs, func(t *testing.T, env *Env) {
48 env.CreateBuffer("a_test.go", ntest)
49 env.SaveBuffer("a_test.go")
50 got := env.Editor.BufferText("a_test.go")
52 t.Errorf("got\n%q, wanted\n%q", got, want)
57 func TestVim1(t *testing.T) {
58 const vim1 = `package main
66 fmt.Printf("This is a test %v\n", foo)
67 fmt.Printf("This is another test %v\n", foo)
68 fmt.Printf("This is also a test %v\n", foo)
72 // The file remains unchanged, but if there are any CodeActions returned, they confuse vim.
73 // Therefore check for no CodeActions
74 Run(t, "", func(t *testing.T, env *Env) {
75 env.CreateBuffer("main.go", vim1)
76 env.OrganizeImports("main.go")
77 actions := env.CodeAction("main.go")
79 got := env.Editor.BufferText("main.go")
80 t.Errorf("unexpected actions %#v", actions)
82 t.Errorf("no changes")
84 t.Errorf("got\n%q", got)
85 t.Errorf("was\n%q", vim1)
91 func TestVim2(t *testing.T) {
92 const vim2 = `package main
103 fmt.Println(blah.Name, useless.Name)
107 Run(t, "", func(t *testing.T, env *Env) {
108 env.CreateBuffer("main.go", vim2)
109 env.OrganizeImports("main.go")
110 actions := env.CodeAction("main.go")
111 if len(actions) > 0 {
112 t.Errorf("unexpected actions %#v", actions)
117 func TestGOMODCACHE(t *testing.T) {
119 -- example.com@v1.2.3/go.mod --
123 -- example.com@v1.2.3/x/x.go --
127 -- example.com@v1.2.3/y/y.go --
138 require example.com v1.2.3
140 example.com v1.2.3 h1:6vTQqzX+pnwngZF1+5gcO3ZEWmix1jJ/h+pWS8wUxK0=
141 example.com v1.2.3/go.mod h1:Y2Rc5rVWjWur0h3pd9aEvK5Pof8YKDANh9gHA2Maujo=
145 import "example.com/x"
149 testenv.NeedsGo1Point(t, 15)
151 modcache, err := ioutil.TempDir("", "TestGOMODCACHE-modcache")
155 defer os.RemoveAll(modcache)
156 editorConfig := EditorConfig{Env: map[string]string{"GOMODCACHE": modcache}}
160 ).Run(t, files, func(t *testing.T, env *Env) {
161 env.OpenFile("main.go")
162 env.Await(env.DiagnosticAtRegexp("main.go", `y.Y`))
163 env.SaveBuffer("main.go")
164 env.Await(EmptyDiagnostics("main.go"))
165 path, _ := env.GoToDefinition("main.go", env.RegexpSearch("main.go", `y.(Y)`))
166 if !strings.HasPrefix(path, filepath.ToSlash(modcache)) {
167 t.Errorf("found module dependency outside of GOMODCACHE: got %v, wanted subdir of %v", path, filepath.ToSlash(modcache))
172 // Tests golang/go#40685.
173 func TestAcceptImportsQuickFixTestVariant(t *testing.T) {
198 func TestA(t *testing.T) {
202 Run(t, pkg, func(t *testing.T, env *Env) {
203 env.OpenFile("a/a.go")
204 var d protocol.PublishDiagnosticsParams
207 env.DiagnosticAtRegexp("a/a.go", "os.Stat"),
208 ReadDiagnostics("a/a.go", &d),
211 env.ApplyQuickFixes("a/a.go", d.Diagnostics)
213 EmptyDiagnostics("a/a.go"),