github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/cmd/gazelle/integration_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  // This file contains integration tests for all of Gazelle. It's meant to test
    17  // common usage patterns and check for errors that are difficult to test in
    18  // unit tests.
    19  
    20  package main
    21  
    22  import (
    23  	"bytes"
    24  	"flag"
    25  	"fmt"
    26  	"io/ioutil"
    27  	"log"
    28  	"os"
    29  	"path/filepath"
    30  	"strings"
    31  	"testing"
    32  
    33  	"github.com/bazelbuild/bazel-gazelle/config"
    34  	"github.com/bazelbuild/bazel-gazelle/testtools"
    35  )
    36  
    37  // skipIfWorkspaceVisible skips the test if the WORKSPACE file for the
    38  // repository is visible. This happens in newer Bazel versions when tests
    39  // are run without sandboxing, since temp directories may be inside the
    40  // exec root.
    41  func skipIfWorkspaceVisible(t *testing.T, dir string) {
    42  	if parent, err := FindRepoRoot(dir); err == nil {
    43  		t.Skipf("WORKSPACE visible in parent %q of tmp %q", parent, dir)
    44  	}
    45  }
    46  
    47  func runGazelle(wd string, args []string) error {
    48  	return run(wd, args)
    49  }
    50  
    51  // TestHelp checks that help commands do not panic due to nil flag values.
    52  // Verifies #256.
    53  func TestHelp(t *testing.T) {
    54  	for _, args := range [][]string{
    55  		{"help"},
    56  		{"fix", "-h"},
    57  		{"update", "-h"},
    58  		{"update-repos", "-h"},
    59  	} {
    60  		t.Run(args[0], func(t *testing.T) {
    61  			if err := runGazelle(".", args); err == nil {
    62  				t.Errorf("%s: got success, want flag.ErrHelp", args[0])
    63  			} else if err != flag.ErrHelp {
    64  				t.Errorf("%s: got %v, want flag.ErrHelp", args[0], err)
    65  			}
    66  		})
    67  	}
    68  }
    69  
    70  func TestNoRepoRootOrWorkspace(t *testing.T) {
    71  	dir, cleanup := testtools.CreateFiles(t, nil)
    72  	defer cleanup()
    73  	skipIfWorkspaceVisible(t, dir)
    74  	want := "-repo_root not specified"
    75  	if err := runGazelle(dir, nil); err == nil {
    76  		t.Fatalf("got success; want %q", want)
    77  	} else if !strings.Contains(err.Error(), want) {
    78  		t.Fatalf("got %q; want %q", err, want)
    79  	}
    80  }
    81  
    82  func TestNoGoPrefixArgOrRule(t *testing.T) {
    83  	dir, cleanup := testtools.CreateFiles(t, []testtools.FileSpec{
    84  		{Path: "WORKSPACE", Content: ""},
    85  		{Path: "hello.go", Content: "package hello"},
    86  	})
    87  	defer cleanup()
    88  	buf := new(bytes.Buffer)
    89  	log.SetOutput(buf)
    90  	defer log.SetOutput(os.Stderr)
    91  	if err := runGazelle(dir, nil); err != nil {
    92  		t.Fatalf("got %#v; want success", err)
    93  	}
    94  	want := "go prefix is not set"
    95  	if !strings.Contains(buf.String(), want) {
    96  		t.Errorf("log does not contain %q\n--begin--\n%s--end--\n", want, buf.String())
    97  	}
    98  }
    99  
   100  // TestSelectLabelsSorted checks that string lists in srcs and deps are sorted
   101  // using buildifier order, even if they are inside select expressions.
   102  // This applies to both new and existing lists and should preserve comments.
   103  // buildifier does not do this yet bazelbuild/buildtools#122, so we do this
   104  // in addition to calling build.Rewrite.
   105  func TestSelectLabelsSorted(t *testing.T) {
   106  	dir, cleanup := testtools.CreateFiles(t, []testtools.FileSpec{
   107  		{Path: "WORKSPACE"},
   108  		{
   109  			Path: "BUILD",
   110  			Content: `
   111  load("@io_bazel_rules_go//go:def.bzl", "go_library")
   112  
   113  go_library(
   114      name = "go_default_library",
   115      srcs = select({
   116          "@io_bazel_rules_go//go/platform:linux": [
   117              # foo comment
   118              "foo.go",  # side comment
   119              # bar comment
   120              "bar.go",
   121          ],
   122          "//conditions:default": [],
   123      }),
   124      importpath = "example.com/foo",
   125  )
   126  `,
   127  		},
   128  		{
   129  			Path: "foo.go",
   130  			Content: `
   131  // +build linux
   132  
   133  package foo
   134  
   135  import (
   136      _ "example.com/foo/outer"
   137      _ "example.com/foo/outer/inner"
   138      _ "github.com/jr_hacker/tools"
   139  )
   140  `,
   141  		},
   142  		{
   143  			Path: "foo_android_build_tag.go",
   144  			Content: `
   145  // +build android
   146  
   147  package foo
   148  
   149  import (
   150      _ "example.com/foo/outer_android_build_tag"
   151  )
   152  `,
   153  		},
   154  		{
   155  			Path: "foo_android.go",
   156  			Content: `
   157  package foo
   158  
   159  import (
   160      _ "example.com/foo/outer_android_suffix"
   161  )
   162  `,
   163  		},
   164  		{
   165  			Path: "bar.go",
   166  			Content: `// +build linux
   167  
   168  package foo
   169  `,
   170  		},
   171  		{Path: "outer/outer.go", Content: "package outer"},
   172  		{Path: "outer_android_build_tag/outer.go", Content: "package outer_android_build_tag"},
   173  		{Path: "outer_android_suffix/outer.go", Content: "package outer_android_suffix"},
   174  		{Path: "outer/inner/inner.go", Content: "package inner"},
   175  	})
   176  	want := `load("@io_bazel_rules_go//go:def.bzl", "go_library")
   177  
   178  go_library(
   179      name = "go_default_library",
   180      srcs = [
   181          # bar comment
   182          "bar.go",
   183          # foo comment
   184          "foo.go",  # side comment
   185          "foo_android.go",
   186          "foo_android_build_tag.go",
   187      ],
   188      importpath = "example.com/foo",
   189      visibility = ["//visibility:public"],
   190      deps = select({
   191          "@io_bazel_rules_go//go/platform:android": [
   192              "//outer:go_default_library",
   193              "//outer/inner:go_default_library",
   194              "//outer_android_build_tag:go_default_library",
   195              "//outer_android_suffix:go_default_library",
   196              "@com_github_jr_hacker_tools//:go_default_library",
   197          ],
   198          "@io_bazel_rules_go//go/platform:linux": [
   199              "//outer:go_default_library",
   200              "//outer/inner:go_default_library",
   201              "@com_github_jr_hacker_tools//:go_default_library",
   202          ],
   203          "//conditions:default": [],
   204      }),
   205  )
   206  `
   207  	defer cleanup()
   208  
   209  	if err := runGazelle(dir, []string{"-go_prefix", "example.com/foo"}); err != nil {
   210  		t.Fatal(err)
   211  	}
   212  	if got, err := ioutil.ReadFile(filepath.Join(dir, "BUILD")); err != nil {
   213  		t.Fatal(err)
   214  	} else if string(got) != want {
   215  		t.Fatalf("got %s ; want %s", string(got), want)
   216  	}
   217  }
   218  
   219  func TestFixAndUpdateChanges(t *testing.T) {
   220  	files := []testtools.FileSpec{
   221  		{Path: "WORKSPACE"},
   222  		{
   223  			Path: "BUILD",
   224  			Content: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_prefix")
   225  load("@io_bazel_rules_go//go:def.bzl", "cgo_library", "go_test")
   226  
   227  go_prefix("example.com/foo")
   228  
   229  go_library(
   230      name = "go_default_library",
   231      srcs = [
   232          "extra.go",
   233          "pure.go",
   234      ],
   235      library = ":cgo_default_library",
   236      visibility = ["//visibility:default"],
   237  )
   238  
   239  cgo_library(
   240      name = "cgo_default_library",
   241      srcs = ["cgo.go"],
   242  )
   243  `,
   244  		},
   245  		{
   246  			Path:    "pure.go",
   247  			Content: "package foo",
   248  		},
   249  		{
   250  			Path: "cgo.go",
   251  			Content: `package foo
   252  
   253  import "C"
   254  `,
   255  		},
   256  	}
   257  
   258  	cases := []struct {
   259  		cmd, want string
   260  	}{
   261  		{
   262  			cmd: "update",
   263  			want: `load("@io_bazel_rules_go//go:def.bzl", "cgo_library", "go_library", "go_prefix")
   264  
   265  go_prefix("example.com/foo")
   266  
   267  go_library(
   268      name = "go_default_library",
   269      srcs = [
   270          "cgo.go",
   271          "pure.go",
   272      ],
   273      cgo = True,
   274      importpath = "example.com/foo",
   275      visibility = ["//visibility:default"],
   276  )
   277  
   278  cgo_library(
   279      name = "cgo_default_library",
   280      srcs = ["cgo.go"],
   281  )
   282  `,
   283  		}, {
   284  			cmd: "fix",
   285  			want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_prefix")
   286  
   287  go_prefix("example.com/foo")
   288  
   289  go_library(
   290      name = "go_default_library",
   291      srcs = [
   292          "cgo.go",
   293          "pure.go",
   294      ],
   295      cgo = True,
   296      importpath = "example.com/foo",
   297      visibility = ["//visibility:default"],
   298  )
   299  `,
   300  		},
   301  	}
   302  
   303  	for _, c := range cases {
   304  		t.Run(c.cmd, func(t *testing.T) {
   305  			dir, cleanup := testtools.CreateFiles(t, files)
   306  			defer cleanup()
   307  
   308  			if err := runGazelle(dir, []string{c.cmd}); err != nil {
   309  				t.Fatal(err)
   310  			}
   311  			if got, err := ioutil.ReadFile(filepath.Join(dir, "BUILD")); err != nil {
   312  				t.Fatal(err)
   313  			} else if string(got) != c.want {
   314  				t.Fatalf("got %s ; want %s", string(got), c.want)
   315  			}
   316  		})
   317  	}
   318  }
   319  
   320  func TestFixUnlinkedCgoLibrary(t *testing.T) {
   321  	files := []testtools.FileSpec{
   322  		{Path: "WORKSPACE"},
   323  		{
   324  			Path: "BUILD",
   325  			Content: `load("@io_bazel_rules_go//go:def.bzl", "cgo_library", "go_library")
   326  
   327  cgo_library(
   328      name = "cgo_default_library",
   329      srcs = ["cgo.go"],
   330  )
   331  
   332  go_library(
   333      name = "go_default_library",
   334      srcs = ["pure.go"],
   335      importpath = "example.com/foo",
   336      visibility = ["//visibility:public"],
   337  )
   338  `,
   339  		}, {
   340  			Path:    "pure.go",
   341  			Content: "package foo",
   342  		},
   343  	}
   344  
   345  	dir, cleanup := testtools.CreateFiles(t, files)
   346  	defer cleanup()
   347  
   348  	want := `load("@io_bazel_rules_go//go:def.bzl", "go_library")
   349  
   350  go_library(
   351      name = "go_default_library",
   352      srcs = ["pure.go"],
   353      importpath = "example.com/foo",
   354      visibility = ["//visibility:public"],
   355  )
   356  `
   357  	if err := runGazelle(dir, []string{"fix", "-go_prefix", "example.com/foo"}); err != nil {
   358  		t.Fatal(err)
   359  	}
   360  	if got, err := ioutil.ReadFile(filepath.Join(dir, "BUILD")); err != nil {
   361  		t.Fatal(err)
   362  	} else if string(got) != want {
   363  		t.Fatalf("got %s ; want %s", string(got), want)
   364  	}
   365  }
   366  
   367  // TestMultipleDirectories checks that all directories in a repository are
   368  // indexed but only directories listed on the command line are updated.
   369  func TestMultipleDirectories(t *testing.T) {
   370  	files := []testtools.FileSpec{
   371  		{Path: "WORKSPACE"},
   372  		{
   373  			Path: "a/BUILD.bazel",
   374  			Content: `# This file shouldn't be modified.
   375  load("@io_bazel_rules_go//go:def.bzl", "go_library")
   376  
   377  go_library(
   378      name = "go_default_library",
   379      srcs = ["a.go"],
   380      importpath = "example.com/foo/x",
   381  )
   382  `,
   383  		}, {
   384  			Path:    "a/a.go",
   385  			Content: "package a",
   386  		}, {
   387  			Path: "b/b.go",
   388  			Content: `
   389  package b
   390  
   391  import _ "example.com/foo/x"
   392  `,
   393  		},
   394  	}
   395  	dir, cleanup := testtools.CreateFiles(t, files)
   396  	defer cleanup()
   397  
   398  	args := []string{"-go_prefix", "example.com/foo", "b"}
   399  	if err := runGazelle(dir, args); err != nil {
   400  		t.Fatal(err)
   401  	}
   402  
   403  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
   404  		files[1], // should not change
   405  		{
   406  			Path: "b/BUILD.bazel",
   407  			Content: `load("@io_bazel_rules_go//go:def.bzl", "go_library")
   408  
   409  go_library(
   410      name = "go_default_library",
   411      srcs = ["b.go"],
   412      importpath = "example.com/foo/b",
   413      visibility = ["//visibility:public"],
   414      deps = ["//a:go_default_library"],
   415  )
   416  `,
   417  		},
   418  	})
   419  }
   420  
   421  func TestErrorOutsideWorkspace(t *testing.T) {
   422  	files := []testtools.FileSpec{
   423  		{Path: "a/"},
   424  		{Path: "b/"},
   425  	}
   426  	dir, cleanup := testtools.CreateFiles(t, files)
   427  	defer cleanup()
   428  	skipIfWorkspaceVisible(t, dir)
   429  
   430  	cases := []struct {
   431  		name, dir, want string
   432  		args            []string
   433  	}{
   434  		{
   435  			name: "outside workspace",
   436  			dir:  dir,
   437  			args: nil,
   438  			want: "WORKSPACE cannot be found",
   439  		}, {
   440  			name: "outside repo_root",
   441  			dir:  filepath.Join(dir, "a"),
   442  			args: []string{"-repo_root", filepath.Join(dir, "b")},
   443  			want: "not a subdirectory of repo root",
   444  		},
   445  	}
   446  	for _, c := range cases {
   447  		t.Run(c.name, func(t *testing.T) {
   448  			if err := runGazelle(c.dir, c.args); err == nil {
   449  				t.Fatalf("got success; want %q", c.want)
   450  			} else if !strings.Contains(err.Error(), c.want) {
   451  				t.Fatalf("got %q; want %q", err, c.want)
   452  			}
   453  		})
   454  	}
   455  }
   456  
   457  func TestBuildFileNameIgnoresBuild(t *testing.T) {
   458  	files := []testtools.FileSpec{
   459  		{Path: "WORKSPACE"},
   460  		{Path: "BUILD/"},
   461  		{
   462  			Path:    "a/BUILD",
   463  			Content: "!!! parse error",
   464  		}, {
   465  			Path:    "a.go",
   466  			Content: "package a",
   467  		},
   468  	}
   469  	dir, cleanup := testtools.CreateFiles(t, files)
   470  	defer cleanup()
   471  
   472  	args := []string{"-go_prefix", "example.com/foo", "-build_file_name", "BUILD.bazel"}
   473  	if err := runGazelle(dir, args); err != nil {
   474  		t.Fatal(err)
   475  	}
   476  	if _, err := os.Stat(filepath.Join(dir, "BUILD.bazel")); err != nil {
   477  		t.Errorf("BUILD.bazel not created: %v", err)
   478  	}
   479  }
   480  
   481  func TestExternalVendor(t *testing.T) {
   482  	files := []testtools.FileSpec{
   483  		{
   484  			Path:    "WORKSPACE",
   485  			Content: `workspace(name = "banana")`,
   486  		}, {
   487  			Path: "a.go",
   488  			Content: `package foo
   489  
   490  import _ "golang.org/x/bar"
   491  `,
   492  		}, {
   493  			Path: "vendor/golang.org/x/bar/bar.go",
   494  			Content: `package bar
   495  
   496  import _ "golang.org/x/baz"
   497  `,
   498  		}, {
   499  			Path:    "vendor/golang.org/x/baz/baz.go",
   500  			Content: "package baz",
   501  		},
   502  	}
   503  	dir, cleanup := testtools.CreateFiles(t, files)
   504  	defer cleanup()
   505  
   506  	args := []string{"-go_prefix", "example.com/foo", "-external", "vendored"}
   507  	if err := runGazelle(dir, args); err != nil {
   508  		t.Fatal(err)
   509  	}
   510  
   511  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
   512  		{
   513  			Path: config.DefaultValidBuildFileNames[0],
   514  			Content: `load("@io_bazel_rules_go//go:def.bzl", "go_library")
   515  
   516  go_library(
   517      name = "foo",
   518      srcs = ["a.go"],
   519      importpath = "example.com/foo",
   520      visibility = ["//visibility:public"],
   521      deps = ["//vendor/golang.org/x/bar"],
   522  )
   523  `,
   524  		}, {
   525  			Path: "vendor/golang.org/x/bar/" + config.DefaultValidBuildFileNames[0],
   526  			Content: `load("@io_bazel_rules_go//go:def.bzl", "go_library")
   527  
   528  go_library(
   529      name = "bar",
   530      srcs = ["bar.go"],
   531      importmap = "example.com/foo/vendor/golang.org/x/bar",
   532      importpath = "golang.org/x/bar",
   533      visibility = ["//visibility:public"],
   534      deps = ["//vendor/golang.org/x/baz"],
   535  )
   536  `,
   537  		},
   538  	})
   539  }
   540  
   541  func TestMigrateProtoRules(t *testing.T) {
   542  	files := []testtools.FileSpec{
   543  		{Path: "WORKSPACE"},
   544  		{
   545  			Path: config.DefaultValidBuildFileNames[0],
   546  			Content: `
   547  load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library")
   548  
   549  filegroup(
   550      name = "go_default_library_protos",
   551      srcs = ["foo.proto"],
   552      visibility = ["//visibility:public"],
   553  )
   554  
   555  go_proto_library(
   556      name = "go_default_library",
   557      srcs = [":go_default_library_protos"],
   558  )
   559  `,
   560  		}, {
   561  			Path: "foo.proto",
   562  			Content: `syntax = "proto3";
   563  
   564  option go_package = "example.com/repo";
   565  `,
   566  		}, {
   567  			Path:    "foo.pb.go",
   568  			Content: `package repo`,
   569  		},
   570  	}
   571  
   572  	for _, tc := range []struct {
   573  		args []string
   574  		want string
   575  	}{
   576  		{
   577  			args: []string{"update", "-go_prefix", "example.com/repo"},
   578  			want: `
   579  load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library")
   580  
   581  filegroup(
   582      name = "go_default_library_protos",
   583      srcs = ["foo.proto"],
   584      visibility = ["//visibility:public"],
   585  )
   586  
   587  go_proto_library(
   588      name = "go_default_library",
   589      srcs = [":go_default_library_protos"],
   590  )
   591  `,
   592  		}, {
   593  			args: []string{"fix", "-go_prefix", "example.com/repo"},
   594  			want: `
   595  load("@rules_proto//proto:defs.bzl", "proto_library")
   596  load("@io_bazel_rules_go//go:def.bzl", "go_library")
   597  load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
   598  
   599  proto_library(
   600      name = "repo_proto",
   601      srcs = ["foo.proto"],
   602      visibility = ["//visibility:public"],
   603  )
   604  
   605  go_proto_library(
   606      name = "repo_go_proto",
   607      importpath = "example.com/repo",
   608      proto = ":repo_proto",
   609      visibility = ["//visibility:public"],
   610  )
   611  
   612  go_library(
   613      name = "go_default_library",
   614      embed = [":repo_go_proto"],
   615      importpath = "example.com/repo",
   616      visibility = ["//visibility:public"],
   617  )
   618  `,
   619  		},
   620  	} {
   621  		t.Run(tc.args[0], func(t *testing.T) {
   622  			dir, cleanup := testtools.CreateFiles(t, files)
   623  			defer cleanup()
   624  
   625  			if err := runGazelle(dir, tc.args); err != nil {
   626  				t.Fatal(err)
   627  			}
   628  
   629  			testtools.CheckFiles(t, dir, []testtools.FileSpec{{
   630  				Path:    config.DefaultValidBuildFileNames[0],
   631  				Content: tc.want,
   632  			}})
   633  		})
   634  	}
   635  }
   636  
   637  func TestRemoveProtoDeletesRules(t *testing.T) {
   638  	files := []testtools.FileSpec{
   639  		{Path: "WORKSPACE"},
   640  		{
   641  			Path: config.DefaultValidBuildFileNames[0],
   642  			Content: `
   643  load("@rules_proto//proto:defs.bzl", "proto_library")
   644  load("@io_bazel_rules_go//go:def.bzl", "go_library")
   645  load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
   646  
   647  filegroup(
   648      name = "go_default_library_protos",
   649      srcs = ["foo.proto"],
   650      visibility = ["//visibility:public"],
   651  )
   652  
   653  proto_library(
   654      name = "repo_proto",
   655      srcs = ["foo.proto"],
   656      visibility = ["//visibility:public"],
   657  )
   658  
   659  go_proto_library(
   660      name = "repo_go_proto",
   661      importpath = "example.com/repo",
   662      proto = ":repo_proto",
   663      visibility = ["//visibility:public"],
   664  )
   665  
   666  go_library(
   667      name = "go_default_library",
   668      srcs = ["extra.go"],
   669      embed = [":repo_go_proto"],
   670      importpath = "example.com/repo",
   671      visibility = ["//visibility:public"],
   672  )
   673  `,
   674  		}, {
   675  			Path:    "extra.go",
   676  			Content: `package repo`,
   677  		},
   678  	}
   679  	dir, cleanup := testtools.CreateFiles(t, files)
   680  	defer cleanup()
   681  
   682  	args := []string{"fix", "-go_prefix", "example.com/repo"}
   683  	if err := runGazelle(dir, args); err != nil {
   684  		t.Fatal(err)
   685  	}
   686  
   687  	testtools.CheckFiles(t, dir, []testtools.FileSpec{{
   688  		Path: config.DefaultValidBuildFileNames[0],
   689  		Content: `
   690  load("@io_bazel_rules_go//go:def.bzl", "go_library")
   691  
   692  go_library(
   693      name = "go_default_library",
   694      srcs = ["extra.go"],
   695      importpath = "example.com/repo",
   696      visibility = ["//visibility:public"],
   697  )
   698  `,
   699  	}})
   700  }
   701  
   702  func TestAddServiceConvertsToGrpc(t *testing.T) {
   703  	files := []testtools.FileSpec{
   704  		{Path: "WORKSPACE"},
   705  		{
   706  			Path: config.DefaultValidBuildFileNames[0],
   707  			Content: `
   708  load("@rules_proto//proto:defs.bzl", "proto_library")
   709  load("@io_bazel_rules_go//go:def.bzl", "go_library")
   710  load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
   711  
   712  proto_library(
   713      name = "repo_proto",
   714      srcs = ["foo.proto"],
   715      visibility = ["//visibility:public"],
   716  )
   717  
   718  go_proto_library(
   719      name = "repo_go_proto",
   720      importpath = "example.com/repo",
   721      proto = ":repo_proto",
   722      visibility = ["//visibility:public"],
   723  )
   724  
   725  go_library(
   726      name = "go_default_library",
   727      embed = [":repo_go_proto"],
   728      importpath = "example.com/repo",
   729      visibility = ["//visibility:public"],
   730  )
   731  `,
   732  		}, {
   733  			Path: "foo.proto",
   734  			Content: `syntax = "proto3";
   735  
   736  option go_package = "example.com/repo";
   737  
   738  service TestService {}
   739  `,
   740  		},
   741  	}
   742  
   743  	dir, cleanup := testtools.CreateFiles(t, files)
   744  	defer cleanup()
   745  
   746  	args := []string{"-go_prefix", "example.com/repo"}
   747  	if err := runGazelle(dir, args); err != nil {
   748  		t.Fatal(err)
   749  	}
   750  
   751  	testtools.CheckFiles(t, dir, []testtools.FileSpec{{
   752  		Path: config.DefaultValidBuildFileNames[0],
   753  		Content: `
   754  load("@rules_proto//proto:defs.bzl", "proto_library")
   755  load("@io_bazel_rules_go//go:def.bzl", "go_library")
   756  load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
   757  
   758  proto_library(
   759      name = "repo_proto",
   760      srcs = ["foo.proto"],
   761      visibility = ["//visibility:public"],
   762  )
   763  
   764  go_proto_library(
   765      name = "repo_go_proto",
   766      compilers = ["@io_bazel_rules_go//proto:go_grpc"],
   767      importpath = "example.com/repo",
   768      proto = ":repo_proto",
   769      visibility = ["//visibility:public"],
   770  )
   771  
   772  go_library(
   773      name = "go_default_library",
   774      embed = [":repo_go_proto"],
   775      importpath = "example.com/repo",
   776      visibility = ["//visibility:public"],
   777  )
   778  `,
   779  	}})
   780  }
   781  
   782  func TestProtoImportPrefix(t *testing.T) {
   783  	files := []testtools.FileSpec{
   784  		{Path: "WORKSPACE"},
   785  		{
   786  			Path: config.DefaultValidBuildFileNames[0],
   787  			Content: `
   788  load("@rules_proto//proto:defs.bzl", "proto_library")
   789  load("@io_bazel_rules_go//go:def.bzl", "go_library")
   790  load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
   791  
   792  proto_library(
   793      name = "foo_proto",
   794      srcs = ["foo.proto"],
   795      visibility = ["//visibility:public"],
   796  )
   797  
   798  go_proto_library(
   799      name = "repo_go_proto",
   800      importpath = "example.com/repo",
   801      proto = ":foo_proto",
   802      visibility = ["//visibility:public"],
   803  )
   804  
   805  go_library(
   806      name = "go_default_library",
   807      embed = [":repo_go_proto"],
   808      importpath = "example.com/repo",
   809      visibility = ["//visibility:public"],
   810  )
   811  `,
   812  		}, {
   813  			Path:    "foo.proto",
   814  			Content: `syntax = "proto3";`,
   815  		},
   816  	}
   817  
   818  	dir, cleanup := testtools.CreateFiles(t, files)
   819  	defer cleanup()
   820  
   821  	args := []string{"update", "-go_prefix", "example.com/repo",
   822  		"-proto_import_prefix", "/bar"}
   823  	if err := runGazelle(dir, args); err != nil {
   824  		t.Fatal(err)
   825  	}
   826  
   827  	testtools.CheckFiles(t, dir, []testtools.FileSpec{{
   828  		Path: config.DefaultValidBuildFileNames[0],
   829  		Content: `
   830  load("@rules_proto//proto:defs.bzl", "proto_library")
   831  load("@io_bazel_rules_go//go:def.bzl", "go_library")
   832  load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
   833  
   834  proto_library(
   835      name = "foo_proto",
   836      srcs = ["foo.proto"],
   837      import_prefix = "/bar",
   838      visibility = ["//visibility:public"],
   839  )
   840  
   841  go_proto_library(
   842      name = "repo_go_proto",
   843      importpath = "example.com/repo",
   844      proto = ":foo_proto",
   845      visibility = ["//visibility:public"],
   846  )
   847  
   848  go_library(
   849      name = "go_default_library",
   850      embed = [":repo_go_proto"],
   851      importpath = "example.com/repo",
   852      visibility = ["//visibility:public"],
   853  )
   854  `,
   855  	}})
   856  }
   857  
   858  func TestEmptyGoPrefix(t *testing.T) {
   859  	files := []testtools.FileSpec{
   860  		{Path: "WORKSPACE"},
   861  		{
   862  			Path:    "foo/foo.go",
   863  			Content: "package foo",
   864  		}, {
   865  			Path: "bar/bar.go",
   866  			Content: `
   867  package bar
   868  
   869  import (
   870  	_ "fmt"
   871  	_ "foo"
   872  )
   873  `,
   874  		},
   875  	}
   876  
   877  	dir, cleanup := testtools.CreateFiles(t, files)
   878  	defer cleanup()
   879  
   880  	args := []string{"-go_prefix", ""}
   881  	if err := runGazelle(dir, args); err != nil {
   882  		t.Fatal(err)
   883  	}
   884  
   885  	testtools.CheckFiles(t, dir, []testtools.FileSpec{{
   886  		Path: filepath.Join("bar", config.DefaultValidBuildFileNames[0]),
   887  		Content: `
   888  load("@io_bazel_rules_go//go:def.bzl", "go_library")
   889  
   890  go_library(
   891      name = "bar",
   892      srcs = ["bar.go"],
   893      importpath = "bar",
   894      visibility = ["//visibility:public"],
   895      deps = ["//foo"],
   896  )
   897  `,
   898  	}})
   899  }
   900  
   901  // TestResolveKeptImportpath checks that Gazelle can resolve dependencies
   902  // against a library with a '# keep' comment on its importpath attribute
   903  // when the importpath doesn't match what Gazelle would infer.
   904  func TestResolveKeptImportpath(t *testing.T) {
   905  	files := []testtools.FileSpec{
   906  		{Path: "WORKSPACE"},
   907  		{
   908  			Path: "foo/foo.go",
   909  			Content: `
   910  package foo
   911  
   912  import _ "example.com/alt/baz"
   913  `,
   914  		}, {
   915  			Path: "bar/BUILD.bazel",
   916  			Content: `load("@io_bazel_rules_go//go:def.bzl", "go_library")
   917  
   918  go_library(
   919      name = "go_default_library",
   920      srcs = ["bar.go"],
   921      importpath = "example.com/alt/baz",  # keep
   922      visibility = ["//visibility:public"],
   923  )
   924  `,
   925  		}, {
   926  			Path:    "bar/bar.go",
   927  			Content: "package bar",
   928  		},
   929  	}
   930  
   931  	dir, cleanup := testtools.CreateFiles(t, files)
   932  	defer cleanup()
   933  
   934  	args := []string{"-go_prefix", "example.com/repo"}
   935  	if err := runGazelle(dir, args); err != nil {
   936  		t.Fatal(err)
   937  	}
   938  
   939  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
   940  		{
   941  			Path: "foo/BUILD.bazel",
   942  			Content: `
   943  load("@io_bazel_rules_go//go:def.bzl", "go_library")
   944  
   945  go_library(
   946      name = "go_default_library",
   947      srcs = ["foo.go"],
   948      importpath = "example.com/repo/foo",
   949      visibility = ["//visibility:public"],
   950      deps = ["//bar:go_default_library"],
   951  )
   952  `,
   953  		}, {
   954  			Path: "bar/BUILD.bazel",
   955  			Content: `load("@io_bazel_rules_go//go:def.bzl", "go_library")
   956  
   957  go_library(
   958      name = "go_default_library",
   959      srcs = ["bar.go"],
   960      importpath = "example.com/alt/baz",  # keep
   961      visibility = ["//visibility:public"],
   962  )
   963  `,
   964  		},
   965  	})
   966  }
   967  
   968  // TestResolveVendorSubdirectory checks that Gazelle can resolve libraries
   969  // in a vendor directory which is not at the repository root.
   970  func TestResolveVendorSubdirectory(t *testing.T) {
   971  	files := []testtools.FileSpec{
   972  		{Path: "WORKSPACE"},
   973  		{
   974  			Path:    "sub/vendor/example.com/foo/foo.go",
   975  			Content: "package foo",
   976  		}, {
   977  			Path: "sub/bar/bar.go",
   978  			Content: `
   979  package bar
   980  
   981  import _ "example.com/foo"
   982  `,
   983  		},
   984  	}
   985  	dir, cleanup := testtools.CreateFiles(t, files)
   986  	defer cleanup()
   987  
   988  	args := []string{"-go_prefix", "example.com/repo"}
   989  	if err := runGazelle(dir, args); err != nil {
   990  		t.Fatal(err)
   991  	}
   992  
   993  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
   994  		{
   995  			Path: "sub/vendor/example.com/foo/BUILD.bazel",
   996  			Content: `
   997  load("@io_bazel_rules_go//go:def.bzl", "go_library")
   998  
   999  go_library(
  1000      name = "foo",
  1001      srcs = ["foo.go"],
  1002      importmap = "example.com/repo/sub/vendor/example.com/foo",
  1003      importpath = "example.com/foo",
  1004      visibility = ["//visibility:public"],
  1005  )
  1006  `,
  1007  		}, {
  1008  			Path: "sub/bar/BUILD.bazel",
  1009  			Content: `
  1010  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  1011  
  1012  go_library(
  1013      name = "bar",
  1014      srcs = ["bar.go"],
  1015      importpath = "example.com/repo/sub/bar",
  1016      visibility = ["//visibility:public"],
  1017      deps = ["//sub/vendor/example.com/foo"],
  1018  )
  1019  `,
  1020  		},
  1021  	})
  1022  }
  1023  
  1024  // TestDeleteProtoWithDeps checks that Gazelle will delete proto rules with
  1025  // dependencies after the proto sources are removed.
  1026  func TestDeleteProtoWithDeps(t *testing.T) {
  1027  	files := []testtools.FileSpec{
  1028  		{Path: "WORKSPACE"},
  1029  		{
  1030  			Path: "foo/BUILD.bazel",
  1031  			Content: `
  1032  load("@rules_proto//proto:defs.bzl", "proto_library")
  1033  load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
  1034  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  1035  
  1036  go_library(
  1037      name = "go_default_library",
  1038      srcs = ["extra.go"],
  1039      embed = [":scratch_go_proto"],
  1040      importpath = "example.com/repo/foo",
  1041      visibility = ["//visibility:public"],
  1042  )
  1043  
  1044  proto_library(
  1045      name = "foo_proto",
  1046      srcs = ["foo.proto"],
  1047      visibility = ["//visibility:public"],
  1048      deps = ["//foo/bar:bar_proto"],
  1049  )
  1050  
  1051  go_proto_library(
  1052      name = "foo_go_proto",
  1053      importpath = "example.com/repo/foo",
  1054      proto = ":foo_proto",
  1055      visibility = ["//visibility:public"],
  1056      deps = ["//foo/bar:go_default_library"],
  1057  )
  1058  `,
  1059  		}, {
  1060  			Path:    "foo/extra.go",
  1061  			Content: "package foo",
  1062  		}, {
  1063  			Path: "foo/bar/bar.proto",
  1064  			Content: `
  1065  syntax = "proto3";
  1066  
  1067  option go_package = "example.com/repo/foo/bar";
  1068  
  1069  message Bar {};
  1070  `,
  1071  		},
  1072  	}
  1073  	dir, cleanup := testtools.CreateFiles(t, files)
  1074  	defer cleanup()
  1075  
  1076  	args := []string{"-go_prefix", "example.com/repo"}
  1077  	if err := runGazelle(dir, args); err != nil {
  1078  		t.Fatal(err)
  1079  	}
  1080  
  1081  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  1082  		{
  1083  			Path: "foo/BUILD.bazel",
  1084  			Content: `
  1085  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  1086  
  1087  go_library(
  1088      name = "go_default_library",
  1089      srcs = ["extra.go"],
  1090      importpath = "example.com/repo/foo",
  1091      visibility = ["//visibility:public"],
  1092  )
  1093  `,
  1094  		},
  1095  	})
  1096  }
  1097  
  1098  func TestCustomRepoNamesMain(t *testing.T) {
  1099  	files := []testtools.FileSpec{
  1100  		{
  1101  			Path: "WORKSPACE",
  1102  			Content: `
  1103  go_repository(
  1104      name = "custom_repo",
  1105      importpath = "example.com/bar",
  1106      commit = "123456",
  1107  )
  1108  `,
  1109  		}, {
  1110  			Path: "foo.go",
  1111  			Content: `
  1112  package foo
  1113  
  1114  import _ "example.com/bar"
  1115  `,
  1116  		},
  1117  	}
  1118  	dir, cleanup := testtools.CreateFiles(t, files)
  1119  	defer cleanup()
  1120  
  1121  	args := []string{"-go_prefix", "example.com/foo"}
  1122  	if err := runGazelle(dir, args); err != nil {
  1123  		t.Fatal(err)
  1124  	}
  1125  
  1126  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  1127  		{
  1128  			Path: "BUILD.bazel",
  1129  			Content: `
  1130  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  1131  
  1132  go_library(
  1133      name = "foo",
  1134      srcs = ["foo.go"],
  1135      importpath = "example.com/foo",
  1136      visibility = ["//visibility:public"],
  1137      deps = ["@custom_repo//:bar"],
  1138  )
  1139  `,
  1140  		},
  1141  	})
  1142  }
  1143  
  1144  func TestCustomRepoNamesExternal(t *testing.T) {
  1145  	files := []testtools.FileSpec{
  1146  		{
  1147  			Path: "main/WORKSPACE",
  1148  			Content: `go_repository(
  1149      name = "custom_repo",
  1150      importpath = "example.com/bar",
  1151      commit = "123456",
  1152  )
  1153  `,
  1154  		}, {
  1155  			Path:    "ext/WORKSPACE",
  1156  			Content: "",
  1157  		}, {
  1158  			Path: "ext/foo.go",
  1159  			Content: `
  1160  package foo
  1161  
  1162  import _ "example.com/bar"
  1163  `,
  1164  		},
  1165  	}
  1166  	dir, cleanup := testtools.CreateFiles(t, files)
  1167  	defer cleanup()
  1168  
  1169  	extDir := filepath.Join(dir, "ext")
  1170  	args := []string{
  1171  		"-go_prefix=example.com/foo",
  1172  		"-go_naming_convention=import_alias",
  1173  		"-mode=fix",
  1174  		"-repo_root=" + extDir,
  1175  		"-repo_config=" + filepath.Join(dir, "main", "WORKSPACE"),
  1176  	}
  1177  	if err := runGazelle(extDir, args); err != nil {
  1178  		t.Fatal(err)
  1179  	}
  1180  
  1181  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  1182  		{
  1183  			Path: "ext/BUILD.bazel",
  1184  			Content: `
  1185  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  1186  
  1187  go_library(
  1188      name = "foo",
  1189      srcs = ["foo.go"],
  1190      importpath = "example.com/foo",
  1191      visibility = ["//visibility:public"],
  1192      deps = ["@custom_repo//:bar"],
  1193  )
  1194  
  1195  alias(
  1196      name = "go_default_library",
  1197      actual = ":foo",
  1198      visibility = ["//visibility:public"],
  1199  )
  1200  `,
  1201  		},
  1202  	})
  1203  }
  1204  
  1205  func TestUpdateReposWithQueryToWorkspace(t *testing.T) {
  1206  	files := []testtools.FileSpec{
  1207  		{
  1208  			Path: "WORKSPACE",
  1209  			Content: `
  1210  load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
  1211  
  1212  http_archive(
  1213      name = "io_bazel_rules_go",
  1214      sha256 = "8df59f11fb697743cbb3f26cfb8750395f30471e9eabde0d174c3aebc7a1cd39",
  1215      urls = [
  1216          "https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/rules_go/releases/download/0.19.1/rules_go-0.19.1.tar.gz",
  1217          "https://github.com/bazelbuild/rules_go/releases/download/0.19.1/rules_go-0.19.1.tar.gz",
  1218      ],
  1219  )
  1220  
  1221  http_archive(
  1222      name = "bazel_gazelle",
  1223      sha256 = "be9296bfd64882e3c08e3283c58fcb461fa6dd3c171764fcc4cf322f60615a9b",
  1224      urls = [
  1225          "https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/bazel-gazelle/releases/download/0.18.1/bazel-gazelle-0.18.1.tar.gz",
  1226          "https://github.com/bazelbuild/bazel-gazelle/releases/download/0.18.1/bazel-gazelle-0.18.1.tar.gz",
  1227      ],
  1228  )
  1229  
  1230  load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
  1231  
  1232  go_rules_dependencies()
  1233  
  1234  go_register_toolchains(nogo = "@bazel_gazelle//:nogo")
  1235  
  1236  load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
  1237  
  1238  gazelle_dependencies()
  1239  `,
  1240  		},
  1241  	}
  1242  
  1243  	dir, cleanup := testtools.CreateFiles(t, files)
  1244  	defer cleanup()
  1245  
  1246  	args := []string{"update-repos", "github.com/sirupsen/logrus@v1.3.0"}
  1247  	if err := runGazelle(dir, args); err != nil {
  1248  		t.Fatal(err)
  1249  	}
  1250  
  1251  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  1252  		{
  1253  			Path: "WORKSPACE",
  1254  			Content: `
  1255  load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
  1256  
  1257  http_archive(
  1258      name = "io_bazel_rules_go",
  1259      sha256 = "8df59f11fb697743cbb3f26cfb8750395f30471e9eabde0d174c3aebc7a1cd39",
  1260      urls = [
  1261          "https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/rules_go/releases/download/0.19.1/rules_go-0.19.1.tar.gz",
  1262          "https://github.com/bazelbuild/rules_go/releases/download/0.19.1/rules_go-0.19.1.tar.gz",
  1263      ],
  1264  )
  1265  
  1266  http_archive(
  1267      name = "bazel_gazelle",
  1268      sha256 = "be9296bfd64882e3c08e3283c58fcb461fa6dd3c171764fcc4cf322f60615a9b",
  1269      urls = [
  1270          "https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/bazel-gazelle/releases/download/0.18.1/bazel-gazelle-0.18.1.tar.gz",
  1271          "https://github.com/bazelbuild/bazel-gazelle/releases/download/0.18.1/bazel-gazelle-0.18.1.tar.gz",
  1272      ],
  1273  )
  1274  
  1275  load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
  1276  
  1277  go_rules_dependencies()
  1278  
  1279  go_register_toolchains(nogo = "@bazel_gazelle//:nogo")
  1280  
  1281  load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
  1282  
  1283  go_repository(
  1284      name = "com_github_sirupsen_logrus",
  1285      importpath = "github.com/sirupsen/logrus",
  1286      sum = "h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME=",
  1287      version = "v1.3.0",
  1288  )
  1289  
  1290  gazelle_dependencies()
  1291  `,
  1292  		}})
  1293  }
  1294  
  1295  func TestImportReposFromDepToWorkspace(t *testing.T) {
  1296  	files := []testtools.FileSpec{
  1297  		{
  1298  			Path: "WORKSPACE",
  1299  			Content: `
  1300  http_archive(
  1301      name = "io_bazel_rules_go",
  1302      sha256 = "4b14d8dd31c6dbaf3ff871adcd03f28c3274e42abc855cb8fb4d01233c0154dc",
  1303      url = "https://github.com/bazelbuild/rules_go/releases/download/0.10.1/rules_go-0.10.1.tar.gz",
  1304  )
  1305  http_archive(
  1306      name = "bazel_gazelle",
  1307      sha256 = "6228d9618ab9536892aa69082c063207c91e777e51bd3c5544c9c060cafe1bd8",
  1308      url = "https://github.com/bazelbuild/bazel-gazelle/releases/download/0.10.0/bazel-gazelle-0.10.0.tar.gz",
  1309  )
  1310  load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains", "go_repository")
  1311  go_rules_dependencies()
  1312  go_register_toolchains()
  1313  
  1314  load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
  1315  gazelle_dependencies()
  1316  
  1317  go_repository(
  1318      name = "org_golang_x_net",
  1319      importpath = "golang.org/x/net",
  1320      tag = "1.2",
  1321  )
  1322  
  1323  # keep
  1324  go_repository(
  1325      name = "org_golang_x_sys",
  1326      importpath = "golang.org/x/sys",
  1327      remote = "https://github.com/golang/sys",
  1328  )
  1329  
  1330  http_archive(
  1331      name = "com_github_go_yaml_yaml",
  1332      sha256 = "1234",
  1333      urls = ["https://example.com/yaml.tar.gz"],
  1334  )
  1335  `,
  1336  		}, {
  1337  			Path: "Gopkg.lock",
  1338  			Content: `# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
  1339  
  1340  
  1341  [[projects]]
  1342    name = "github.com/pkg/errors"
  1343    packages = ["."]
  1344    revision = "645ef00459ed84a119197bfb8d8205042c6df63d"
  1345    version = "v0.8.0"
  1346  
  1347  [[projects]]
  1348    branch = "master"
  1349    name = "golang.org/x/net"
  1350    packages = ["context"]
  1351    revision = "66aacef3dd8a676686c7ae3716979581e8b03c47"
  1352  
  1353  [[projects]]
  1354    branch = "master"
  1355    name = "golang.org/x/sys"
  1356    packages = ["unix"]
  1357    revision = "bb24a47a89eac6c1227fbcb2ae37a8b9ed323366"
  1358  
  1359  [[projects]]
  1360    branch = "v2"
  1361    name = "github.com/go-yaml/yaml"
  1362    packages = ["."]
  1363    revision = "cd8b52f8269e0feb286dfeef29f8fe4d5b397e0b"
  1364  
  1365  [solve-meta]
  1366    analyzer-name = "dep"
  1367    analyzer-version = 1
  1368    inputs-digest = "05c1cd69be2c917c0cc4b32942830c2acfa044d8200fdc94716aae48a8083702"
  1369    solver-name = "gps-cdcl"
  1370    solver-version = 1
  1371  `,
  1372  		},
  1373  	}
  1374  	dir, cleanup := testtools.CreateFiles(t, files)
  1375  	defer cleanup()
  1376  
  1377  	args := []string{"update-repos", "-build_file_generation", "off", "-from_file", "Gopkg.lock"}
  1378  	if err := runGazelle(dir, args); err != nil {
  1379  		t.Fatal(err)
  1380  	}
  1381  
  1382  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  1383  		{
  1384  			Path: "WORKSPACE",
  1385  			Content: `
  1386  http_archive(
  1387      name = "io_bazel_rules_go",
  1388      sha256 = "4b14d8dd31c6dbaf3ff871adcd03f28c3274e42abc855cb8fb4d01233c0154dc",
  1389      url = "https://github.com/bazelbuild/rules_go/releases/download/0.10.1/rules_go-0.10.1.tar.gz",
  1390  )
  1391  
  1392  http_archive(
  1393      name = "bazel_gazelle",
  1394      sha256 = "6228d9618ab9536892aa69082c063207c91e777e51bd3c5544c9c060cafe1bd8",
  1395      url = "https://github.com/bazelbuild/bazel-gazelle/releases/download/0.10.0/bazel-gazelle-0.10.0.tar.gz",
  1396  )
  1397  
  1398  load("@io_bazel_rules_go//go:def.bzl", "go_register_toolchains", "go_rules_dependencies")
  1399  
  1400  go_rules_dependencies()
  1401  
  1402  go_register_toolchains()
  1403  
  1404  load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
  1405  
  1406  go_repository(
  1407      name = "com_github_pkg_errors",
  1408      build_file_generation = "off",
  1409      commit = "645ef00459ed84a119197bfb8d8205042c6df63d",
  1410      importpath = "github.com/pkg/errors",
  1411  )
  1412  
  1413  gazelle_dependencies()
  1414  
  1415  go_repository(
  1416      name = "org_golang_x_net",
  1417      build_file_generation = "off",
  1418      commit = "66aacef3dd8a676686c7ae3716979581e8b03c47",
  1419      importpath = "golang.org/x/net",
  1420  )
  1421  
  1422  # keep
  1423  go_repository(
  1424      name = "org_golang_x_sys",
  1425      importpath = "golang.org/x/sys",
  1426      remote = "https://github.com/golang/sys",
  1427  )
  1428  
  1429  http_archive(
  1430      name = "com_github_go_yaml_yaml",
  1431      sha256 = "1234",
  1432      urls = ["https://example.com/yaml.tar.gz"],
  1433  )
  1434  `,
  1435  		}})
  1436  }
  1437  
  1438  func TestImportReposFromDepToWorkspaceWithMacro(t *testing.T) {
  1439  	files := []testtools.FileSpec{
  1440  		{
  1441  			Path: "WORKSPACE",
  1442  			Content: `
  1443  http_archive(
  1444      name = "io_bazel_rules_go",
  1445      sha256 = "4b14d8dd31c6dbaf3ff871adcd03f28c3274e42abc855cb8fb4d01233c0154dc",
  1446      url = "https://github.com/bazelbuild/rules_go/releases/download/0.10.1/rules_go-0.10.1.tar.gz",
  1447  )
  1448  http_archive(
  1449      name = "bazel_gazelle",
  1450      sha256 = "6228d9618ab9536892aa69082c063207c91e777e51bd3c5544c9c060cafe1bd8",
  1451      url = "https://github.com/bazelbuild/bazel-gazelle/releases/download/0.10.0/bazel-gazelle-0.10.0.tar.gz",
  1452  )
  1453  load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains", "go_repository")
  1454  go_rules_dependencies()
  1455  go_register_toolchains()
  1456  
  1457  load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
  1458  gazelle_dependencies()
  1459  
  1460  http_archive(
  1461  	name = "com_github_go_yaml_yaml",
  1462  	sha256 = "1234",
  1463  	urls = ["https://example.com/yaml.tar.gz"],
  1464  )
  1465  
  1466  # gazelle:repository_macro repositories.bzl%go_repositories
  1467  `,
  1468  		}, {
  1469  			Path: "repositories.bzl",
  1470  			Content: `
  1471  load("@bazel_gazelle//:deps.bzl", "go_repository")
  1472  
  1473  def go_repositories():
  1474      go_repository(
  1475          name = "org_golang_x_net",
  1476          importpath = "golang.org/x/net",
  1477          tag = "1.2",
  1478      )
  1479  
  1480      # keep
  1481      go_repository(
  1482          name = "org_golang_x_sys",
  1483          importpath = "golang.org/x/sys",
  1484          remote = "https://github.com/golang/sys",
  1485      )
  1486  `,
  1487  		}, {
  1488  			Path: "Gopkg.lock",
  1489  			Content: `# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
  1490  
  1491  
  1492  [[projects]]
  1493    name = "github.com/pkg/errors"
  1494    packages = ["."]
  1495    revision = "645ef00459ed84a119197bfb8d8205042c6df63d"
  1496    version = "v0.8.0"
  1497  
  1498  [[projects]]
  1499    branch = "master"
  1500    name = "golang.org/x/net"
  1501    packages = ["context"]
  1502    revision = "66aacef3dd8a676686c7ae3716979581e8b03c47"
  1503  
  1504  [[projects]]
  1505    branch = "master"
  1506    name = "golang.org/x/sys"
  1507    packages = ["unix"]
  1508    revision = "bb24a47a89eac6c1227fbcb2ae37a8b9ed323366"
  1509  
  1510  [[projects]]
  1511    branch = "v2"
  1512    name = "github.com/go-yaml/yaml"
  1513    packages = ["."]
  1514    revision = "cd8b52f8269e0feb286dfeef29f8fe4d5b397e0b"
  1515  
  1516  [solve-meta]
  1517    analyzer-name = "dep"
  1518    analyzer-version = 1
  1519    inputs-digest = "05c1cd69be2c917c0cc4b32942830c2acfa044d8200fdc94716aae48a8083702"
  1520    solver-name = "gps-cdcl"
  1521    solver-version = 1
  1522  `,
  1523  		},
  1524  	}
  1525  	dir, cleanup := testtools.CreateFiles(t, files)
  1526  	defer cleanup()
  1527  
  1528  	args := []string{"update-repos", "-build_file_generation", "off", "-from_file", "Gopkg.lock"}
  1529  	if err := runGazelle(dir, args); err != nil {
  1530  		t.Fatal(err)
  1531  	}
  1532  
  1533  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  1534  		{
  1535  			Path: "WORKSPACE",
  1536  			Content: `
  1537  http_archive(
  1538      name = "io_bazel_rules_go",
  1539      sha256 = "4b14d8dd31c6dbaf3ff871adcd03f28c3274e42abc855cb8fb4d01233c0154dc",
  1540      url = "https://github.com/bazelbuild/rules_go/releases/download/0.10.1/rules_go-0.10.1.tar.gz",
  1541  )
  1542  
  1543  http_archive(
  1544      name = "bazel_gazelle",
  1545      sha256 = "6228d9618ab9536892aa69082c063207c91e777e51bd3c5544c9c060cafe1bd8",
  1546      url = "https://github.com/bazelbuild/bazel-gazelle/releases/download/0.10.0/bazel-gazelle-0.10.0.tar.gz",
  1547  )
  1548  
  1549  load("@io_bazel_rules_go//go:def.bzl", "go_register_toolchains", "go_rules_dependencies")
  1550  
  1551  go_rules_dependencies()
  1552  
  1553  go_register_toolchains()
  1554  
  1555  load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
  1556  
  1557  go_repository(
  1558      name = "com_github_pkg_errors",
  1559      build_file_generation = "off",
  1560      commit = "645ef00459ed84a119197bfb8d8205042c6df63d",
  1561      importpath = "github.com/pkg/errors",
  1562  )
  1563  
  1564  gazelle_dependencies()
  1565  
  1566  http_archive(
  1567      name = "com_github_go_yaml_yaml",
  1568      sha256 = "1234",
  1569      urls = ["https://example.com/yaml.tar.gz"],
  1570  )
  1571  
  1572  # gazelle:repository_macro repositories.bzl%go_repositories
  1573  `,
  1574  		}, {
  1575  			Path: "repositories.bzl",
  1576  			Content: `
  1577  load("@bazel_gazelle//:deps.bzl", "go_repository")
  1578  
  1579  def go_repositories():
  1580      go_repository(
  1581          name = "org_golang_x_net",
  1582          build_file_generation = "off",
  1583          commit = "66aacef3dd8a676686c7ae3716979581e8b03c47",
  1584          importpath = "golang.org/x/net",
  1585      )
  1586  
  1587      # keep
  1588      go_repository(
  1589          name = "org_golang_x_sys",
  1590          importpath = "golang.org/x/sys",
  1591          remote = "https://github.com/golang/sys",
  1592      )
  1593  
  1594  `,
  1595  		},
  1596  	})
  1597  }
  1598  
  1599  func TestImportReposFromDepToMacroWithWorkspace(t *testing.T) {
  1600  	files := []testtools.FileSpec{
  1601  		{
  1602  			Path: "WORKSPACE",
  1603  			Content: `
  1604  http_archive(
  1605      name = "io_bazel_rules_go",
  1606      sha256 = "4b14d8dd31c6dbaf3ff871adcd03f28c3274e42abc855cb8fb4d01233c0154dc",
  1607      url = "https://github.com/bazelbuild/rules_go/releases/download/0.10.1/rules_go-0.10.1.tar.gz",
  1608  )
  1609  
  1610  http_archive(
  1611      name = "bazel_gazelle",
  1612      sha256 = "6228d9618ab9536892aa69082c063207c91e777e51bd3c5544c9c060cafe1bd8",
  1613      url = "https://github.com/bazelbuild/bazel-gazelle/releases/download/0.10.0/bazel-gazelle-0.10.0.tar.gz",
  1614  )
  1615  
  1616  load("@io_bazel_rules_go//go:def.bzl", "go_register_toolchains", "go_rules_dependencies")
  1617  
  1618  go_rules_dependencies()
  1619  
  1620  go_register_toolchains()
  1621  
  1622  load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
  1623  
  1624  gazelle_dependencies()
  1625  
  1626  http_archive(
  1627      name = "com_github_go_yaml_yaml",
  1628      sha256 = "1234",
  1629      urls = ["https://example.com/yaml.tar.gz"],
  1630  )
  1631  
  1632  # gazelle:repository_macro repositories.bzl%go_repositories
  1633  # gazelle:repository_macro repositories.bzl%foo_repositories
  1634  `,
  1635  		}, {
  1636  			Path: "repositories.bzl",
  1637  			Content: `
  1638  load("@bazel_gazelle//:deps.bzl", "go_repository")
  1639  
  1640  def go_repositories():
  1641      go_repository(
  1642          name = "org_golang_x_sys",
  1643          importpath = "golang.org/x/sys",
  1644          remote = "https://github.com/golang/sys",
  1645      )
  1646  
  1647  def foo_repositories():
  1648      go_repository(
  1649          name = "org_golang_x_net",
  1650          importpath = "golang.org/x/net",
  1651          tag = "1.2",
  1652      )
  1653  `,
  1654  		}, {
  1655  			Path: "Gopkg.lock",
  1656  			Content: `# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
  1657  
  1658  
  1659  [[projects]]
  1660    name = "github.com/pkg/errors"
  1661    packages = ["."]
  1662    revision = "645ef00459ed84a119197bfb8d8205042c6df63d"
  1663    version = "v0.8.0"
  1664  
  1665  [[projects]]
  1666    branch = "master"
  1667    name = "golang.org/x/net"
  1668    packages = ["context"]
  1669    revision = "66aacef3dd8a676686c7ae3716979581e8b03c47"
  1670  
  1671  [[projects]]
  1672    branch = "master"
  1673    name = "golang.org/x/sys"
  1674    packages = ["unix"]
  1675    revision = "bb24a47a89eac6c1227fbcb2ae37a8b9ed323366"
  1676  
  1677  [solve-meta]
  1678    analyzer-name = "dep"
  1679    analyzer-version = 1
  1680    inputs-digest = "05c1cd69be2c917c0cc4b32942830c2acfa044d8200fdc94716aae48a8083702"
  1681    solver-name = "gps-cdcl"
  1682    solver-version = 1
  1683  `,
  1684  		},
  1685  	}
  1686  	dir, cleanup := testtools.CreateFiles(t, files)
  1687  	defer cleanup()
  1688  
  1689  	args := []string{"update-repos", "-build_file_generation", "off", "-from_file", "Gopkg.lock", "-to_macro", "repositories.bzl%foo_repositories"}
  1690  	if err := runGazelle(dir, args); err != nil {
  1691  		t.Fatal(err)
  1692  	}
  1693  
  1694  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  1695  		{
  1696  			Path: "WORKSPACE",
  1697  			Content: `
  1698  http_archive(
  1699      name = "io_bazel_rules_go",
  1700      sha256 = "4b14d8dd31c6dbaf3ff871adcd03f28c3274e42abc855cb8fb4d01233c0154dc",
  1701      url = "https://github.com/bazelbuild/rules_go/releases/download/0.10.1/rules_go-0.10.1.tar.gz",
  1702  )
  1703  
  1704  http_archive(
  1705      name = "bazel_gazelle",
  1706      sha256 = "6228d9618ab9536892aa69082c063207c91e777e51bd3c5544c9c060cafe1bd8",
  1707      url = "https://github.com/bazelbuild/bazel-gazelle/releases/download/0.10.0/bazel-gazelle-0.10.0.tar.gz",
  1708  )
  1709  
  1710  load("@io_bazel_rules_go//go:def.bzl", "go_register_toolchains", "go_rules_dependencies")
  1711  
  1712  go_rules_dependencies()
  1713  
  1714  go_register_toolchains()
  1715  
  1716  load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
  1717  
  1718  gazelle_dependencies()
  1719  
  1720  http_archive(
  1721      name = "com_github_go_yaml_yaml",
  1722      sha256 = "1234",
  1723      urls = ["https://example.com/yaml.tar.gz"],
  1724  )
  1725  
  1726  # gazelle:repository_macro repositories.bzl%go_repositories
  1727  # gazelle:repository_macro repositories.bzl%foo_repositories
  1728  `,
  1729  		}, {
  1730  			Path: "repositories.bzl",
  1731  			Content: `
  1732  load("@bazel_gazelle//:deps.bzl", "go_repository")
  1733  
  1734  def go_repositories():
  1735      go_repository(
  1736          name = "org_golang_x_sys",
  1737          build_file_generation = "off",
  1738          commit = "bb24a47a89eac6c1227fbcb2ae37a8b9ed323366",
  1739          importpath = "golang.org/x/sys",
  1740      )
  1741  
  1742  def foo_repositories():
  1743      go_repository(
  1744          name = "com_github_pkg_errors",
  1745          build_file_generation = "off",
  1746          commit = "645ef00459ed84a119197bfb8d8205042c6df63d",
  1747          importpath = "github.com/pkg/errors",
  1748      )
  1749  
  1750      go_repository(
  1751          name = "org_golang_x_net",
  1752          build_file_generation = "off",
  1753          commit = "66aacef3dd8a676686c7ae3716979581e8b03c47",
  1754          importpath = "golang.org/x/net",
  1755      )
  1756  `,
  1757  		},
  1758  	})
  1759  }
  1760  
  1761  func TestImportReposFromDepToEmptyMacro(t *testing.T) {
  1762  	files := []testtools.FileSpec{
  1763  		{
  1764  			Path: "WORKSPACE",
  1765  			Content: `
  1766  local_repository(
  1767      name = "io_bazel_rules_go",
  1768      path = "???",
  1769  )
  1770  
  1771  local_repository(
  1772      name = "bazel_gazelle",
  1773      path = "???",
  1774  )
  1775  
  1776  load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
  1777  load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
  1778  
  1779  go_rules_dependencies()
  1780  
  1781  go_register_toolchains()
  1782  
  1783  gazelle_dependencies()
  1784  `,
  1785  		},
  1786  		{
  1787  			Path: "Gopkg.lock",
  1788  			Content: `# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
  1789  
  1790  
  1791  [[projects]]
  1792    name = "github.com/pkg/errors"
  1793    packages = ["."]
  1794    revision = "645ef00459ed84a119197bfb8d8205042c6df63d"
  1795    version = "v0.8.0"
  1796  
  1797  [[projects]]
  1798    branch = "master"
  1799    name = "golang.org/x/net"
  1800    packages = ["context"]
  1801    revision = "66aacef3dd8a676686c7ae3716979581e8b03c47"
  1802  
  1803  [[projects]]
  1804    branch = "master"
  1805    name = "golang.org/x/sys"
  1806    packages = ["unix"]
  1807    revision = "bb24a47a89eac6c1227fbcb2ae37a8b9ed323366"
  1808  
  1809  [[projects]]
  1810    branch = "v2"
  1811    name = "github.com/go-yaml/yaml"
  1812    packages = ["."]
  1813    revision = "cd8b52f8269e0feb286dfeef29f8fe4d5b397e0b"
  1814  
  1815  [solve-meta]
  1816    analyzer-name = "dep"
  1817    analyzer-version = 1
  1818    inputs-digest = "05c1cd69be2c917c0cc4b32942830c2acfa044d8200fdc94716aae48a8083702"
  1819    solver-name = "gps-cdcl"
  1820    solver-version = 1
  1821  `,
  1822  		},
  1823  	}
  1824  	dir, cleanup := testtools.CreateFiles(t, files)
  1825  	defer cleanup()
  1826  
  1827  	args := []string{"update-repos", "-build_file_generation", "off", "-from_file", "Gopkg.lock", "-to_macro", "repositories.bzl%go_repositories"}
  1828  	if err := runGazelle(dir, args); err != nil {
  1829  		t.Fatal(err)
  1830  	}
  1831  
  1832  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  1833  		{
  1834  			Path: "WORKSPACE",
  1835  			Content: `
  1836  local_repository(
  1837      name = "io_bazel_rules_go",
  1838      path = "???",
  1839  )
  1840  
  1841  local_repository(
  1842      name = "bazel_gazelle",
  1843      path = "???",
  1844  )
  1845  
  1846  load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
  1847  load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
  1848  load("//:repositories.bzl", "go_repositories")
  1849  
  1850  # gazelle:repository_macro repositories.bzl%go_repositories
  1851  go_repositories()
  1852  
  1853  go_rules_dependencies()
  1854  
  1855  go_register_toolchains()
  1856  
  1857  gazelle_dependencies()
  1858  `,
  1859  		}, {
  1860  			Path: "repositories.bzl",
  1861  			Content: `
  1862  load("@bazel_gazelle//:deps.bzl", "go_repository")
  1863  
  1864  def go_repositories():
  1865      go_repository(
  1866          name = "com_github_go_yaml_yaml",
  1867          build_file_generation = "off",
  1868          commit = "cd8b52f8269e0feb286dfeef29f8fe4d5b397e0b",
  1869          importpath = "github.com/go-yaml/yaml",
  1870      )
  1871      go_repository(
  1872          name = "com_github_pkg_errors",
  1873          build_file_generation = "off",
  1874          commit = "645ef00459ed84a119197bfb8d8205042c6df63d",
  1875          importpath = "github.com/pkg/errors",
  1876      )
  1877      go_repository(
  1878          name = "org_golang_x_net",
  1879          build_file_generation = "off",
  1880          commit = "66aacef3dd8a676686c7ae3716979581e8b03c47",
  1881          importpath = "golang.org/x/net",
  1882      )
  1883      go_repository(
  1884          name = "org_golang_x_sys",
  1885          build_file_generation = "off",
  1886          commit = "bb24a47a89eac6c1227fbcb2ae37a8b9ed323366",
  1887          importpath = "golang.org/x/sys",
  1888      )
  1889  `,
  1890  		}})
  1891  }
  1892  
  1893  func TestPruneRepoRules(t *testing.T) {
  1894  	files := []testtools.FileSpec{
  1895  		{
  1896  			Path: "WORKSPACE",
  1897  			Content: `
  1898  http_archive(
  1899      name = "io_bazel_rules_go",
  1900      sha256 = "4b14d8dd31c6dbaf3ff871adcd03f28c3274e42abc855cb8fb4d01233c0154dc",
  1901      url = "https://github.com/bazelbuild/rules_go/releases/download/0.10.1/rules_go-0.10.1.tar.gz",
  1902  )
  1903  
  1904  http_archive(
  1905      name = "bazel_gazelle",
  1906      sha256 = "6228d9618ab9536892aa69082c063207c91e777e51bd3c5544c9c060cafe1bd8",
  1907      url = "https://github.com/bazelbuild/bazel-gazelle/releases/download/0.10.0/bazel-gazelle-0.10.0.tar.gz",
  1908  )
  1909  
  1910  load("@io_bazel_rules_go//go:def.bzl", "go_register_toolchains", "go_rules_dependencies")
  1911  
  1912  go_rules_dependencies()
  1913  
  1914  go_register_toolchains()
  1915  
  1916  load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
  1917  
  1918  gazelle_dependencies()
  1919  
  1920  http_archive(
  1921      name = "com_github_go_yaml_yaml",
  1922      sha256 = "1234",
  1923      urls = ["https://example.com/yaml.tar.gz"],
  1924  )
  1925  
  1926  go_repository(
  1927      name = "pruneMe",
  1928      importpath = "pruneMe",
  1929  )
  1930  
  1931  # keep
  1932  go_repository(
  1933      name = "keepMe",
  1934      importpath = "keepMe",
  1935  )
  1936  
  1937  # gazelle:repository_macro repositories.bzl%go_repositories
  1938  # gazelle:repository_macro repositories.bzl%foo_repositories
  1939  `,
  1940  		}, {
  1941  			Path: "repositories.bzl",
  1942  			Content: `
  1943  load("@bazel_gazelle//:deps.bzl", "go_repository")
  1944  
  1945  def go_repositories():
  1946      go_repository(
  1947          name = "org_golang_x_sys",
  1948          importpath = "golang.org/x/sys",
  1949          remote = "https://github.com/golang/sys",
  1950      )
  1951      go_repository(
  1952          name = "foo",
  1953          importpath = "foo",
  1954      )
  1955  
  1956  def foo_repositories():
  1957      go_repository(
  1958          name = "org_golang_x_net",
  1959          importpath = "golang.org/x/net",
  1960          tag = "1.2",
  1961      )
  1962      go_repository(
  1963          name = "bar",
  1964          importpath = "bar",
  1965      )
  1966  
  1967      # keep
  1968      go_repository(
  1969          name = "stay",
  1970          importpath = "stay",
  1971      )
  1972  `,
  1973  		}, {
  1974  			Path: "Gopkg.lock",
  1975  			Content: `# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
  1976  
  1977  
  1978  [[projects]]
  1979    name = "github.com/pkg/errors"
  1980    packages = ["."]
  1981    revision = "645ef00459ed84a119197bfb8d8205042c6df63d"
  1982    version = "v0.8.0"
  1983  
  1984  [[projects]]
  1985    branch = "master"
  1986    name = "golang.org/x/net"
  1987    packages = ["context"]
  1988    revision = "66aacef3dd8a676686c7ae3716979581e8b03c47"
  1989  
  1990  [[projects]]
  1991    branch = "master"
  1992    name = "golang.org/x/sys"
  1993    packages = ["unix"]
  1994    revision = "bb24a47a89eac6c1227fbcb2ae37a8b9ed323366"
  1995  
  1996  [solve-meta]
  1997    analyzer-name = "dep"
  1998    analyzer-version = 1
  1999    inputs-digest = "05c1cd69be2c917c0cc4b32942830c2acfa044d8200fdc94716aae48a8083702"
  2000    solver-name = "gps-cdcl"
  2001    solver-version = 1
  2002  `,
  2003  		},
  2004  	}
  2005  	dir, cleanup := testtools.CreateFiles(t, files)
  2006  	defer cleanup()
  2007  
  2008  	args := []string{"update-repos", "-build_file_generation", "off", "-from_file", "Gopkg.lock", "-to_macro", "repositories.bzl%foo_repositories", "-prune"}
  2009  	if err := runGazelle(dir, args); err != nil {
  2010  		t.Fatal(err)
  2011  	}
  2012  
  2013  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  2014  		{
  2015  			Path: "WORKSPACE",
  2016  			Content: `
  2017  http_archive(
  2018      name = "io_bazel_rules_go",
  2019      sha256 = "4b14d8dd31c6dbaf3ff871adcd03f28c3274e42abc855cb8fb4d01233c0154dc",
  2020      url = "https://github.com/bazelbuild/rules_go/releases/download/0.10.1/rules_go-0.10.1.tar.gz",
  2021  )
  2022  
  2023  http_archive(
  2024      name = "bazel_gazelle",
  2025      sha256 = "6228d9618ab9536892aa69082c063207c91e777e51bd3c5544c9c060cafe1bd8",
  2026      url = "https://github.com/bazelbuild/bazel-gazelle/releases/download/0.10.0/bazel-gazelle-0.10.0.tar.gz",
  2027  )
  2028  
  2029  load("@io_bazel_rules_go//go:def.bzl", "go_register_toolchains", "go_rules_dependencies")
  2030  
  2031  go_rules_dependencies()
  2032  
  2033  go_register_toolchains()
  2034  
  2035  load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
  2036  
  2037  gazelle_dependencies()
  2038  
  2039  http_archive(
  2040      name = "com_github_go_yaml_yaml",
  2041      sha256 = "1234",
  2042      urls = ["https://example.com/yaml.tar.gz"],
  2043  )
  2044  
  2045  # keep
  2046  go_repository(
  2047      name = "keepMe",
  2048      importpath = "keepMe",
  2049  )
  2050  
  2051  # gazelle:repository_macro repositories.bzl%go_repositories
  2052  # gazelle:repository_macro repositories.bzl%foo_repositories
  2053  `,
  2054  		}, {
  2055  			Path: "repositories.bzl",
  2056  			Content: `
  2057  load("@bazel_gazelle//:deps.bzl", "go_repository")
  2058  
  2059  def go_repositories():
  2060      go_repository(
  2061          name = "org_golang_x_sys",
  2062          build_file_generation = "off",
  2063          commit = "bb24a47a89eac6c1227fbcb2ae37a8b9ed323366",
  2064          importpath = "golang.org/x/sys",
  2065      )
  2066  
  2067  def foo_repositories():
  2068      go_repository(
  2069          name = "com_github_pkg_errors",
  2070          build_file_generation = "off",
  2071          commit = "645ef00459ed84a119197bfb8d8205042c6df63d",
  2072          importpath = "github.com/pkg/errors",
  2073      )
  2074  
  2075      go_repository(
  2076          name = "org_golang_x_net",
  2077          build_file_generation = "off",
  2078          commit = "66aacef3dd8a676686c7ae3716979581e8b03c47",
  2079          importpath = "golang.org/x/net",
  2080      )
  2081  
  2082      # keep
  2083      go_repository(
  2084          name = "stay",
  2085          importpath = "stay",
  2086      )
  2087  `,
  2088  		},
  2089  	})
  2090  }
  2091  
  2092  func TestDeleteRulesInEmptyDir(t *testing.T) {
  2093  	files := []testtools.FileSpec{
  2094  		{Path: "WORKSPACE"},
  2095  		{
  2096  			Path: "BUILD.bazel",
  2097  			Content: `
  2098  load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_binary")
  2099  
  2100  go_library(
  2101      name = "go_default_library",
  2102      srcs = [
  2103          "bar.go",
  2104          "foo.go",
  2105      ],
  2106      importpath = "example.com/repo",
  2107      visibility = ["//visibility:private"],
  2108  )
  2109  
  2110  go_binary(
  2111      name = "cmd",
  2112      embed = [":go_default_library"],
  2113      visibility = ["//visibility:public"],
  2114  )
  2115  `,
  2116  		},
  2117  	}
  2118  	dir, cleanup := testtools.CreateFiles(t, files)
  2119  	defer cleanup()
  2120  
  2121  	args := []string{"-go_prefix=example.com/repo"}
  2122  	if err := runGazelle(dir, args); err != nil {
  2123  		t.Fatal(err)
  2124  	}
  2125  
  2126  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  2127  		{
  2128  			Path:    "BUILD.bazel",
  2129  			Content: "",
  2130  		},
  2131  	})
  2132  }
  2133  
  2134  func TestFixWorkspaceWithoutGazelle(t *testing.T) {
  2135  	files := []testtools.FileSpec{
  2136  		{
  2137  			Path: "WORKSPACE",
  2138  			Content: `
  2139  load("@io_bazel_rules_go//go:def.bzl", "go_repository")
  2140  
  2141  go_repository(
  2142      name = "com_example_repo",
  2143      importpath = "example.com/repo",
  2144      tag = "1.2.3",
  2145  )
  2146  `,
  2147  		},
  2148  	}
  2149  	dir, cleanup := testtools.CreateFiles(t, files)
  2150  	defer cleanup()
  2151  
  2152  	if err := runGazelle(dir, []string{"fix", "-go_prefix="}); err == nil {
  2153  		t.Error("got success; want error")
  2154  	} else if want := "bazel_gazelle is not declared"; !strings.Contains(err.Error(), want) {
  2155  		t.Errorf("got error %v; want error containing %q", err, want)
  2156  	}
  2157  }
  2158  
  2159  // TestFixGazelle checks that loads of the gazelle macro from the old location
  2160  // in rules_go are replaced with the new location in @bazel_gazelle.
  2161  func TestFixGazelle(t *testing.T) {
  2162  	files := []testtools.FileSpec{
  2163  		{
  2164  			Path: "WORKSPACE",
  2165  		}, {
  2166  			Path: "BUILD.bazel",
  2167  			Content: `
  2168  load("@io_bazel_rules_go//go:def.bzl", "gazelle", "go_library")
  2169  
  2170  gazelle(name = "gazelle")
  2171  
  2172  # keep
  2173  go_library(name = "go_default_library")
  2174  `,
  2175  		},
  2176  	}
  2177  	dir, cleanup := testtools.CreateFiles(t, files)
  2178  	defer cleanup()
  2179  
  2180  	if err := runGazelle(dir, nil); err != nil {
  2181  		t.Fatal(err)
  2182  	}
  2183  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  2184  		{
  2185  			Path: "BUILD.bazel",
  2186  			Content: `
  2187  load("@bazel_gazelle//:def.bzl", "gazelle")
  2188  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  2189  
  2190  gazelle(name = "gazelle")
  2191  
  2192  # keep
  2193  go_library(name = "go_default_library")
  2194  `,
  2195  		},
  2196  	})
  2197  }
  2198  
  2199  // TestKeepDeps checks rules with keep comments on the rule or on the deps
  2200  // attribute will not be modified during dependency resolution. Verifies #212.
  2201  func TestKeepDeps(t *testing.T) {
  2202  	files := []testtools.FileSpec{
  2203  		{
  2204  			Path: "WORKSPACE",
  2205  		}, {
  2206  			Path: "BUILD.bazel",
  2207  			Content: `
  2208  load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
  2209  
  2210  # gazelle:prefix example.com/repo
  2211  
  2212  # keep
  2213  go_library(
  2214      name = "go_default_library",
  2215      srcs = ["lib.go"],
  2216      importpath = "example.com/repo",
  2217      deps = [":dont_remove"],
  2218  )
  2219  
  2220  go_test(
  2221      name = "go_default_test",
  2222      # keep
  2223      srcs = ["lib_test.go"],
  2224      # keep
  2225      embed = [":go_default_library"],
  2226      # keep
  2227      deps = [":dont_remove"],
  2228  )
  2229  `,
  2230  		},
  2231  	}
  2232  	dir, cleanup := testtools.CreateFiles(t, files)
  2233  	defer cleanup()
  2234  
  2235  	if err := runGazelle(dir, nil); err != nil {
  2236  		t.Fatal(err)
  2237  	}
  2238  
  2239  	testtools.CheckFiles(t, dir, files)
  2240  }
  2241  
  2242  func TestDontCreateBuildFileInEmptyDir(t *testing.T) {
  2243  	files := []testtools.FileSpec{
  2244  		{Path: "WORKSPACE"},
  2245  		{Path: "sub/"},
  2246  	}
  2247  	dir, cleanup := testtools.CreateFiles(t, files)
  2248  	defer cleanup()
  2249  
  2250  	if err := runGazelle(dir, nil); err != nil {
  2251  		t.Error(err)
  2252  	}
  2253  	for _, f := range []string{"BUILD.bazel", "sub/BUILD.bazel"} {
  2254  		path := filepath.Join(dir, filepath.FromSlash(f))
  2255  		_, err := os.Stat(path)
  2256  		if err == nil {
  2257  			t.Errorf("%s: build file was created", f)
  2258  		}
  2259  	}
  2260  }
  2261  
  2262  // TestNoIndex checks that gazelle does not index existing or generated
  2263  // library rules with the flag -index=false. Verifies #384.
  2264  func TestNoIndex(t *testing.T) {
  2265  	files := []testtools.FileSpec{
  2266  		{Path: "WORKSPACE"},
  2267  		{
  2268  			Path: "foo/foo.go",
  2269  			Content: `
  2270  package foo
  2271  
  2272  import _ "example.com/bar"
  2273  `,
  2274  		}, {
  2275  			Path:    "third_party/example.com/bar/bar.go",
  2276  			Content: "package bar",
  2277  		}, {
  2278  			Path:    "third_party/BUILD.bazel",
  2279  			Content: "# gazelle:prefix",
  2280  		}, {
  2281  			Path: "third_party/example.com/bar/BUILD.bazel",
  2282  			Content: `
  2283  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  2284  
  2285  go_library(
  2286      name = "bar",
  2287      srcs = ["bar.go"],
  2288      importpath = "example.com/bar",
  2289      visibility = ["//visibility:public"],
  2290  )
  2291  `,
  2292  		},
  2293  	}
  2294  	dir, cleanup := testtools.CreateFiles(t, files)
  2295  	defer cleanup()
  2296  
  2297  	args := []string{
  2298  		"-go_prefix=example.com/repo",
  2299  		"-external=vendored",
  2300  		"-index=false",
  2301  	}
  2302  	if err := runGazelle(dir, args); err != nil {
  2303  		t.Fatal(err)
  2304  	}
  2305  
  2306  	testtools.CheckFiles(t, dir, []testtools.FileSpec{{
  2307  		Path: "foo/BUILD.bazel",
  2308  		Content: `
  2309  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  2310  
  2311  go_library(
  2312      name = "foo",
  2313      srcs = ["foo.go"],
  2314      importpath = "example.com/repo/foo",
  2315      visibility = ["//visibility:public"],
  2316      deps = ["//vendor/example.com/bar"],
  2317  )
  2318  `,
  2319  	}})
  2320  }
  2321  
  2322  // TestNoIndexNoRecurse checks that gazelle behaves correctly with the flags
  2323  // -r=false -index=false. Gazelle should not generate build files in
  2324  // subdirectories and should not resolve dependencies to local libraries.
  2325  func TestNoIndexNoRecurse(t *testing.T) {
  2326  	barBuildFile := testtools.FileSpec{
  2327  		Path:    "foo/bar/BUILD.bazel",
  2328  		Content: "# this should not be updated because -r=false",
  2329  	}
  2330  	files := []testtools.FileSpec{
  2331  		{Path: "WORKSPACE"},
  2332  		{
  2333  			Path: "foo/foo.go",
  2334  			Content: `package foo
  2335  
  2336  import (
  2337  	_ "example.com/dep/baz"
  2338  )
  2339  `,
  2340  		},
  2341  		barBuildFile,
  2342  		{
  2343  			Path:    "foo/bar/bar.go",
  2344  			Content: "package bar",
  2345  		}, {
  2346  			Path: "third_party/baz/BUILD.bazel",
  2347  			Content: `load("@io_bazel_rules_go//go:def.bzl", "go_library")
  2348  
  2349  # this should be ignored because -index=false
  2350  go_library(
  2351      name = "baz",
  2352      srcs = ["baz.go"],
  2353      importpath = "example.com/dep/baz",
  2354      visibility = ["//visibility:public"],
  2355  )
  2356  `,
  2357  		}, {
  2358  			Path:    "third_party/baz/baz.go",
  2359  			Content: "package baz",
  2360  		},
  2361  	}
  2362  	dir, cleanup := testtools.CreateFiles(t, files)
  2363  	defer cleanup()
  2364  
  2365  	args := []string{
  2366  		"-go_prefix=example.com/repo",
  2367  		"-external=vendored",
  2368  		"-r=false",
  2369  		"-index=false",
  2370  		"foo",
  2371  	}
  2372  	if err := runGazelle(dir, args); err != nil {
  2373  		t.Fatal(err)
  2374  	}
  2375  
  2376  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  2377  		{
  2378  			Path: "foo/BUILD.bazel",
  2379  			Content: `load("@io_bazel_rules_go//go:def.bzl", "go_library")
  2380  
  2381  go_library(
  2382      name = "foo",
  2383      srcs = ["foo.go"],
  2384      importpath = "example.com/repo/foo",
  2385      visibility = ["//visibility:public"],
  2386      deps = ["//vendor/example.com/dep/baz"],
  2387  )
  2388  `,
  2389  		},
  2390  		barBuildFile,
  2391  	})
  2392  }
  2393  
  2394  // TestNoIndexRecurse checks that gazelle behaves correctly with the flags
  2395  // -r=true -index=false. Gazelle should generate build files in directories
  2396  // and subdirectories, but should not resolve dependencies to local libraries.
  2397  func TestNoIndexRecurse(t *testing.T) {
  2398  	files := []testtools.FileSpec{
  2399  		{Path: "WORKSPACE"}, {
  2400  			Path: "foo/foo.go",
  2401  			Content: `package foo
  2402  
  2403  import (
  2404  	_ "example.com/dep/baz"
  2405  )
  2406  `,
  2407  		}, {
  2408  			Path:    "foo/bar/bar.go",
  2409  			Content: "package bar",
  2410  		}, {
  2411  			Path: "third_party/baz/BUILD.bazel",
  2412  			Content: `load("@io_bazel_rules_go//go:def.bzl", "go_library")
  2413  
  2414  # this should be ignored because -index=false
  2415  go_library(
  2416      name = "baz",
  2417      srcs = ["baz.go"],
  2418      importpath = "example.com/dep/baz",
  2419      visibility = ["//visibility:public"],
  2420  )
  2421  `,
  2422  		}, {
  2423  			Path:    "third_party/baz/baz.go",
  2424  			Content: "package baz",
  2425  		},
  2426  	}
  2427  	dir, cleanup := testtools.CreateFiles(t, files)
  2428  	defer cleanup()
  2429  
  2430  	args := []string{
  2431  		"-go_prefix=example.com/repo",
  2432  		"-external=vendored",
  2433  		"-r=true",
  2434  		"-index=false",
  2435  		"foo",
  2436  	}
  2437  	if err := runGazelle(dir, args); err != nil {
  2438  		t.Fatal(err)
  2439  	}
  2440  
  2441  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  2442  		{
  2443  			Path: "foo/BUILD.bazel",
  2444  			Content: `load("@io_bazel_rules_go//go:def.bzl", "go_library")
  2445  
  2446  go_library(
  2447      name = "foo",
  2448      srcs = ["foo.go"],
  2449      importpath = "example.com/repo/foo",
  2450      visibility = ["//visibility:public"],
  2451      deps = ["//vendor/example.com/dep/baz"],
  2452  )
  2453  `,
  2454  		}, {
  2455  			Path: "foo/bar/BUILD.bazel",
  2456  			Content: `load("@io_bazel_rules_go//go:def.bzl", "go_library")
  2457  
  2458  go_library(
  2459      name = "bar",
  2460      srcs = ["bar.go"],
  2461      importpath = "example.com/repo/foo/bar",
  2462      visibility = ["//visibility:public"],
  2463  )
  2464  `,
  2465  		}})
  2466  }
  2467  
  2468  // TestSubdirectoryPrefixExternal checks that directives set in subdirectories
  2469  // may be used in dependency resolution. Verifies #412.
  2470  func TestSubdirectoryPrefixExternal(t *testing.T) {
  2471  	files := []testtools.FileSpec{
  2472  		{
  2473  			Path: "WORKSPACE",
  2474  		}, {
  2475  			Path:    "BUILD.bazel",
  2476  			Content: "# gazelle:prefix",
  2477  		}, {
  2478  			Path:    "sub/BUILD.bazel",
  2479  			Content: "# gazelle:prefix example.com/sub",
  2480  		}, {
  2481  			Path: "sub/sub.go",
  2482  			Content: `
  2483  package sub
  2484  
  2485  import (
  2486  	_ "example.com/sub/missing"
  2487  	_ "example.com/external"
  2488  )
  2489  `,
  2490  		},
  2491  	}
  2492  	dir, cleanup := testtools.CreateFiles(t, files)
  2493  	defer cleanup()
  2494  
  2495  	if err := runGazelle(dir, []string{"-external=vendored", "-index=false"}); err != nil {
  2496  		t.Fatal(err)
  2497  	}
  2498  
  2499  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  2500  		{
  2501  			Path: "sub/BUILD.bazel",
  2502  			Content: `
  2503  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  2504  
  2505  # gazelle:prefix example.com/sub
  2506  
  2507  go_library(
  2508      name = "sub",
  2509      srcs = ["sub.go"],
  2510      importpath = "example.com/sub",
  2511      visibility = ["//visibility:public"],
  2512      deps = [
  2513          "//sub/missing",
  2514          "//vendor/example.com/external",
  2515      ],
  2516  )
  2517  `,
  2518  		},
  2519  	})
  2520  }
  2521  
  2522  func TestGoGrpcProtoFlag(t *testing.T) {
  2523  	files := []testtools.FileSpec{
  2524  		{
  2525  			Path: "WORKSPACE",
  2526  		}, {
  2527  			Path: "BUILD.bazel",
  2528  			Content: `
  2529  load("@rules_proto//proto:defs.bzl", "proto_library")
  2530  load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
  2531  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  2532  
  2533  go_proto_library(
  2534      name = "foo_go_proto",
  2535      importpath = "example.com/repo/foo",
  2536      proto = ":foo_proto",
  2537      visibility = ["//visibility:public"],
  2538  )
  2539  
  2540  proto_library(
  2541      name = "foo_proto",
  2542      srcs = ["foo.proto"],
  2543      visibility = ["//visibility:public"],
  2544  )
  2545  
  2546  go_library(
  2547      name = "go_default_library",
  2548      embed = [":foo_go_proto"],
  2549      importpath = "example.com/repo/foo",
  2550      visibility = ["//visibility:public"],
  2551  )
  2552  `,
  2553  		}, {
  2554  			Path: "foo.proto",
  2555  			Content: `
  2556  syntax = "proto3";
  2557  
  2558  option go_package = "example.com/repo/foo";
  2559  
  2560  message Bar {};
  2561  `,
  2562  		}, {
  2563  			Path: "service/BUILD.bazel",
  2564  			Content: `
  2565  load("@rules_proto//proto:defs.bzl", "proto_library")
  2566  load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
  2567  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  2568  
  2569  go_proto_library(
  2570      name = "service_go_proto",
  2571      compilers = ["@io_bazel_rules_go//proto:go_grpc"],
  2572      importpath = "example.com/repo/service",
  2573      proto = ":service_proto",
  2574      visibility = ["//visibility:public"],
  2575  )
  2576  
  2577  proto_library(
  2578      name = "service_proto",
  2579      srcs = ["service.proto"],
  2580      visibility = ["//visibility:public"],
  2581  )
  2582  
  2583  go_library(
  2584      name = "go_default_library",
  2585      embed = [":service_go_proto"],
  2586      importpath = "example.com/repo/service",
  2587      visibility = ["//visibility:public"],
  2588  )
  2589  `,
  2590  		}, {
  2591  			Path: "service/service.proto",
  2592  			Content: `
  2593  syntax = "proto3";
  2594  
  2595  option go_package = "example.com/repo/service";
  2596  
  2597  service TestService {}
  2598  `,
  2599  		},
  2600  	}
  2601  	dir, cleanup := testtools.CreateFiles(t, files)
  2602  	defer cleanup()
  2603  
  2604  	args := []string{"update", "-go_grpc_compiler", "//foo", "-go_proto_compiler", "//bar"}
  2605  	if err := runGazelle(dir, args); err != nil {
  2606  		t.Fatal(err)
  2607  	}
  2608  
  2609  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  2610  		{
  2611  			Path: "BUILD.bazel",
  2612  			Content: `
  2613  load("@rules_proto//proto:defs.bzl", "proto_library")
  2614  load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
  2615  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  2616  
  2617  go_proto_library(
  2618      name = "foo_go_proto",
  2619      compilers = ["//bar"],
  2620      importpath = "example.com/repo/foo",
  2621      proto = ":foo_proto",
  2622      visibility = ["//visibility:public"],
  2623  )
  2624  
  2625  proto_library(
  2626      name = "foo_proto",
  2627      srcs = ["foo.proto"],
  2628      visibility = ["//visibility:public"],
  2629  )
  2630  
  2631  go_library(
  2632      name = "go_default_library",
  2633      embed = [":foo_go_proto"],
  2634      importpath = "example.com/repo/foo",
  2635      visibility = ["//visibility:public"],
  2636  )
  2637  `,
  2638  		},
  2639  		{
  2640  			Path: "service/BUILD.bazel",
  2641  			Content: `
  2642  load("@rules_proto//proto:defs.bzl", "proto_library")
  2643  load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
  2644  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  2645  
  2646  go_proto_library(
  2647      name = "service_go_proto",
  2648      compilers = ["//foo"],
  2649      importpath = "example.com/repo/service",
  2650      proto = ":service_proto",
  2651      visibility = ["//visibility:public"],
  2652  )
  2653  
  2654  proto_library(
  2655      name = "service_proto",
  2656      srcs = ["service.proto"],
  2657      visibility = ["//visibility:public"],
  2658  )
  2659  
  2660  go_library(
  2661      name = "go_default_library",
  2662      embed = [":service_go_proto"],
  2663      importpath = "example.com/repo/service",
  2664      visibility = ["//visibility:public"],
  2665  )
  2666  `,
  2667  		},
  2668  	})
  2669  }
  2670  
  2671  // TestMapKind tests the gazelle:map_kind directive.
  2672  // Verifies #448
  2673  func TestMapKind(t *testing.T) {
  2674  	files := []testtools.FileSpec{
  2675  		{
  2676  			Path: "WORKSPACE",
  2677  		}, {
  2678  			Path: "BUILD.bazel",
  2679  			Content: `
  2680  # gazelle:prefix example.com/mapkind
  2681  # gazelle:go_naming_convention go_default_library
  2682  `,
  2683  		}, {
  2684  			Path:    "root_lib.go",
  2685  			Content: `package mapkind`,
  2686  		}, {
  2687  			Path:    "enabled/BUILD.bazel",
  2688  			Content: "# gazelle:map_kind go_library my_library //tools/go:def.bzl",
  2689  		}, {
  2690  			Path:    "enabled/enabled_lib.go",
  2691  			Content: `package enabled`,
  2692  		}, {
  2693  			Path: "enabled/inherited/BUILD.bazel",
  2694  		}, {
  2695  			Path:    "enabled/inherited/inherited_lib.go",
  2696  			Content: `package inherited`,
  2697  		}, {
  2698  			Path: "enabled/existing_rules/mapped/BUILD.bazel",
  2699  			Content: `
  2700  load("//tools/go:def.bzl", "my_library")
  2701  
  2702  # An existing rule with a mapped type is updated
  2703  my_library(
  2704      name = "go_default_library",
  2705      srcs = ["deleted_file.go", "mapped_lib.go"],
  2706      importpath = "example.com/mapkind/enabled/existing_rules/mapped",
  2707      visibility = ["//visibility:public"],
  2708  )
  2709  `,
  2710  		}, {
  2711  			Path:    "enabled/existing_rules/mapped/mapped_lib.go",
  2712  			Content: `package mapped`,
  2713  		}, {
  2714  			Path:    "enabled/existing_rules/mapped/mapped_lib2.go",
  2715  			Content: `package mapped`,
  2716  		}, {
  2717  			Path: "enabled/existing_rules/unmapped/BUILD.bazel",
  2718  			Content: `
  2719  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  2720  
  2721  # An existing rule with an unmapped type is preserved
  2722  go_library(
  2723      name = "go_default_library",
  2724      srcs = ["unmapped_lib.go"],
  2725      importpath = "example.com/mapkind/enabled/existing_rules/unmapped",
  2726      visibility = ["//visibility:public"],
  2727  )
  2728  `,
  2729  		}, {
  2730  			Path:    "enabled/existing_rules/unmapped/unmapped_lib.go",
  2731  			Content: `package unmapped`,
  2732  		}, {
  2733  			Path:    "enabled/existing_rules/nobuild/nobuild_lib.go",
  2734  			Content: `package nobuild`,
  2735  		}, {
  2736  			Path:    "enabled/overridden/BUILD.bazel",
  2737  			Content: "# gazelle:map_kind go_library overridden_library //tools/overridden:def.bzl",
  2738  		}, {
  2739  			Path:    "enabled/overridden/overridden_lib.go",
  2740  			Content: `package overridden`,
  2741  		}, {
  2742  			Path: "disabled/BUILD.bazel",
  2743  		}, {
  2744  			Path:    "disabled/disabled_lib.go",
  2745  			Content: `package disabled`,
  2746  		}, {
  2747  			Path: "enabled/multiple_mappings/BUILD.bazel",
  2748  			Content: `
  2749  # gazelle:map_kind go_binary go_binary //tools/go:def.bzl
  2750  # gazelle:map_kind go_library go_library //tools/go:def.bzl
  2751  `,
  2752  		}, {
  2753  			Path:    "enabled/multiple_mappings/multiple_mappings.go",
  2754  			Content: `package main`,
  2755  		},
  2756  		{
  2757  			Path: "depend_on_mapped_kind/lib.go",
  2758  			Content: `package depend_on_mapped_kind
  2759  import (
  2760  	_ "example.com/mapkind/disabled"
  2761  	_ "example.com/mapkind/enabled"
  2762  	_ "example.com/mapkind/enabled/existing_rules/mapped"
  2763  	_ "example.com/mapkind/enabled/existing_rules/unmapped"
  2764  	_ "example.com/mapkind/enabled/overridden"
  2765  )`,
  2766  		},
  2767  	}
  2768  	dir, cleanup := testtools.CreateFiles(t, files)
  2769  	defer cleanup()
  2770  
  2771  	if err := runGazelle(dir, []string{"-external=vendored"}); err != nil {
  2772  		t.Fatal(err)
  2773  	}
  2774  
  2775  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  2776  		{
  2777  			Path: "BUILD.bazel",
  2778  			Content: `
  2779  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  2780  
  2781  # gazelle:prefix example.com/mapkind
  2782  # gazelle:go_naming_convention go_default_library
  2783  
  2784  go_library(
  2785      name = "go_default_library",
  2786      srcs = ["root_lib.go"],
  2787      importpath = "example.com/mapkind",
  2788      visibility = ["//visibility:public"],
  2789  )
  2790  `,
  2791  		},
  2792  		{
  2793  			Path: "enabled/BUILD.bazel",
  2794  			Content: `
  2795  load("//tools/go:def.bzl", "my_library")
  2796  
  2797  # gazelle:map_kind go_library my_library //tools/go:def.bzl
  2798  
  2799  my_library(
  2800      name = "go_default_library",
  2801      srcs = ["enabled_lib.go"],
  2802      importpath = "example.com/mapkind/enabled",
  2803      visibility = ["//visibility:public"],
  2804  )
  2805  `,
  2806  		},
  2807  		{
  2808  			Path: "enabled/inherited/BUILD.bazel",
  2809  			Content: `
  2810  load("//tools/go:def.bzl", "my_library")
  2811  
  2812  my_library(
  2813      name = "go_default_library",
  2814      srcs = ["inherited_lib.go"],
  2815      importpath = "example.com/mapkind/enabled/inherited",
  2816      visibility = ["//visibility:public"],
  2817  )
  2818  `,
  2819  		},
  2820  		{
  2821  			Path: "enabled/overridden/BUILD.bazel",
  2822  			Content: `
  2823  load("//tools/overridden:def.bzl", "overridden_library")
  2824  
  2825  # gazelle:map_kind go_library overridden_library //tools/overridden:def.bzl
  2826  
  2827  overridden_library(
  2828      name = "go_default_library",
  2829      srcs = ["overridden_lib.go"],
  2830      importpath = "example.com/mapkind/enabled/overridden",
  2831      visibility = ["//visibility:public"],
  2832  )
  2833  `,
  2834  		},
  2835  		{
  2836  			Path: "disabled/BUILD.bazel",
  2837  			Content: `
  2838  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  2839  
  2840  go_library(
  2841      name = "go_default_library",
  2842      srcs = ["disabled_lib.go"],
  2843      importpath = "example.com/mapkind/disabled",
  2844      visibility = ["//visibility:public"],
  2845  )
  2846  `,
  2847  		},
  2848  		{
  2849  			Path: "enabled/existing_rules/mapped/BUILD.bazel",
  2850  			Content: `
  2851  load("//tools/go:def.bzl", "my_library")
  2852  
  2853  # An existing rule with a mapped type is updated
  2854  my_library(
  2855      name = "go_default_library",
  2856      srcs = [
  2857          "mapped_lib.go",
  2858          "mapped_lib2.go",
  2859      ],
  2860      importpath = "example.com/mapkind/enabled/existing_rules/mapped",
  2861      visibility = ["//visibility:public"],
  2862  )
  2863  `,
  2864  		},
  2865  		{
  2866  			Path: "enabled/existing_rules/unmapped/BUILD.bazel",
  2867  			Content: `
  2868  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  2869  
  2870  # An existing rule with an unmapped type is preserved
  2871  go_library(
  2872      name = "go_default_library",
  2873      srcs = ["unmapped_lib.go"],
  2874      importpath = "example.com/mapkind/enabled/existing_rules/unmapped",
  2875      visibility = ["//visibility:public"],
  2876  )
  2877  `,
  2878  		},
  2879  		{
  2880  			Path: "enabled/existing_rules/nobuild/BUILD.bazel",
  2881  			Content: `
  2882  load("//tools/go:def.bzl", "my_library")
  2883  
  2884  my_library(
  2885      name = "go_default_library",
  2886      srcs = ["nobuild_lib.go"],
  2887      importpath = "example.com/mapkind/enabled/existing_rules/nobuild",
  2888      visibility = ["//visibility:public"],
  2889  )
  2890  `,
  2891  		},
  2892  		{
  2893  			Path: "enabled/multiple_mappings/BUILD.bazel",
  2894  			Content: `
  2895  load("//tools/go:def.bzl", "go_binary", "go_library")
  2896  
  2897  # gazelle:map_kind go_binary go_binary //tools/go:def.bzl
  2898  # gazelle:map_kind go_library go_library //tools/go:def.bzl
  2899  
  2900  go_library(
  2901      name = "go_default_library",
  2902      srcs = ["multiple_mappings.go"],
  2903      importpath = "example.com/mapkind/enabled/multiple_mappings",
  2904      visibility = ["//visibility:private"],
  2905  )
  2906  
  2907  go_binary(
  2908      name = "multiple_mappings",
  2909      embed = [":go_default_library"],
  2910      visibility = ["//visibility:public"],
  2911  )
  2912  `,
  2913  		},
  2914  		{
  2915  			Path: "depend_on_mapped_kind/BUILD.bazel",
  2916  			Content: `
  2917  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  2918  
  2919  go_library(
  2920      name = "go_default_library",
  2921      srcs = ["lib.go"],
  2922      importpath = "example.com/mapkind/depend_on_mapped_kind",
  2923      visibility = ["//visibility:public"],
  2924      deps = [
  2925          "//disabled:go_default_library",
  2926          "//enabled:go_default_library",
  2927          "//enabled/existing_rules/mapped:go_default_library",
  2928          "//enabled/existing_rules/unmapped:go_default_library",
  2929          "//enabled/overridden:go_default_library",
  2930      ],
  2931  )
  2932  `,
  2933  		},
  2934  	})
  2935  }
  2936  
  2937  // TestMinimalModuleCompatibilityAliases checks that importpath_aliases
  2938  // are emitted for go_libraries when needed. This can't easily be checked
  2939  // in language/go because the generator tests don't support running at
  2940  // the repository root or with additional flags, both of which are required.
  2941  func TestMinimalModuleCompatibilityAliases(t *testing.T) {
  2942  	files := []testtools.FileSpec{
  2943  		{
  2944  			Path:    "go.mod",
  2945  			Content: "module example.com/foo/v2",
  2946  		}, {
  2947  			Path:    "foo.go",
  2948  			Content: "package foo",
  2949  		}, {
  2950  			Path:    "bar/bar.go",
  2951  			Content: "package bar",
  2952  		},
  2953  	}
  2954  	dir, cleanup := testtools.CreateFiles(t, files)
  2955  	defer cleanup()
  2956  
  2957  	args := []string{"update", "-repo_root", dir, "-go_prefix", "example.com/foo/v2", "-go_repository_mode", "-go_repository_module_mode"}
  2958  	if err := runGazelle(dir, args); err != nil {
  2959  		t.Fatal(err)
  2960  	}
  2961  
  2962  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  2963  		{
  2964  			Path: "BUILD.bazel",
  2965  			Content: `
  2966  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  2967  
  2968  go_library(
  2969      name = "foo",
  2970      srcs = ["foo.go"],
  2971      importpath = "example.com/foo/v2",
  2972      importpath_aliases = ["example.com/foo"],
  2973      visibility = ["//visibility:public"],
  2974  )
  2975  `,
  2976  		}, {
  2977  			Path: "bar/BUILD.bazel",
  2978  			Content: `
  2979  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  2980  
  2981  go_library(
  2982      name = "bar",
  2983      srcs = ["bar.go"],
  2984      importpath = "example.com/foo/v2/bar",
  2985      importpath_aliases = ["example.com/foo/bar"],
  2986      visibility = ["//visibility:public"],
  2987  )
  2988  `,
  2989  		},
  2990  	})
  2991  }
  2992  
  2993  // TestGoImportVisibility checks that submodules implicitly declared with
  2994  // go_repository rules in the repo config file (WORKSPACE) have visibility
  2995  // for rules generated in internal directories where appropriate.
  2996  // Verifies #619.
  2997  func TestGoImportVisibility(t *testing.T) {
  2998  	files := []testtools.FileSpec{
  2999  		{
  3000  			Path: "WORKSPACE",
  3001  			Content: `
  3002  go_repository(
  3003  		name = "com_example_m_logging",
  3004      importpath = "example.com/m/logging",
  3005  )
  3006  `,
  3007  		}, {
  3008  			Path:    "internal/version/version.go",
  3009  			Content: "package version",
  3010  		}, {
  3011  			Path:    "internal/version/version_test.go",
  3012  			Content: "package version",
  3013  		},
  3014  	}
  3015  	dir, cleanup := testtools.CreateFiles(t, files)
  3016  	defer cleanup()
  3017  
  3018  	args := []string{"update", "-go_prefix", "example.com/m"}
  3019  	if err := runGazelle(dir, args); err != nil {
  3020  		t.Fatal(err)
  3021  	}
  3022  
  3023  	testtools.CheckFiles(t, dir, []testtools.FileSpec{{
  3024  		Path: "internal/version/BUILD.bazel",
  3025  		Content: `
  3026  load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
  3027  
  3028  go_library(
  3029      name = "version",
  3030      srcs = ["version.go"],
  3031      importpath = "example.com/m/internal/version",
  3032      visibility = [
  3033          "//:__subpackages__",
  3034          "@com_example_m_logging//:__subpackages__",
  3035      ],
  3036  )
  3037  
  3038  go_test(
  3039      name = "version_test",
  3040      srcs = ["version_test.go"],
  3041      embed = [":version"],
  3042  )
  3043  `,
  3044  	}})
  3045  }
  3046  
  3047  // TestGoInternalVisibility_TopLevel checks that modules that are
  3048  // named internal/ expand visibility to repos that have a sibling
  3049  // importpath.
  3050  //
  3051  // Verifies #960
  3052  func TestGoInternalVisibility_TopLevel(t *testing.T) {
  3053  	files := []testtools.FileSpec{
  3054  		{
  3055  			Path:    "WORKSPACE",
  3056  			Content: `go_repository(name="org_modernc_ccgo", importpath="modernc.org/ccgo")`,
  3057  		}, {
  3058  			Path:    "BUILD.bazel",
  3059  			Content: `# gazelle:prefix modernc.org/internal`,
  3060  		}, {
  3061  			Path:    "internal.go",
  3062  			Content: "package internal",
  3063  		}, {
  3064  			Path:    "buffer/buffer.go",
  3065  			Content: "package buffer",
  3066  		},
  3067  	}
  3068  	dir, cleanup := testtools.CreateFiles(t, files)
  3069  	defer cleanup()
  3070  
  3071  	args := []string{"update"}
  3072  	if err := runGazelle(dir, args); err != nil {
  3073  		t.Fatal(err)
  3074  	}
  3075  
  3076  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  3077  		{
  3078  			Path: "BUILD.bazel",
  3079  			Content: `
  3080  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  3081  
  3082  # gazelle:prefix modernc.org/internal
  3083  
  3084  go_library(
  3085      name = "internal",
  3086      srcs = ["internal.go"],
  3087      importpath = "modernc.org/internal",
  3088      visibility = [
  3089          "//:__subpackages__",
  3090          "@org_modernc_ccgo//:__subpackages__",
  3091      ],
  3092  )
  3093  `,
  3094  		},
  3095  		{
  3096  			Path: "buffer/BUILD.bazel",
  3097  			Content: `
  3098  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  3099  
  3100  go_library(
  3101      name = "buffer",
  3102      srcs = ["buffer.go"],
  3103      importpath = "modernc.org/internal/buffer",
  3104      visibility = [
  3105          "//:__subpackages__",
  3106          "@org_modernc_ccgo//:__subpackages__",
  3107      ],
  3108  )
  3109  `,
  3110  		},
  3111  	})
  3112  }
  3113  
  3114  func TestImportCollision(t *testing.T) {
  3115  	files := []testtools.FileSpec{
  3116  		{
  3117  			Path: "WORKSPACE",
  3118  		},
  3119  		{
  3120  			Path: "go.mod",
  3121  			Content: `
  3122  module example.com/importcases
  3123  
  3124  go 1.13
  3125  
  3126  require (
  3127  	github.com/Selvatico/go-mocket v1.0.7
  3128  	github.com/selvatico/go-mocket v1.0.7
  3129  )
  3130  `,
  3131  		},
  3132  		{
  3133  			Path: "go.sum",
  3134  			Content: `
  3135  github.com/Selvatico/go-mocket v1.0.7/go.mod h1:4gO2v+uQmsL+jzQgLANy3tyEFzaEzHlymVbZ3GP2Oes=
  3136  github.com/selvatico/go-mocket v1.0.7/go.mod h1:7bSWzuNieCdUlanCVu3w0ppS0LvDtPAZmKBIlhoTcp8=
  3137  `,
  3138  		},
  3139  	}
  3140  	dir, cleanup := testtools.CreateFiles(t, files)
  3141  	defer cleanup()
  3142  
  3143  	args := []string{"update-repos", "--from_file=go.mod"}
  3144  	errMsg := "imports github.com/Selvatico/go-mocket and github.com/selvatico/go-mocket resolve to the same repository rule name com_github_selvatico_go_mocket"
  3145  	if err := runGazelle(dir, args); err == nil {
  3146  		t.Fatal("expected error, got nil")
  3147  	} else if err.Error() != errMsg {
  3148  		t.Error(fmt.Sprintf("want %s, got %s", errMsg, err.Error()))
  3149  	}
  3150  }
  3151  
  3152  func TestImportCollisionWithReplace(t *testing.T) {
  3153  	files := []testtools.FileSpec{
  3154  		{
  3155  			Path:    "WORKSPACE",
  3156  			Content: "# gazelle:repo bazel_gazelle",
  3157  		},
  3158  		{
  3159  			Path: "go.mod",
  3160  			Content: `
  3161  module github.com/linzhp/go_examples/importcases
  3162  
  3163  go 1.13
  3164  
  3165  require (
  3166  	github.com/Selvatico/go-mocket v1.0.7
  3167  	github.com/selvatico/go-mocket v0.0.0-00010101000000-000000000000
  3168  )
  3169  
  3170  replace github.com/selvatico/go-mocket => github.com/Selvatico/go-mocket v1.0.7
  3171  `,
  3172  		},
  3173  		{
  3174  			Path: "go.sum",
  3175  			Content: `
  3176  github.com/Selvatico/go-mocket v1.0.7/go.mod h1:4gO2v+uQmsL+jzQgLANy3tyEFzaEzHlymVbZ3GP2Oes=
  3177  `,
  3178  		},
  3179  	}
  3180  	dir, cleanup := testtools.CreateFiles(t, files)
  3181  	defer cleanup()
  3182  
  3183  	args := []string{"update-repos", "--from_file=go.mod"}
  3184  	if err := runGazelle(dir, args); err != nil {
  3185  		t.Fatal(err)
  3186  	}
  3187  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  3188  		{
  3189  			Path: "WORKSPACE",
  3190  			Content: `
  3191  load("@bazel_gazelle//:deps.bzl", "go_repository")
  3192  
  3193  # gazelle:repo bazel_gazelle
  3194  
  3195  go_repository(
  3196      name = "com_github_selvatico_go_mocket",
  3197      importpath = "github.com/selvatico/go-mocket",
  3198      replace = "github.com/Selvatico/go-mocket",
  3199      sum = "h1:sXuFMnMfVL9b/Os8rGXPgbOFbr4HJm8aHsulD/uMTUk=",
  3200      version = "v1.0.7",
  3201  )
  3202  `,
  3203  		},
  3204  	})
  3205  }
  3206  
  3207  // TestUpdateReposWithGlobalBuildTags is a regresion test for issue #711.
  3208  // It also ensures that existings build_tags get merged with requested build_tags.
  3209  func TestUpdateReposWithGlobalBuildTags(t *testing.T) {
  3210  	files := []testtools.FileSpec{
  3211  		{
  3212  			Path: "WORKSPACE",
  3213  			Content: `
  3214  load("@bazel_gazelle//:deps.bzl", "go_repository")
  3215  
  3216  # gazelle:repo bazel_gazelle
  3217  
  3218  go_repository(
  3219      name = "com_github_selvatico_go_mocket",
  3220      build_tags = [
  3221          "bar",
  3222      ],
  3223      importpath = "github.com/selvatico/go-mocket",
  3224      replace = "github.com/Selvatico/go-mocket",
  3225      sum = "h1:sXuFMnMfVL9b/Os8rGXPgbOFbr4HJm8aHsulD/uMTUk=",
  3226      version = "v1.0.7",
  3227  )
  3228  `,
  3229  		},
  3230  		{
  3231  			Path: "go.mod",
  3232  			Content: `
  3233  module github.com/linzhp/go_examples/importcases
  3234  
  3235  go 1.13
  3236  
  3237  require (
  3238  	github.com/Selvatico/go-mocket v1.0.7
  3239  	github.com/selvatico/go-mocket v0.0.0-00010101000000-000000000000
  3240  )
  3241  
  3242  replace github.com/selvatico/go-mocket => github.com/Selvatico/go-mocket v1.0.7
  3243  `,
  3244  		},
  3245  		{
  3246  			Path: "go.sum",
  3247  			Content: `
  3248  github.com/Selvatico/go-mocket v1.0.7/go.mod h1:4gO2v+uQmsL+jzQgLANy3tyEFzaEzHlymVbZ3GP2Oes=
  3249  `,
  3250  		},
  3251  	}
  3252  	dir, cleanup := testtools.CreateFiles(t, files)
  3253  	defer cleanup()
  3254  
  3255  	args := []string{"update-repos", "--from_file=go.mod", "--build_tags=bar,foo"}
  3256  	if err := runGazelle(dir, args); err != nil {
  3257  		t.Fatal(err)
  3258  	}
  3259  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  3260  		{
  3261  			Path: "WORKSPACE",
  3262  			Content: `
  3263  load("@bazel_gazelle//:deps.bzl", "go_repository")
  3264  
  3265  # gazelle:repo bazel_gazelle
  3266  
  3267  go_repository(
  3268      name = "com_github_selvatico_go_mocket",
  3269      build_tags = [
  3270          "bar",
  3271          "foo",
  3272      ],
  3273      importpath = "github.com/selvatico/go-mocket",
  3274      replace = "github.com/Selvatico/go-mocket",
  3275      sum = "h1:sXuFMnMfVL9b/Os8rGXPgbOFbr4HJm8aHsulD/uMTUk=",
  3276      version = "v1.0.7",
  3277  )
  3278  `,
  3279  		},
  3280  	})
  3281  }
  3282  
  3283  func TestMatchProtoLibrary(t *testing.T) {
  3284  	files := []testtools.FileSpec{
  3285  		{
  3286  			Path: "WORKSPACE",
  3287  		},
  3288  		{
  3289  			Path: "proto/BUILD.bazel",
  3290  			Content: `
  3291  load("@rules_proto//proto:defs.bzl", "proto_library")
  3292  # gazelle:prefix example.com/foo
  3293  
  3294  proto_library(
  3295  	name = "existing_proto",
  3296  	srcs = ["foo.proto"],
  3297  )
  3298  `,
  3299  		},
  3300  		{
  3301  			Path:    "proto/foo.proto",
  3302  			Content: `syntax = "proto3";`,
  3303  		},
  3304  	}
  3305  	dir, cleanup := testtools.CreateFiles(t, files)
  3306  	defer cleanup()
  3307  
  3308  	args := []string{"update"}
  3309  	if err := runGazelle(dir, args); err != nil {
  3310  		t.Fatal(err)
  3311  	}
  3312  
  3313  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  3314  		{
  3315  			Path: "proto/BUILD.bazel",
  3316  			Content: `
  3317  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  3318  load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
  3319  load("@rules_proto//proto:defs.bzl", "proto_library")
  3320  # gazelle:prefix example.com/foo
  3321  
  3322  proto_library(
  3323      name = "existing_proto",
  3324      srcs = ["foo.proto"],
  3325      visibility = ["//visibility:public"],
  3326  )
  3327  
  3328  go_proto_library(
  3329      name = "foo_go_proto",
  3330      importpath = "example.com/foo",
  3331      proto = ":existing_proto",
  3332      visibility = ["//visibility:public"],
  3333  )
  3334  
  3335  go_library(
  3336      name = "foo",
  3337      embed = [":foo_go_proto"],
  3338      importpath = "example.com/foo",
  3339      visibility = ["//visibility:public"],
  3340  )`,
  3341  		},
  3342  	})
  3343  }
  3344  
  3345  func TestConfigLang(t *testing.T) {
  3346  	// Gazelle is run with "-lang=proto".
  3347  	files := []testtools.FileSpec{
  3348  		{Path: "WORKSPACE"},
  3349  
  3350  		// Verify that Gazelle does not create a BUILD file.
  3351  		{Path: "foo/foo.go", Content: "package foo"},
  3352  
  3353  		// Verify that Gazelle only creates the proto rule.
  3354  		{Path: "pb/pb.go", Content: "package pb"},
  3355  		{Path: "pb/pb.proto", Content: `syntax = "proto3";`},
  3356  
  3357  		// Verify that Gazelle does create a BUILD file, because of the override.
  3358  		{Path: "bar/BUILD.bazel", Content: "# gazelle:lang"},
  3359  		{Path: "bar/bar.go", Content: "package bar"},
  3360  		{Path: "baz/BUILD.bazel", Content: "# gazelle:lang go,proto"},
  3361  		{Path: "baz/baz.go", Content: "package baz"},
  3362  
  3363  		// Verify that Gazelle does not index go_library rules in // or //baz/protos.
  3364  		// In those directories, lang is set to proto by flag and directive, respectively.
  3365  		// Confirm it does index and resolve a rule in a directory where go is activated.
  3366  		{Path: "invisible1.go", Content: "package invisible1"},
  3367  		{Path: "BUILD.bazel", Content: `
  3368  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  3369  
  3370  # gazelle:prefix root
  3371  
  3372  go_library(
  3373      name = "go_default_library",
  3374      srcs = ["invisible1.go"],
  3375      importpath = "root",
  3376      visibility = ["//visibility:public"],
  3377  )
  3378  `},
  3379  		{Path: "baz/protos/invisible2.go", Content: "package invisible2"},
  3380  		{Path: "baz/protos/BUILD.bazel", Content: `
  3381  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  3382  
  3383  # gazelle:lang proto
  3384  # gazelle:prefix github.com/rule_indexing/invisible2
  3385  
  3386  go_library(
  3387      name = "go_default_library",
  3388      srcs = ["invisible2.go"],
  3389      importpath = "github.com/rule_indexing/invisible2",
  3390      visibility = ["//visibility:public"],
  3391  )
  3392  `},
  3393  		{Path: "visible/visible.go", Content: "package visible"},
  3394  		{Path: "visible/BUILD.bazel", Content: `
  3395  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  3396  
  3397  # gazelle:lang go,proto
  3398  # gazelle:prefix github.com/rule_indexing/visible
  3399  
  3400  go_library(
  3401      name = "go_default_library",
  3402      srcs = ["visible.go"],
  3403      importpath = "github.com/rule_indexing/visible",
  3404      visibility = ["//visibility:public"],
  3405  )
  3406  `},
  3407  		{Path: "baz/test_no_index/test_no_index.go", Content: `
  3408  package test_no_index
  3409  
  3410  import (
  3411  	_ "github.com/rule_indexing/invisible1"
  3412  	_ "github.com/rule_indexing/invisible2"
  3413  	_ "github.com/rule_indexing/visible"
  3414  )
  3415  `},
  3416  	}
  3417  
  3418  	dir, cleanup := testtools.CreateFiles(t, files)
  3419  	defer cleanup()
  3420  
  3421  	args := []string{"-lang", "proto"}
  3422  	if err := runGazelle(dir, args); err != nil {
  3423  		t.Fatal(err)
  3424  	}
  3425  
  3426  	testtools.CheckFiles(t, dir, []testtools.FileSpec{{
  3427  		Path:     filepath.Join("foo", "BUILD.bazel"),
  3428  		NotExist: true,
  3429  	}, {
  3430  		Path: filepath.Join("pb", "BUILD.bazel"),
  3431  		Content: `
  3432  load("@rules_proto//proto:defs.bzl", "proto_library")
  3433  
  3434  proto_library(
  3435      name = "pb_proto",
  3436      srcs = ["pb.proto"],
  3437      visibility = ["//visibility:public"],
  3438  )`,
  3439  	},
  3440  		{
  3441  			Path: filepath.Join("bar", "BUILD.bazel"),
  3442  			Content: `
  3443  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  3444  
  3445  # gazelle:lang
  3446  
  3447  go_library(
  3448      name = "go_default_library",
  3449      srcs = ["bar.go"],
  3450      importpath = "root/bar",
  3451      visibility = ["//visibility:public"],
  3452  )`,
  3453  		},
  3454  		{
  3455  			Path: filepath.Join("baz", "BUILD.bazel"),
  3456  			Content: `
  3457  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  3458  
  3459  # gazelle:lang go,proto
  3460  
  3461  go_library(
  3462      name = "go_default_library",
  3463      srcs = ["baz.go"],
  3464      importpath = "root/baz",
  3465      visibility = ["//visibility:public"],
  3466  )`,
  3467  		},
  3468  
  3469  		{Path: "baz/test_no_index/BUILD.bazel", Content: `
  3470  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  3471  
  3472  go_library(
  3473      name = "go_default_library",
  3474      srcs = ["test_no_index.go"],
  3475      importpath = "root/baz/test_no_index",
  3476      visibility = ["//visibility:public"],
  3477      deps = [
  3478          "//visible:go_default_library",
  3479          "@com_github_rule_indexing_invisible1//:go_default_library",
  3480          "@com_github_rule_indexing_invisible2//:go_default_library",
  3481      ],
  3482  )
  3483  `},
  3484  	})
  3485  }
  3486  
  3487  func TestUpdateRepos_LangFilter(t *testing.T) {
  3488  	dir, cleanup := testtools.CreateFiles(t, []testtools.FileSpec{
  3489  		{Path: "WORKSPACE"},
  3490  	})
  3491  	defer cleanup()
  3492  
  3493  	args := []string{"update-repos", "-lang=proto", "github.com/sirupsen/logrus@v1.3.0"}
  3494  	err := runGazelle(dir, args)
  3495  	if err == nil {
  3496  		t.Fatal("expected an error, got none")
  3497  	}
  3498  	if !strings.Contains(err.Error(), "no languages can update repositories") {
  3499  		t.Fatalf("unexpected error: %+v", err)
  3500  	}
  3501  }
  3502  
  3503  func TestGoGenerateProto(t *testing.T) {
  3504  	files := []testtools.FileSpec{
  3505  		{
  3506  			Path: "WORKSPACE",
  3507  		},
  3508  		{
  3509  			Path: "proto/BUILD.bazel",
  3510  			Content: `# gazelle:go_generate_proto false
  3511  # gazelle:prefix example.com/proto
  3512  `,
  3513  		},
  3514  		{
  3515  			Path:    "proto/foo.proto",
  3516  			Content: `syntax = "proto3";`,
  3517  		},
  3518  		{
  3519  			Path:    "proto/foo.pb.go",
  3520  			Content: "package proto",
  3521  		},
  3522  	}
  3523  	dir, cleanup := testtools.CreateFiles(t, files)
  3524  	defer cleanup()
  3525  
  3526  	args := []string{"update"}
  3527  	if err := runGazelle(dir, args); err != nil {
  3528  		t.Fatal(err)
  3529  	}
  3530  
  3531  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  3532  		{
  3533  			Path: "proto/BUILD.bazel",
  3534  			Content: `load("@rules_proto//proto:defs.bzl", "proto_library")
  3535  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  3536  
  3537  # gazelle:go_generate_proto false
  3538  # gazelle:prefix example.com/proto
  3539  
  3540  proto_library(
  3541      name = "foo_proto",
  3542      srcs = ["foo.proto"],
  3543      visibility = ["//visibility:public"],
  3544  )
  3545  
  3546  go_library(
  3547      name = "proto",
  3548      srcs = ["foo.pb.go"],
  3549      importpath = "example.com/proto",
  3550      visibility = ["//visibility:public"],
  3551  )`,
  3552  		},
  3553  	})
  3554  }
  3555  
  3556  func TestGoMainLibraryRemoved(t *testing.T) {
  3557  	files := []testtools.FileSpec{
  3558  		{
  3559  			Path: "WORKSPACE",
  3560  		},
  3561  		{
  3562  			Path: "BUILD.bazel",
  3563  			Content: `
  3564  # gazelle:prefix example.com
  3565  # gazelle:go_naming_convention import
  3566  `,
  3567  		},
  3568  		{
  3569  			Path: "cmd/foo/BUILD.bazel",
  3570  			Content: `load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
  3571  
  3572  go_library(
  3573  		name = "foo_lib",
  3574  		srcs = ["foo.go"],
  3575  		importpath = "example.com/cmd/foo",
  3576  		visibility = ["//visibility:private"],
  3577  )
  3578  
  3579  go_binary(
  3580  		name = "foo",
  3581  		embed = [":foo_lib"],
  3582  		visibility = ["//visibility:public"],
  3583  )
  3584  `,
  3585  		},
  3586  	}
  3587  	dir, cleanup := testtools.CreateFiles(t, files)
  3588  	defer cleanup()
  3589  
  3590  	args := []string{"update"}
  3591  	if err := runGazelle(dir, args); err != nil {
  3592  		t.Fatal(err)
  3593  	}
  3594  
  3595  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  3596  		{
  3597  			Path:    "cmd/foo/BUILD.bazel",
  3598  			Content: "",
  3599  		},
  3600  	})
  3601  }
  3602  
  3603  func TestUpdateReposOldBoilerplateNewRepo(t *testing.T) {
  3604  	files := []testtools.FileSpec{
  3605  		{
  3606  			Path: "WORKSPACE",
  3607  			Content: `
  3608  load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
  3609  
  3610  http_archive(
  3611      name = "io_bazel_rules_go",
  3612      sha256 = "2697f6bc7c529ee5e6a2d9799870b9ec9eaeb3ee7d70ed50b87a2c2c97e13d9e",
  3613      urls = [
  3614          "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.23.8/rules_go-v0.23.8.tar.gz",
  3615          "https://github.com/bazelbuild/rules_go/releases/download/v0.23.8/rules_go-v0.23.8.tar.gz",
  3616      ],
  3617  )
  3618  
  3619  http_archive(
  3620      name = "bazel_gazelle",
  3621      sha256 = "cdb02a887a7187ea4d5a27452311a75ed8637379a1287d8eeb952138ea485f7d",
  3622      urls = [
  3623          "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.21.1/bazel-gazelle-v0.21.1.tar.gz",
  3624          "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.21.1/bazel-gazelle-v0.21.1.tar.gz",
  3625      ],
  3626  )
  3627  
  3628  load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains")
  3629  
  3630  go_rules_dependencies()
  3631  
  3632  go_register_toolchains()
  3633  
  3634  load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
  3635  
  3636  gazelle_dependencies()
  3637  `,
  3638  		},
  3639  	}
  3640  
  3641  	dir, cleanup := testtools.CreateFiles(t, files)
  3642  	defer cleanup()
  3643  
  3644  	args := []string{"update-repos", "golang.org/x/mod@v0.3.0"}
  3645  	if err := runGazelle(dir, args); err != nil {
  3646  		t.Fatal(err)
  3647  	}
  3648  
  3649  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  3650  		{
  3651  			Path: "WORKSPACE",
  3652  			Content: `
  3653  load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
  3654  
  3655  http_archive(
  3656      name = "io_bazel_rules_go",
  3657      sha256 = "2697f6bc7c529ee5e6a2d9799870b9ec9eaeb3ee7d70ed50b87a2c2c97e13d9e",
  3658      urls = [
  3659          "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.23.8/rules_go-v0.23.8.tar.gz",
  3660          "https://github.com/bazelbuild/rules_go/releases/download/v0.23.8/rules_go-v0.23.8.tar.gz",
  3661      ],
  3662  )
  3663  
  3664  http_archive(
  3665      name = "bazel_gazelle",
  3666      sha256 = "cdb02a887a7187ea4d5a27452311a75ed8637379a1287d8eeb952138ea485f7d",
  3667      urls = [
  3668          "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.21.1/bazel-gazelle-v0.21.1.tar.gz",
  3669          "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.21.1/bazel-gazelle-v0.21.1.tar.gz",
  3670      ],
  3671  )
  3672  
  3673  load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
  3674  
  3675  go_rules_dependencies()
  3676  
  3677  go_register_toolchains()
  3678  
  3679  load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
  3680  
  3681  go_repository(
  3682      name = "org_golang_x_mod",
  3683      importpath = "golang.org/x/mod",
  3684      sum = "h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=",
  3685      version = "v0.3.0",
  3686  )
  3687  
  3688  gazelle_dependencies()
  3689  `,
  3690  		},
  3691  	})
  3692  }
  3693  
  3694  func TestUpdateReposOldBoilerplateNewMacro(t *testing.T) {
  3695  	files := []testtools.FileSpec{
  3696  		{
  3697  			Path: "WORKSPACE",
  3698  			Content: `
  3699  load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
  3700  
  3701  http_archive(
  3702      name = "io_bazel_rules_go",
  3703      sha256 = "2697f6bc7c529ee5e6a2d9799870b9ec9eaeb3ee7d70ed50b87a2c2c97e13d9e",
  3704      urls = [
  3705          "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.23.8/rules_go-v0.23.8.tar.gz",
  3706          "https://github.com/bazelbuild/rules_go/releases/download/v0.23.8/rules_go-v0.23.8.tar.gz",
  3707      ],
  3708  )
  3709  
  3710  http_archive(
  3711      name = "bazel_gazelle",
  3712      sha256 = "cdb02a887a7187ea4d5a27452311a75ed8637379a1287d8eeb952138ea485f7d",
  3713      urls = [
  3714          "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.21.1/bazel-gazelle-v0.21.1.tar.gz",
  3715          "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.21.1/bazel-gazelle-v0.21.1.tar.gz",
  3716      ],
  3717  )
  3718  
  3719  load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains")
  3720  
  3721  go_rules_dependencies()
  3722  
  3723  go_register_toolchains()
  3724  
  3725  load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
  3726  
  3727  gazelle_dependencies()
  3728  `,
  3729  		},
  3730  	}
  3731  
  3732  	dir, cleanup := testtools.CreateFiles(t, files)
  3733  	defer cleanup()
  3734  
  3735  	args := []string{"update-repos", "-to_macro=deps.bzl%deps", "golang.org/x/mod@v0.3.0"}
  3736  	if err := runGazelle(dir, args); err != nil {
  3737  		t.Fatal(err)
  3738  	}
  3739  
  3740  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  3741  		{
  3742  			Path: "WORKSPACE",
  3743  			Content: `
  3744  load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
  3745  
  3746  http_archive(
  3747      name = "io_bazel_rules_go",
  3748      sha256 = "2697f6bc7c529ee5e6a2d9799870b9ec9eaeb3ee7d70ed50b87a2c2c97e13d9e",
  3749      urls = [
  3750          "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.23.8/rules_go-v0.23.8.tar.gz",
  3751          "https://github.com/bazelbuild/rules_go/releases/download/v0.23.8/rules_go-v0.23.8.tar.gz",
  3752      ],
  3753  )
  3754  
  3755  http_archive(
  3756      name = "bazel_gazelle",
  3757      sha256 = "cdb02a887a7187ea4d5a27452311a75ed8637379a1287d8eeb952138ea485f7d",
  3758      urls = [
  3759          "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.21.1/bazel-gazelle-v0.21.1.tar.gz",
  3760          "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.21.1/bazel-gazelle-v0.21.1.tar.gz",
  3761      ],
  3762  )
  3763  
  3764  load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
  3765  
  3766  go_rules_dependencies()
  3767  
  3768  go_register_toolchains()
  3769  
  3770  load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
  3771  load("//:deps.bzl", "deps")
  3772  
  3773  # gazelle:repository_macro deps.bzl%deps
  3774  deps()
  3775  
  3776  gazelle_dependencies()
  3777  `,
  3778  		},
  3779  	})
  3780  }
  3781  
  3782  func TestUpdateReposNewBoilerplateNewRepo(t *testing.T) {
  3783  	files := []testtools.FileSpec{
  3784  		{
  3785  			Path: "WORKSPACE",
  3786  			Content: `
  3787  load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
  3788  
  3789  http_archive(
  3790      name = "io_bazel_rules_go",
  3791      sha256 = "2697f6bc7c529ee5e6a2d9799870b9ec9eaeb3ee7d70ed50b87a2c2c97e13d9e",
  3792      urls = [
  3793          "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.23.8/rules_go-v0.23.8.tar.gz",
  3794          "https://github.com/bazelbuild/rules_go/releases/download/v0.23.8/rules_go-v0.23.8.tar.gz",
  3795      ],
  3796  )
  3797  
  3798  http_archive(
  3799      name = "bazel_gazelle",
  3800      sha256 = "cdb02a887a7187ea4d5a27452311a75ed8637379a1287d8eeb952138ea485f7d",
  3801      urls = [
  3802          "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.21.1/bazel-gazelle-v0.21.1.tar.gz",
  3803          "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.21.1/bazel-gazelle-v0.21.1.tar.gz",
  3804      ],
  3805  )
  3806  
  3807  load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
  3808  load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
  3809  
  3810  go_rules_dependencies()
  3811  
  3812  go_register_toolchains()
  3813  
  3814  gazelle_dependencies()
  3815  `,
  3816  		},
  3817  	}
  3818  
  3819  	dir, cleanup := testtools.CreateFiles(t, files)
  3820  	defer cleanup()
  3821  
  3822  	args := []string{"update-repos", "golang.org/x/mod@v0.3.0"}
  3823  	if err := runGazelle(dir, args); err != nil {
  3824  		t.Fatal(err)
  3825  	}
  3826  
  3827  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  3828  		{
  3829  			Path: "WORKSPACE",
  3830  			Content: `
  3831  load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
  3832  
  3833  http_archive(
  3834      name = "io_bazel_rules_go",
  3835      sha256 = "2697f6bc7c529ee5e6a2d9799870b9ec9eaeb3ee7d70ed50b87a2c2c97e13d9e",
  3836      urls = [
  3837          "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.23.8/rules_go-v0.23.8.tar.gz",
  3838          "https://github.com/bazelbuild/rules_go/releases/download/v0.23.8/rules_go-v0.23.8.tar.gz",
  3839      ],
  3840  )
  3841  
  3842  http_archive(
  3843      name = "bazel_gazelle",
  3844      sha256 = "cdb02a887a7187ea4d5a27452311a75ed8637379a1287d8eeb952138ea485f7d",
  3845      urls = [
  3846          "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.21.1/bazel-gazelle-v0.21.1.tar.gz",
  3847          "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.21.1/bazel-gazelle-v0.21.1.tar.gz",
  3848      ],
  3849  )
  3850  
  3851  load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
  3852  load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
  3853  
  3854  go_repository(
  3855      name = "org_golang_x_mod",
  3856      importpath = "golang.org/x/mod",
  3857      sum = "h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=",
  3858      version = "v0.3.0",
  3859  )
  3860  
  3861  go_rules_dependencies()
  3862  
  3863  go_register_toolchains()
  3864  
  3865  gazelle_dependencies()
  3866  `,
  3867  		},
  3868  	})
  3869  }
  3870  
  3871  func TestUpdateReposNewBoilerplateNewMacro(t *testing.T) {
  3872  	files := []testtools.FileSpec{
  3873  		{
  3874  			Path: "WORKSPACE",
  3875  			Content: `
  3876  load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
  3877  
  3878  http_archive(
  3879      name = "io_bazel_rules_go",
  3880      sha256 = "2697f6bc7c529ee5e6a2d9799870b9ec9eaeb3ee7d70ed50b87a2c2c97e13d9e",
  3881      urls = [
  3882          "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.23.8/rules_go-v0.23.8.tar.gz",
  3883          "https://github.com/bazelbuild/rules_go/releases/download/v0.23.8/rules_go-v0.23.8.tar.gz",
  3884      ],
  3885  )
  3886  
  3887  http_archive(
  3888      name = "bazel_gazelle",
  3889      sha256 = "cdb02a887a7187ea4d5a27452311a75ed8637379a1287d8eeb952138ea485f7d",
  3890      urls = [
  3891          "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.21.1/bazel-gazelle-v0.21.1.tar.gz",
  3892          "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.21.1/bazel-gazelle-v0.21.1.tar.gz",
  3893      ],
  3894  )
  3895  
  3896  load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
  3897  load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
  3898  
  3899  go_rules_dependencies()
  3900  
  3901  go_register_toolchains()
  3902  
  3903  gazelle_dependencies()
  3904  `,
  3905  		},
  3906  	}
  3907  
  3908  	dir, cleanup := testtools.CreateFiles(t, files)
  3909  	defer cleanup()
  3910  
  3911  	args := []string{"update-repos", "-to_macro=deps.bzl%deps", "golang.org/x/mod@v0.3.0"}
  3912  	if err := runGazelle(dir, args); err != nil {
  3913  		t.Fatal(err)
  3914  	}
  3915  
  3916  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  3917  		{
  3918  			Path: "WORKSPACE",
  3919  			Content: `
  3920  load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
  3921  
  3922  http_archive(
  3923      name = "io_bazel_rules_go",
  3924      sha256 = "2697f6bc7c529ee5e6a2d9799870b9ec9eaeb3ee7d70ed50b87a2c2c97e13d9e",
  3925      urls = [
  3926          "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.23.8/rules_go-v0.23.8.tar.gz",
  3927          "https://github.com/bazelbuild/rules_go/releases/download/v0.23.8/rules_go-v0.23.8.tar.gz",
  3928      ],
  3929  )
  3930  
  3931  http_archive(
  3932      name = "bazel_gazelle",
  3933      sha256 = "cdb02a887a7187ea4d5a27452311a75ed8637379a1287d8eeb952138ea485f7d",
  3934      urls = [
  3935          "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.21.1/bazel-gazelle-v0.21.1.tar.gz",
  3936          "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.21.1/bazel-gazelle-v0.21.1.tar.gz",
  3937      ],
  3938  )
  3939  
  3940  load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
  3941  load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
  3942  load("//:deps.bzl", "deps")
  3943  
  3944  # gazelle:repository_macro deps.bzl%deps
  3945  deps()
  3946  
  3947  go_rules_dependencies()
  3948  
  3949  go_register_toolchains()
  3950  
  3951  gazelle_dependencies()
  3952  `,
  3953  		},
  3954  	})
  3955  }
  3956  
  3957  func TestExternalOnly(t *testing.T) {
  3958  	files := []testtools.FileSpec{
  3959  		{
  3960  			Path: "WORKSPACE",
  3961  		},
  3962  		{
  3963  			Path: "foo/foo.go",
  3964  			Content: `package foo
  3965  import _ "golang.org/x/baz"
  3966  `,
  3967  		},
  3968  		{
  3969  			Path: "foo/foo_test.go",
  3970  			Content: `package foo_test
  3971  import _ "golang.org/x/baz"
  3972  import _ "example.com/foo"
  3973  `,
  3974  		},
  3975  		{
  3976  			Path: "foo/BUILD.bazel",
  3977  			Content: `# gazelle:prefix example.com/foo
  3978  load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
  3979  
  3980  go_library(
  3981      name = "foo",
  3982      srcs = ["foo.go"],
  3983      importpath = "example.com/foo",
  3984      visibility = ["//visibility:public"],
  3985      deps = ["@org_golang_x_baz//:go_default_library"],
  3986  )
  3987  
  3988  go_test(
  3989      name = "foo_test",
  3990      srcs = ["foo_test.go"],
  3991      embed = [":foo"],
  3992  )`,
  3993  		},
  3994  	}
  3995  	dir, cleanup := testtools.CreateFiles(t, files)
  3996  	defer cleanup()
  3997  
  3998  	var args []string
  3999  	if err := runGazelle(dir, args); err != nil {
  4000  		t.Fatal(err)
  4001  	}
  4002  
  4003  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  4004  		{
  4005  			Path: "foo/BUILD.bazel",
  4006  			Content: `# gazelle:prefix example.com/foo
  4007  load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
  4008  
  4009  go_library(
  4010      name = "foo",
  4011      srcs = ["foo.go"],
  4012      importpath = "example.com/foo",
  4013      visibility = ["//visibility:public"],
  4014      deps = ["@org_golang_x_baz//:go_default_library"],
  4015  )
  4016  
  4017  go_test(
  4018      name = "foo_test",
  4019      srcs = ["foo_test.go"],
  4020      deps = [
  4021          ":foo",
  4022          "@org_golang_x_baz//:go_default_library",
  4023      ],
  4024  )`,
  4025  		},
  4026  	})
  4027  }
  4028  
  4029  func TestFindRulesGoVersionWithWORKSPACE(t *testing.T) {
  4030  	files := []testtools.FileSpec{
  4031  		{
  4032  			Path: "WORKSPACE",
  4033  			Content: `
  4034  load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
  4035  
  4036  http_archive(
  4037      name = "io_bazel_rules_go",
  4038      sha256 = "7b9bbe3ea1fccb46dcfa6c3f3e29ba7ec740d8733370e21cdc8937467b4a4349",
  4039      urls = [
  4040          "https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/rules_go/releases/download/v0.22.4/rules_go-v0.22.4.tar.gz",
  4041          "https://github.com/bazelbuild/rules_go/releases/download/v0.22.4/rules_go-v0.22.4.tar.gz",
  4042      ],
  4043  )
  4044  `,
  4045  		},
  4046  		{
  4047  			Path: "foo_illumos.go",
  4048  			Content: `
  4049  // illumos not supported in rules_go v0.22.4
  4050  package foo
  4051  `,
  4052  		},
  4053  		{
  4054  			Path: "BUILD.bazel",
  4055  			Content: `
  4056  # gazelle:prefix example.com/foo
  4057  `,
  4058  		},
  4059  	}
  4060  
  4061  	dir, cleanup := testtools.CreateFiles(t, files)
  4062  	defer cleanup()
  4063  
  4064  	if err := runGazelle(dir, []string{"update"}); err != nil {
  4065  		t.Fatal(err)
  4066  	}
  4067  
  4068  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  4069  		{
  4070  			Path: "BUILD.bazel",
  4071  			Content: `
  4072  # gazelle:prefix example.com/foo
  4073  `,
  4074  		},
  4075  	})
  4076  }
  4077  
  4078  func TestPlatformSpecificEmbedsrcs(t *testing.T) {
  4079  	files := []testtools.FileSpec{
  4080  		{
  4081  			Path: "WORKSPACE",
  4082  		},
  4083  		{
  4084  			Path: "BUILD.bazel",
  4085  			Content: `
  4086  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  4087  
  4088  # gazelle:prefix example.com/foo
  4089  
  4090  go_library(
  4091      name = "foo",
  4092      embedsrcs = ["deleted.txt"],
  4093      importpath = "example.com/foo",
  4094      srcs = ["foo.go"],
  4095  )
  4096  `,
  4097  		},
  4098  		{
  4099  			Path: "foo.go",
  4100  			Content: `
  4101  // +build windows
  4102  
  4103  package foo
  4104  
  4105  import _ "embed"
  4106  
  4107  //go:embed windows.txt
  4108  var s string
  4109  `,
  4110  		},
  4111  		{
  4112  			Path: "windows.txt",
  4113  		},
  4114  	}
  4115  
  4116  	dir, cleanup := testtools.CreateFiles(t, files)
  4117  	defer cleanup()
  4118  
  4119  	if err := runGazelle(dir, []string{"update"}); err != nil {
  4120  		t.Fatal(err)
  4121  	}
  4122  
  4123  	testtools.CheckFiles(t, dir, []testtools.FileSpec{
  4124  		{
  4125  			Path: "BUILD.bazel",
  4126  			Content: `
  4127  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  4128  
  4129  # gazelle:prefix example.com/foo
  4130  
  4131  go_library(
  4132      name = "foo",
  4133      srcs = ["foo.go"],
  4134      embedsrcs = select({
  4135          "@io_bazel_rules_go//go/platform:windows": [
  4136              "windows.txt",
  4137          ],
  4138          "//conditions:default": [],
  4139      }),
  4140      importpath = "example.com/foo",
  4141      visibility = ["//visibility:public"],
  4142  )
  4143  `,
  4144  		},
  4145  	})
  4146  }
  4147  
  4148  // Checks that go:embed directives with spaces and quotes are parsed correctly.
  4149  // This probably belongs in //language/go:go_test, but we need file names with
  4150  // spaces, and Bazel doesn't allow those in runfiles, which that test depends
  4151  // on.
  4152  func TestQuotedEmbedsrcs(t *testing.T) {
  4153  	files := []testtools.FileSpec{
  4154  		{
  4155  			Path: "WORKSPACE",
  4156  		},
  4157  		{
  4158  			Path:    "BUILD.bazel",
  4159  			Content: "# gazelle:prefix example.com/foo",
  4160  		},
  4161  		{
  4162  			Path: "foo.go",
  4163  			Content: strings.Join([]string{
  4164  				"package foo",
  4165  				"import \"embed\"",
  4166  				"//go:embed q1.txt q2.txt \"q 3.txt\" `q 4.txt`",
  4167  				"var fs embed.FS",
  4168  			}, "\n"),
  4169  		},
  4170  		{
  4171  			Path: "q1.txt",
  4172  		},
  4173  		{
  4174  			Path: "q2.txt",
  4175  		},
  4176  		{
  4177  			Path: "q 3.txt",
  4178  		},
  4179  		{
  4180  			Path: "q 4.txt",
  4181  		},
  4182  	}
  4183  	dir, cleanup := testtools.CreateFiles(t, files)
  4184  	defer cleanup()
  4185  
  4186  	if err := runGazelle(dir, []string{"update"}); err != nil {
  4187  		t.Fatal(err)
  4188  	}
  4189  
  4190  	testtools.CheckFiles(t, dir, []testtools.FileSpec{{
  4191  		Path: "BUILD.bazel",
  4192  		Content: `
  4193  load("@io_bazel_rules_go//go:def.bzl", "go_library")
  4194  
  4195  # gazelle:prefix example.com/foo
  4196  
  4197  go_library(
  4198      name = "foo",
  4199      srcs = ["foo.go"],
  4200      embedsrcs = [
  4201          "q 3.txt",
  4202          "q 4.txt",
  4203          "q1.txt",
  4204          "q2.txt",
  4205      ],
  4206      importpath = "example.com/foo",
  4207      visibility = ["//visibility:public"],
  4208  )
  4209  `,
  4210  	}})
  4211  }
  4212  
  4213  // TestUpdateReposDoesNotModifyGoSum verifies that commands executed by
  4214  // update-repos do not modify go.sum, particularly 'go mod download' when
  4215  // a sum is missing. Verifies #990.
  4216  //
  4217  // This could also be tested in language/go/update_import_test.go, but that
  4218  // test relies on stubs for speed, and it's important to run the real
  4219  // go command here.
  4220  func TestUpdateReposDoesNotModifyGoSum(t *testing.T) {
  4221  	if testing.Short() {
  4222  		// Test may download small files over network.
  4223  		t.Skip()
  4224  	}
  4225  	goSumFile := testtools.FileSpec{
  4226  		// go.sum only contains the sum for the mod file, not the content.
  4227  		// This is common for transitive dependencies not needed by the main module.
  4228  		Path:    "go.sum",
  4229  		Content: "golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=\n",
  4230  	}
  4231  	files := []testtools.FileSpec{
  4232  		{
  4233  			Path:    "WORKSPACE",
  4234  			Content: "# gazelle:repo bazel_gazelle",
  4235  		},
  4236  		{
  4237  			Path: "go.mod",
  4238  			Content: `
  4239  module test
  4240  
  4241  go 1.16
  4242  
  4243  require golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
  4244  `,
  4245  		},
  4246  		goSumFile,
  4247  	}
  4248  	dir, cleanup := testtools.CreateFiles(t, files)
  4249  	defer cleanup()
  4250  
  4251  	if err := runGazelle(dir, []string{"update-repos", "-from_file=go.mod"}); err != nil {
  4252  		t.Fatal(err)
  4253  	}
  4254  	testtools.CheckFiles(t, dir, []testtools.FileSpec{goSumFile})
  4255  }