10 "golang.org/x/tools/internal/lsp/protocol"
11 "golang.org/x/tools/internal/testenv"
14 // Tests golang/go#38815.
15 func TestIssue38815(t *testing.T) {
24 const ntest = `package main
25 func TestZ(t *testing.T) {
29 const want = `package main
33 func TestZ(t *testing.T) {
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")
45 t.Errorf("got\n%q, wanted\n%q", got, want)
50 func TestVim1(t *testing.T) {
51 const vim1 = `package 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)
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")
72 got := env.Editor.BufferText("main.go")
73 t.Errorf("unexpected actions %#v", actions)
75 t.Errorf("no changes")
77 t.Errorf("got\n%q", got)
78 t.Errorf("was\n%q", vim1)
84 func TestVim2(t *testing.T) {
85 const vim2 = `package main
96 fmt.Println(blah.Name, useless.Name)
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)
110 func TestGOMODCACHE(t *testing.T) {
112 -- example.com@v1.2.3/go.mod --
116 -- example.com@v1.2.3/x/x.go --
120 -- example.com@v1.2.3/y/y.go --
129 require example.com v1.2.3
134 import "example.com/x"
138 testenv.NeedsGo1Point(t, 15)
140 modcache, err := ioutil.TempDir("", "TestGOMODCACHE-modcache")
144 defer os.RemoveAll(modcache)
145 editorConfig := EditorConfig{Env: map[string]string{"GOMODCACHE": modcache}}
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))
161 // Tests golang/go#40685.
162 func TestAcceptImportsQuickFixTestVariant(t *testing.T) {
187 func TestA(t *testing.T) {
191 run(t, pkg, func(t *testing.T, env *Env) {
192 env.OpenFile("a/a.go")
193 var d protocol.PublishDiagnosticsParams
196 env.DiagnosticAtRegexp("a/a.go", "os.Stat"),
197 ReadDiagnostics("a/a.go", &d),
200 env.ApplyQuickFixes("a/a.go", d.Diagnostics)
202 EmptyDiagnostics("a/a.go"),