Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools / gopls@v0.5.2 / internal / regtest / imports_test.go
1 package regtest
2
3 import (
4         "io/ioutil"
5         "os"
6         "path/filepath"
7         "strings"
8         "testing"
9
10         "golang.org/x/tools/internal/lsp/protocol"
11         "golang.org/x/tools/internal/testenv"
12 )
13
14 // Tests golang/go#38815.
15 func TestIssue38815(t *testing.T) {
16         const needs = `
17 -- go.mod --
18 module foo
19
20 -- a.go --
21 package main
22 func f() {}
23 `
24         const ntest = `package main
25 func TestZ(t *testing.T) {
26         f()
27 }
28 `
29         const want = `package main
30
31 import "testing"
32
33 func TestZ(t *testing.T) {
34         f()
35 }
36 `
37
38         // it was returning
39         // "package main\nimport \"testing\"\npackage main..."
40         runner.Run(t, needs, func(t *testing.T, env *Env) {
41                 env.CreateBuffer("a_test.go", ntest)
42                 env.SaveBuffer("a_test.go")
43                 got := env.Editor.BufferText("a_test.go")
44                 if want != got {
45                         t.Errorf("got\n%q, wanted\n%q", got, want)
46                 }
47         })
48 }
49
50 func TestVim1(t *testing.T) {
51         const vim1 = `package main
52
53 import "fmt"
54
55 var foo = 1
56 var bar = 2
57
58 func main() {
59         fmt.Printf("This is a test %v\n", foo)
60         fmt.Printf("This is another test %v\n", foo)
61         fmt.Printf("This is also a test %v\n", foo)
62 }
63 `
64
65         // The file remains unchanged, but if there are any CodeActions returned, they confuse vim.
66         // Therefore check for no CodeActions
67         runner.Run(t, "", func(t *testing.T, env *Env) {
68                 env.CreateBuffer("main.go", vim1)
69                 env.OrganizeImports("main.go")
70                 actions := env.CodeAction("main.go")
71                 if len(actions) > 0 {
72                         got := env.Editor.BufferText("main.go")
73                         t.Errorf("unexpected actions %#v", actions)
74                         if got == vim1 {
75                                 t.Errorf("no changes")
76                         } else {
77                                 t.Errorf("got\n%q", got)
78                                 t.Errorf("was\n%q", vim1)
79                         }
80                 }
81         })
82 }
83
84 func TestVim2(t *testing.T) {
85         const vim2 = `package main
86
87 import (
88         "fmt"
89
90         "example.com/blah"
91
92         "rubbish.com/useless"
93 )
94
95 func main() {
96         fmt.Println(blah.Name, useless.Name)
97 }
98 `
99
100         runner.Run(t, "", func(t *testing.T, env *Env) {
101                 env.CreateBuffer("main.go", vim2)
102                 env.OrganizeImports("main.go")
103                 actions := env.CodeAction("main.go")
104                 if len(actions) > 0 {
105                         t.Errorf("unexpected actions %#v", actions)
106                 }
107         })
108 }
109
110 func TestGOMODCACHE(t *testing.T) {
111         const proxy = `
112 -- example.com@v1.2.3/go.mod --
113 module example.com
114
115 go 1.12
116 -- example.com@v1.2.3/x/x.go --
117 package x
118
119 const X = 1
120 -- example.com@v1.2.3/y/y.go --
121 package y
122
123 const Y = 2
124 `
125         const files = `
126 -- go.mod --
127 module mod.com
128
129 require example.com v1.2.3
130
131 -- main.go --
132 package main
133
134 import "example.com/x"
135
136 var _, _ = x.X, y.Y
137 `
138         testenv.NeedsGo1Point(t, 15)
139
140         modcache, err := ioutil.TempDir("", "TestGOMODCACHE-modcache")
141         if err != nil {
142                 t.Fatal(err)
143         }
144         defer os.RemoveAll(modcache)
145         editorConfig := EditorConfig{Env: map[string]string{"GOMODCACHE": modcache}}
146         withOptions(
147                 editorConfig,
148                 WithProxyFiles(proxy),
149         ).run(t, files, func(t *testing.T, env *Env) {
150                 env.OpenFile("main.go")
151                 env.Await(env.DiagnosticAtRegexp("main.go", `y.Y`))
152                 env.SaveBuffer("main.go")
153                 env.Await(EmptyDiagnostics("main.go"))
154                 path, _ := env.GoToDefinition("main.go", env.RegexpSearch("main.go", `y.(Y)`))
155                 if !strings.HasPrefix(path, filepath.ToSlash(modcache)) {
156                         t.Errorf("found module dependency outside of GOMODCACHE: got %v, wanted subdir of %v", path, filepath.ToSlash(modcache))
157                 }
158         })
159 }
160
161 // Tests golang/go#40685.
162 func TestAcceptImportsQuickFixTestVariant(t *testing.T) {
163         const pkg = `
164 -- go.mod --
165 module mod.com
166
167 go 1.12
168 -- a/a.go --
169 package a
170
171 import (
172         "fmt"
173 )
174
175 func _() {
176         fmt.Println("")
177         os.Stat("")
178 }
179 -- a/a_test.go --
180 package a
181
182 import (
183         "os"
184         "testing"
185 )
186
187 func TestA(t *testing.T) {
188         os.Stat("")
189 }
190 `
191         run(t, pkg, func(t *testing.T, env *Env) {
192                 env.OpenFile("a/a.go")
193                 var d protocol.PublishDiagnosticsParams
194                 env.Await(
195                         OnceMet(
196                                 env.DiagnosticAtRegexp("a/a.go", "os.Stat"),
197                                 ReadDiagnostics("a/a.go", &d),
198                         ),
199                 )
200                 env.ApplyQuickFixes("a/a.go", d.Diagnostics)
201                 env.Await(
202                         EmptyDiagnostics("a/a.go"),
203                 )
204         })
205 }