| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package codelens |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"os" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"golang.org/x/tools/gopls/internal/server" |
| 13 |
"golang.org/x/tools/gopls/internal/settings" |
| 14 |
"golang.org/x/tools/gopls/internal/test/compare" |
| 15 |
. "golang.org/x/tools/gopls/internal/test/integration" |
| 16 |
"golang.org/x/tools/gopls/internal/util/bug" |
| 17 |
|
| 18 |
"golang.org/x/tools/gopls/internal/protocol" |
| 19 |
"golang.org/x/tools/gopls/internal/protocol/command" |
| 20 |
"golang.org/x/tools/internal/testenv" |
| 21 |
) |
| 22 |
|
| 23 |
func TestMain(m *testing.M) { |
| 24 |
bug.PanicOnBugs = true |
| 25 |
os.Exit(Main(m)) |
| 26 |
} |
| 27 |
|
| 28 |
func TestDisablingCodeLens(t *testing.T) { |
| 29 |
const workspace = ` |
| 30 |
-- go.mod -- |
| 31 |
module codelens.test |
| 32 |
|
| 33 |
go 1.12 |
| 34 |
-- lib.go -- |
| 35 |
package lib |
| 36 |
|
| 37 |
type Number int |
| 38 |
|
| 39 |
const ( |
| 40 |
Zero Number = iota |
| 41 |
One |
| 42 |
Two |
| 43 |
) |
| 44 |
|
| 45 |
//` + `go:generate stringer -type=Number |
| 46 |
` |
| 47 |
tests := []struct { |
| 48 |
label string |
| 49 |
enabled map[string]bool |
| 50 |
wantCodeLens bool |
| 51 |
}{ |
| 52 |
{ |
| 53 |
label: "default", |
| 54 |
wantCodeLens: true, |
| 55 |
}, |
| 56 |
{ |
| 57 |
label: "generate disabled", |
| 58 |
enabled: map[string]bool{string(settings.CodeLensGenerate): false}, |
| 59 |
wantCodeLens: false, |
| 60 |
}, |
| 61 |
} |
| 62 |
for _, test := range tests { |
| 63 |
t.Run(test.label, func(t *testing.T) { |
| 64 |
WithOptions( |
| 65 |
Settings{"codelenses": test.enabled}, |
| 66 |
).Run(t, workspace, func(t *testing.T, env *Env) { |
| 67 |
env.OpenFile("lib.go") |
| 68 |
lens := env.CodeLens("lib.go") |
| 69 |
if gotCodeLens := len(lens) > 0; gotCodeLens != test.wantCodeLens { |
| 70 |
t.Errorf("got codeLens: %t, want %t", gotCodeLens, test.wantCodeLens) |
| 71 |
} |
| 72 |
}) |
| 73 |
}) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
const proxyWithLatest = ` |
| 78 |
-- golang.org/x/[email protected]/go.mod -- |
| 79 |
module golang.org/x/hello |
| 80 |
|
| 81 |
go 1.12 |
| 82 |
-- golang.org/x/[email protected]/hi/hi.go -- |
| 83 |
package hi |
| 84 |
|
| 85 |
var Goodbye error |
| 86 |
-- golang.org/x/[email protected]/go.mod -- |
| 87 |
module golang.org/x/hello |
| 88 |
|
| 89 |
go 1.12 |
| 90 |
-- golang.org/x/[email protected]/hi/hi.go -- |
| 91 |
package hi |
| 92 |
|
| 93 |
var Goodbye error |
| 94 |
` |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
func TestUpgradeCodelens_Workspace(t *testing.T) { |
| 103 |
const shouldUpdateDep = ` |
| 104 |
-- go.work -- |
| 105 |
go 1.18 |
| 106 |
|
| 107 |
use ( |
| 108 |
./a |
| 109 |
./b |
| 110 |
) |
| 111 |
-- a/go.mod -- |
| 112 |
module mod.com/a |
| 113 |
|
| 114 |
go 1.14 |
| 115 |
|
| 116 |
require golang.org/x/hello v1.2.3 |
| 117 |
-- a/go.sum -- |
| 118 |
golang.org/x/hello v1.2.3 h1:7Wesfkx/uBd+eFgPrq0irYj/1XfmbvLV8jZ/W7C2Dwg= |
| 119 |
golang.org/x/hello v1.2.3/go.mod h1:OgtlzsxVMUUdsdQCIDYgaauCTH47B8T8vofouNJfzgY= |
| 120 |
-- a/main.go -- |
| 121 |
package main |
| 122 |
|
| 123 |
import "golang.org/x/hello/hi" |
| 124 |
|
| 125 |
func main() { |
| 126 |
_ = hi.Goodbye |
| 127 |
} |
| 128 |
-- b/go.mod -- |
| 129 |
module mod.com/b |
| 130 |
|
| 131 |
go 1.14 |
| 132 |
|
| 133 |
require golang.org/x/hello v1.2.3 |
| 134 |
-- b/go.sum -- |
| 135 |
golang.org/x/hello v1.2.3 h1:7Wesfkx/uBd+eFgPrq0irYj/1XfmbvLV8jZ/W7C2Dwg= |
| 136 |
golang.org/x/hello v1.2.3/go.mod h1:OgtlzsxVMUUdsdQCIDYgaauCTH47B8T8vofouNJfzgY= |
| 137 |
-- b/main.go -- |
| 138 |
package main |
| 139 |
|
| 140 |
import ( |
| 141 |
"golang.org/x/hello/hi" |
| 142 |
) |
| 143 |
|
| 144 |
func main() { |
| 145 |
_ = hi.Goodbye |
| 146 |
} |
| 147 |
` |
| 148 |
|
| 149 |
const wantGoModA = `module mod.com/a |
| 150 |
|
| 151 |
go 1.14 |
| 152 |
|
| 153 |
require golang.org/x/hello v1.3.3 |
| 154 |
` |
| 155 |
|
| 156 |
|
| 157 |
const wantGoModB = `module mod.com/b |
| 158 |
|
| 159 |
go 1.14 |
| 160 |
|
| 161 |
require golang.org/x/hello v1.2.3 |
| 162 |
` |
| 163 |
|
| 164 |
for _, commandTitle := range []string{ |
| 165 |
"Upgrade transitive dependencies", |
| 166 |
"Upgrade direct dependencies", |
| 167 |
} { |
| 168 |
t.Run(commandTitle, func(t *testing.T) { |
| 169 |
WithOptions( |
| 170 |
ProxyFiles(proxyWithLatest), |
| 171 |
).Run(t, shouldUpdateDep, func(t *testing.T, env *Env) { |
| 172 |
env.OpenFile("a/go.mod") |
| 173 |
env.OpenFile("b/go.mod") |
| 174 |
var lens protocol.CodeLens |
| 175 |
var found bool |
| 176 |
for _, l := range env.CodeLens("a/go.mod") { |
| 177 |
if l.Command.Title == commandTitle { |
| 178 |
lens = l |
| 179 |
found = true |
| 180 |
} |
| 181 |
} |
| 182 |
if !found { |
| 183 |
t.Fatalf("found no command with the title %s", commandTitle) |
| 184 |
} |
| 185 |
if err := env.Editor.ExecuteCommand(env.Ctx, &protocol.ExecuteCommandParams{ |
| 186 |
Command: lens.Command.Command, |
| 187 |
Arguments: lens.Command.Arguments, |
| 188 |
}, nil); err != nil { |
| 189 |
t.Fatal(err) |
| 190 |
} |
| 191 |
env.AfterChange() |
| 192 |
if got := env.BufferText("a/go.mod"); got != wantGoModA { |
| 193 |
t.Fatalf("a/go.mod upgrade failed:\n%s", compare.Text(wantGoModA, got)) |
| 194 |
} |
| 195 |
if got := env.BufferText("b/go.mod"); got != wantGoModB { |
| 196 |
t.Fatalf("b/go.mod changed unexpectedly:\n%s", compare.Text(wantGoModB, got)) |
| 197 |
} |
| 198 |
}) |
| 199 |
}) |
| 200 |
} |
| 201 |
for _, vendoring := range []bool{false, true} { |
| 202 |
t.Run(fmt.Sprintf("Upgrade individual dependency vendoring=%v", vendoring), func(t *testing.T) { |
| 203 |
WithOptions( |
| 204 |
ProxyFiles(proxyWithLatest), |
| 205 |
).Run(t, shouldUpdateDep, func(t *testing.T, env *Env) { |
| 206 |
if vendoring { |
| 207 |
env.RunGoCommandInDirWithEnv("a", []string{"GOWORK=off"}, "mod", "vendor") |
| 208 |
} |
| 209 |
env.AfterChange() |
| 210 |
env.OpenFile("a/go.mod") |
| 211 |
env.OpenFile("b/go.mod") |
| 212 |
|
| 213 |
env.ExecuteCodeLensCommand("a/go.mod", command.CheckUpgrades, nil) |
| 214 |
d := &protocol.PublishDiagnosticsParams{} |
| 215 |
env.OnceMet( |
| 216 |
CompletedWork(server.DiagnosticWorkTitle(server.FromCheckUpgrades), 1, true), |
| 217 |
Diagnostics(env.AtRegexp("a/go.mod", `require`), WithMessage("can be upgraded")), |
| 218 |
ReadDiagnostics("a/go.mod", d), |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
NoDiagnostics(env.AtRegexp("b/go.mod", `require`)), |
| 224 |
) |
| 225 |
|
| 226 |
env.ExecuteCodeLensCommand("b/go.mod", command.CheckUpgrades, nil) |
| 227 |
env.OnceMet( |
| 228 |
CompletedWork(server.DiagnosticWorkTitle(server.FromCheckUpgrades), 2, true), |
| 229 |
Diagnostics(env.AtRegexp("b/go.mod", `require`), WithMessage("can be upgraded")), |
| 230 |
) |
| 231 |
env.ExecuteCodeLensCommand("b/go.mod", command.ResetGoModDiagnostics, nil) |
| 232 |
env.OnceMet( |
| 233 |
CompletedWork(server.DiagnosticWorkTitle(server.FromResetGoModDiagnostics), 1, true), |
| 234 |
NoDiagnostics(ForFile("b/go.mod")), |
| 235 |
) |
| 236 |
|
| 237 |
|
| 238 |
env.ApplyQuickFixes("a/go.mod", d.Diagnostics) |
| 239 |
env.AfterChange() |
| 240 |
if got := env.BufferText("a/go.mod"); got != wantGoModA { |
| 241 |
t.Fatalf("a/go.mod upgrade failed:\n%s", compare.Text(wantGoModA, got)) |
| 242 |
} |
| 243 |
if got := env.BufferText("b/go.mod"); got != wantGoModB { |
| 244 |
t.Fatalf("b/go.mod changed unexpectedly:\n%s", compare.Text(wantGoModB, got)) |
| 245 |
} |
| 246 |
}) |
| 247 |
}) |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
func TestUpgradeCodelens_ModVendor(t *testing.T) { |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
testenv.NeedsGoCommand1Point(t, 22) |
| 256 |
|
| 257 |
const shouldUpdateDep = ` |
| 258 |
-- go.mod -- |
| 259 |
module mod.com/a |
| 260 |
|
| 261 |
go 1.22 |
| 262 |
|
| 263 |
require golang.org/x/hello v1.2.3 |
| 264 |
-- main.go -- |
| 265 |
package main |
| 266 |
|
| 267 |
import "golang.org/x/hello/hi" |
| 268 |
|
| 269 |
func main() { |
| 270 |
_ = hi.Goodbye |
| 271 |
} |
| 272 |
` |
| 273 |
|
| 274 |
const wantGoModA = `module mod.com/a |
| 275 |
|
| 276 |
go 1.22 |
| 277 |
|
| 278 |
require golang.org/x/hello v1.3.3 |
| 279 |
` |
| 280 |
|
| 281 |
WithOptions( |
| 282 |
WriteGoSum("."), |
| 283 |
ProxyFiles(proxyWithLatest), |
| 284 |
).Run(t, shouldUpdateDep, func(t *testing.T, env *Env) { |
| 285 |
env.RunGoCommand("mod", "vendor") |
| 286 |
env.AfterChange() |
| 287 |
env.OpenFile("go.mod") |
| 288 |
|
| 289 |
env.ExecuteCodeLensCommand("go.mod", command.CheckUpgrades, nil) |
| 290 |
d := &protocol.PublishDiagnosticsParams{} |
| 291 |
env.OnceMet( |
| 292 |
CompletedWork(server.DiagnosticWorkTitle(server.FromCheckUpgrades), 1, true), |
| 293 |
Diagnostics(env.AtRegexp("go.mod", `require`), WithMessage("can be upgraded")), |
| 294 |
ReadDiagnostics("go.mod", d), |
| 295 |
) |
| 296 |
|
| 297 |
|
| 298 |
env.ApplyQuickFixes("go.mod", d.Diagnostics) |
| 299 |
env.AfterChange() |
| 300 |
if got := env.BufferText("go.mod"); got != wantGoModA { |
| 301 |
t.Fatalf("go.mod upgrade failed:\n%s", compare.Text(wantGoModA, got)) |
| 302 |
} |
| 303 |
}) |
| 304 |
} |
| 305 |
|
| 306 |
func TestUnusedDependenciesCodelens(t *testing.T) { |
| 307 |
const proxy = ` |
| 308 |
-- golang.org/x/[email protected]/go.mod -- |
| 309 |
module golang.org/x/hello |
| 310 |
|
| 311 |
go 1.14 |
| 312 |
-- golang.org/x/[email protected]/hi/hi.go -- |
| 313 |
package hi |
| 314 |
|
| 315 |
var Goodbye error |
| 316 |
-- golang.org/x/[email protected]/go.mod -- |
| 317 |
module golang.org/x/unused |
| 318 |
|
| 319 |
go 1.14 |
| 320 |
-- golang.org/x/[email protected]/nouse/nouse.go -- |
| 321 |
package nouse |
| 322 |
|
| 323 |
var NotUsed error |
| 324 |
` |
| 325 |
|
| 326 |
const shouldRemoveDep = ` |
| 327 |
-- go.mod -- |
| 328 |
module mod.com |
| 329 |
|
| 330 |
go 1.14 |
| 331 |
|
| 332 |
require golang.org/x/hello v1.0.0 |
| 333 |
require golang.org/x/unused v1.0.0 |
| 334 |
|
| 335 |
// EOF |
| 336 |
-- main.go -- |
| 337 |
package main |
| 338 |
|
| 339 |
import "golang.org/x/hello/hi" |
| 340 |
|
| 341 |
func main() { |
| 342 |
_ = hi.Goodbye |
| 343 |
} |
| 344 |
` |
| 345 |
WithOptions( |
| 346 |
WriteGoSum("."), |
| 347 |
ProxyFiles(proxy), |
| 348 |
).Run(t, shouldRemoveDep, func(t *testing.T, env *Env) { |
| 349 |
env.OpenFile("go.mod") |
| 350 |
env.RegexpReplace("go.mod", "// EOF", "// EOF unsaved edit") |
| 351 |
env.ExecuteCodeLensCommand("go.mod", command.Tidy, nil) |
| 352 |
env.AfterChange() |
| 353 |
got := env.BufferText("go.mod") |
| 354 |
const wantGoMod = `module mod.com |
| 355 |
|
| 356 |
go 1.14 |
| 357 |
|
| 358 |
require golang.org/x/hello v1.0.0 |
| 359 |
|
| 360 |
// EOF unsaved edit |
| 361 |
` |
| 362 |
if got != wantGoMod { |
| 363 |
t.Fatalf("go.mod tidy failed:\n%s", compare.Text(wantGoMod, got)) |
| 364 |
} |
| 365 |
}) |
| 366 |
} |
| 367 |
|
| 368 |
func TestRegenerateCgo(t *testing.T) { |
| 369 |
testenv.NeedsTool(t, "cgo") |
| 370 |
const workspace = ` |
| 371 |
-- go.mod -- |
| 372 |
module example.com |
| 373 |
|
| 374 |
go 1.12 |
| 375 |
-- cgo.go -- |
| 376 |
package x |
| 377 |
|
| 378 |
/* |
| 379 |
int fortythree() { return 42; } |
| 380 |
*/ |
| 381 |
import "C" |
| 382 |
|
| 383 |
func Foo() { |
| 384 |
print(C.fortytwo()) |
| 385 |
} |
| 386 |
` |
| 387 |
Run(t, workspace, func(t *testing.T, env *Env) { |
| 388 |
|
| 389 |
env.OpenFile("cgo.go") |
| 390 |
env.AfterChange( |
| 391 |
Diagnostics(env.AtRegexp("cgo.go", ``), WithMessage("go list failed to return CompiledGoFiles")), |
| 392 |
) |
| 393 |
|
| 394 |
|
| 395 |
env.RegexpReplace("cgo.go", `int fortythree`, "int fortytwo") |
| 396 |
env.SaveBuffer("cgo.go") |
| 397 |
env.AfterChange( |
| 398 |
Diagnostics(env.AtRegexp("cgo.go", ``), WithMessage("go list failed to return CompiledGoFiles")), |
| 399 |
) |
| 400 |
|
| 401 |
|
| 402 |
env.ExecuteCodeLensCommand("cgo.go", command.RegenerateCgo, nil) |
| 403 |
env.OnceMet( |
| 404 |
CompletedWork(server.DiagnosticWorkTitle(server.FromRegenerateCgo), 1, true), |
| 405 |
NoDiagnostics(ForFile("cgo.go")), |
| 406 |
) |
| 407 |
}) |
| 408 |
} |