| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package workspace |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"os" |
| 11 |
"sort" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/google/go-cmp/cmp" |
| 16 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 17 |
"golang.org/x/tools/gopls/internal/cache" |
| 18 |
"golang.org/x/tools/gopls/internal/protocol" |
| 19 |
"golang.org/x/tools/gopls/internal/protocol/command" |
| 20 |
"golang.org/x/tools/gopls/internal/test/integration/fake" |
| 21 |
"golang.org/x/tools/gopls/internal/util/bug" |
| 22 |
"golang.org/x/tools/gopls/internal/util/goversion" |
| 23 |
"golang.org/x/tools/internal/gocommand" |
| 24 |
"golang.org/x/tools/internal/testenv" |
| 25 |
|
| 26 |
. "golang.org/x/tools/gopls/internal/test/integration" |
| 27 |
) |
| 28 |
|
| 29 |
func TestMain(m *testing.M) { |
| 30 |
bug.PanicOnBugs = true |
| 31 |
os.Exit(Main(m)) |
| 32 |
} |
| 33 |
|
| 34 |
const workspaceProxy = ` |
| 35 |
-- [email protected]/go.mod -- |
| 36 |
module example.com |
| 37 |
|
| 38 |
go 1.12 |
| 39 |
-- [email protected]/blah/blah.go -- |
| 40 |
package blah |
| 41 |
|
| 42 |
import "fmt" |
| 43 |
|
| 44 |
func SaySomething() { |
| 45 |
fmt.Println("something") |
| 46 |
} |
| 47 |
-- [email protected]/go.mod -- |
| 48 |
module random.org |
| 49 |
|
| 50 |
go 1.12 |
| 51 |
-- [email protected]/bye/bye.go -- |
| 52 |
package bye |
| 53 |
|
| 54 |
func Goodbye() { |
| 55 |
println("Bye") |
| 56 |
} |
| 57 |
` |
| 58 |
|
| 59 |
|
| 60 |
const workspaceModule = ` |
| 61 |
-- pkg/go.mod -- |
| 62 |
module mod.com |
| 63 |
|
| 64 |
go 1.14 |
| 65 |
|
| 66 |
require ( |
| 67 |
example.com v1.2.3 |
| 68 |
random.org v1.2.3 |
| 69 |
) |
| 70 |
-- pkg/go.sum -- |
| 71 |
example.com v1.2.3 h1:veRD4tUnatQRgsULqULZPjeoBGFr2qBhevSCZllD2Ds= |
| 72 |
example.com v1.2.3/go.mod h1:Y2Rc5rVWjWur0h3pd9aEvK5Pof8YKDANh9gHA2Maujo= |
| 73 |
random.org v1.2.3 h1:+JE2Fkp7gS0zsHXGEQJ7hraom3pNTlkxC4b2qPfA+/Q= |
| 74 |
random.org v1.2.3/go.mod h1:E9KM6+bBX2g5ykHZ9H27w16sWo3QwgonyjM44Dnej3I= |
| 75 |
-- pkg/main.go -- |
| 76 |
package main |
| 77 |
|
| 78 |
import ( |
| 79 |
"example.com/blah" |
| 80 |
"mod.com/inner" |
| 81 |
"random.org/bye" |
| 82 |
) |
| 83 |
|
| 84 |
func main() { |
| 85 |
blah.SaySomething() |
| 86 |
inner.Hi() |
| 87 |
bye.Goodbye() |
| 88 |
} |
| 89 |
-- pkg/main2.go -- |
| 90 |
package main |
| 91 |
|
| 92 |
import "fmt" |
| 93 |
|
| 94 |
func _() { |
| 95 |
fmt.Print("%s") |
| 96 |
} |
| 97 |
-- pkg/inner/inner.go -- |
| 98 |
package inner |
| 99 |
|
| 100 |
import "example.com/blah" |
| 101 |
|
| 102 |
func Hi() { |
| 103 |
blah.SaySomething() |
| 104 |
} |
| 105 |
-- goodbye/bye/bye.go -- |
| 106 |
package bye |
| 107 |
|
| 108 |
func Bye() {} |
| 109 |
-- goodbye/go.mod -- |
| 110 |
module random.org |
| 111 |
|
| 112 |
go 1.12 |
| 113 |
` |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
func TestReferences(t *testing.T) { |
| 118 |
for _, tt := range []struct { |
| 119 |
name, rootPath string |
| 120 |
}{ |
| 121 |
{ |
| 122 |
name: "module root", |
| 123 |
rootPath: "pkg", |
| 124 |
}, |
| 125 |
{ |
| 126 |
name: "subdirectory", |
| 127 |
rootPath: "pkg/inner", |
| 128 |
}, |
| 129 |
} { |
| 130 |
t.Run(tt.name, func(t *testing.T) { |
| 131 |
opts := []RunOption{ProxyFiles(workspaceProxy)} |
| 132 |
if tt.rootPath != "" { |
| 133 |
opts = append(opts, WorkspaceFolders(tt.rootPath)) |
| 134 |
} |
| 135 |
WithOptions(opts...).Run(t, workspaceModule, func(t *testing.T, env *Env) { |
| 136 |
f := "pkg/inner/inner.go" |
| 137 |
env.OpenFile(f) |
| 138 |
locations := env.References(env.RegexpSearch(f, `SaySomething`)) |
| 139 |
want := 3 |
| 140 |
if got := len(locations); got != want { |
| 141 |
t.Fatalf("expected %v locations, got %v", want, got) |
| 142 |
} |
| 143 |
}) |
| 144 |
}) |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
func TestClearAnalysisDiagnostics(t *testing.T) { |
| 153 |
WithOptions( |
| 154 |
ProxyFiles(workspaceProxy), |
| 155 |
WorkspaceFolders("pkg/inner"), |
| 156 |
).Run(t, workspaceModule, func(t *testing.T, env *Env) { |
| 157 |
env.OpenFile("pkg/main.go") |
| 158 |
env.AfterChange( |
| 159 |
Diagnostics(env.AtRegexp("pkg/main2.go", "fmt.Print")), |
| 160 |
) |
| 161 |
env.CloseBuffer("pkg/main.go") |
| 162 |
env.AfterChange( |
| 163 |
NoDiagnostics(ForFile("pkg/main2.go")), |
| 164 |
) |
| 165 |
}) |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
func TestReloadOnlyOnce(t *testing.T) { |
| 175 |
WithOptions( |
| 176 |
ProxyFiles(workspaceProxy), |
| 177 |
WorkspaceFolders("pkg"), |
| 178 |
).Run(t, workspaceModule, func(t *testing.T, env *Env) { |
| 179 |
dir := env.Sandbox.Workdir.URI("goodbye").Path() |
| 180 |
goModWithReplace := fmt.Sprintf(`%s |
| 181 |
replace random.org => %s |
| 182 |
`, env.ReadWorkspaceFile("pkg/go.mod"), dir) |
| 183 |
env.WriteWorkspaceFile("pkg/go.mod", goModWithReplace) |
| 184 |
env.Await( |
| 185 |
LogMatching(protocol.Info, `packages\.Load #\d+\n`, 2, false), |
| 186 |
) |
| 187 |
}) |
| 188 |
} |
| 189 |
|
| 190 |
const workspaceModuleProxy = ` |
| 191 |
-- [email protected]/go.mod -- |
| 192 |
module example.com |
| 193 |
|
| 194 |
go 1.12 |
| 195 |
-- [email protected]/blah/blah.go -- |
| 196 |
package blah |
| 197 |
|
| 198 |
import "fmt" |
| 199 |
|
| 200 |
func SaySomething() { |
| 201 |
fmt.Println("something") |
| 202 |
} |
| 203 |
-- [email protected]/go.mod -- |
| 204 |
module b.com |
| 205 |
|
| 206 |
go 1.12 |
| 207 |
-- [email protected]/b/b.go -- |
| 208 |
package b |
| 209 |
|
| 210 |
func Hello() {} |
| 211 |
` |
| 212 |
|
| 213 |
const multiModule = ` |
| 214 |
-- moda/a/go.mod -- |
| 215 |
module a.com |
| 216 |
|
| 217 |
require b.com v1.2.3 |
| 218 |
-- moda/a/go.sum -- |
| 219 |
b.com v1.2.3 h1:tXrlXP0rnjRpKNmkbLYoWBdq0ikb3C3bKK9//moAWBI= |
| 220 |
b.com v1.2.3/go.mod h1:D+J7pfFBZK5vdIdZEFquR586vKKIkqG7Qjw9AxG5BQ8= |
| 221 |
-- moda/a/a.go -- |
| 222 |
package a |
| 223 |
|
| 224 |
import ( |
| 225 |
"b.com/b" |
| 226 |
) |
| 227 |
|
| 228 |
func main() { |
| 229 |
var x int |
| 230 |
_ = b.Hello() |
| 231 |
} |
| 232 |
-- modb/go.mod -- |
| 233 |
module b.com |
| 234 |
|
| 235 |
-- modb/b/b.go -- |
| 236 |
package b |
| 237 |
|
| 238 |
func Hello() int { |
| 239 |
var x int |
| 240 |
} |
| 241 |
` |
| 242 |
|
| 243 |
func TestAutomaticWorkspaceModule_Interdependent(t *testing.T) { |
| 244 |
WithOptions( |
| 245 |
ProxyFiles(workspaceModuleProxy), |
| 246 |
).Run(t, multiModule, func(t *testing.T, env *Env) { |
| 247 |
env.RunGoCommand("work", "init") |
| 248 |
env.RunGoCommand("work", "use", "-r", ".") |
| 249 |
env.AfterChange( |
| 250 |
Diagnostics(env.AtRegexp("moda/a/a.go", "x")), |
| 251 |
Diagnostics(env.AtRegexp("modb/b/b.go", "x")), |
| 252 |
NoDiagnostics(env.AtRegexp("moda/a/a.go", `"b.com/b"`)), |
| 253 |
) |
| 254 |
}) |
| 255 |
} |
| 256 |
|
| 257 |
func TestWorkspaceVendoring(t *testing.T) { |
| 258 |
testenv.NeedsGoCommand1Point(t, 22) |
| 259 |
WithOptions( |
| 260 |
ProxyFiles(workspaceModuleProxy), |
| 261 |
).Run(t, multiModule, func(t *testing.T, env *Env) { |
| 262 |
env.RunGoCommand("work", "init") |
| 263 |
env.RunGoCommand("work", "use", "moda/a") |
| 264 |
env.AfterChange() |
| 265 |
env.OpenFile("moda/a/a.go") |
| 266 |
env.RunGoCommand("work", "vendor") |
| 267 |
env.AfterChange() |
| 268 |
loc := env.FirstDefinition(env.RegexpSearch("moda/a/a.go", "b.(Hello)")) |
| 269 |
const want = "vendor/b.com/b/b.go" |
| 270 |
if got := env.Sandbox.Workdir.URIToPath(loc.URI); got != want { |
| 271 |
t.Errorf("Definition: got location %q, want %q", got, want) |
| 272 |
} |
| 273 |
}) |
| 274 |
} |
| 275 |
|
| 276 |
func TestModuleWithExclude(t *testing.T) { |
| 277 |
const proxy = ` |
| 278 |
-- [email protected]/go.mod -- |
| 279 |
module c.com |
| 280 |
|
| 281 |
go 1.12 |
| 282 |
|
| 283 |
require b.com v1.2.3 |
| 284 |
-- [email protected]/blah/blah.go -- |
| 285 |
package blah |
| 286 |
|
| 287 |
import "fmt" |
| 288 |
|
| 289 |
func SaySomething() { |
| 290 |
fmt.Println("something") |
| 291 |
} |
| 292 |
-- [email protected]/go.mod -- |
| 293 |
module b.com |
| 294 |
|
| 295 |
go 1.12 |
| 296 |
-- [email protected]/b/b.go -- |
| 297 |
package b |
| 298 |
|
| 299 |
func Hello() {} |
| 300 |
-- [email protected]/go.mod -- |
| 301 |
module b.com |
| 302 |
|
| 303 |
go 1.12 |
| 304 |
` |
| 305 |
const files = ` |
| 306 |
-- go.mod -- |
| 307 |
module a.com |
| 308 |
|
| 309 |
require c.com v1.2.3 |
| 310 |
|
| 311 |
exclude b.com v1.2.3 |
| 312 |
-- main.go -- |
| 313 |
package a |
| 314 |
|
| 315 |
func main() { |
| 316 |
var x int |
| 317 |
} |
| 318 |
` |
| 319 |
WithOptions( |
| 320 |
WriteGoSum("."), |
| 321 |
ProxyFiles(proxy), |
| 322 |
).Run(t, files, func(t *testing.T, env *Env) { |
| 323 |
env.OnceMet( |
| 324 |
InitialWorkspaceLoad, |
| 325 |
Diagnostics(env.AtRegexp("main.go", "x")), |
| 326 |
) |
| 327 |
}) |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
func TestDeleteModule_Interdependent(t *testing.T) { |
| 336 |
const multiModule = ` |
| 337 |
-- go.work -- |
| 338 |
go 1.18 |
| 339 |
|
| 340 |
use ( |
| 341 |
moda/a |
| 342 |
modb |
| 343 |
) |
| 344 |
-- moda/a/go.mod -- |
| 345 |
module a.com |
| 346 |
|
| 347 |
require b.com v1.2.3 |
| 348 |
-- moda/a/go.sum -- |
| 349 |
b.com v1.2.3 h1:tXrlXP0rnjRpKNmkbLYoWBdq0ikb3C3bKK9//moAWBI= |
| 350 |
b.com v1.2.3/go.mod h1:D+J7pfFBZK5vdIdZEFquR586vKKIkqG7Qjw9AxG5BQ8= |
| 351 |
-- moda/a/a.go -- |
| 352 |
package a |
| 353 |
|
| 354 |
import ( |
| 355 |
"b.com/b" |
| 356 |
) |
| 357 |
|
| 358 |
func main() { |
| 359 |
var x int |
| 360 |
_ = b.Hello() |
| 361 |
} |
| 362 |
-- modb/go.mod -- |
| 363 |
module b.com |
| 364 |
|
| 365 |
-- modb/b/b.go -- |
| 366 |
package b |
| 367 |
|
| 368 |
func Hello() int { |
| 369 |
var x int |
| 370 |
} |
| 371 |
` |
| 372 |
WithOptions( |
| 373 |
ProxyFiles(workspaceModuleProxy), |
| 374 |
).Run(t, multiModule, func(t *testing.T, env *Env) { |
| 375 |
env.OpenFile("moda/a/a.go") |
| 376 |
env.Await(env.DoneWithOpen()) |
| 377 |
|
| 378 |
originalLoc := env.FirstDefinition(env.RegexpSearch("moda/a/a.go", "Hello")) |
| 379 |
original := env.Sandbox.Workdir.URIToPath(originalLoc.URI) |
| 380 |
if want := "modb/b/b.go"; !strings.HasSuffix(original, want) { |
| 381 |
t.Errorf("expected %s, got %v", want, original) |
| 382 |
} |
| 383 |
env.AfterChange() |
| 384 |
|
| 385 |
env.RemoveWorkspaceFile("modb/b/b.go") |
| 386 |
env.RemoveWorkspaceFile("modb/go.mod") |
| 387 |
env.WriteWorkspaceFile("go.work", "go 1.18\nuse moda/a") |
| 388 |
env.AfterChange() |
| 389 |
|
| 390 |
gotLoc := env.FirstDefinition(env.RegexpSearch("moda/a/a.go", "Hello")) |
| 391 |
got := env.Sandbox.Workdir.URIToPath(gotLoc.URI) |
| 392 |
if want := "[email protected]/b/b.go"; !strings.HasSuffix(got, want) { |
| 393 |
t.Errorf("expected %s, got %v", want, got) |
| 394 |
} |
| 395 |
}) |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
func TestCreateModule_Interdependent(t *testing.T) { |
| 401 |
const multiModule = ` |
| 402 |
-- go.work -- |
| 403 |
go 1.18 |
| 404 |
|
| 405 |
use ( |
| 406 |
moda/a |
| 407 |
) |
| 408 |
-- moda/a/go.mod -- |
| 409 |
module a.com |
| 410 |
|
| 411 |
require b.com v1.2.3 |
| 412 |
-- moda/a/go.sum -- |
| 413 |
b.com v1.2.3 h1:tXrlXP0rnjRpKNmkbLYoWBdq0ikb3C3bKK9//moAWBI= |
| 414 |
b.com v1.2.3/go.mod h1:D+J7pfFBZK5vdIdZEFquR586vKKIkqG7Qjw9AxG5BQ8= |
| 415 |
-- moda/a/a.go -- |
| 416 |
package a |
| 417 |
|
| 418 |
import ( |
| 419 |
"b.com/b" |
| 420 |
) |
| 421 |
|
| 422 |
func main() { |
| 423 |
var x int |
| 424 |
_ = b.Hello() |
| 425 |
} |
| 426 |
` |
| 427 |
WithOptions( |
| 428 |
ProxyFiles(workspaceModuleProxy), |
| 429 |
).Run(t, multiModule, func(t *testing.T, env *Env) { |
| 430 |
env.OpenFile("moda/a/a.go") |
| 431 |
loc := env.FirstDefinition(env.RegexpSearch("moda/a/a.go", "Hello")) |
| 432 |
original := env.Sandbox.Workdir.URIToPath(loc.URI) |
| 433 |
if want := "[email protected]/b/b.go"; !strings.HasSuffix(original, want) { |
| 434 |
t.Errorf("expected %s, got %v", want, original) |
| 435 |
} |
| 436 |
env.WriteWorkspaceFiles(map[string]string{ |
| 437 |
"go.work": `go 1.18 |
| 438 |
|
| 439 |
use ( |
| 440 |
moda/a |
| 441 |
modb |
| 442 |
) |
| 443 |
`, |
| 444 |
"modb/go.mod": "module b.com", |
| 445 |
"modb/b/b.go": `package b |
| 446 |
|
| 447 |
func Hello() int { |
| 448 |
var x int |
| 449 |
} |
| 450 |
`, |
| 451 |
}) |
| 452 |
env.AfterChange(Diagnostics(env.AtRegexp("modb/b/b.go", "x"))) |
| 453 |
gotLoc := env.FirstDefinition(env.RegexpSearch("moda/a/a.go", "Hello")) |
| 454 |
got := env.Sandbox.Workdir.URIToPath(gotLoc.URI) |
| 455 |
if want := "modb/b/b.go"; !strings.HasSuffix(got, want) { |
| 456 |
t.Errorf("expected %s, got %v", want, original) |
| 457 |
} |
| 458 |
}) |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
func TestOneBrokenModule(t *testing.T) { |
| 464 |
const multiModule = ` |
| 465 |
-- go.work -- |
| 466 |
go 1.18 |
| 467 |
|
| 468 |
use ( |
| 469 |
moda/a |
| 470 |
modb |
| 471 |
) |
| 472 |
-- moda/a/go.mod -- |
| 473 |
module a.com |
| 474 |
|
| 475 |
require b.com v1.2.3 |
| 476 |
|
| 477 |
-- moda/a/a.go -- |
| 478 |
package a |
| 479 |
|
| 480 |
import ( |
| 481 |
"b.com/b" |
| 482 |
) |
| 483 |
|
| 484 |
func main() { |
| 485 |
var x int |
| 486 |
_ = b.Hello() |
| 487 |
} |
| 488 |
-- modb/go.mod -- |
| 489 |
modul b.com // typo here |
| 490 |
|
| 491 |
-- modb/b/b.go -- |
| 492 |
package b |
| 493 |
|
| 494 |
func Hello() int { |
| 495 |
var x int |
| 496 |
} |
| 497 |
` |
| 498 |
WithOptions( |
| 499 |
ProxyFiles(workspaceModuleProxy), |
| 500 |
).Run(t, multiModule, func(t *testing.T, env *Env) { |
| 501 |
env.OpenFile("modb/go.mod") |
| 502 |
env.AfterChange( |
| 503 |
Diagnostics(AtPosition("modb/go.mod", 0, 0)), |
| 504 |
) |
| 505 |
env.RegexpReplace("modb/go.mod", "modul", "module") |
| 506 |
env.SaveBufferWithoutActions("modb/go.mod") |
| 507 |
env.AfterChange( |
| 508 |
Diagnostics(env.AtRegexp("modb/b/b.go", "x")), |
| 509 |
) |
| 510 |
}) |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
func TestBadGoWork(t *testing.T) { |
| 515 |
const files = ` |
| 516 |
-- go.work -- |
| 517 |
use ./bar |
| 518 |
-- bar/go.mod -- |
| 519 |
module example.com/bar |
| 520 |
` |
| 521 |
Run(t, files, func(t *testing.T, env *Env) { |
| 522 |
env.OpenFile("go.work") |
| 523 |
}) |
| 524 |
} |
| 525 |
|
| 526 |
func TestUseGoWork(t *testing.T) { |
| 527 |
|
| 528 |
|
| 529 |
const multiModule = ` |
| 530 |
-- moda/a/go.mod -- |
| 531 |
module a.com |
| 532 |
|
| 533 |
require b.com v1.2.3 |
| 534 |
-- moda/a/go.sum -- |
| 535 |
b.com v1.2.3 h1:tXrlXP0rnjRpKNmkbLYoWBdq0ikb3C3bKK9//moAWBI= |
| 536 |
b.com v1.2.3/go.mod h1:D+J7pfFBZK5vdIdZEFquR586vKKIkqG7Qjw9AxG5BQ8= |
| 537 |
-- moda/a/a.go -- |
| 538 |
package a |
| 539 |
|
| 540 |
import ( |
| 541 |
"b.com/b" |
| 542 |
) |
| 543 |
|
| 544 |
func main() { |
| 545 |
var x int |
| 546 |
_ = b.Hello() |
| 547 |
} |
| 548 |
-- modb/go.mod -- |
| 549 |
module b.com |
| 550 |
|
| 551 |
require example.com v1.2.3 |
| 552 |
-- modb/go.sum -- |
| 553 |
example.com v1.2.3 h1:Yryq11hF02fEf2JlOS2eph+ICE2/ceevGV3C9dl5V/c= |
| 554 |
example.com v1.2.3/go.mod h1:Y2Rc5rVWjWur0h3pd9aEvK5Pof8YKDANh9gHA2Maujo= |
| 555 |
-- modb/b/b.go -- |
| 556 |
package b |
| 557 |
|
| 558 |
func Hello() int { |
| 559 |
var x int |
| 560 |
} |
| 561 |
-- go.work -- |
| 562 |
go 1.17 |
| 563 |
|
| 564 |
use ( |
| 565 |
./moda/a |
| 566 |
) |
| 567 |
` |
| 568 |
WithOptions( |
| 569 |
ProxyFiles(workspaceModuleProxy), |
| 570 |
Settings{ |
| 571 |
"subdirWatchPatterns": "on", |
| 572 |
}, |
| 573 |
).Run(t, multiModule, func(t *testing.T, env *Env) { |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
env.OnceMet( |
| 579 |
InitialWorkspaceLoad, |
| 580 |
NoFileWatchMatching("modb"), |
| 581 |
) |
| 582 |
env.OpenFile("moda/a/a.go") |
| 583 |
env.Await(env.DoneWithOpen()) |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
checkHelloLocation := func(want string) error { |
| 588 |
loc := env.FirstDefinition(env.RegexpSearch("moda/a/a.go", "Hello")) |
| 589 |
file := env.Sandbox.Workdir.URIToPath(loc.URI) |
| 590 |
if !strings.HasSuffix(file, want) { |
| 591 |
return fmt.Errorf("expected %s, got %v", want, file) |
| 592 |
} |
| 593 |
return nil |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
if err := checkHelloLocation("[email protected]/b/b.go"); err != nil { |
| 598 |
t.Fatal(err) |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
env.WriteWorkspaceFile("go.work", ` |
| 604 |
go 1.17 |
| 605 |
|
| 606 |
use ( |
| 607 |
./moda/a |
| 608 |
./modb |
| 609 |
) |
| 610 |
`) |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
env.AfterChange( |
| 615 |
Diagnostics(env.AtRegexp("modb/b/b.go", "x")), |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
) |
| 621 |
|
| 622 |
|
| 623 |
if err := checkHelloLocation("modb/b/b.go"); err != nil { |
| 624 |
t.Fatal(err) |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
env.OpenFile("go.work") |
| 630 |
env.AfterChange( |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
FileWatchMatching("modb"), |
| 636 |
) |
| 637 |
env.SetBufferContent("go.work", `go 1.17 |
| 638 |
|
| 639 |
use ( |
| 640 |
./moda/a |
| 641 |
)`) |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
env.AfterChange() |
| 649 |
if err := checkHelloLocation("modb/b/b.go"); err != nil { |
| 650 |
t.Fatal(err) |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
env.SaveBufferWithoutActions("go.work") |
| 655 |
if err := checkHelloLocation("[email protected]/b/b.go"); err != nil { |
| 656 |
t.Fatal(err) |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
env.AfterChange(NoDiagnostics(ForFile("modb/go.mod"))) |
| 662 |
|
| 663 |
|
| 664 |
env.SetBufferContent("go.work", `go 1.18 |
| 665 |
use ( |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
./moda/a |
| 670 |
) |
| 671 |
`) |
| 672 |
env.SaveBuffer("go.work") |
| 673 |
env.AfterChange() |
| 674 |
gotWorkContents := env.ReadWorkspaceFile("go.work") |
| 675 |
wantWorkContents := `go 1.18 |
| 676 |
|
| 677 |
use ( |
| 678 |
./moda/a |
| 679 |
) |
| 680 |
` |
| 681 |
if gotWorkContents != wantWorkContents { |
| 682 |
t.Fatalf("formatted contents of workspace: got %q; want %q", gotWorkContents, wantWorkContents) |
| 683 |
} |
| 684 |
}) |
| 685 |
} |
| 686 |
|
| 687 |
func TestUseGoWorkDiagnosticMissingModule(t *testing.T) { |
| 688 |
const files = ` |
| 689 |
-- go.work -- |
| 690 |
go 1.18 |
| 691 |
|
| 692 |
use ./foo |
| 693 |
-- bar/go.mod -- |
| 694 |
module example.com/bar |
| 695 |
` |
| 696 |
Run(t, files, func(t *testing.T, env *Env) { |
| 697 |
env.OpenFile("go.work") |
| 698 |
env.AfterChange( |
| 699 |
Diagnostics(env.AtRegexp("go.work", "use"), WithMessage("directory ./foo does not contain a module")), |
| 700 |
) |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
env.SetBufferContent("go.work", "go 1.18 \n\n use ./bar\n") |
| 708 |
env.AfterChange( |
| 709 |
NoDiagnostics(env.AtRegexp("go.work", "use")), |
| 710 |
) |
| 711 |
env.SetBufferContent("go.work", "go 1.18 \n\n use ./foo\n") |
| 712 |
env.AfterChange( |
| 713 |
Diagnostics(env.AtRegexp("go.work", "use"), WithMessage("directory ./foo does not contain a module")), |
| 714 |
) |
| 715 |
}) |
| 716 |
} |
| 717 |
|
| 718 |
func TestUseGoWorkDiagnosticSyntaxError(t *testing.T) { |
| 719 |
const files = ` |
| 720 |
-- go.work -- |
| 721 |
go 1.18 |
| 722 |
|
| 723 |
usa ./foo |
| 724 |
replace |
| 725 |
` |
| 726 |
Run(t, files, func(t *testing.T, env *Env) { |
| 727 |
env.OpenFile("go.work") |
| 728 |
env.AfterChange( |
| 729 |
Diagnostics(env.AtRegexp("go.work", "usa"), WithMessage("unknown directive: usa")), |
| 730 |
Diagnostics(env.AtRegexp("go.work", "replace"), WithMessage("usage: replace")), |
| 731 |
) |
| 732 |
}) |
| 733 |
} |
| 734 |
|
| 735 |
func TestUseGoWorkHover(t *testing.T) { |
| 736 |
const files = ` |
| 737 |
-- go.work -- |
| 738 |
go 1.18 |
| 739 |
|
| 740 |
use ./foo |
| 741 |
use ( |
| 742 |
./bar |
| 743 |
./bar/baz |
| 744 |
) |
| 745 |
-- foo/go.mod -- |
| 746 |
module example.com/foo |
| 747 |
-- bar/go.mod -- |
| 748 |
module example.com/bar |
| 749 |
-- bar/baz/go.mod -- |
| 750 |
module example.com/bar/baz |
| 751 |
` |
| 752 |
Run(t, files, func(t *testing.T, env *Env) { |
| 753 |
env.OpenFile("go.work") |
| 754 |
|
| 755 |
tcs := map[string]string{ |
| 756 |
`\./foo`: "example.com/foo", |
| 757 |
`(?m)\./bar$`: "example.com/bar", |
| 758 |
`\./bar/baz`: "example.com/bar/baz", |
| 759 |
} |
| 760 |
|
| 761 |
for hoverRE, want := range tcs { |
| 762 |
got, _ := env.Hover(env.RegexpSearch("go.work", hoverRE)) |
| 763 |
if got.Value != want { |
| 764 |
t.Errorf(`hover on %q: got %q, want %q`, hoverRE, got, want) |
| 765 |
} |
| 766 |
} |
| 767 |
}) |
| 768 |
} |
| 769 |
|
| 770 |
func TestExpandToGoWork(t *testing.T) { |
| 771 |
const workspace = ` |
| 772 |
-- moda/a/go.mod -- |
| 773 |
module a.com |
| 774 |
|
| 775 |
require b.com v1.2.3 |
| 776 |
-- moda/a/a.go -- |
| 777 |
package a |
| 778 |
|
| 779 |
import ( |
| 780 |
"b.com/b" |
| 781 |
) |
| 782 |
|
| 783 |
func main() { |
| 784 |
var x int |
| 785 |
_ = b.Hello() |
| 786 |
} |
| 787 |
-- modb/go.mod -- |
| 788 |
module b.com |
| 789 |
|
| 790 |
require example.com v1.2.3 |
| 791 |
-- modb/b/b.go -- |
| 792 |
package b |
| 793 |
|
| 794 |
func Hello() int { |
| 795 |
var x int |
| 796 |
} |
| 797 |
-- go.work -- |
| 798 |
go 1.17 |
| 799 |
|
| 800 |
use ( |
| 801 |
./moda/a |
| 802 |
./modb |
| 803 |
) |
| 804 |
` |
| 805 |
WithOptions( |
| 806 |
WorkspaceFolders("moda/a"), |
| 807 |
).Run(t, workspace, func(t *testing.T, env *Env) { |
| 808 |
env.OpenFile("moda/a/a.go") |
| 809 |
env.Await(env.DoneWithOpen()) |
| 810 |
loc := env.FirstDefinition(env.RegexpSearch("moda/a/a.go", "Hello")) |
| 811 |
file := env.Sandbox.Workdir.URIToPath(loc.URI) |
| 812 |
want := "modb/b/b.go" |
| 813 |
if !strings.HasSuffix(file, want) { |
| 814 |
t.Errorf("expected %s, got %v", want, file) |
| 815 |
} |
| 816 |
}) |
| 817 |
} |
| 818 |
|
| 819 |
func TestInnerGoWork(t *testing.T) { |
| 820 |
|
| 821 |
|
| 822 |
const workspace = ` |
| 823 |
-- go.mod -- |
| 824 |
module a.com |
| 825 |
|
| 826 |
require b.com v1.2.3 |
| 827 |
-- a/go.work -- |
| 828 |
go 1.18 |
| 829 |
|
| 830 |
use ( |
| 831 |
.. |
| 832 |
../b |
| 833 |
) |
| 834 |
-- a/a.go -- |
| 835 |
package a |
| 836 |
|
| 837 |
import "b.com/b" |
| 838 |
|
| 839 |
var _ = b.B |
| 840 |
-- b/go.mod -- |
| 841 |
module b.com/b |
| 842 |
|
| 843 |
-- b/b.go -- |
| 844 |
package b |
| 845 |
|
| 846 |
const B = 0 |
| 847 |
` |
| 848 |
WithOptions( |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
WorkspaceFolders("a"), |
| 853 |
).Run(t, workspace, func(t *testing.T, env *Env) { |
| 854 |
env.OpenFile("a/a.go") |
| 855 |
loc := env.FirstDefinition(env.RegexpSearch("a/a.go", "b.(B)")) |
| 856 |
got := env.Sandbox.Workdir.URIToPath(loc.URI) |
| 857 |
want := "b/b.go" |
| 858 |
if got != want { |
| 859 |
t.Errorf("Definition(b.B): got %q, want %q", got, want) |
| 860 |
} |
| 861 |
}) |
| 862 |
} |
| 863 |
|
| 864 |
func TestNonWorkspaceFileCreation(t *testing.T) { |
| 865 |
const files = ` |
| 866 |
-- work/go.mod -- |
| 867 |
module mod.com |
| 868 |
|
| 869 |
go 1.12 |
| 870 |
-- work/x.go -- |
| 871 |
package x |
| 872 |
` |
| 873 |
|
| 874 |
const code = ` |
| 875 |
package foo |
| 876 |
import "fmt" |
| 877 |
var _ = fmt.Printf |
| 878 |
` |
| 879 |
WithOptions( |
| 880 |
WorkspaceFolders("work"), |
| 881 |
).Run(t, files, func(t *testing.T, env *Env) { |
| 882 |
env.CreateBuffer("outside/foo.go", "") |
| 883 |
env.EditBuffer("outside/foo.go", fake.NewEdit(0, 0, 0, 0, code)) |
| 884 |
env.FirstDefinition(env.RegexpSearch("outside/foo.go", `Printf`)) |
| 885 |
}) |
| 886 |
} |
| 887 |
|
| 888 |
func TestGoWork_V2Module(t *testing.T) { |
| 889 |
|
| 890 |
const proxy = ` |
| 891 |
-- b.com/[email protected]/go.mod -- |
| 892 |
module b.com/v2 |
| 893 |
|
| 894 |
go 1.12 |
| 895 |
-- b.com/[email protected]/b/b.go -- |
| 896 |
package b |
| 897 |
|
| 898 |
func Ciao()() int { |
| 899 |
return 0 |
| 900 |
} |
| 901 |
` |
| 902 |
|
| 903 |
const multiModule = ` |
| 904 |
-- go.work -- |
| 905 |
go 1.18 |
| 906 |
|
| 907 |
use ( |
| 908 |
moda/a |
| 909 |
modb |
| 910 |
modb/v2 |
| 911 |
modc |
| 912 |
) |
| 913 |
-- moda/a/go.mod -- |
| 914 |
module a.com |
| 915 |
|
| 916 |
require b.com/v2 v2.1.9 |
| 917 |
-- moda/a/a.go -- |
| 918 |
package a |
| 919 |
|
| 920 |
import ( |
| 921 |
"b.com/v2/b" |
| 922 |
) |
| 923 |
|
| 924 |
func main() { |
| 925 |
var x int |
| 926 |
_ = b.Hi() |
| 927 |
} |
| 928 |
-- modb/go.mod -- |
| 929 |
module b.com |
| 930 |
|
| 931 |
-- modb/b/b.go -- |
| 932 |
package b |
| 933 |
|
| 934 |
func Hello() int { |
| 935 |
var x int |
| 936 |
} |
| 937 |
-- modb/v2/go.mod -- |
| 938 |
module b.com/v2 |
| 939 |
|
| 940 |
-- modb/v2/b/b.go -- |
| 941 |
package b |
| 942 |
|
| 943 |
func Hi() int { |
| 944 |
var x int |
| 945 |
} |
| 946 |
-- modc/go.mod -- |
| 947 |
module gopkg.in/yaml.v1 // test gopkg.in versions |
| 948 |
-- modc/main.go -- |
| 949 |
package main |
| 950 |
|
| 951 |
func main() { |
| 952 |
var x int |
| 953 |
} |
| 954 |
` |
| 955 |
|
| 956 |
WithOptions( |
| 957 |
ProxyFiles(proxy), |
| 958 |
).Run(t, multiModule, func(t *testing.T, env *Env) { |
| 959 |
env.OnceMet( |
| 960 |
InitialWorkspaceLoad, |
| 961 |
|
| 962 |
|
| 963 |
Diagnostics(env.AtRegexp("moda/a/a.go", "x")), |
| 964 |
Diagnostics(env.AtRegexp("modb/b/b.go", "x")), |
| 965 |
Diagnostics(env.AtRegexp("modb/v2/b/b.go", "x")), |
| 966 |
Diagnostics(env.AtRegexp("modc/main.go", "x")), |
| 967 |
) |
| 968 |
}) |
| 969 |
} |
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
func TestMultiModule_OneBrokenModule(t *testing.T) { |
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
t.Skip("golang/go#57509: go.mod diagnostics do not work in go.work mode") |
| 979 |
const files = ` |
| 980 |
-- go.work -- |
| 981 |
go 1.18 |
| 982 |
|
| 983 |
use ( |
| 984 |
a |
| 985 |
b |
| 986 |
) |
| 987 |
-- go.work.sum -- |
| 988 |
-- a/go.mod -- |
| 989 |
module a.com |
| 990 |
|
| 991 |
go 1.12 |
| 992 |
-- a/main.go -- |
| 993 |
package main |
| 994 |
-- b/go.mod -- |
| 995 |
module b.com |
| 996 |
|
| 997 |
go 1.12 |
| 998 |
|
| 999 |
require ( |
| 1000 |
example.com v1.2.3 |
| 1001 |
) |
| 1002 |
-- b/go.sum -- |
| 1003 |
-- b/main.go -- |
| 1004 |
package b |
| 1005 |
|
| 1006 |
import "example.com/blah" |
| 1007 |
|
| 1008 |
func main() { |
| 1009 |
blah.Hello() |
| 1010 |
} |
| 1011 |
` |
| 1012 |
WithOptions( |
| 1013 |
ProxyFiles(workspaceProxy), |
| 1014 |
).Run(t, files, func(t *testing.T, env *Env) { |
| 1015 |
params := &protocol.PublishDiagnosticsParams{} |
| 1016 |
env.OpenFile("b/go.mod") |
| 1017 |
env.AfterChange( |
| 1018 |
Diagnostics( |
| 1019 |
env.AtRegexp("go.mod", `example.com v1.2.3`), |
| 1020 |
WithMessage("go.sum is out of sync"), |
| 1021 |
), |
| 1022 |
ReadDiagnostics("b/go.mod", params), |
| 1023 |
) |
| 1024 |
for _, d := range params.Diagnostics { |
| 1025 |
if !strings.Contains(d.Message, "go.sum is out of sync") { |
| 1026 |
continue |
| 1027 |
} |
| 1028 |
actions := env.GetQuickFixes("b/go.mod", []protocol.Diagnostic{d}) |
| 1029 |
if len(actions) != 2 { |
| 1030 |
t.Fatalf("expected 2 code actions, got %v", len(actions)) |
| 1031 |
} |
| 1032 |
env.ApplyQuickFixes("b/go.mod", []protocol.Diagnostic{d}) |
| 1033 |
} |
| 1034 |
env.AfterChange( |
| 1035 |
NoDiagnostics(ForFile("b/go.mod")), |
| 1036 |
) |
| 1037 |
}) |
| 1038 |
} |
| 1039 |
|
| 1040 |
|
| 1041 |
func TestChangeTestVariant_Issue52500(t *testing.T) { |
| 1042 |
const src = ` |
| 1043 |
-- go.mod -- |
| 1044 |
module mod.test |
| 1045 |
|
| 1046 |
go 1.12 |
| 1047 |
-- main_test.go -- |
| 1048 |
package main_test |
| 1049 |
|
| 1050 |
type Server struct{} |
| 1051 |
|
| 1052 |
const MainConst = otherConst |
| 1053 |
-- other_test.go -- |
| 1054 |
package main_test |
| 1055 |
|
| 1056 |
const otherConst = 0 |
| 1057 |
|
| 1058 |
func (Server) Foo() {} |
| 1059 |
` |
| 1060 |
|
| 1061 |
Run(t, src, func(t *testing.T, env *Env) { |
| 1062 |
env.OpenFile("other_test.go") |
| 1063 |
env.RegexpReplace("other_test.go", "main_test", "main") |
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
env.Await( |
| 1071 |
Diagnostics(env.AtRegexp("other_test.go", "Server")), |
| 1072 |
Diagnostics(env.AtRegexp("main_test.go", "otherConst")), |
| 1073 |
) |
| 1074 |
env.RegexpReplace("other_test.go", "main", "main_test") |
| 1075 |
env.AfterChange( |
| 1076 |
NoDiagnostics(ForFile("other_test.go")), |
| 1077 |
NoDiagnostics(ForFile("main_test.go")), |
| 1078 |
) |
| 1079 |
|
| 1080 |
|
| 1081 |
_ = env.FirstDefinition(env.RegexpSearch("other_test.go", "Server")) |
| 1082 |
}) |
| 1083 |
} |
| 1084 |
|
| 1085 |
|
| 1086 |
func TestClearNonWorkspaceDiagnostics(t *testing.T) { |
| 1087 |
const ws = ` |
| 1088 |
-- go.work -- |
| 1089 |
go 1.18 |
| 1090 |
|
| 1091 |
use ( |
| 1092 |
./b |
| 1093 |
) |
| 1094 |
-- a/go.mod -- |
| 1095 |
module a |
| 1096 |
|
| 1097 |
go 1.17 |
| 1098 |
-- a/main.go -- |
| 1099 |
package main |
| 1100 |
|
| 1101 |
func main() { |
| 1102 |
var V string |
| 1103 |
} |
| 1104 |
-- b/go.mod -- |
| 1105 |
module b |
| 1106 |
|
| 1107 |
go 1.17 |
| 1108 |
-- b/main.go -- |
| 1109 |
package b |
| 1110 |
|
| 1111 |
import ( |
| 1112 |
_ "fmt" |
| 1113 |
) |
| 1114 |
` |
| 1115 |
Run(t, ws, func(t *testing.T, env *Env) { |
| 1116 |
env.OpenFile("b/main.go") |
| 1117 |
env.AfterChange( |
| 1118 |
NoDiagnostics(ForFile("a/main.go")), |
| 1119 |
) |
| 1120 |
env.OpenFile("a/main.go") |
| 1121 |
env.AfterChange( |
| 1122 |
Diagnostics(env.AtRegexp("a/main.go", "V"), WithMessage("not used")), |
| 1123 |
) |
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
if got, want := len(env.Views()), 2; got != want { |
| 1129 |
t.Errorf("after opening a/main.go, got %d views, want %d", got, want) |
| 1130 |
} |
| 1131 |
env.CloseBuffer("a/main.go") |
| 1132 |
env.AfterChange( |
| 1133 |
NoDiagnostics(ForFile("a/main.go")), |
| 1134 |
) |
| 1135 |
if got, want := len(env.Views()), 1; got != want { |
| 1136 |
t.Errorf("after closing a/main.go, got %d views, want %d", got, want) |
| 1137 |
} |
| 1138 |
}) |
| 1139 |
} |
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
func TestClearNonWorkspaceDiagnostics_NoView(t *testing.T) { |
| 1146 |
const ws = ` |
| 1147 |
-- a/go.mod -- |
| 1148 |
module example.com/a |
| 1149 |
|
| 1150 |
go 1.18 |
| 1151 |
|
| 1152 |
require example.com/b v1.2.3 |
| 1153 |
|
| 1154 |
replace example.com/b => ../b |
| 1155 |
|
| 1156 |
-- a/a.go -- |
| 1157 |
package a |
| 1158 |
|
| 1159 |
import "example.com/b" |
| 1160 |
|
| 1161 |
func _() { |
| 1162 |
V := b.B // unused |
| 1163 |
} |
| 1164 |
|
| 1165 |
-- b/go.mod -- |
| 1166 |
module b |
| 1167 |
|
| 1168 |
go 1.18 |
| 1169 |
|
| 1170 |
-- b/b.go -- |
| 1171 |
package b |
| 1172 |
|
| 1173 |
const B = 2 |
| 1174 |
|
| 1175 |
func _() { |
| 1176 |
var V int // unused |
| 1177 |
} |
| 1178 |
|
| 1179 |
-- b/b2.go -- |
| 1180 |
package b |
| 1181 |
|
| 1182 |
const B2 = B |
| 1183 |
|
| 1184 |
-- c/c.go -- |
| 1185 |
package main |
| 1186 |
|
| 1187 |
func main() { |
| 1188 |
var V int // unused |
| 1189 |
} |
| 1190 |
` |
| 1191 |
WithOptions( |
| 1192 |
WorkspaceFolders("a"), |
| 1193 |
).Run(t, ws, func(t *testing.T, env *Env) { |
| 1194 |
env.OpenFile("a/a.go") |
| 1195 |
env.AfterChange( |
| 1196 |
Diagnostics(env.AtRegexp("a/a.go", "V"), WithMessage("not used")), |
| 1197 |
NoDiagnostics(ForFile("b/b.go")), |
| 1198 |
NoDiagnostics(ForFile("c/c.go")), |
| 1199 |
) |
| 1200 |
env.OpenFile("b/b.go") |
| 1201 |
env.AfterChange( |
| 1202 |
Diagnostics(env.AtRegexp("a/a.go", "V"), WithMessage("not used")), |
| 1203 |
Diagnostics(env.AtRegexp("b/b.go", "V"), WithMessage("not used")), |
| 1204 |
NoDiagnostics(ForFile("c/c.go")), |
| 1205 |
) |
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
if got, want := len(env.Views()), 1; got != want { |
| 1212 |
t.Errorf("after opening b/b.go, got %d views, want %d", got, want) |
| 1213 |
} |
| 1214 |
env.CloseBuffer("b/b.go") |
| 1215 |
env.AfterChange( |
| 1216 |
Diagnostics(env.AtRegexp("a/a.go", "V"), WithMessage("not used")), |
| 1217 |
NoDiagnostics(ForFile("b/b.go")), |
| 1218 |
NoDiagnostics(ForFile("c/c.go")), |
| 1219 |
) |
| 1220 |
|
| 1221 |
|
| 1222 |
bUse := env.RegexpSearch("a/a.go", `b\.(B)`) |
| 1223 |
refs := env.References(bUse) |
| 1224 |
wantRefs := []string{"a/a.go", "b/b.go", "b/b2.go"} |
| 1225 |
var gotRefs []string |
| 1226 |
for _, ref := range refs { |
| 1227 |
gotRefs = append(gotRefs, env.Sandbox.Workdir.URIToPath(ref.URI)) |
| 1228 |
} |
| 1229 |
sort.Strings(gotRefs) |
| 1230 |
if diff := cmp.Diff(wantRefs, gotRefs); diff != "" { |
| 1231 |
t.Errorf("references(b.B) mismatch (-want +got)\n%s", diff) |
| 1232 |
} |
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
env.OpenFile("c/c.go") |
| 1237 |
env.AfterChange( |
| 1238 |
Diagnostics(env.AtRegexp("a/a.go", "V"), WithMessage("not used")), |
| 1239 |
NoDiagnostics(ForFile("b/b.go")), |
| 1240 |
Diagnostics(env.AtRegexp("c/c.go", "V"), WithMessage("not used")), |
| 1241 |
) |
| 1242 |
if got, want := len(env.Views()), 1; got != want { |
| 1243 |
t.Errorf("after opening b/b.go, got %d views, want %d", got, want) |
| 1244 |
} |
| 1245 |
|
| 1246 |
env.CloseBuffer("c/c.go") |
| 1247 |
env.AfterChange( |
| 1248 |
Diagnostics(env.AtRegexp("a/a.go", "V"), WithMessage("not used")), |
| 1249 |
NoDiagnostics(ForFile("b/b.go")), |
| 1250 |
NoDiagnostics(ForFile("c/c.go")), |
| 1251 |
) |
| 1252 |
env.CloseBuffer("a/a.go") |
| 1253 |
env.AfterChange( |
| 1254 |
Diagnostics(env.AtRegexp("a/a.go", "V"), WithMessage("not used")), |
| 1255 |
NoDiagnostics(ForFile("b/b.go")), |
| 1256 |
NoDiagnostics(ForFile("c/c.go")), |
| 1257 |
) |
| 1258 |
}) |
| 1259 |
} |
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
func TestOldGoNotification_SupportedVersion(t *testing.T) { |
| 1264 |
v := goVersion(t) |
| 1265 |
if v < goversion.OldestSupported() { |
| 1266 |
t.Skipf("go version 1.%d is unsupported", v) |
| 1267 |
} |
| 1268 |
|
| 1269 |
Run(t, "", func(t *testing.T, env *Env) { |
| 1270 |
env.OnceMet( |
| 1271 |
InitialWorkspaceLoad, |
| 1272 |
NoShownMessage("upgrade"), |
| 1273 |
) |
| 1274 |
}) |
| 1275 |
} |
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
func TestOldGoNotification_UnsupportedVersion(t *testing.T) { |
| 1281 |
v := goVersion(t) |
| 1282 |
if v >= goversion.OldestSupported() { |
| 1283 |
t.Skipf("go version 1.%d is supported", v) |
| 1284 |
} |
| 1285 |
|
| 1286 |
Run(t, "", func(t *testing.T, env *Env) { |
| 1287 |
env.Await( |
| 1288 |
|
| 1289 |
|
| 1290 |
ShownMessage("Please upgrade"), |
| 1291 |
) |
| 1292 |
}) |
| 1293 |
} |
| 1294 |
|
| 1295 |
func TestOldGoNotification_Fake(t *testing.T) { |
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
ctx := context.Background() |
| 1302 |
version, err := gocommand.GoVersion(ctx, gocommand.Invocation{}, &gocommand.Runner{}) |
| 1303 |
if err != nil { |
| 1304 |
t.Fatal(err) |
| 1305 |
} |
| 1306 |
defer func(t []goversion.Support) { |
| 1307 |
goversion.Supported = t |
| 1308 |
}(goversion.Supported) |
| 1309 |
goversion.Supported = []goversion.Support{ |
| 1310 |
{GoVersion: version, InstallGoplsVersion: "v1.0.0"}, |
| 1311 |
} |
| 1312 |
|
| 1313 |
Run(t, "", func(t *testing.T, env *Env) { |
| 1314 |
env.Await( |
| 1315 |
|
| 1316 |
|
| 1317 |
ShownMessage("Please upgrade"), |
| 1318 |
) |
| 1319 |
}) |
| 1320 |
} |
| 1321 |
|
| 1322 |
|
| 1323 |
func goVersion(t *testing.T) int { |
| 1324 |
t.Helper() |
| 1325 |
ctx := context.Background() |
| 1326 |
goversion, err := gocommand.GoVersion(ctx, gocommand.Invocation{}, &gocommand.Runner{}) |
| 1327 |
if err != nil { |
| 1328 |
t.Fatal(err) |
| 1329 |
} |
| 1330 |
return goversion |
| 1331 |
} |
| 1332 |
|
| 1333 |
func TestGoworkMutation(t *testing.T) { |
| 1334 |
WithOptions( |
| 1335 |
ProxyFiles(workspaceModuleProxy), |
| 1336 |
).Run(t, multiModule, func(t *testing.T, env *Env) { |
| 1337 |
env.RunGoCommand("work", "init") |
| 1338 |
env.RunGoCommand("work", "use", "-r", ".") |
| 1339 |
env.AfterChange( |
| 1340 |
Diagnostics(env.AtRegexp("moda/a/a.go", "x")), |
| 1341 |
Diagnostics(env.AtRegexp("modb/b/b.go", "x")), |
| 1342 |
NoDiagnostics(env.AtRegexp("moda/a/a.go", `b\.Hello`)), |
| 1343 |
) |
| 1344 |
env.RunGoCommand("work", "edit", "-dropuse", "modb") |
| 1345 |
env.Await( |
| 1346 |
Diagnostics(env.AtRegexp("moda/a/a.go", "x")), |
| 1347 |
NoDiagnostics(env.AtRegexp("modb/b/b.go", "x")), |
| 1348 |
Diagnostics(env.AtRegexp("moda/a/a.go", `b\.Hello`)), |
| 1349 |
) |
| 1350 |
}) |
| 1351 |
} |
| 1352 |
|
| 1353 |
func TestInitializeWithNonFileWorkspaceFolders(t *testing.T) { |
| 1354 |
for _, tt := range []struct { |
| 1355 |
name string |
| 1356 |
folders []string |
| 1357 |
wantViewRoots []string |
| 1358 |
}{ |
| 1359 |
{ |
| 1360 |
name: "real,virtual", |
| 1361 |
folders: []string{"modb", "virtual:///virtualpath"}, |
| 1362 |
wantViewRoots: []string{"./modb"}, |
| 1363 |
}, |
| 1364 |
{ |
| 1365 |
name: "virtual,real", |
| 1366 |
folders: []string{"virtual:///virtualpath", "modb"}, |
| 1367 |
wantViewRoots: []string{"./modb"}, |
| 1368 |
}, |
| 1369 |
{ |
| 1370 |
name: "real,virtual,real", |
| 1371 |
folders: []string{"moda/a", "virtual:///virtualpath", "modb"}, |
| 1372 |
wantViewRoots: []string{"./moda/a", "./modb"}, |
| 1373 |
}, |
| 1374 |
{ |
| 1375 |
name: "virtual", |
| 1376 |
folders: []string{"virtual:///virtualpath"}, |
| 1377 |
wantViewRoots: nil, |
| 1378 |
}, |
| 1379 |
} { |
| 1380 |
|
| 1381 |
t.Run(tt.name, func(t *testing.T) { |
| 1382 |
opts := []RunOption{ProxyFiles(workspaceProxy), WorkspaceFolders(tt.folders...)} |
| 1383 |
WithOptions(opts...).Run(t, multiModule, func(t *testing.T, env *Env) { |
| 1384 |
summary := func(typ cache.ViewType, root, folder string) command.View { |
| 1385 |
return command.View{ |
| 1386 |
Type: typ.String(), |
| 1387 |
Root: env.Sandbox.Workdir.URI(root), |
| 1388 |
Folder: env.Sandbox.Workdir.URI(folder), |
| 1389 |
} |
| 1390 |
} |
| 1391 |
checkViews := func(want ...command.View) { |
| 1392 |
got := env.Views() |
| 1393 |
if diff := cmp.Diff(want, got, cmpopts.IgnoreFields(command.View{}, "ID")); diff != "" { |
| 1394 |
t.Errorf("SummarizeViews() mismatch (-want +got):\n%s", diff) |
| 1395 |
} |
| 1396 |
} |
| 1397 |
var wantViews []command.View |
| 1398 |
for _, root := range tt.wantViewRoots { |
| 1399 |
wantViews = append(wantViews, summary(cache.GoModView, root, root)) |
| 1400 |
} |
| 1401 |
env.Await( |
| 1402 |
LogMatching(protocol.Warning, "skip adding virtual folder", 1, false), |
| 1403 |
) |
| 1404 |
checkViews(wantViews...) |
| 1405 |
}) |
| 1406 |
}) |
| 1407 |
} |
| 1408 |
} |
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
func TestChangeAddedWorkspaceFolders(t *testing.T) { |
| 1418 |
for _, tt := range []struct { |
| 1419 |
name string |
| 1420 |
after []string |
| 1421 |
wantViewRoots []string |
| 1422 |
}{ |
| 1423 |
{ |
| 1424 |
name: "add an already added file", |
| 1425 |
after: []string{"modb"}, |
| 1426 |
wantViewRoots: []string{"./modb"}, |
| 1427 |
}, |
| 1428 |
{ |
| 1429 |
name: "add an already added file but with an ending slash", |
| 1430 |
after: []string{"modb/"}, |
| 1431 |
wantViewRoots: []string{"./modb"}, |
| 1432 |
}, |
| 1433 |
{ |
| 1434 |
name: "add an already added file and a new file", |
| 1435 |
after: []string{"modb", "moda/a"}, |
| 1436 |
wantViewRoots: []string{"./modb", "moda/a"}, |
| 1437 |
}, |
| 1438 |
} { |
| 1439 |
t.Run(tt.name, func(t *testing.T) { |
| 1440 |
opts := []RunOption{ProxyFiles(workspaceProxy), RootPath("modb"), NoDefaultWorkspaceFiles()} |
| 1441 |
WithOptions(opts...).Run(t, multiModule, func(t *testing.T, env *Env) { |
| 1442 |
summary := func(typ cache.ViewType, root, folder string) command.View { |
| 1443 |
return command.View{ |
| 1444 |
Type: typ.String(), |
| 1445 |
Root: env.Sandbox.Workdir.URI(root), |
| 1446 |
Folder: env.Sandbox.Workdir.URI(folder), |
| 1447 |
} |
| 1448 |
} |
| 1449 |
checkViews := func(want ...command.View) { |
| 1450 |
got := env.Views() |
| 1451 |
if diff := cmp.Diff(want, got, cmpopts.IgnoreFields(command.View{}, "ID")); diff != "" { |
| 1452 |
t.Errorf("SummarizeViews() mismatch (-want +got):\n%s", diff) |
| 1453 |
} |
| 1454 |
} |
| 1455 |
var wantViews []command.View |
| 1456 |
for _, root := range tt.wantViewRoots { |
| 1457 |
wantViews = append(wantViews, summary(cache.GoModView, root, root)) |
| 1458 |
} |
| 1459 |
env.ChangeWorkspaceFolders(tt.after...) |
| 1460 |
env.Await( |
| 1461 |
LogMatching(protocol.Warning, "skip adding the already added folder", 1, false), |
| 1462 |
NoOutstandingWork(IgnoreTelemetryPromptWork), |
| 1463 |
) |
| 1464 |
checkViews(wantViews...) |
| 1465 |
}) |
| 1466 |
}) |
| 1467 |
} |
| 1468 |
} |
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
func TestChangeNonFileWorkspaceFolders(t *testing.T) { |
| 1473 |
for _, tt := range []struct { |
| 1474 |
name string |
| 1475 |
before []string |
| 1476 |
after []string |
| 1477 |
wantViewRoots []string |
| 1478 |
}{ |
| 1479 |
{ |
| 1480 |
name: "add", |
| 1481 |
before: []string{"modb"}, |
| 1482 |
after: []string{"modb", "moda/a", "virtual:///virtualpath"}, |
| 1483 |
wantViewRoots: []string{"./modb", "moda/a"}, |
| 1484 |
}, |
| 1485 |
{ |
| 1486 |
name: "remove", |
| 1487 |
before: []string{"modb", "virtual:///virtualpath", "moda/a"}, |
| 1488 |
after: []string{"modb"}, |
| 1489 |
wantViewRoots: []string{"./modb"}, |
| 1490 |
}, |
| 1491 |
} { |
| 1492 |
t.Run(tt.name, func(t *testing.T) { |
| 1493 |
opts := []RunOption{ProxyFiles(workspaceProxy), WorkspaceFolders(tt.before...)} |
| 1494 |
WithOptions(opts...).Run(t, multiModule, func(t *testing.T, env *Env) { |
| 1495 |
summary := func(typ cache.ViewType, root, folder string) command.View { |
| 1496 |
return command.View{ |
| 1497 |
Type: typ.String(), |
| 1498 |
Root: env.Sandbox.Workdir.URI(root), |
| 1499 |
Folder: env.Sandbox.Workdir.URI(folder), |
| 1500 |
} |
| 1501 |
} |
| 1502 |
checkViews := func(want ...command.View) { |
| 1503 |
got := env.Views() |
| 1504 |
if diff := cmp.Diff(want, got, cmpopts.IgnoreFields(command.View{}, "ID")); diff != "" { |
| 1505 |
t.Errorf("SummarizeViews() mismatch (-want +got):\n%s", diff) |
| 1506 |
} |
| 1507 |
} |
| 1508 |
var wantViews []command.View |
| 1509 |
for _, root := range tt.wantViewRoots { |
| 1510 |
wantViews = append(wantViews, summary(cache.GoModView, root, root)) |
| 1511 |
} |
| 1512 |
env.ChangeWorkspaceFolders(tt.after...) |
| 1513 |
env.Await( |
| 1514 |
LogMatching(protocol.Warning, "skip adding virtual folder", 1, false), |
| 1515 |
NoOutstandingWork(IgnoreTelemetryPromptWork), |
| 1516 |
) |
| 1517 |
checkViews(wantViews...) |
| 1518 |
}) |
| 1519 |
}) |
| 1520 |
} |
| 1521 |
} |