Debian Codegraph @

gopls/internal/test/integration/workspace/standalone_test.go

raw symbols SCIP
1 // Copyright 2022 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 workspace
6
7 import (
8 "sort"
9 "testing"
10
11 "github.com/google/go-cmp/cmp"
12 "golang.org/x/tools/gopls/internal/protocol"
13 . "golang.org/x/tools/gopls/internal/test/integration"
14 )
15
16 func TestStandaloneFiles(t *testing.T) {
17 const files = `
18 -- go.mod --
19 module mod.test
20
21 go 1.16
22 -- lib/lib.go --
23 package lib
24
25 const K = 0
26
27 type I interface {
28 M()
29 }
30 -- lib/ignore.go --
31 //go:build ignore
32
33 package main
34
35 import (
36 "mod.test/lib"
37 )
38
39 const K = 1
40
41 type Mer struct{}
42 func (Mer) M()
43
44 func main() {
45 println(lib.K + K)
46 }
47 `
48 WithOptions(
49 // On Go 1.17 and earlier, this test fails with
50 // experimentalWorkspaceModule. Not investigated, as
51 // experimentalWorkspaceModule will be removed.
52 Modes(Default),
53 ).Run(t, files, func(t *testing.T, env *Env) {
54 // Initially, gopls should not know about the standalone file as it hasn't
55 // been opened. Therefore, we should only find one symbol 'K'.
56 //
57 // (The choice of "K" is a little sleazy: it was originally "C" until
58 // we started adding "unsafe" to the workspace unconditionally, which
59 // caused a spurious match of "unsafe.Slice". But in practice every
60 // workspace depends on unsafe.)
61 syms := env.Symbol("K")
62 if got, want := len(syms), 1; got != want {
63 t.Errorf("got %d symbols, want %d (%+v)", got, want, syms)
64 }
65
66 // Similarly, we should only find one reference to "K", and no
67 // implementations of I.
68 checkLocations := func(method string, gotLocations []protocol.Location, wantFiles ...string) {
69 var gotFiles []string
70 for _, l := range gotLocations {
71 gotFiles = append(gotFiles, env.Sandbox.Workdir.URIToPath(l.URI))
72 }
73 sort.Strings(gotFiles)
74 sort.Strings(wantFiles)
75 if diff := cmp.Diff(wantFiles, gotFiles); diff != "" {
76 t.Errorf("%s(...): unexpected locations (-want +got):\n%s", method, diff)
77 }
78 }
79
80 env.OpenFile("lib/lib.go")
81 env.AfterChange(NoDiagnostics())
82
83 // Replacing K with D should not cause any workspace diagnostics, since we
84 // haven't yet opened the standalone file.
85 env.RegexpReplace("lib/lib.go", "K", "D")
86 env.AfterChange(NoDiagnostics())
87 env.RegexpReplace("lib/lib.go", "D", "K")
88 env.AfterChange(NoDiagnostics())
89
90 refs := env.References(env.RegexpSearch("lib/lib.go", "K"))
91 checkLocations("References", refs, "lib/lib.go")
92
93 impls := env.Implementations(env.RegexpSearch("lib/lib.go", "I"))
94 checkLocations("Implementations", impls) // no implementations
95
96 // Opening the standalone file should not result in any diagnostics.
97 env.OpenFile("lib/ignore.go")
98 env.AfterChange(NoDiagnostics())
99
100 // Having opened the standalone file, we should find its symbols in the
101 // workspace.
102 syms = env.Symbol("K")
103 if got, want := len(syms), 2; got != want {
104 t.Fatalf("got %d symbols, want %d", got, want)
105 }
106
107 foundMainK := false
108 var symNames []string
109 for _, sym := range syms {
110 symNames = append(symNames, sym.Name)
111 if sym.Name == "main.K" {
112 foundMainK = true
113 }
114 }
115 if !foundMainK {
116 t.Errorf("WorkspaceSymbol(\"K\") = %v, want containing main.K", symNames)
117 }
118
119 // We should resolve workspace definitions in the standalone file.
120 fileLoc := env.FirstDefinition(env.RegexpSearch("lib/ignore.go", "lib.(K)"))
121 file := env.Sandbox.Workdir.URIToPath(fileLoc.URI)
122 if got, want := file, "lib/lib.go"; got != want {
123 t.Errorf("Definition(lib.K) = %v, want %v", got, want)
124 }
125
126 // ...as well as intra-file definitions
127 loc := env.FirstDefinition(env.RegexpSearch("lib/ignore.go", "\\+ (K)"))
128 wantLoc := env.RegexpSearch("lib/ignore.go", "const (K)")
129 if loc != wantLoc {
130 t.Errorf("Definition(K) = %v, want %v", loc, wantLoc)
131 }
132
133 // Renaming "lib.K" to "lib.D" should cause a diagnostic in the standalone
134 // file.
135 env.RegexpReplace("lib/lib.go", "K", "D")
136 env.AfterChange(Diagnostics(env.AtRegexp("lib/ignore.go", "lib.(K)")))
137
138 // Undoing the replacement should fix diagnostics
139 env.RegexpReplace("lib/lib.go", "D", "K")
140 env.AfterChange(NoDiagnostics())
141
142 // Now that our workspace has no errors, we should be able to find
143 // references and rename.
144 refs = env.References(env.RegexpSearch("lib/lib.go", "K"))
145 checkLocations("References", refs, "lib/lib.go", "lib/ignore.go")
146
147 impls = env.Implementations(env.RegexpSearch("lib/lib.go", "I"))
148 checkLocations("Implementations", impls, "lib/ignore.go")
149
150 // Renaming should rename in the standalone package.
151 env.Rename(env.RegexpSearch("lib/lib.go", "K"), "D")
152 env.RegexpSearch("lib/ignore.go", "lib.D")
153 })
154 }
155
156 func TestStandaloneFiles_Configuration(t *testing.T) {
157 const files = `
158 -- go.mod --
159 module mod.test
160
161 go 1.18
162 -- lib.go --
163 package lib // without this package, files are loaded as command-line-arguments
164 -- ignore.go --
165 //go:build ignore
166
167 package main
168
169 // An arbitrary comment.
170
171 func main() {}
172 -- standalone.go --
173 //go:build standalone
174
175 package main
176
177 func main() {}
178 `
179
180 WithOptions(
181 Settings{
182 "standaloneTags": []string{"standalone", "script"},
183 },
184 ).Run(t, files, func(t *testing.T, env *Env) {
185 env.OpenFile("ignore.go")
186 env.OpenFile("standalone.go")
187
188 env.AfterChange(
189 Diagnostics(env.AtRegexp("ignore.go", "package (main)")),
190 NoDiagnostics(ForFile("standalone.go")),
191 )
192
193 cfg := env.Editor.Config()
194 cfg.Settings = map[string]any{
195 "standaloneTags": []string{"ignore"},
196 }
197 env.ChangeConfiguration(cfg)
198 env.AfterChange(
199 NoDiagnostics(ForFile("ignore.go")),
200 Diagnostics(env.AtRegexp("standalone.go", "package (main)")),
201 )
202 })
203 }