| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 50 |
|
| 51 |
|
| 52 |
Modes(Default), |
| 53 |
).Run(t, files, func(t *testing.T, env *Env) { |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 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 |
|
| 67 |
|
| 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 |
|
| 84 |
|
| 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) |
| 95 |
|
| 96 |
|
| 97 |
env.OpenFile("lib/ignore.go") |
| 98 |
env.AfterChange(NoDiagnostics()) |
| 99 |
|
| 100 |
|
| 101 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 134 |
|
| 135 |
env.RegexpReplace("lib/lib.go", "K", "D") |
| 136 |
env.AfterChange(Diagnostics(env.AtRegexp("lib/ignore.go", "lib.(K)"))) |
| 137 |
|
| 138 |
|
| 139 |
env.RegexpReplace("lib/lib.go", "D", "K") |
| 140 |
env.AfterChange(NoDiagnostics()) |
| 141 |
|
| 142 |
|
| 143 |
|
| 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 |
|
| 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 |
} |