| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package misc |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"golang.org/x/tools/gopls/internal/protocol" |
| 13 |
"golang.org/x/tools/gopls/internal/test/compare" |
| 14 |
. "golang.org/x/tools/gopls/internal/test/integration" |
| 15 |
) |
| 16 |
|
| 17 |
func TestPrepareRenameMainPackage(t *testing.T) { |
| 18 |
const files = ` |
| 19 |
-- go.mod -- |
| 20 |
module mod.com |
| 21 |
|
| 22 |
go 1.18 |
| 23 |
-- main.go -- |
| 24 |
package main |
| 25 |
|
| 26 |
import ( |
| 27 |
"fmt" |
| 28 |
) |
| 29 |
|
| 30 |
func main() { |
| 31 |
fmt.Println(1) |
| 32 |
} |
| 33 |
` |
| 34 |
const wantErr = "can't rename package \"main\"" |
| 35 |
Run(t, files, func(t *testing.T, env *Env) { |
| 36 |
env.OpenFile("main.go") |
| 37 |
loc := env.RegexpSearch("main.go", `main`) |
| 38 |
params := &protocol.PrepareRenameParams{ |
| 39 |
TextDocumentPositionParams: protocol.LocationTextDocumentPositionParams(loc), |
| 40 |
} |
| 41 |
_, err := env.Editor.Server.PrepareRename(env.Ctx, params) |
| 42 |
if err == nil { |
| 43 |
t.Errorf("missing can't rename package main error from PrepareRename") |
| 44 |
} |
| 45 |
|
| 46 |
if err.Error() != wantErr { |
| 47 |
t.Errorf("got %v, want %v", err.Error(), wantErr) |
| 48 |
} |
| 49 |
}) |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
func TestRenameWithUnsafeSlice(t *testing.T) { |
| 54 |
const files = ` |
| 55 |
-- go.mod -- |
| 56 |
module mod.com |
| 57 |
|
| 58 |
go 1.18 |
| 59 |
-- p.go -- |
| 60 |
package p |
| 61 |
|
| 62 |
import "unsafe" |
| 63 |
|
| 64 |
type T struct{} |
| 65 |
|
| 66 |
func (T) M() {} |
| 67 |
|
| 68 |
func _() { |
| 69 |
x := [3]int{1, 2, 3} |
| 70 |
ptr := unsafe.Pointer(&x) |
| 71 |
_ = unsafe.Slice((*int)(ptr), 3) |
| 72 |
} |
| 73 |
` |
| 74 |
|
| 75 |
Run(t, files, func(t *testing.T, env *Env) { |
| 76 |
env.OpenFile("p.go") |
| 77 |
env.Rename(env.RegexpSearch("p.go", "M"), "N") |
| 78 |
}) |
| 79 |
} |
| 80 |
|
| 81 |
func TestPrepareRenameWithNoPackageDeclaration(t *testing.T) { |
| 82 |
const files = ` |
| 83 |
go 1.14 |
| 84 |
-- lib/a.go -- |
| 85 |
import "fmt" |
| 86 |
|
| 87 |
const A = 1 |
| 88 |
|
| 89 |
func bar() { |
| 90 |
fmt.Println("Bar") |
| 91 |
} |
| 92 |
|
| 93 |
-- main.go -- |
| 94 |
package main |
| 95 |
|
| 96 |
import "fmt" |
| 97 |
|
| 98 |
func main() { |
| 99 |
fmt.Println("Hello") |
| 100 |
} |
| 101 |
` |
| 102 |
Run(t, files, func(t *testing.T, env *Env) { |
| 103 |
env.OpenFile("lib/a.go") |
| 104 |
err := env.Editor.Rename(env.Ctx, env.RegexpSearch("lib/a.go", "fmt"), "fmt1") |
| 105 |
if got, want := fmt.Sprint(err), "no identifier found"; got != want { |
| 106 |
t.Errorf("Rename: got error %v, want %v", got, want) |
| 107 |
} |
| 108 |
}) |
| 109 |
} |
| 110 |
|
| 111 |
func TestPrepareRenameFailWithUnknownModule(t *testing.T) { |
| 112 |
const files = ` |
| 113 |
go 1.14 |
| 114 |
-- lib/a.go -- |
| 115 |
package lib |
| 116 |
|
| 117 |
const A = 1 |
| 118 |
|
| 119 |
-- main.go -- |
| 120 |
package main |
| 121 |
|
| 122 |
import ( |
| 123 |
"mod.com/lib" |
| 124 |
) |
| 125 |
|
| 126 |
func main() { |
| 127 |
println("Hello") |
| 128 |
} |
| 129 |
` |
| 130 |
const wantErr = "can't rename package: missing module information for package" |
| 131 |
Run(t, files, func(t *testing.T, env *Env) { |
| 132 |
loc := env.RegexpSearch("lib/a.go", "lib") |
| 133 |
params := &protocol.PrepareRenameParams{ |
| 134 |
TextDocumentPositionParams: protocol.LocationTextDocumentPositionParams(loc), |
| 135 |
} |
| 136 |
_, err := env.Editor.Server.PrepareRename(env.Ctx, params) |
| 137 |
if err == nil || !strings.Contains(err.Error(), wantErr) { |
| 138 |
t.Errorf("missing cannot rename packages with unknown module from PrepareRename") |
| 139 |
} |
| 140 |
}) |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
func TestRenamePackageWithConflicts(t *testing.T) { |
| 146 |
const files = ` |
| 147 |
-- go.mod -- |
| 148 |
module mod.com |
| 149 |
|
| 150 |
go 1.18 |
| 151 |
-- lib/a.go -- |
| 152 |
package lib |
| 153 |
|
| 154 |
const A = 1 |
| 155 |
|
| 156 |
-- lib/nested/a.go -- |
| 157 |
package nested |
| 158 |
|
| 159 |
const B = 1 |
| 160 |
|
| 161 |
-- lib/x/a.go -- |
| 162 |
package nested1 |
| 163 |
|
| 164 |
const C = 1 |
| 165 |
|
| 166 |
-- main.go -- |
| 167 |
package main |
| 168 |
|
| 169 |
import ( |
| 170 |
"mod.com/lib" |
| 171 |
"mod.com/lib/nested" |
| 172 |
nested1 "mod.com/lib/x" |
| 173 |
) |
| 174 |
|
| 175 |
func main() { |
| 176 |
println("Hello") |
| 177 |
} |
| 178 |
` |
| 179 |
Run(t, files, func(t *testing.T, env *Env) { |
| 180 |
env.OpenFile("lib/a.go") |
| 181 |
env.Rename(env.RegexpSearch("lib/a.go", "lib"), "nested") |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
env.RegexpSearch("nested/a.go", "package nested") |
| 186 |
env.RegexpSearch("main.go", `nested2 "mod.com/nested"`) |
| 187 |
env.RegexpSearch("main.go", "mod.com/nested") |
| 188 |
env.RegexpSearch("main.go", "mod.com/lib/nested") |
| 189 |
env.RegexpSearch("main.go", `nested1 "mod.com/lib/x"`) |
| 190 |
}) |
| 191 |
|
| 192 |
WithOptions(Settings{"renameMovesSubpackages": true}).Run(t, files, func(t *testing.T, env *Env) { |
| 193 |
env.OpenFile("lib/a.go") |
| 194 |
env.Rename(env.RegexpSearch("lib/a.go", "lib"), "nested") |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
env.RegexpSearch("nested/a.go", "package nested") |
| 199 |
env.RegexpSearch("main.go", `nested2 "mod.com/nested"`) |
| 200 |
env.RegexpSearch("main.go", "mod.com/nested") |
| 201 |
env.RegexpSearch("main.go", "mod.com/nested/nested") |
| 202 |
env.RegexpSearch("main.go", `nested1 "mod.com/nested/x"`) |
| 203 |
}) |
| 204 |
} |
| 205 |
|
| 206 |
func TestRenamePackageWithAlias(t *testing.T) { |
| 207 |
const files = ` |
| 208 |
-- go.mod -- |
| 209 |
module mod.com |
| 210 |
|
| 211 |
go 1.18 |
| 212 |
-- lib/a.go -- |
| 213 |
package lib |
| 214 |
|
| 215 |
const A = 1 |
| 216 |
|
| 217 |
-- lib/nested/a.go -- |
| 218 |
package nested |
| 219 |
|
| 220 |
const B = 1 |
| 221 |
|
| 222 |
-- main.go -- |
| 223 |
package main |
| 224 |
|
| 225 |
import ( |
| 226 |
"mod.com/lib" |
| 227 |
lib1 "mod.com/lib/nested" |
| 228 |
) |
| 229 |
|
| 230 |
func main() { |
| 231 |
println("Hello") |
| 232 |
} |
| 233 |
` |
| 234 |
Run(t, files, func(t *testing.T, env *Env) { |
| 235 |
env.OpenFile("lib/a.go") |
| 236 |
env.Rename(env.RegexpSearch("lib/a.go", "lib"), "nested") |
| 237 |
|
| 238 |
|
| 239 |
env.RegexpSearch("nested/a.go", "package nested") |
| 240 |
env.RegexpSearch("main.go", "mod.com/nested") |
| 241 |
env.RegexpSearch("main.go", `lib1 "mod.com/lib/nested"`) |
| 242 |
}) |
| 243 |
} |
| 244 |
|
| 245 |
func TestRenamePackageWithDifferentDirectoryPath(t *testing.T) { |
| 246 |
const files = ` |
| 247 |
-- go.mod -- |
| 248 |
module mod.com |
| 249 |
|
| 250 |
go 1.18 |
| 251 |
-- lib/a.go -- |
| 252 |
package lib |
| 253 |
|
| 254 |
const A = 1 |
| 255 |
|
| 256 |
-- lib/nested/a.go -- |
| 257 |
package foo |
| 258 |
|
| 259 |
const B = 1 |
| 260 |
|
| 261 |
-- main.go -- |
| 262 |
package main |
| 263 |
|
| 264 |
import ( |
| 265 |
"mod.com/lib" |
| 266 |
foo "mod.com/lib/nested" |
| 267 |
) |
| 268 |
|
| 269 |
func main() { |
| 270 |
println("Hello") |
| 271 |
} |
| 272 |
` |
| 273 |
Run(t, files, func(t *testing.T, env *Env) { |
| 274 |
env.OpenFile("lib/a.go") |
| 275 |
env.Rename(env.RegexpSearch("lib/a.go", "lib"), "nested") |
| 276 |
|
| 277 |
|
| 278 |
env.RegexpSearch("nested/a.go", "package nested") |
| 279 |
env.RegexpSearch("main.go", "mod.com/nested") |
| 280 |
|
| 281 |
env.RegexpSearch("main.go", `foo "mod.com/lib/nested"`) |
| 282 |
}) |
| 283 |
|
| 284 |
WithOptions(Settings{"renameMovesSubpackages": true}).Run(t, files, func(t *testing.T, env *Env) { |
| 285 |
env.OpenFile("lib/a.go") |
| 286 |
env.Rename(env.RegexpSearch("lib/a.go", "lib"), "nested") |
| 287 |
|
| 288 |
|
| 289 |
env.RegexpSearch("nested/a.go", "package nested") |
| 290 |
env.RegexpSearch("main.go", "mod.com/nested") |
| 291 |
|
| 292 |
env.RegexpSearch("main.go", `foo "mod.com/nested/nested"`) |
| 293 |
}) |
| 294 |
} |
| 295 |
|
| 296 |
func TestRenamePackage(t *testing.T) { |
| 297 |
const files = ` |
| 298 |
-- go.mod -- |
| 299 |
module mod.com |
| 300 |
|
| 301 |
go 1.18 |
| 302 |
-- lib/a.go -- |
| 303 |
package lib |
| 304 |
|
| 305 |
const A = 1 |
| 306 |
|
| 307 |
-- lib/b.go -- |
| 308 |
package lib |
| 309 |
|
| 310 |
const B = 1 |
| 311 |
|
| 312 |
-- lib/nested/a.go -- |
| 313 |
package nested |
| 314 |
|
| 315 |
const C = 1 |
| 316 |
|
| 317 |
-- main.go -- |
| 318 |
package main |
| 319 |
|
| 320 |
import ( |
| 321 |
"mod.com/lib" |
| 322 |
"mod.com/lib/nested" |
| 323 |
) |
| 324 |
|
| 325 |
func main() { |
| 326 |
println("Hello") |
| 327 |
} |
| 328 |
` |
| 329 |
Run(t, files, func(t *testing.T, env *Env) { |
| 330 |
env.OpenFile("lib/a.go") |
| 331 |
env.Rename(env.RegexpSearch("lib/a.go", "lib"), "lib1") |
| 332 |
|
| 333 |
|
| 334 |
env.RegexpSearch("lib1/a.go", "package lib1") |
| 335 |
env.RegexpSearch("lib1/b.go", "package lib1") |
| 336 |
env.RegexpSearch("main.go", "mod.com/lib1") |
| 337 |
env.RegexpSearch("main.go", "mod.com/lib/nested") |
| 338 |
}) |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
func TestRenameInTestVariant(t *testing.T) { |
| 343 |
const files = ` |
| 344 |
-- go.mod -- |
| 345 |
module mod.com |
| 346 |
|
| 347 |
go 1.12 |
| 348 |
-- stringutil/stringutil.go -- |
| 349 |
package stringutil |
| 350 |
|
| 351 |
func Identity(s string) string { |
| 352 |
return s |
| 353 |
} |
| 354 |
-- stringutil/stringutil_test.go -- |
| 355 |
package stringutil |
| 356 |
|
| 357 |
func TestIdentity(t *testing.T) { |
| 358 |
if got := Identity("foo"); got != "foo" { |
| 359 |
t.Errorf("bad") |
| 360 |
} |
| 361 |
} |
| 362 |
-- main.go -- |
| 363 |
package main |
| 364 |
|
| 365 |
import ( |
| 366 |
"fmt" |
| 367 |
|
| 368 |
"mod.com/stringutil" |
| 369 |
) |
| 370 |
|
| 371 |
func main() { |
| 372 |
fmt.Println(stringutil.Identity("hello world")) |
| 373 |
} |
| 374 |
` |
| 375 |
|
| 376 |
Run(t, files, func(t *testing.T, env *Env) { |
| 377 |
env.OpenFile("main.go") |
| 378 |
env.Rename(env.RegexpSearch("main.go", `stringutil\.(Identity)`), "Identityx") |
| 379 |
env.OpenFile("stringutil/stringutil_test.go") |
| 380 |
text := env.BufferText("stringutil/stringutil_test.go") |
| 381 |
if !strings.Contains(text, "Identityx") { |
| 382 |
t.Errorf("stringutil/stringutil_test.go: missing expected token `Identityx` after rename:\n%s", text) |
| 383 |
} |
| 384 |
}) |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
func TestRenameFileFromEditor(t *testing.T) { |
| 389 |
const files = ` |
| 390 |
-- go.mod -- |
| 391 |
module mod.com |
| 392 |
|
| 393 |
go 1.16 |
| 394 |
-- a/a.go -- |
| 395 |
package a |
| 396 |
|
| 397 |
const X = 1 |
| 398 |
-- a/x.go -- |
| 399 |
package a |
| 400 |
|
| 401 |
const X = 2 |
| 402 |
-- b/b.go -- |
| 403 |
package b |
| 404 |
` |
| 405 |
|
| 406 |
Run(t, files, func(t *testing.T, env *Env) { |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
env.OnceMet( |
| 411 |
InitialWorkspaceLoad, |
| 412 |
Diagnostics(env.AtRegexp("a/a.go", "X")), |
| 413 |
Diagnostics(env.AtRegexp("a/x.go", "X")), |
| 414 |
) |
| 415 |
|
| 416 |
|
| 417 |
env.RenameFile("a/x.go", "b/x.go") |
| 418 |
env.AfterChange( |
| 419 |
NoDiagnostics(ForFile("a/a.go")), |
| 420 |
Diagnostics(env.AtRegexp("b/b.go", "package")), |
| 421 |
) |
| 422 |
|
| 423 |
|
| 424 |
env.OpenFile("b/x.go") |
| 425 |
|
| 426 |
|
| 427 |
env.RenameFile("b/x.go", "a/x.go") |
| 428 |
env.AfterChange( |
| 429 |
Diagnostics(env.AtRegexp("a/a.go", "X")), |
| 430 |
Diagnostics(env.AtRegexp("a/x.go", "X")), |
| 431 |
) |
| 432 |
|
| 433 |
|
| 434 |
env.RenameFile("a", "x") |
| 435 |
env.AfterChange( |
| 436 |
Diagnostics(env.AtRegexp("x/a.go", "X")), |
| 437 |
Diagnostics(env.AtRegexp("x/x.go", "X")), |
| 438 |
) |
| 439 |
|
| 440 |
|
| 441 |
if text := env.BufferText("x/x.go"); text == "" { |
| 442 |
t.Fatal("got empty buffer for x/x.go") |
| 443 |
} |
| 444 |
}) |
| 445 |
} |
| 446 |
|
| 447 |
func TestRenamePackage_Tests(t *testing.T) { |
| 448 |
const files = ` |
| 449 |
-- go.mod -- |
| 450 |
module mod.com |
| 451 |
|
| 452 |
go 1.18 |
| 453 |
-- lib/a.go -- |
| 454 |
package lib |
| 455 |
|
| 456 |
const A = 1 |
| 457 |
|
| 458 |
-- lib/b.go -- |
| 459 |
package lib |
| 460 |
|
| 461 |
const B = 1 |
| 462 |
|
| 463 |
-- lib/a_test.go -- |
| 464 |
package lib_test |
| 465 |
|
| 466 |
import ( |
| 467 |
"mod.com/lib" |
| 468 |
"fmt |
| 469 |
) |
| 470 |
|
| 471 |
const C = 1 |
| 472 |
|
| 473 |
-- lib/b_test.go -- |
| 474 |
package lib |
| 475 |
|
| 476 |
import ( |
| 477 |
"fmt |
| 478 |
) |
| 479 |
|
| 480 |
const D = 1 |
| 481 |
|
| 482 |
-- lib/nested/a.go -- |
| 483 |
package nested |
| 484 |
|
| 485 |
const D = 1 |
| 486 |
|
| 487 |
-- main.go -- |
| 488 |
package main |
| 489 |
|
| 490 |
import ( |
| 491 |
"mod.com/lib" |
| 492 |
"mod.com/lib/nested" |
| 493 |
) |
| 494 |
|
| 495 |
func main() { |
| 496 |
println("Hello") |
| 497 |
} |
| 498 |
` |
| 499 |
Run(t, files, func(t *testing.T, env *Env) { |
| 500 |
env.OpenFile("lib/a.go") |
| 501 |
env.Rename(env.RegexpSearch("lib/a.go", "lib"), "lib1") |
| 502 |
|
| 503 |
|
| 504 |
env.RegexpSearch("lib1/a.go", "package lib1") |
| 505 |
env.RegexpSearch("lib1/b.go", "package lib1") |
| 506 |
env.RegexpSearch("main.go", "mod.com/lib1") |
| 507 |
env.RegexpSearch("main.go", "mod.com/lib/nested") |
| 508 |
|
| 509 |
|
| 510 |
env.RegexpSearch("lib1/a_test.go", "package lib1_test") |
| 511 |
env.RegexpSearch("lib1/b_test.go", "package lib1") |
| 512 |
}) |
| 513 |
} |
| 514 |
|
| 515 |
func TestRenamePackage_NestedModule(t *testing.T) { |
| 516 |
const files = ` |
| 517 |
-- go.work -- |
| 518 |
go 1.18 |
| 519 |
use ( |
| 520 |
. |
| 521 |
./foo/bar |
| 522 |
./foo/baz |
| 523 |
) |
| 524 |
|
| 525 |
-- go.mod -- |
| 526 |
module mod.com |
| 527 |
|
| 528 |
go 1.18 |
| 529 |
|
| 530 |
require ( |
| 531 |
mod.com/foo/bar v0.0.0 |
| 532 |
) |
| 533 |
|
| 534 |
replace ( |
| 535 |
mod.com/foo/bar => ./foo/bar |
| 536 |
mod.com/foo/baz => ./foo/baz |
| 537 |
) |
| 538 |
-- foo/foo.go -- |
| 539 |
package foo |
| 540 |
|
| 541 |
import "fmt" |
| 542 |
|
| 543 |
func Bar() { |
| 544 |
fmt.Println("In foo before renamed to foox.") |
| 545 |
} |
| 546 |
|
| 547 |
-- foo/bar/go.mod -- |
| 548 |
module mod.com/foo/bar |
| 549 |
|
| 550 |
-- foo/bar/bar.go -- |
| 551 |
package bar |
| 552 |
|
| 553 |
const Msg = "Hi from package bar" |
| 554 |
|
| 555 |
-- foo/baz/go.mod -- |
| 556 |
module mod.com/foo/baz |
| 557 |
|
| 558 |
-- foo/baz/baz.go -- |
| 559 |
package baz |
| 560 |
|
| 561 |
const Msg = "Hi from package baz" |
| 562 |
|
| 563 |
-- main.go -- |
| 564 |
package main |
| 565 |
|
| 566 |
import ( |
| 567 |
"fmt" |
| 568 |
"mod.com/foo/bar" |
| 569 |
"mod.com/foo/baz" |
| 570 |
"mod.com/foo" |
| 571 |
) |
| 572 |
|
| 573 |
func main() { |
| 574 |
foo.Bar() |
| 575 |
fmt.Println(bar.Msg) |
| 576 |
fmt.Println(baz.Msg) |
| 577 |
} |
| 578 |
` |
| 579 |
Run(t, files, func(t *testing.T, env *Env) { |
| 580 |
env.OpenFile("foo/foo.go") |
| 581 |
env.Rename(env.RegexpSearch("foo/foo.go", "foo"), "foox") |
| 582 |
|
| 583 |
env.RegexpSearch("foox/foo.go", "package foox") |
| 584 |
|
| 585 |
env.OpenFile("foo/bar/bar.go") |
| 586 |
env.OpenFile("foo/bar/go.mod") |
| 587 |
|
| 588 |
env.RegexpSearch("main.go", "mod.com/foo/bar") |
| 589 |
env.RegexpSearch("main.go", "mod.com/foox") |
| 590 |
env.RegexpSearch("main.go", "foox.Bar()") |
| 591 |
|
| 592 |
env.RegexpSearch("go.mod", "./foo/bar") |
| 593 |
env.RegexpSearch("go.mod", "./foo/baz") |
| 594 |
}) |
| 595 |
|
| 596 |
WithOptions(Settings{"renameMovesSubpackages": true}).Run(t, files, func(t *testing.T, env *Env) { |
| 597 |
env.OpenFile("foo/foo.go") |
| 598 |
env.Rename(env.RegexpSearch("foo/foo.go", "foo"), "foox") |
| 599 |
|
| 600 |
env.RegexpSearch("foox/foo.go", "package foox") |
| 601 |
|
| 602 |
env.OpenFile("foox/bar/bar.go") |
| 603 |
env.OpenFile("foox/bar/go.mod") |
| 604 |
|
| 605 |
env.RegexpSearch("main.go", "mod.com/foo/bar") |
| 606 |
env.RegexpSearch("main.go", "mod.com/foox") |
| 607 |
env.RegexpSearch("main.go", "foox.Bar()") |
| 608 |
|
| 609 |
env.RegexpSearch("go.mod", "./foox/bar") |
| 610 |
env.RegexpSearch("go.mod", "./foox/baz") |
| 611 |
}) |
| 612 |
} |
| 613 |
|
| 614 |
func TestRenamePackage_DuplicateImport(t *testing.T) { |
| 615 |
const files = ` |
| 616 |
-- go.mod -- |
| 617 |
module mod.com |
| 618 |
|
| 619 |
go 1.18 |
| 620 |
-- lib/a.go -- |
| 621 |
package lib |
| 622 |
|
| 623 |
const A = 1 |
| 624 |
|
| 625 |
-- lib/nested/a.go -- |
| 626 |
package nested |
| 627 |
|
| 628 |
const B = 1 |
| 629 |
|
| 630 |
-- main.go -- |
| 631 |
package main |
| 632 |
|
| 633 |
import ( |
| 634 |
"mod.com/lib" |
| 635 |
lib1 "mod.com/lib" |
| 636 |
lib2 "mod.com/lib/nested" |
| 637 |
) |
| 638 |
|
| 639 |
func main() { |
| 640 |
println("Hello") |
| 641 |
} |
| 642 |
` |
| 643 |
Run(t, files, func(t *testing.T, env *Env) { |
| 644 |
env.OpenFile("lib/a.go") |
| 645 |
env.Rename(env.RegexpSearch("lib/a.go", "lib"), "nested") |
| 646 |
|
| 647 |
|
| 648 |
env.RegexpSearch("nested/a.go", "package nested") |
| 649 |
env.RegexpSearch("main.go", "mod.com/nested") |
| 650 |
env.RegexpSearch("main.go", `lib1 "mod.com/nested"`) |
| 651 |
|
| 652 |
env.RegexpSearch("main.go", `lib2 "mod.com/lib/nested"`) |
| 653 |
}) |
| 654 |
|
| 655 |
WithOptions(Settings{"renameMovesSubpackages": true}).Run(t, files, func(t *testing.T, env *Env) { |
| 656 |
env.OpenFile("lib/a.go") |
| 657 |
env.Rename(env.RegexpSearch("lib/a.go", "lib"), "nested") |
| 658 |
|
| 659 |
|
| 660 |
env.RegexpSearch("nested/a.go", "package nested") |
| 661 |
env.RegexpSearch("main.go", "mod.com/nested") |
| 662 |
env.RegexpSearch("main.go", `lib1 "mod.com/nested"`) |
| 663 |
|
| 664 |
env.RegexpSearch("main.go", `lib2 "mod.com/nested/nested"`) |
| 665 |
}) |
| 666 |
} |
| 667 |
|
| 668 |
func TestRenamePackage_DuplicateBlankImport(t *testing.T) { |
| 669 |
const files = ` |
| 670 |
-- go.mod -- |
| 671 |
module mod.com |
| 672 |
|
| 673 |
go 1.18 |
| 674 |
-- lib/a.go -- |
| 675 |
package lib |
| 676 |
|
| 677 |
const A = 1 |
| 678 |
|
| 679 |
-- lib/nested/a.go -- |
| 680 |
package nested |
| 681 |
|
| 682 |
const B = 1 |
| 683 |
|
| 684 |
-- main.go -- |
| 685 |
package main |
| 686 |
|
| 687 |
import ( |
| 688 |
"mod.com/lib" |
| 689 |
_ "mod.com/lib" |
| 690 |
lib1 "mod.com/lib/nested" |
| 691 |
) |
| 692 |
|
| 693 |
func main() { |
| 694 |
println("Hello") |
| 695 |
} |
| 696 |
` |
| 697 |
Run(t, files, func(t *testing.T, env *Env) { |
| 698 |
env.OpenFile("lib/a.go") |
| 699 |
env.Rename(env.RegexpSearch("lib/a.go", "lib"), "nested") |
| 700 |
|
| 701 |
|
| 702 |
env.RegexpSearch("nested/a.go", "package nested") |
| 703 |
env.RegexpSearch("main.go", "mod.com/nested") |
| 704 |
env.RegexpSearch("main.go", `_ "mod.com/nested"`) |
| 705 |
|
| 706 |
env.RegexpSearch("main.go", `lib1 "mod.com/lib/nested"`) |
| 707 |
}) |
| 708 |
|
| 709 |
WithOptions(Settings{"renameMovesSubpackages": true}).Run(t, files, func(t *testing.T, env *Env) { |
| 710 |
env.OpenFile("lib/a.go") |
| 711 |
env.Rename(env.RegexpSearch("lib/a.go", "lib"), "nested") |
| 712 |
|
| 713 |
|
| 714 |
env.RegexpSearch("nested/a.go", "package nested") |
| 715 |
env.RegexpSearch("main.go", "mod.com/nested") |
| 716 |
env.RegexpSearch("main.go", `_ "mod.com/nested"`) |
| 717 |
|
| 718 |
env.RegexpSearch("main.go", `lib1 "mod.com/nested/nested"`) |
| 719 |
}) |
| 720 |
} |
| 721 |
|
| 722 |
func TestRenamePackage_TestVariant(t *testing.T) { |
| 723 |
const files = ` |
| 724 |
-- go.mod -- |
| 725 |
module mod.com |
| 726 |
|
| 727 |
go 1.12 |
| 728 |
-- foo/foo.go -- |
| 729 |
package foo |
| 730 |
|
| 731 |
const Foo = 42 |
| 732 |
-- bar/bar.go -- |
| 733 |
package bar |
| 734 |
|
| 735 |
import "mod.com/foo" |
| 736 |
|
| 737 |
const Bar = foo.Foo |
| 738 |
-- bar/bar_test.go -- |
| 739 |
package bar |
| 740 |
|
| 741 |
import "mod.com/foo" |
| 742 |
|
| 743 |
const Baz = foo.Foo |
| 744 |
-- testdata/bar/bar.go -- |
| 745 |
package bar |
| 746 |
|
| 747 |
import "mod.com/foox" |
| 748 |
|
| 749 |
const Bar = foox.Foo |
| 750 |
-- testdata/bar/bar_test.go -- |
| 751 |
package bar |
| 752 |
|
| 753 |
import "mod.com/foox" |
| 754 |
|
| 755 |
const Baz = foox.Foo |
| 756 |
` |
| 757 |
Run(t, files, func(t *testing.T, env *Env) { |
| 758 |
env.OpenFile("foo/foo.go") |
| 759 |
env.Rename(env.RegexpSearch("foo/foo.go", "package (foo)"), "foox") |
| 760 |
|
| 761 |
checkTestdata(t, env) |
| 762 |
}) |
| 763 |
} |
| 764 |
|
| 765 |
func TestRenamePackage_IntermediateTestVariant(t *testing.T) { |
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
const files = ` |
| 775 |
-- go.mod -- |
| 776 |
module foo.mod |
| 777 |
|
| 778 |
go 1.12 |
| 779 |
-- foo/foo.go -- |
| 780 |
package foo |
| 781 |
|
| 782 |
import "foo.mod/bar" |
| 783 |
|
| 784 |
const Foo = 42 |
| 785 |
|
| 786 |
const _ = bar.Bar |
| 787 |
-- baz/baz.go -- |
| 788 |
package baz |
| 789 |
|
| 790 |
import "foo.mod/foo" |
| 791 |
|
| 792 |
const Baz = foo.Foo |
| 793 |
-- bar/bar.go -- |
| 794 |
package bar |
| 795 |
|
| 796 |
var Bar = 123 |
| 797 |
-- bar/bar_test.go -- |
| 798 |
package bar |
| 799 |
|
| 800 |
const _ = Bar |
| 801 |
-- bar/bar_x_test.go -- |
| 802 |
package bar_test |
| 803 |
|
| 804 |
import ( |
| 805 |
"foo.mod/bar" |
| 806 |
"foo.mod/baz" |
| 807 |
"foo.mod/foo" |
| 808 |
) |
| 809 |
|
| 810 |
const _ = bar.Bar + baz.Baz + foo.Foo |
| 811 |
-- testdata/foox/foo.go -- |
| 812 |
package foox |
| 813 |
|
| 814 |
import "foo.mod/bar" |
| 815 |
|
| 816 |
const Foo = 42 |
| 817 |
|
| 818 |
const _ = bar.Bar |
| 819 |
-- testdata/baz/baz.go -- |
| 820 |
package baz |
| 821 |
|
| 822 |
import "foo.mod/foox" |
| 823 |
|
| 824 |
const Baz = foox.Foo |
| 825 |
-- testdata/bar/bar_x_test.go -- |
| 826 |
package bar_test |
| 827 |
|
| 828 |
import ( |
| 829 |
"foo.mod/bar" |
| 830 |
"foo.mod/baz" |
| 831 |
"foo.mod/foox" |
| 832 |
) |
| 833 |
|
| 834 |
const _ = bar.Bar + baz.Baz + foox.Foo |
| 835 |
` |
| 836 |
|
| 837 |
Run(t, files, func(t *testing.T, env *Env) { |
| 838 |
env.OpenFile("foo/foo.go") |
| 839 |
env.Rename(env.RegexpSearch("foo/foo.go", "package (foo)"), "foox") |
| 840 |
|
| 841 |
checkTestdata(t, env) |
| 842 |
}) |
| 843 |
} |
| 844 |
|
| 845 |
func TestRenamePackage_Nesting(t *testing.T) { |
| 846 |
const files = ` |
| 847 |
-- go.mod -- |
| 848 |
module mod.com |
| 849 |
|
| 850 |
go 1.18 |
| 851 |
-- lib/a.go -- |
| 852 |
package lib |
| 853 |
|
| 854 |
import "mod.com/lib/nested" |
| 855 |
|
| 856 |
const A = 1 + nested.B |
| 857 |
-- lib/nested/a.go -- |
| 858 |
package nested |
| 859 |
|
| 860 |
const B = 1 |
| 861 |
-- other/other.go -- |
| 862 |
package other |
| 863 |
|
| 864 |
import ( |
| 865 |
"mod.com/lib" |
| 866 |
"mod.com/lib/nested" |
| 867 |
) |
| 868 |
|
| 869 |
const C = lib.A + nested.B |
| 870 |
-- testdata/libx/a.go -- |
| 871 |
package libx |
| 872 |
|
| 873 |
import "mod.com/lib/nested" |
| 874 |
|
| 875 |
const A = 1 + nested.B |
| 876 |
-- testdata/other/other.go -- |
| 877 |
package other |
| 878 |
|
| 879 |
import ( |
| 880 |
"mod.com/libx" |
| 881 |
"mod.com/lib/nested" |
| 882 |
) |
| 883 |
|
| 884 |
const C = libx.A + nested.B |
| 885 |
` |
| 886 |
Run(t, files, func(t *testing.T, env *Env) { |
| 887 |
env.OpenFile("lib/a.go") |
| 888 |
env.Rename(env.RegexpSearch("lib/a.go", "package (lib)"), "libx") |
| 889 |
|
| 890 |
checkTestdata(t, env) |
| 891 |
}) |
| 892 |
} |
| 893 |
|
| 894 |
func TestRenamePackage_InvalidName(t *testing.T) { |
| 895 |
const files = ` |
| 896 |
-- go.mod -- |
| 897 |
module mod.com |
| 898 |
|
| 899 |
go 1.18 |
| 900 |
-- lib/a.go -- |
| 901 |
package lib |
| 902 |
|
| 903 |
import "mod.com/lib/nested" |
| 904 |
|
| 905 |
const A = 1 + nested.B |
| 906 |
` |
| 907 |
|
| 908 |
Run(t, files, func(t *testing.T, env *Env) { |
| 909 |
env.OpenFile("lib/a.go") |
| 910 |
loc := env.RegexpSearch("lib/a.go", "package (lib)") |
| 911 |
|
| 912 |
for _, badName := range []string{"$$$", "lib_test"} { |
| 913 |
if err := env.Editor.Rename(env.Ctx, loc, badName); err == nil { |
| 914 |
t.Errorf("Rename(lib, libx) succeeded unexpectedly") |
| 915 |
} |
| 916 |
} |
| 917 |
}) |
| 918 |
} |
| 919 |
|
| 920 |
func TestRenamePackage_InternalPackage(t *testing.T) { |
| 921 |
const files = ` |
| 922 |
-- go.mod -- |
| 923 |
module mod.com |
| 924 |
|
| 925 |
go 1.18 |
| 926 |
-- lib/a.go -- |
| 927 |
package lib |
| 928 |
|
| 929 |
import ( |
| 930 |
"fmt" |
| 931 |
"mod.com/lib/internal/x" |
| 932 |
) |
| 933 |
|
| 934 |
const A = 1 |
| 935 |
|
| 936 |
func print() { |
| 937 |
fmt.Println(x.B) |
| 938 |
} |
| 939 |
|
| 940 |
-- lib/internal/x/a.go -- |
| 941 |
package x |
| 942 |
|
| 943 |
const B = 1 |
| 944 |
|
| 945 |
-- main.go -- |
| 946 |
package main |
| 947 |
|
| 948 |
import "mod.com/lib" |
| 949 |
|
| 950 |
func main() { |
| 951 |
lib.print() |
| 952 |
} |
| 953 |
` |
| 954 |
Run(t, files, func(t *testing.T, env *Env) { |
| 955 |
env.OpenFile("lib/internal/x/a.go") |
| 956 |
env.Rename(env.RegexpSearch("lib/internal/x/a.go", "x"), "utils") |
| 957 |
|
| 958 |
|
| 959 |
env.RegexpSearch("lib/a.go", "mod.com/lib/internal/utils") |
| 960 |
env.RegexpSearch("lib/a.go", "utils.B") |
| 961 |
|
| 962 |
|
| 963 |
env.RegexpSearch("lib/internal/utils/a.go", "package utils") |
| 964 |
|
| 965 |
env.OpenFile("lib/a.go") |
| 966 |
env.Rename(env.RegexpSearch("lib/a.go", "lib"), "lib1") |
| 967 |
|
| 968 |
|
| 969 |
env.RegexpSearch("lib1/a.go", "package lib1") |
| 970 |
|
| 971 |
env.RegexpSearch("lib1/a.go", "mod.com/lib/internal/utils") |
| 972 |
env.RegexpSearch("main.go", `import "mod.com/lib1"`) |
| 973 |
env.RegexpSearch("main.go", "lib1.print()") |
| 974 |
}) |
| 975 |
} |
| 976 |
func TestRenamePackage_InvalidPackageMove(t *testing.T) { |
| 977 |
const files = ` |
| 978 |
-- go.mod -- |
| 979 |
module mod.com |
| 980 |
|
| 981 |
go 1.21 |
| 982 |
-- test1/test1.go -- |
| 983 |
package test1 |
| 984 |
|
| 985 |
-- test2/test2.go -- |
| 986 |
package test2 |
| 987 |
|
| 988 |
` |
| 989 |
Run(t, files, func(t *testing.T, env *Env) { |
| 990 |
env.OpenFile("test1/test1.go") |
| 991 |
loc := env.RegexpSearch("test1/test1.go", "test1") |
| 992 |
for _, test := range []struct{ badName, expectedErrMatch string }{ |
| 993 |
{"test2", "invalid package identifier"}, |
| 994 |
{"mod.com/test2", "not empty"}, |
| 995 |
{"differentmod.com/test2", "cannot move package across module boundary"}, |
| 996 |
{"mod.com/$$$", "invalid package name"}, |
| 997 |
{"mod*.$+", "invalid package path"}, |
| 998 |
{"mod.commmm", "cannot move package across module boundary"}, |
| 999 |
} { |
| 1000 |
err := env.Editor.Rename(env.Ctx, loc, test.badName) |
| 1001 |
if err == nil { |
| 1002 |
t.Errorf("Rename to %s succeeded unexpectedly", test.badName) |
| 1003 |
} else if !strings.Contains(err.Error(), test.expectedErrMatch) { |
| 1004 |
t.Errorf("Rename to %s produced incorrect error message, got %s, want %s", test.badName, err.Error(), test.expectedErrMatch) |
| 1005 |
} |
| 1006 |
} |
| 1007 |
}) |
| 1008 |
} |
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
func checkTestdata(t *testing.T, env *Env) { |
| 1013 |
t.Helper() |
| 1014 |
files := env.ListFiles("testdata") |
| 1015 |
if len(files) == 0 { |
| 1016 |
t.Fatal("no files in testdata directory") |
| 1017 |
} |
| 1018 |
for _, file := range files { |
| 1019 |
suffix := strings.TrimPrefix(file, "testdata/") |
| 1020 |
got := env.BufferText(suffix) |
| 1021 |
want := env.ReadWorkspaceFile(file) |
| 1022 |
if diff := compare.Text(want, got); diff != "" { |
| 1023 |
t.Errorf("Rename: unexpected buffer content for %s (-want +got):\n%s", suffix, diff) |
| 1024 |
} |
| 1025 |
} |
| 1026 |
} |