github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/language/go/fix_test.go (about) 1 /* Copyright 2017 The Bazel Authors. All rights reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package golang 17 18 import ( 19 "path/filepath" 20 "testing" 21 22 "github.com/bazelbuild/bazel-gazelle/merger" 23 "github.com/bazelbuild/bazel-gazelle/rule" 24 ) 25 26 type fixTestCase struct { 27 desc, old, want string 28 namingConvention namingConvention 29 } 30 31 func TestFixFile(t *testing.T) { 32 for _, tc := range []fixTestCase{ 33 // migrateNamingConvention tests 34 { 35 desc: "go_naming_convention=go_default_library -> import for lib", 36 namingConvention: importNamingConvention, 37 old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 38 39 go_library( 40 name = "go_default_library", 41 srcs = ["foo.go"], 42 importpath = "example.com/foo", 43 ) 44 45 go_test( 46 name = "go_default_test", 47 srcs = ["foo_test.go"], 48 embed = [":go_default_library"], 49 ) 50 `, 51 want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 52 53 go_library( 54 name = "foo", 55 srcs = ["foo.go"], 56 importpath = "example.com/foo", 57 ) 58 59 go_test( 60 name = "foo_test", 61 srcs = ["foo_test.go"], 62 embed = [":foo"], 63 ) 64 `, 65 }, 66 { 67 desc: "go_naming_convention=go_default_library -> import for bin", 68 namingConvention: importNamingConvention, 69 old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 70 71 go_binary( 72 name = "foo", 73 embed = [":go_default_library"], 74 ) 75 76 go_library( 77 name = "go_default_library", 78 importpath = "example.com/foo", 79 srcs = ["foo.go"], 80 ) 81 82 go_test( 83 name = "go_default_test", 84 srcs = ["foo_test.go"], 85 embed = [":go_default_library"], 86 ) 87 `, 88 want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 89 90 go_binary( 91 name = "foo", 92 embed = [":foo_lib"], 93 ) 94 95 go_library( 96 name = "foo_lib", 97 srcs = ["foo.go"], 98 importpath = "example.com/foo", 99 ) 100 101 go_test( 102 name = "foo_test", 103 srcs = ["foo_test.go"], 104 embed = [":foo_lib"], 105 ) 106 `, 107 }, 108 { 109 desc: "go_naming_convention=go_default_library -> import conflict", 110 namingConvention: importNamingConvention, 111 old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 112 load(":build_defs.bzl", "x_binary") 113 114 go_library( 115 name = "go_default_library", 116 srcs = ["foo.go"], 117 importpath = "example.com/foo", 118 visibility = ["//visibility:private"], 119 ) 120 121 x_binary( 122 name = "foo", 123 ) 124 125 go_test( 126 name = "go_default_test", 127 srcs = ["foo_test.go"], 128 embed = [":go_default_library"], 129 ) 130 `, 131 want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 132 load(":build_defs.bzl", "x_binary") 133 134 go_library( 135 name = "go_default_library", 136 srcs = ["foo.go"], 137 importpath = "example.com/foo", 138 visibility = ["//visibility:private"], 139 ) 140 141 x_binary( 142 name = "foo", 143 ) 144 145 go_test( 146 name = "foo_test", 147 srcs = ["foo_test.go"], 148 embed = [":go_default_library"], 149 ) 150 `, 151 }, 152 { 153 desc: "go_naming_convention=import -> go_default_library for lib", 154 namingConvention: goDefaultLibraryNamingConvention, 155 old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 156 157 go_library( 158 name = "foo", 159 srcs = ["foo.go"], 160 importpath = "example.com/foo", 161 ) 162 163 go_test( 164 name = "foo_test", 165 srcs = ["foo_test.go"], 166 embed = [":foo"], 167 ) 168 `, 169 want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 170 171 go_library( 172 name = "go_default_library", 173 srcs = ["foo.go"], 174 importpath = "example.com/foo", 175 ) 176 177 go_test( 178 name = "go_default_test", 179 srcs = ["foo_test.go"], 180 embed = [":go_default_library"], 181 ) 182 `, 183 }, 184 { 185 desc: "go_naming_convention=import -> go_default_library for bin", 186 namingConvention: goDefaultLibraryNamingConvention, 187 old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 188 189 go_binary( 190 name = "foo", 191 embed = [":foo_lib"], 192 ) 193 194 go_library( 195 name = "foo_lib", 196 srcs = ["foo.go"], 197 importpath = "example.com/foo", 198 ) 199 200 go_test( 201 name = "foo_test", 202 srcs = ["foo_test.go"], 203 embed = [":foo_lib"], 204 ) 205 `, 206 want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 207 208 go_binary( 209 name = "foo", 210 embed = [":go_default_library"], 211 ) 212 213 go_library( 214 name = "go_default_library", 215 srcs = ["foo.go"], 216 importpath = "example.com/foo", 217 ) 218 219 go_test( 220 name = "go_default_test", 221 srcs = ["foo_test.go"], 222 embed = [":go_default_library"], 223 ) 224 `, 225 }, 226 { 227 desc: "go_naming_convention=go_default_library -> import_alias for lib", 228 namingConvention: importAliasNamingConvention, 229 old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 230 231 go_library( 232 name = "go_default_library", 233 srcs = ["foo.go"], 234 importpath = "example.com/foo", 235 visibility = ["//visibility:private"], 236 ) 237 238 go_test( 239 name = "go_default_test", 240 srcs = ["foo_test.go"], 241 embed = [":go_default_library"], 242 ) 243 `, 244 want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 245 246 go_library( 247 name = "foo", 248 srcs = ["foo.go"], 249 importpath = "example.com/foo", 250 visibility = ["//visibility:private"], 251 ) 252 253 go_test( 254 name = "foo_test", 255 srcs = ["foo_test.go"], 256 embed = [":foo"], 257 ) 258 `, 259 }, 260 { 261 desc: "go_naming_convention=import_alias -> go_default_library for lib", 262 namingConvention: goDefaultLibraryNamingConvention, 263 old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 264 265 go_library( 266 name = "foo", 267 srcs = ["foo.go"], 268 importpath = "example.com/foo", 269 ) 270 271 go_test( 272 name = "foo_test", 273 srcs = ["foo_test.go"], 274 embed = [":foo"], 275 ) 276 `, 277 want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 278 279 go_library( 280 name = "go_default_library", 281 srcs = ["foo.go"], 282 importpath = "example.com/foo", 283 ) 284 285 go_test( 286 name = "go_default_test", 287 srcs = ["foo_test.go"], 288 embed = [":go_default_library"], 289 ) 290 `, 291 }, 292 { 293 desc: "go_naming_convention=go_default_library -> import_alias for bin", 294 namingConvention: importAliasNamingConvention, 295 old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 296 297 go_binary( 298 name = "foo", 299 embed = [":go_default_library"], 300 ) 301 302 go_library( 303 name = "go_default_library", 304 srcs = ["foo.go"], 305 importpath = "example.com/foo", 306 ) 307 308 go_test( 309 name = "go_default_test", 310 srcs = ["foo_test.go"], 311 embed = [":go_default_library"], 312 ) 313 `, 314 want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 315 316 go_binary( 317 name = "foo", 318 embed = [":foo_lib"], 319 ) 320 321 go_library( 322 name = "foo_lib", 323 srcs = ["foo.go"], 324 importpath = "example.com/foo", 325 ) 326 327 go_test( 328 name = "foo_test", 329 srcs = ["foo_test.go"], 330 embed = [":foo_lib"], 331 ) 332 `, 333 }, 334 { 335 desc: "go_naming_convention=import -> import_alias for lib", 336 namingConvention: importAliasNamingConvention, 337 old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 338 339 go_library( 340 name = "foo", 341 srcs = ["foo.go"], 342 importpath = "example.com/foo", 343 ) 344 345 go_test( 346 name = "foo_test", 347 srcs = ["foo_test.go"], 348 embed = [":foo"], 349 ) 350 `, 351 want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 352 353 go_library( 354 name = "foo", 355 srcs = ["foo.go"], 356 importpath = "example.com/foo", 357 ) 358 359 go_test( 360 name = "foo_test", 361 srcs = ["foo_test.go"], 362 embed = [":foo"], 363 ) 364 `, 365 }, 366 { 367 desc: "go_naming_convention import_alias -> import for lib", 368 namingConvention: importNamingConvention, 369 old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 370 371 go_library( 372 name = "foo", 373 srcs = ["foo.go"], 374 importpath = "example.com/foo", 375 ) 376 377 go_test( 378 name = "foo_test", 379 srcs = ["foo_test.go"], 380 embed = [":foo"], 381 ) 382 `, 383 want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 384 385 go_library( 386 name = "foo", 387 srcs = ["foo.go"], 388 importpath = "example.com/foo", 389 ) 390 391 go_test( 392 name = "foo_test", 393 srcs = ["foo_test.go"], 394 embed = [":foo"], 395 ) 396 `, 397 }, 398 { 399 desc: "go_naming_convention=import -> import for lib", 400 namingConvention: importNamingConvention, 401 old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 402 403 go_library( 404 name = "foo", 405 srcs = ["foo.go"], 406 importpath = "example.com/foo", 407 ) 408 409 go_test( 410 name = "foo_test", 411 srcs = ["foo_test.go"], 412 embed = [":foo"], 413 ) 414 `, 415 want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 416 417 go_library( 418 name = "foo", 419 srcs = ["foo.go"], 420 importpath = "example.com/foo", 421 ) 422 423 go_test( 424 name = "foo_test", 425 srcs = ["foo_test.go"], 426 embed = [":foo"], 427 ) 428 `, 429 }, 430 { 431 desc: "go_naming_convention go_default_library -> go_default_library for lib", 432 namingConvention: goDefaultLibraryNamingConvention, 433 old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 434 435 go_library( 436 name = "go_default_library", 437 srcs = ["foo.go"], 438 importpath = "example.com/foo", 439 visibility = ["//visibility:private"], 440 ) 441 442 go_test( 443 name = "go_default_test", 444 srcs = ["foo_test.go"], 445 embed = [":go_default_library"], 446 ) 447 `, 448 want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 449 450 go_library( 451 name = "go_default_library", 452 srcs = ["foo.go"], 453 importpath = "example.com/foo", 454 visibility = ["//visibility:private"], 455 ) 456 457 go_test( 458 name = "go_default_test", 459 srcs = ["foo_test.go"], 460 embed = [":go_default_library"], 461 ) 462 `, 463 }, 464 { 465 desc: "go_naming_convention=import_alias -> import_alias for lib", 466 namingConvention: importAliasNamingConvention, 467 old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 468 469 go_library( 470 name = "foo", 471 srcs = ["foo.go"], 472 importpath = "example.com/foo", 473 ) 474 475 go_test( 476 name = "foo_test", 477 srcs = ["foo_test.go"], 478 embed = [":foo"], 479 ) 480 `, 481 want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 482 483 go_library( 484 name = "foo", 485 srcs = ["foo.go"], 486 importpath = "example.com/foo", 487 ) 488 489 go_test( 490 name = "foo_test", 491 srcs = ["foo_test.go"], 492 embed = [":foo"], 493 ) 494 `, 495 }, 496 { 497 // migrateLibraryEmbed tests 498 desc: "library migrated to embed", 499 old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 500 501 go_library( 502 name = "go_default_library", 503 srcs = ["foo.go"], 504 ) 505 506 go_test( 507 name = "go_default_test", 508 srcs = ["foo_test.go"], 509 library = ":go_default_library", 510 ) 511 `, 512 want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 513 514 go_library( 515 name = "go_default_library", 516 srcs = ["foo.go"], 517 ) 518 519 go_test( 520 name = "go_default_test", 521 srcs = ["foo_test.go"], 522 embed = [":go_default_library"], 523 ) 524 `, 525 }, 526 { 527 // verifies #211 528 desc: "other library not migrated", 529 old: ` 530 gomock( 531 name = "stripe_mock", 532 out = "stripe_mock_test.go", 533 interfaces = [ 534 "stSubscriptions", 535 "stCustomers", 536 ], 537 library = ":go_default_library", 538 package = "main", 539 source = "stripe.go", 540 ) 541 `, 542 want: ` 543 gomock( 544 name = "stripe_mock", 545 out = "stripe_mock_test.go", 546 interfaces = [ 547 "stSubscriptions", 548 "stCustomers", 549 ], 550 library = ":go_default_library", 551 package = "main", 552 source = "stripe.go", 553 ) 554 `, 555 }, 556 // migrateGrpcCompilers tests 557 { 558 desc: "go_grpc_library migrated to compilers", 559 old: `load("@io_bazel_rules_go//proto:def.bzl", "go_grpc_library") 560 561 proto_library( 562 name = "foo_proto", 563 srcs = ["foo.proto"], 564 visibility = ["//visibility:public"], 565 ) 566 567 go_grpc_library( 568 name = "foo_go_proto", 569 importpath = "example.com/repo", 570 proto = ":foo_proto", 571 visibility = ["//visibility:public"], 572 ) 573 `, 574 want: `load("@io_bazel_rules_go//proto:def.bzl", "go_grpc_library") 575 576 proto_library( 577 name = "foo_proto", 578 srcs = ["foo.proto"], 579 visibility = ["//visibility:public"], 580 ) 581 582 go_proto_library( 583 name = "foo_go_proto", 584 compilers = ["@io_bazel_rules_go//proto:go_grpc"], 585 importpath = "example.com/repo", 586 proto = ":foo_proto", 587 visibility = ["//visibility:public"], 588 ) 589 `, 590 }, 591 // flattenSrcs tests 592 { 593 desc: "flatten srcs", 594 old: `load("@io_bazel_rules_go//go:def.bzl", "go_library") 595 596 go_library( 597 name = "go_default_library", 598 srcs = [ 599 "gen.go", 600 ] + select({ 601 "@io_bazel_rules_go//platform:darwin_amd64": [ 602 # darwin 603 "foo.go", # keep 604 ], 605 "@io_bazel_rules_go//platform:linux_amd64": [ 606 # linux 607 "foo.go", # keep 608 ], 609 }), 610 ) 611 `, 612 want: `load("@io_bazel_rules_go//go:def.bzl", "go_library") 613 614 go_library( 615 name = "go_default_library", 616 srcs = [ 617 # darwin 618 # linux 619 "foo.go", # keep 620 "gen.go", 621 ], 622 ) 623 `, 624 }, 625 // squashCgoLibrary tests 626 { 627 desc: "no cgo_library", 628 old: `load("@io_bazel_rules_go//go:def.bzl", "go_library") 629 630 go_library( 631 name = "go_default_library", 632 ) 633 `, 634 want: `load("@io_bazel_rules_go//go:def.bzl", "go_library") 635 636 go_library( 637 name = "go_default_library", 638 ) 639 `, 640 }, 641 { 642 desc: "non-default cgo_library not removed", 643 old: `load("@io_bazel_rules_go//go:def.bzl", "cgo_library") 644 645 cgo_library( 646 name = "something_else", 647 ) 648 `, 649 want: `load("@io_bazel_rules_go//go:def.bzl", "cgo_library") 650 651 cgo_library( 652 name = "something_else", 653 ) 654 `, 655 }, 656 { 657 desc: "unlinked cgo_library removed", 658 old: `load("@io_bazel_rules_go//go:def.bzl", "cgo_library", "go_library") 659 660 go_library( 661 name = "go_default_library", 662 library = ":something_else", 663 ) 664 665 cgo_library( 666 name = "cgo_default_library", 667 ) 668 `, 669 want: `load("@io_bazel_rules_go//go:def.bzl", "cgo_library", "go_library") 670 671 go_library( 672 name = "go_default_library", 673 cgo = True, 674 ) 675 `, 676 }, 677 { 678 desc: "cgo_library replaced with go_library", 679 old: `load("@io_bazel_rules_go//go:def.bzl", "cgo_library") 680 681 # before comment 682 cgo_library( 683 name = "cgo_default_library", 684 cdeps = ["cdeps"], 685 clinkopts = ["clinkopts"], 686 copts = ["copts"], 687 data = ["data"], 688 deps = ["deps"], 689 gc_goopts = ["gc_goopts"], 690 srcs = [ 691 "foo.go" # keep 692 ], 693 visibility = ["//visibility:private"], 694 ) 695 # after comment 696 `, 697 want: `load("@io_bazel_rules_go//go:def.bzl", "cgo_library") 698 699 # before comment 700 go_library( 701 name = "go_default_library", 702 srcs = [ 703 "foo.go", # keep 704 ], 705 cdeps = ["cdeps"], 706 cgo = True, 707 clinkopts = ["clinkopts"], 708 copts = ["copts"], 709 data = ["data"], 710 gc_goopts = ["gc_goopts"], 711 visibility = ["//visibility:private"], 712 deps = ["deps"], 713 ) 714 # after comment 715 `, 716 }, 717 { 718 desc: "cgo_library merged with go_library", 719 old: `load("@io_bazel_rules_go//go:def.bzl", "go_library") 720 721 # before go_library 722 go_library( 723 name = "go_default_library", 724 srcs = ["pure.go"], 725 deps = ["pure_deps"], 726 data = ["pure_data"], 727 gc_goopts = ["pure_gc_goopts"], 728 library = ":cgo_default_library", 729 cgo = False, 730 ) 731 # after go_library 732 733 # before cgo_library 734 cgo_library( 735 name = "cgo_default_library", 736 srcs = ["cgo.go"], 737 deps = ["cgo_deps"], 738 data = ["cgo_data"], 739 gc_goopts = ["cgo_gc_goopts"], 740 copts = ["copts"], 741 cdeps = ["cdeps"], 742 ) 743 # after cgo_library 744 `, 745 want: `load("@io_bazel_rules_go//go:def.bzl", "go_library") 746 747 # before go_library 748 # before cgo_library 749 go_library( 750 name = "go_default_library", 751 srcs = [ 752 "cgo.go", 753 "pure.go", 754 ], 755 cdeps = ["cdeps"], 756 cgo = True, 757 copts = ["copts"], 758 data = [ 759 "cgo_data", 760 "pure_data", 761 ], 762 gc_goopts = [ 763 "cgo_gc_goopts", 764 "pure_gc_goopts", 765 ], 766 deps = [ 767 "cgo_deps", 768 "pure_deps", 769 ], 770 ) 771 # after go_library 772 # after cgo_library 773 `, 774 }, 775 // squashXtest tests 776 { 777 desc: "rename xtest", 778 old: `load("@io_bazel_rules_go//go:def.bzl", "go_test") 779 go_test( 780 name = "go_default_xtest", 781 srcs = ["x_test.go"], 782 ) 783 `, 784 want: `load("@io_bazel_rules_go//go:def.bzl", "go_test") 785 786 go_test( 787 name = "go_default_test", 788 srcs = ["x_test.go"], 789 ) 790 `, 791 }, 792 { 793 desc: "squash xtest", 794 old: `load("@io_bazel_rules_go//go:def.bzl", "go_test") 795 796 go_test( 797 name = "go_default_test", 798 srcs = ["i_test.go"], 799 deps = [ 800 ":i_dep", 801 ":shared_dep", 802 ], 803 visibility = ["//visibility:public"], 804 ) 805 806 go_test( 807 name = "go_default_xtest", 808 srcs = ["x_test.go"], 809 deps = [ 810 ":x_dep", 811 ":shared_dep", 812 ], 813 visibility = ["//visibility:public"], 814 ) 815 `, 816 want: `load("@io_bazel_rules_go//go:def.bzl", "go_test") 817 818 go_test( 819 name = "go_default_test", 820 srcs = [ 821 "i_test.go", 822 "x_test.go", 823 ], 824 visibility = ["//visibility:public"], 825 deps = [ 826 ":i_dep", 827 ":shared_dep", 828 ":x_dep", 829 ], 830 ) 831 `, 832 }, 833 // removeLegacyProto tests 834 { 835 desc: "current proto preserved", 836 old: `load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") 837 838 go_proto_library( 839 name = "foo_go_proto", 840 proto = ":foo_proto", 841 ) 842 `, 843 want: `load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") 844 845 go_proto_library( 846 name = "foo_go_proto", 847 proto = ":foo_proto", 848 ) 849 `, 850 }, 851 { 852 desc: "load and proto removed", 853 old: `load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library") 854 855 go_proto_library( 856 name = "go_default_library_protos", 857 srcs = ["foo.proto"], 858 visibility = ["//visibility:private"], 859 ) 860 `, 861 want: "", 862 }, 863 { 864 desc: "proto filegroup removed", 865 old: `filegroup( 866 name = "go_default_library_protos", 867 srcs = ["foo.proto"], 868 ) 869 870 go_proto_library(name = "foo_proto") 871 `, 872 want: `go_proto_library(name = "foo_proto") 873 `, 874 }, 875 } { 876 t.Run(tc.desc, func(t *testing.T) { 877 testFix(t, tc, func(f *rule.File) { 878 c, langs, _ := testConfig(t, 879 "-go_naming_convention="+tc.namingConvention.String(), 880 "-go_prefix=example.com/foo", 881 ) 882 c.ShouldFix = true 883 for _, lang := range langs { 884 lang.Fix(c, f) 885 } 886 }) 887 }) 888 } 889 } 890 891 func TestFixLoads(t *testing.T) { 892 for _, tc := range []fixTestCase{ 893 { 894 desc: "empty file", 895 old: "", 896 want: "", 897 }, { 898 desc: "non-Go file", 899 old: `load("@io_bazel_rules_intercal//intercal:def.bzl", "intercal_library") 900 901 intercal_library( 902 name = "intercal_default_library", 903 srcs = ["foo.ic"], 904 ) 905 `, 906 want: `load("@io_bazel_rules_intercal//intercal:def.bzl", "intercal_library") 907 908 intercal_library( 909 name = "intercal_default_library", 910 srcs = ["foo.ic"], 911 ) 912 `, 913 }, { 914 desc: "add and remove loaded symbols", 915 old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 916 917 go_library(name = "go_default_library") 918 919 go_binary(name = "cmd") 920 `, 921 want: `load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") 922 923 go_library(name = "go_default_library") 924 925 go_binary(name = "cmd") 926 `, 927 }, { 928 desc: "consolidate load statements", 929 old: `load("@io_bazel_rules_go//go:def.bzl", "go_library") 930 load("@io_bazel_rules_go//go:def.bzl", "go_library") 931 load("@io_bazel_rules_go//go:def.bzl", "go_test") 932 933 go_library(name = "go_default_library") 934 935 go_test(name = "go_default_test") 936 `, 937 want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 938 939 go_library(name = "go_default_library") 940 941 go_test(name = "go_default_test") 942 `, 943 }, { 944 desc: "new load statement", 945 old: `go_library( 946 name = "go_default_library", 947 ) 948 949 go_embed_data( 950 name = "data", 951 ) 952 `, 953 want: `load("@io_bazel_rules_go//go:def.bzl", "go_library") 954 955 go_library( 956 name = "go_default_library", 957 ) 958 959 go_embed_data( 960 name = "data", 961 ) 962 `, 963 }, { 964 desc: "proto symbols", 965 old: `go_proto_library( 966 name = "foo_proto", 967 ) 968 969 go_grpc_library( 970 name = "bar_proto", 971 ) 972 `, 973 want: `load("@io_bazel_rules_go//proto:def.bzl", "go_grpc_library", "go_proto_library") 974 975 go_proto_library( 976 name = "foo_proto", 977 ) 978 979 go_grpc_library( 980 name = "bar_proto", 981 ) 982 `, 983 }, { 984 desc: "fixLoad doesn't touch other symbols or loads", 985 old: `load( 986 "@io_bazel_rules_go//go:def.bzl", 987 "go_embed_data", # embed 988 "go_test", 989 foo = "go_binary", # binary 990 ) 991 load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library") 992 993 go_library( 994 name = "go_default_library", 995 ) 996 `, 997 want: `load( 998 "@io_bazel_rules_go//go:def.bzl", 999 "go_embed_data", # embed 1000 "go_library", 1001 foo = "go_binary", # binary 1002 ) 1003 load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library") 1004 1005 go_library( 1006 name = "go_default_library", 1007 ) 1008 `, 1009 }, { 1010 desc: "fixLoad doesn't touch loads from other files", 1011 old: `load( 1012 "@com_github_pubref_rules_protobuf//go:rules.bzl", 1013 "go_proto_library", 1014 go_grpc_library = "go_proto_library", 1015 ) 1016 1017 go_proto_library( 1018 name = "foo_go_proto", 1019 ) 1020 1021 grpc_proto_library( 1022 name = "bar_go_proto", 1023 ) 1024 `, 1025 want: `load( 1026 "@com_github_pubref_rules_protobuf//go:rules.bzl", 1027 "go_proto_library", 1028 go_grpc_library = "go_proto_library", 1029 ) 1030 1031 go_proto_library( 1032 name = "foo_go_proto", 1033 ) 1034 1035 grpc_proto_library( 1036 name = "bar_go_proto", 1037 ) 1038 `, 1039 }, { 1040 desc: "moved symbol", 1041 old: ` 1042 load("@io_bazel_rules_go//go:def.bzl", "go_repository") 1043 1044 go_repository(name = "foo") 1045 `, 1046 want: ` 1047 load("@bazel_gazelle//:deps.bzl", "go_repository") 1048 1049 go_repository(name = "foo") 1050 `, 1051 }, { 1052 desc: "moved symbols with others", 1053 old: ` 1054 load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_repository") 1055 1056 go_rules_dependencies() 1057 1058 go_repository(name = "foo") 1059 `, 1060 want: `load("@bazel_gazelle//:deps.bzl", "go_repository") 1061 load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies") 1062 1063 go_rules_dependencies() 1064 1065 go_repository(name = "foo") 1066 `, 1067 }, { 1068 desc: "load go_repository", 1069 old: ` 1070 load("@io_bazel_rules_go//go:def.bzl", "go_register_toolchains", "go_rules_dependencies") 1071 1072 go_rules_dependencies() 1073 1074 go_register_toolchains() 1075 1076 go_repository(name = "foo") 1077 `, 1078 want: `load("@bazel_gazelle//:deps.bzl", "go_repository") 1079 load("@io_bazel_rules_go//go:def.bzl", "go_register_toolchains", "go_rules_dependencies") 1080 1081 go_rules_dependencies() 1082 1083 go_register_toolchains() 1084 1085 go_repository(name = "foo") 1086 `, 1087 }, 1088 } { 1089 t.Run(tc.desc, func(t *testing.T) { 1090 testFix(t, tc, func(f *rule.File) { 1091 merger.FixLoads(f, goLoadsForTesting) 1092 }) 1093 }) 1094 } 1095 } 1096 1097 func testFix(t *testing.T, tc fixTestCase, fix func(*rule.File)) { 1098 f, err := rule.LoadData(filepath.Join("old", "BUILD.bazel"), "", []byte(tc.old)) 1099 if err != nil { 1100 t.Fatalf("%s: parse error: %v", tc.desc, err) 1101 } 1102 fix(f) 1103 want := tc.want 1104 if len(want) > 0 && want[0] == '\n' { 1105 // Strip leading newline, added for readability 1106 want = want[1:] 1107 } 1108 if got := string(f.Format()); got != want { 1109 t.Fatalf("%s: got %s; want %s", tc.desc, got, want) 1110 } 1111 }