| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package misc |
| 6 |
|
| 7 |
import ( |
| 8 |
"os" |
| 9 |
"path/filepath" |
| 10 |
"runtime" |
| 11 |
"strings" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/google/go-cmp/cmp" |
| 16 |
"golang.org/x/tools/gopls/internal/test/compare" |
| 17 |
. "golang.org/x/tools/gopls/internal/test/integration" |
| 18 |
"golang.org/x/tools/gopls/internal/test/integration/fake" |
| 19 |
"golang.org/x/tools/internal/modindex" |
| 20 |
|
| 21 |
"golang.org/x/tools/gopls/internal/protocol" |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
func TestIssue38815(t *testing.T) { |
| 26 |
const needs = ` |
| 27 |
-- go.mod -- |
| 28 |
module foo |
| 29 |
|
| 30 |
go 1.12 |
| 31 |
-- a.go -- |
| 32 |
package main |
| 33 |
func f() {} |
| 34 |
` |
| 35 |
const ntest = `package main |
| 36 |
func TestZ(t *testing.T) { |
| 37 |
f() |
| 38 |
} |
| 39 |
` |
| 40 |
const want = `package main |
| 41 |
|
| 42 |
import "testing" |
| 43 |
|
| 44 |
func TestZ(t *testing.T) { |
| 45 |
f() |
| 46 |
} |
| 47 |
` |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
Run(t, needs, func(t *testing.T, env *Env) { |
| 52 |
env.CreateBuffer("a_test.go", ntest) |
| 53 |
env.SaveBuffer("a_test.go") |
| 54 |
got := env.BufferText("a_test.go") |
| 55 |
if want != got { |
| 56 |
t.Errorf("got\n%q, wanted\n%q", got, want) |
| 57 |
} |
| 58 |
}) |
| 59 |
} |
| 60 |
|
| 61 |
func TestIssue59124(t *testing.T) { |
| 62 |
const stuff = ` |
| 63 |
-- go.mod -- |
| 64 |
module foo |
| 65 |
go 1.19 |
| 66 |
-- a.go -- |
| 67 |
//line foo.y:102 |
| 68 |
package main |
| 69 |
|
| 70 |
import "fmt" |
| 71 |
|
| 72 |
//this comment is necessary for failure |
| 73 |
func _() { |
| 74 |
fmt.Println("hello") |
| 75 |
} |
| 76 |
` |
| 77 |
Run(t, stuff, func(t *testing.T, env *Env) { |
| 78 |
env.OpenFile("a.go") |
| 79 |
was := env.BufferText("a.go") |
| 80 |
env.AfterChange(NoDiagnostics()) |
| 81 |
env.OrganizeImports("a.go") |
| 82 |
is := env.BufferText("a.go") |
| 83 |
if diff := compare.Text(was, is); diff != "" { |
| 84 |
t.Errorf("unexpected diff after organizeImports:\n%s", diff) |
| 85 |
} |
| 86 |
}) |
| 87 |
} |
| 88 |
|
| 89 |
func TestIssue66407(t *testing.T) { |
| 90 |
const files = ` |
| 91 |
-- go.mod -- |
| 92 |
module foo |
| 93 |
go 1.21 |
| 94 |
-- a.go -- |
| 95 |
package foo |
| 96 |
|
| 97 |
func f(x float64) float64 { |
| 98 |
return x + rand.Float64() |
| 99 |
} |
| 100 |
-- b.go -- |
| 101 |
package foo |
| 102 |
|
| 103 |
func _() { |
| 104 |
_ = rand.Int63() |
| 105 |
} |
| 106 |
` |
| 107 |
WithOptions(Modes(Default)). |
| 108 |
Run(t, files, func(t *testing.T, env *Env) { |
| 109 |
env.OpenFile("a.go") |
| 110 |
was := env.BufferText("a.go") |
| 111 |
env.OrganizeImports("a.go") |
| 112 |
is := env.BufferText("a.go") |
| 113 |
|
| 114 |
env.AfterChange(Diagnostics(ForFile("a.go"))) |
| 115 |
diff := compare.Text(was, is) |
| 116 |
|
| 117 |
if !strings.Contains(diff, `import "math/rand/v2"`) { |
| 118 |
t.Errorf("expected rand/v2, got %q", diff) |
| 119 |
} |
| 120 |
env.OpenFile("b.go") |
| 121 |
was = env.BufferText("b.go") |
| 122 |
env.OrganizeImports("b.go") |
| 123 |
|
| 124 |
env.AfterChange(Diagnostics(ForFile("a.go")), |
| 125 |
NoDiagnostics(ForFile("b.go"))) |
| 126 |
is = env.BufferText("b.go") |
| 127 |
diff = compare.Text(was, is) |
| 128 |
if !strings.Contains(diff, `import "math/rand"`) { |
| 129 |
t.Errorf("expected math/rand, got %q", diff) |
| 130 |
} |
| 131 |
}) |
| 132 |
} |
| 133 |
|
| 134 |
func TestVim1(t *testing.T) { |
| 135 |
const vim1 = `package main |
| 136 |
|
| 137 |
import "fmt" |
| 138 |
|
| 139 |
var foo = 1 |
| 140 |
var bar = 2 |
| 141 |
|
| 142 |
func main() { |
| 143 |
fmt.Printf("This is a test %v\n", foo) |
| 144 |
fmt.Printf("This is another test %v\n", foo) |
| 145 |
fmt.Printf("This is also a test %v\n", foo) |
| 146 |
} |
| 147 |
` |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
Run(t, "", func(t *testing.T, env *Env) { |
| 153 |
env.CreateBuffer("main.go", vim1) |
| 154 |
env.OrganizeImports("main.go") |
| 155 |
|
| 156 |
|
| 157 |
for _, act := range env.CodeActionForFile("main.go", nil) { |
| 158 |
if act.Kind == protocol.QuickFix { |
| 159 |
t.Errorf("unexpected quick fix action: %#v", act) |
| 160 |
} |
| 161 |
} |
| 162 |
if t.Failed() { |
| 163 |
got := env.BufferText("main.go") |
| 164 |
if got == vim1 { |
| 165 |
t.Errorf("no changes") |
| 166 |
} else { |
| 167 |
t.Errorf("got\n%q", got) |
| 168 |
t.Errorf("was\n%q", vim1) |
| 169 |
} |
| 170 |
} |
| 171 |
}) |
| 172 |
} |
| 173 |
|
| 174 |
func TestVim2(t *testing.T) { |
| 175 |
const vim2 = `package main |
| 176 |
|
| 177 |
import ( |
| 178 |
"fmt" |
| 179 |
|
| 180 |
"example.com/blah" |
| 181 |
|
| 182 |
"rubbish.com/useless" |
| 183 |
) |
| 184 |
|
| 185 |
func main() { |
| 186 |
fmt.Println(blah.Name, useless.Name) |
| 187 |
} |
| 188 |
` |
| 189 |
|
| 190 |
Run(t, "", func(t *testing.T, env *Env) { |
| 191 |
env.CreateBuffer("main.go", vim2) |
| 192 |
env.OrganizeImports("main.go") |
| 193 |
|
| 194 |
|
| 195 |
for _, act := range env.CodeActionForFile("main.go", nil) { |
| 196 |
if act.Kind == protocol.QuickFix { |
| 197 |
t.Errorf("unexpected quick-fix action: %#v", act) |
| 198 |
} |
| 199 |
} |
| 200 |
}) |
| 201 |
} |
| 202 |
|
| 203 |
const exampleProxy = ` |
| 204 |
-- [email protected]/go.mod -- |
| 205 |
module example.com |
| 206 |
|
| 207 |
go 1.12 |
| 208 |
-- [email protected]/x/x.go -- |
| 209 |
package x |
| 210 |
|
| 211 |
const X = 1 |
| 212 |
-- [email protected]/y/y.go -- |
| 213 |
package y |
| 214 |
|
| 215 |
const Y = 2 |
| 216 |
` |
| 217 |
|
| 218 |
func TestGOMODCACHE(t *testing.T) { |
| 219 |
const files = ` |
| 220 |
-- go.mod -- |
| 221 |
module mod.com |
| 222 |
|
| 223 |
go 1.12 |
| 224 |
|
| 225 |
require example.com v1.2.3 |
| 226 |
-- main.go -- |
| 227 |
package main |
| 228 |
|
| 229 |
import "example.com/x" |
| 230 |
|
| 231 |
var _, _ = x.X, y.Y |
| 232 |
` |
| 233 |
modcache := t.TempDir() |
| 234 |
defer CleanModCache(t, modcache) |
| 235 |
|
| 236 |
opts := []RunOption{ |
| 237 |
EnvVars{"GOMODCACHE": modcache}, |
| 238 |
ProxyFiles(exampleProxy), |
| 239 |
WriteGoSum("."), |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
t.Run("setup", func(t *testing.T) { |
| 245 |
WithOptions(opts...).Run(t, files, func(t *testing.T, env *Env) {}) |
| 246 |
}) |
| 247 |
|
| 248 |
WithOptions(opts...).Run(t, files, func(t *testing.T, env *Env) { |
| 249 |
|
| 250 |
env.OpenFile("main.go") |
| 251 |
env.AfterChange( |
| 252 |
Diagnostics( |
| 253 |
env.AtRegexp("main.go", `y.Y`), |
| 254 |
WithMessage("undefined")), |
| 255 |
) |
| 256 |
|
| 257 |
|
| 258 |
env.SaveBuffer("main.go") |
| 259 |
if true { |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
ix, err := modindex.Read(modcache) |
| 264 |
if err != nil { |
| 265 |
t.Logf("could not read modcache index: %v", err) |
| 266 |
} else if len(ix.Entries) != 2 { |
| 267 |
t.Logf("%d modcache entries", len(ix.Entries)) |
| 268 |
if len(ix.Entries) == 0 { |
| 269 |
fis, err := os.ReadDir(modcache) |
| 270 |
if err != nil { |
| 271 |
t.Logf("could not read modcache dir: %v", err) |
| 272 |
} |
| 273 |
t.Logf("%d modcache files", len(fis)) |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
diags := make(map[string]*protocol.PublishDiagnosticsParams) |
| 279 |
env.AfterChange(ReadAllDiagnostics(&diags)) |
| 280 |
dump := false |
| 281 |
for k, v := range diags { |
| 282 |
if len(v.Diagnostics) != 0 { |
| 283 |
t.Errorf("Unexpected diagnostics for %s: %#v", k, v) |
| 284 |
dump = true |
| 285 |
} |
| 286 |
} |
| 287 |
if dump { |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
for i, e := range ix.Entries { |
| 293 |
t.Logf("%d: %s, %s, %q", i, e.ImportPath, e.Dir, e.Names) |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
loc := env.FirstDefinition(env.RegexpSearch("main.go", `y.(Y)`)) |
| 300 |
path := env.Sandbox.Workdir.URIToPath(loc.URI) |
| 301 |
if !strings.HasPrefix(path, filepath.ToSlash(modcache)) { |
| 302 |
t.Errorf("found module dependency outside of GOMODCACHE: got %v, wanted subdir of %v", path, filepath.ToSlash(modcache)) |
| 303 |
} |
| 304 |
}) |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
func Test58382(t *testing.T) { |
| 314 |
files := `-- main.go -- |
| 315 |
package main |
| 316 |
import "fmt" |
| 317 |
func main() { |
| 318 |
fmt.Println(xurls.Relaxed().FindAllString()) |
| 319 |
} |
| 320 |
-- go.mod -- |
| 321 |
module demo |
| 322 |
go 1.20 |
| 323 |
` |
| 324 |
cache := `-- mvdan.cc/[email protected]/xurls.go -- |
| 325 |
package xurls |
| 326 |
func Relaxed() *regexp.Regexp { |
| 327 |
return nil |
| 328 |
} |
| 329 |
-- github.com/mvdan/xurls/[email protected]/xurls.go -- |
| 330 |
package xurls |
| 331 |
func Relaxed() *regexp.Regexp { |
| 332 |
return nil |
| 333 |
} |
| 334 |
` |
| 335 |
modcache := t.TempDir() |
| 336 |
defer CleanModCache(t, modcache) |
| 337 |
mx := fake.UnpackTxt(cache) |
| 338 |
|
| 339 |
for k, v := range mx { |
| 340 |
fname := filepath.Join(modcache, k) |
| 341 |
dir := filepath.Dir(fname) |
| 342 |
os.MkdirAll(dir, 0777) |
| 343 |
if err := os.WriteFile(fname, v, 0644); err != nil { |
| 344 |
t.Fatal(err) |
| 345 |
} |
| 346 |
if true { |
| 347 |
|
| 348 |
t.Logf("wrote %s:%d", fname, len(v)) |
| 349 |
} |
| 350 |
} |
| 351 |
WithOptions( |
| 352 |
EnvVars{"GOMODCACHE": modcache}, |
| 353 |
WriteGoSum("."), |
| 354 |
NoLogsOnError(), |
| 355 |
).Run(t, files, func(t *testing.T, env *Env) { |
| 356 |
|
| 357 |
env.OpenFile("main.go") |
| 358 |
env.SaveBuffer("main.go") |
| 359 |
out := env.BufferText("main.go") |
| 360 |
if !strings.Contains(out, "xurls/v2") { |
| 361 |
t.Errorf("did not get v2 in %q", out) |
| 362 |
|
| 363 |
ix, err := modindex.Read(modcache) |
| 364 |
if err != nil { |
| 365 |
t.Fatal(err) |
| 366 |
} |
| 367 |
t.Logf("%d entries", len(ix.Entries)) |
| 368 |
for i, e := range ix.Entries { |
| 369 |
t.Logf("%d: %s, %s, %d", i, e.ImportPath, e.Dir, len(e.Names)) |
| 370 |
} |
| 371 |
} |
| 372 |
}) |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
func Test61208(t *testing.T) { |
| 377 |
files := `-- main.go -- |
| 378 |
package main |
| 379 |
import "fmt" |
| 380 |
func main() { |
| 381 |
fmt.Println(xurls.Relaxed().FindAllString()) |
| 382 |
} |
| 383 |
-- go.mod -- |
| 384 |
module demo |
| 385 |
go 1.20 |
| 386 |
require github.com/mvdan/xurls v1.1.0 |
| 387 |
` |
| 388 |
cache := `-- mvdan.cc/xurls/[email protected]/a/xurls.go -- |
| 389 |
package xurls |
| 390 |
func Relaxed() *regexp.Regexp { |
| 391 |
return nil |
| 392 |
} |
| 393 |
-- github.com/mvdan/[email protected]/a/xurls.go -- |
| 394 |
package xurls |
| 395 |
func Relaxed() *regexp.Regexp { |
| 396 |
return nil |
| 397 |
} |
| 398 |
` |
| 399 |
modcache := t.TempDir() |
| 400 |
defer CleanModCache(t, modcache) |
| 401 |
mx := fake.UnpackTxt(cache) |
| 402 |
for k, v := range mx { |
| 403 |
fname := filepath.Join(modcache, k) |
| 404 |
dir := filepath.Dir(fname) |
| 405 |
os.MkdirAll(dir, 0777) |
| 406 |
if err := os.WriteFile(fname, v, 0644); err != nil { |
| 407 |
t.Fatal(err) |
| 408 |
} |
| 409 |
} |
| 410 |
WithOptions( |
| 411 |
EnvVars{"GOMODCACHE": modcache}, |
| 412 |
WriteGoSum("."), |
| 413 |
NoLogsOnError(), |
| 414 |
).Run(t, files, func(t *testing.T, env *Env) { |
| 415 |
env.OpenFile("main.go") |
| 416 |
if true { |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
ix, err := modindex.Read(modcache) |
| 421 |
if err != nil { |
| 422 |
|
| 423 |
time.Sleep(3 * time.Second) |
| 424 |
ix, err = modindex.Read(modcache) |
| 425 |
if err != nil { |
| 426 |
t.Logf("could not read modcache index: %v", err) |
| 427 |
} else { |
| 428 |
t.Logf("re-read modcache index, found %d entries", len(ix.Entries)) |
| 429 |
} |
| 430 |
} else if len(ix.Entries) != 2 { |
| 431 |
t.Logf("%d modcache entries", len(ix.Entries)) |
| 432 |
if len(ix.Entries) == 0 { |
| 433 |
fis, err := os.ReadDir(modcache) |
| 434 |
if err != nil { |
| 435 |
t.Logf("could not read modcache dir: %v", err) |
| 436 |
} |
| 437 |
t.Logf("%d modcache files", len(fis)) |
| 438 |
} else { |
| 439 |
for i, e := range ix.Entries { |
| 440 |
t.Logf("ix %d: %s, %s, %q", i, e.ImportPath, e.Dir, e.Names) |
| 441 |
} |
| 442 |
} |
| 443 |
} |
| 444 |
env.SaveBuffer("main.go") |
| 445 |
out := env.BufferText("main.go") |
| 446 |
if !strings.Contains(out, "github.com/mvdan/xurls") { |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
for i, e := range ix.Entries { |
| 452 |
t.Errorf("%d: %s, %s, %q", i, e.ImportPath, e.Dir, e.Names) |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
out := env.BufferText("main.go") |
| 457 |
if !strings.Contains(out, "github.com/mvdan/xurls") { |
| 458 |
t.Errorf("did not get github.com/mvdan/xurls in %q", out) |
| 459 |
} |
| 460 |
}) |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
func Test60663(t *testing.T) { |
| 465 |
files := `-- main.go -- |
| 466 |
package main |
| 467 |
import "fmt" |
| 468 |
func main() { |
| 469 |
fmt.Println(xurls.Relaxed().FindAllString()) |
| 470 |
} |
| 471 |
-- go.mod -- |
| 472 |
module demo |
| 473 |
go 1.20 |
| 474 |
-- a.go -- |
| 475 |
package main |
| 476 |
import "github.com/mvdan/xurls" |
| 477 |
var _ = xurls.Relaxed() |
| 478 |
` |
| 479 |
cache := `-- mvdan.cc/xurls/[email protected]/xurls.go -- |
| 480 |
package xurls |
| 481 |
func Relaxed() *regexp.Regexp { |
| 482 |
return nil |
| 483 |
} |
| 484 |
-- github.com/mvdan/[email protected]/xurls.go -- |
| 485 |
package xurls |
| 486 |
func Relaxed() *regexp.Regexp { |
| 487 |
return nil |
| 488 |
} |
| 489 |
` |
| 490 |
modcache := t.TempDir() |
| 491 |
defer CleanModCache(t, modcache) |
| 492 |
mx := fake.UnpackTxt(cache) |
| 493 |
for k, v := range mx { |
| 494 |
fname := filepath.Join(modcache, k) |
| 495 |
dir := filepath.Dir(fname) |
| 496 |
os.MkdirAll(dir, 0777) |
| 497 |
if err := os.WriteFile(fname, v, 0644); err != nil { |
| 498 |
t.Fatal(err) |
| 499 |
} |
| 500 |
} |
| 501 |
WithOptions( |
| 502 |
EnvVars{"GOMODCACHE": modcache}, |
| 503 |
WriteGoSum("."), |
| 504 |
).Run(t, files, func(t *testing.T, env *Env) { |
| 505 |
env.OpenFile("main.go") |
| 506 |
env.SaveBuffer("main.go") |
| 507 |
out := env.BufferText("main.go") |
| 508 |
if !strings.Contains(out, "github.com/mvdan/xurls") { |
| 509 |
t.Errorf("did not get github.com/mvdan/xurls in %q", out) |
| 510 |
} |
| 511 |
}) |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
func Test44510(t *testing.T) { |
| 516 |
const files = `-- go.mod -- |
| 517 |
module test |
| 518 |
go 1.19 |
| 519 |
-- foo/foo.go -- |
| 520 |
package main |
| 521 |
import strs "strings" |
| 522 |
var _ = strs.Count |
| 523 |
-- bar/bar.go -- |
| 524 |
package main |
| 525 |
var _ = strs.Builder |
| 526 |
` |
| 527 |
WithOptions( |
| 528 |
WriteGoSum("."), |
| 529 |
).Run(t, files, func(T *testing.T, env *Env) { |
| 530 |
env.OpenFile("bar/bar.go") |
| 531 |
env.SaveBuffer("bar/bar.go") |
| 532 |
buf := env.BufferText("bar/bar.go") |
| 533 |
if !strings.Contains(buf, "strs") { |
| 534 |
t.Error(buf) |
| 535 |
} |
| 536 |
}) |
| 537 |
} |
| 538 |
|
| 539 |
func TestIssue67156(t *testing.T) { |
| 540 |
const files = ` |
| 541 |
-- go.mod -- |
| 542 |
module mod.com/a |
| 543 |
|
| 544 |
go 1.20 |
| 545 |
|
| 546 |
require example.com v1.2.3 |
| 547 |
|
| 548 |
-- main.go -- |
| 549 |
package main |
| 550 |
|
| 551 |
import "example.com/x" |
| 552 |
|
| 553 |
var _, _ = x.X, y.Y |
| 554 |
` |
| 555 |
modcache := t.TempDir() |
| 556 |
base := filepath.Base(modcache) |
| 557 |
defer CleanModCache(t, modcache) |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
const sep = string(filepath.Separator) |
| 562 |
modcache += strings.Repeat(sep+".."+sep+base, 10) |
| 563 |
|
| 564 |
opts := []RunOption{ |
| 565 |
EnvVars{"GOMODCACHE": modcache}, |
| 566 |
ProxyFiles(exampleProxy), |
| 567 |
WriteGoSum("."), |
| 568 |
} |
| 569 |
|
| 570 |
t.Run("setup", func(t *testing.T) { |
| 571 |
|
| 572 |
WithOptions(opts...).Run(t, files, func(t *testing.T, env *Env) {}) |
| 573 |
|
| 574 |
|
| 575 |
if ix, err := modindex.Update(modcache); err != nil { |
| 576 |
t.Fatalf("failed to obtain updated module index: %v", err) |
| 577 |
} else if len(ix.Entries) != 2 { |
| 578 |
t.Fatalf("got %v, want 2 entries", ix) |
| 579 |
} |
| 580 |
}) |
| 581 |
|
| 582 |
WithOptions(opts...).Run(t, files, func(t *testing.T, env *Env) { |
| 583 |
env.OpenFile("main.go") |
| 584 |
env.AfterChange(Diagnostics(env.AtRegexp("main.go", `y.Y`))) |
| 585 |
env.SaveBuffer("main.go") |
| 586 |
env.AfterChange(NoDiagnostics(ForFile("main.go"))) |
| 587 |
}) |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
func TestAcceptImportsQuickFixTestVariant(t *testing.T) { |
| 592 |
const pkg = ` |
| 593 |
-- go.mod -- |
| 594 |
module mod.com |
| 595 |
|
| 596 |
go 1.12 |
| 597 |
-- a/a.go -- |
| 598 |
package a |
| 599 |
|
| 600 |
import ( |
| 601 |
"fmt" |
| 602 |
) |
| 603 |
|
| 604 |
func _() { |
| 605 |
fmt.Println("") |
| 606 |
os.Stat("") |
| 607 |
} |
| 608 |
-- a/a_test.go -- |
| 609 |
package a |
| 610 |
|
| 611 |
import ( |
| 612 |
"os" |
| 613 |
"testing" |
| 614 |
) |
| 615 |
|
| 616 |
func TestA(t *testing.T) { |
| 617 |
os.Stat("") |
| 618 |
} |
| 619 |
` |
| 620 |
Run(t, pkg, func(t *testing.T, env *Env) { |
| 621 |
env.OpenFile("a/a.go") |
| 622 |
var d protocol.PublishDiagnosticsParams |
| 623 |
env.AfterChange( |
| 624 |
Diagnostics(env.AtRegexp("a/a.go", "os.Stat")), |
| 625 |
ReadDiagnostics("a/a.go", &d), |
| 626 |
) |
| 627 |
env.ApplyQuickFixes("a/a.go", d.Diagnostics) |
| 628 |
env.AfterChange( |
| 629 |
NoDiagnostics(ForFile("a/a.go")), |
| 630 |
) |
| 631 |
}) |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
func TestQuickFixIssue70755(t *testing.T) { |
| 636 |
const files = ` |
| 637 |
-- go.mod -- |
| 638 |
module mod.com |
| 639 |
go 1.19.0 // with go 1.23.0 this fails on some builders |
| 640 |
-- bar/bar.go -- |
| 641 |
package notbar |
| 642 |
type NotBar struct {} |
| 643 |
-- baz/baz.go -- |
| 644 |
package baz |
| 645 |
type Baz struct {} |
| 646 |
-- foo/foo.go -- |
| 647 |
package foo |
| 648 |
type Foo struct { |
| 649 |
bar notbar.NotBar |
| 650 |
baz baz.Baz |
| 651 |
}` |
| 652 |
Run(t, files, func(t *testing.T, env *Env) { |
| 653 |
env.OpenFile("foo/foo.go") |
| 654 |
var d protocol.PublishDiagnosticsParams |
| 655 |
env.AfterChange(ReadDiagnostics("foo/foo.go", &d)) |
| 656 |
env.ApplyQuickFixes("foo/foo.go", d.Diagnostics) |
| 657 |
|
| 658 |
|
| 659 |
y := env.BufferText("foo/foo.go") |
| 660 |
if !strings.Contains(y, `notbar "mod.com/bar"`) { |
| 661 |
t.Error("quick fix did not find notbar") |
| 662 |
} |
| 663 |
env.SaveBuffer("foo/foo.go") |
| 664 |
env.AfterChange(NoDiagnostics(ForFile("foo/foo.go"))) |
| 665 |
}) |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
func TestGoWorkImports(t *testing.T) { |
| 670 |
const pkg = ` |
| 671 |
-- go.work -- |
| 672 |
go 1.19 |
| 673 |
|
| 674 |
use ( |
| 675 |
./caller |
| 676 |
./mod |
| 677 |
) |
| 678 |
-- caller/go.mod -- |
| 679 |
module caller.com |
| 680 |
|
| 681 |
go 1.18 |
| 682 |
|
| 683 |
require mod.com v0.0.0 |
| 684 |
|
| 685 |
replace mod.com => ../mod |
| 686 |
-- caller/caller.go -- |
| 687 |
package main |
| 688 |
|
| 689 |
func main() { |
| 690 |
a.Test() |
| 691 |
} |
| 692 |
-- mod/go.mod -- |
| 693 |
module mod.com |
| 694 |
|
| 695 |
go 1.18 |
| 696 |
-- mod/a/a.go -- |
| 697 |
package a |
| 698 |
|
| 699 |
func Test() { |
| 700 |
} |
| 701 |
` |
| 702 |
Run(t, pkg, func(t *testing.T, env *Env) { |
| 703 |
env.OpenFile("caller/caller.go") |
| 704 |
env.AfterChange(Diagnostics(env.AtRegexp("caller/caller.go", "a.Test"))) |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
env.SaveBuffer("caller/caller.go") |
| 709 |
env.AfterChange(NoDiagnostics(ForFile("caller/caller.go"))) |
| 710 |
}) |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
func TestDeprecated70736(t *testing.T) { |
| 715 |
t.Logf("GOOS %s, GARCH %s version %s", runtime.GOOS, runtime.GOARCH, runtime.Version()) |
| 716 |
files := `-- main.go -- |
| 717 |
package main |
| 718 |
func main() { |
| 719 |
var v = xurls.Relaxed().FindAllString() |
| 720 |
var w = xurls.A |
| 721 |
} |
| 722 |
-- go.mod -- |
| 723 |
module demo |
| 724 |
go 1.20 |
| 725 |
` |
| 726 |
cache := `-- mvdan.cc/xurls/[email protected]/xurls.go -- |
| 727 |
package xurls |
| 728 |
// Deprecated: |
| 729 |
func Relaxed() *regexp.Regexp { |
| 730 |
return nil |
| 731 |
} |
| 732 |
var A int |
| 733 |
-- github.com/mvdan/[email protected]/xurls.go -- |
| 734 |
package xurls |
| 735 |
func Relaxed() *regexp.Regexp { |
| 736 |
return nil |
| 737 |
} |
| 738 |
var A int |
| 739 |
` |
| 740 |
modcache := t.TempDir() |
| 741 |
defer CleanModCache(t, modcache) |
| 742 |
mx := fake.UnpackTxt(cache) |
| 743 |
for k, v := range mx { |
| 744 |
fname := filepath.Join(modcache, k) |
| 745 |
dir := filepath.Dir(fname) |
| 746 |
os.MkdirAll(dir, 0777) |
| 747 |
if err := os.WriteFile(fname, v, 0644); err != nil { |
| 748 |
t.Fatal(err) |
| 749 |
} |
| 750 |
} |
| 751 |
WithOptions( |
| 752 |
EnvVars{"GOMODCACHE": modcache}, |
| 753 |
WriteGoSum("."), |
| 754 |
).Run(t, files, func(t *testing.T, env *Env) { |
| 755 |
env.OpenFile("main.go") |
| 756 |
env.SaveBuffer("main.go") |
| 757 |
out := env.BufferText("main.go") |
| 758 |
if strings.Contains(out, "xurls/v2") { |
| 759 |
t.Errorf("chose deprecated v2 in %q", out) |
| 760 |
} |
| 761 |
}) |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
func TestTestImports(t *testing.T) { |
| 766 |
const pkg = ` |
| 767 |
-- go.work -- |
| 768 |
go 1.19 |
| 769 |
|
| 770 |
use ( |
| 771 |
./caller |
| 772 |
./mod |
| 773 |
./xxx |
| 774 |
) |
| 775 |
-- caller/go.mod -- |
| 776 |
module caller.com |
| 777 |
|
| 778 |
go 1.18 |
| 779 |
|
| 780 |
require mod.com v0.0.0 |
| 781 |
require xxx.com v0.0.0 |
| 782 |
|
| 783 |
replace mod.com => ../mod |
| 784 |
replace xxx.com => ../xxx |
| 785 |
-- caller/caller_test.go -- |
| 786 |
package main |
| 787 |
|
| 788 |
var _ = a.Test |
| 789 |
-- xxx/go.mod -- |
| 790 |
module xxx.com |
| 791 |
|
| 792 |
go 1.18 |
| 793 |
-- xxx/a/a_test.go -- |
| 794 |
package a |
| 795 |
|
| 796 |
func Test() { |
| 797 |
} |
| 798 |
-- mod/go.mod -- |
| 799 |
module mod.com |
| 800 |
|
| 801 |
go 1.18 |
| 802 |
-- mod/a/a.go -- |
| 803 |
package a |
| 804 |
|
| 805 |
func Test() { |
| 806 |
} |
| 807 |
` |
| 808 |
WithOptions(Modes(Default)).Run(t, pkg, func(t *testing.T, env *Env) { |
| 809 |
env.OpenFile("caller/caller_test.go") |
| 810 |
env.AfterChange(Diagnostics(env.AtRegexp("caller/caller_test.go", "a.Test"))) |
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
env.SaveBuffer("caller/caller_test.go") |
| 815 |
env.AfterChange(NoDiagnostics(ForFile("caller/caller_test.go"))) |
| 816 |
buf := env.BufferText("caller/caller_test.go") |
| 817 |
if !strings.Contains(buf, "mod.com/a") { |
| 818 |
t.Errorf("got %q, expected a mod.com/a", buf) |
| 819 |
} |
| 820 |
}) |
| 821 |
} |
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
func Test67973(t *testing.T) { |
| 826 |
const files = `-- go.mod -- |
| 827 |
module hello |
| 828 |
go 1.19 |
| 829 |
-- hello.go -- |
| 830 |
package main |
| 831 |
var _ = foo.Bar |
| 832 |
-- internal/foo/foo.go -- |
| 833 |
package bar |
| 834 |
func Bar() {} |
| 835 |
` |
| 836 |
Run(t, files, func(t *testing.T, env *Env) { |
| 837 |
env.OpenFile("hello.go") |
| 838 |
env.AfterChange(env.DoneWithOpen()) |
| 839 |
env.SaveBuffer("hello.go") |
| 840 |
env.OpenFile("internal/foo/foo.go") |
| 841 |
env.RegexpReplace("internal/foo/foo.go", "bar", "foo") |
| 842 |
env.SaveBuffer("internal/foo/foo.go") |
| 843 |
env.SaveBuffer("hello.go") |
| 844 |
buf := env.BufferText("hello.go") |
| 845 |
if !strings.Contains(buf, "internal/foo") { |
| 846 |
t.Errorf(`expected import "hello/internal/foo" but got %q`, buf) |
| 847 |
} |
| 848 |
}) |
| 849 |
} |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
func TestNoIndex(t *testing.T) { |
| 857 |
const files = `-- go.mod -- |
| 858 |
module foo |
| 859 |
go 1.24 |
| 860 |
-- ok.go -- |
| 861 |
package main |
| 862 |
var x = fmt.Println |
| 863 |
-- fail.go -- |
| 864 |
package main |
| 865 |
var x = foo.Var |
| 866 |
` |
| 867 |
modcache := t.TempDir() |
| 868 |
|
| 869 |
if err := os.Chmod(modcache, 0555); err != nil { |
| 870 |
t.Fatal(err) |
| 871 |
} |
| 872 |
|
| 873 |
ok := func(t *testing.T, env *Env) { |
| 874 |
env.OpenFile("ok.go") |
| 875 |
env.AfterChange(env.DoneWithOpen()) |
| 876 |
env.OrganizeImports("ok.go") |
| 877 |
env.AfterChange(NoDiagnostics(ForFile("a.go"))) |
| 878 |
buf := env.BufferText("ok.go") |
| 879 |
if !strings.Contains(buf, `import "fmt"`) { |
| 880 |
t.Errorf(`expected import "fmt" but got %q`, buf) |
| 881 |
} |
| 882 |
} |
| 883 |
|
| 884 |
notok := func(t *testing.T, env *Env) { |
| 885 |
env.OpenFile("fail.go") |
| 886 |
env.AfterChange(env.DoneWithOpen()) |
| 887 |
obuf := env.BufferText("fail.go") |
| 888 |
env.OrganizeImports("fail.go") |
| 889 |
buf := env.BufferText("fail.go") |
| 890 |
if !cmp.Equal(buf, obuf) { |
| 891 |
t.Errorf("unexpected change %q\n%s", obuf, cmp.Diff(obuf, buf)) |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
dir := t.TempDir() |
| 896 |
|
| 897 |
|
| 898 |
modindex.IndexDir = filepath.Join(dir, "nope") |
| 899 |
if err := os.MkdirAll(modindex.IndexDir, 0555); err != nil { |
| 900 |
t.Fatal(err) |
| 901 |
} |
| 902 |
|
| 903 |
|
| 904 |
WithOptions(Modes(Default), |
| 905 |
EnvVars{"GOMODCACHE": modcache}, |
| 906 |
).Run(t, files, ok) |
| 907 |
|
| 908 |
WithOptions(Modes(Default), |
| 909 |
EnvVars{"GOMODCACHE": modcache}, |
| 910 |
).Run(t, files, notok) |
| 911 |
|
| 912 |
|
| 913 |
modindex.IndexDir = "" |
| 914 |
|
| 915 |
WithOptions(Modes(Default), |
| 916 |
EnvVars{"GOMODCACHE": modcache}, |
| 917 |
).Run(t, files, ok) |
| 918 |
|
| 919 |
WithOptions(Modes(Default), |
| 920 |
EnvVars{"GOMODCACHE": modcache}, |
| 921 |
).Run(t, files, notok) |
| 922 |
} |