github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/language/go/update_import_test.go (about)

     1  /* Copyright 2017 The Bazel Authors. All rights reserved.
     2  
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7     http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package golang
    17  
    18  import (
    19  	"fmt"
    20  	"path/filepath"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/bazelbuild/bazel-gazelle/config"
    25  	"github.com/bazelbuild/bazel-gazelle/language"
    26  	"github.com/bazelbuild/bazel-gazelle/repo"
    27  	"github.com/bazelbuild/bazel-gazelle/rule"
    28  	"github.com/bazelbuild/bazel-gazelle/testtools"
    29  )
    30  
    31  func TestImports(t *testing.T) {
    32  	for _, tc := range []struct {
    33  		desc, want        string
    34  		wantErr           string
    35  		stubGoModDownload func(string, []string) ([]byte, error)
    36  		stubGoListModules func(string) ([]byte, error)
    37  		files             []testtools.FileSpec
    38  	}{
    39  		{
    40  			desc: "modules",
    41  			files: []testtools.FileSpec{
    42  				{
    43  					Path: "go.mod",
    44  					Content: `
    45  module github.com/bazelbuild/bazel-gazelle
    46  
    47  require (
    48  	github.com/BurntSushi/toml v0.3.1 // indirect
    49  	github.com/bazelbuild/buildtools v0.0.0-20190202002759-027686e28d67
    50  	github.com/davecgh/go-spew v1.1.1 // indirect
    51  	github.com/fork/go-toml v0.0.0-20190116191733-b6c0e53d7304
    52  	github.com/fsnotify/fsnotify v1.4.7
    53  	github.com/kr/pretty v0.1.0 // indirect
    54  	github.com/pelletier/go-toml v1.0.1
    55  	github.com/pmezard/go-difflib v1.0.0
    56  	golang.org/x/sys v0.0.0-20190122071731-054c452bb702 // indirect
    57  	golang.org/x/tools v0.0.0-20190122202912-9c309ee22fab
    58  	gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
    59  	gopkg.in/yaml.v2 v2.2.2 // indirect
    60  )
    61  
    62  replace (
    63  	github.com/pelletier/go-toml => github.com/fork/go-toml v0.0.0-20190425002759-70bc0436ed16
    64  )
    65  `,
    66  				}, {
    67  					// Note: the sum for x/tools has been deleted to force a call
    68  					// to goModDownload.
    69  					Path: "go.sum",
    70  					Content: `
    71  github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
    72  github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
    73  github.com/bazelbuild/buildtools v0.0.0-20190202002759-027686e28d67 h1:zS8p6ZRbNVa7QfK3tpoIRDqGzCA2J0uJffaMTWoneac=
    74  github.com/bazelbuild/buildtools v0.0.0-20190202002759-027686e28d67/go.mod h1:5JP0TXzWDHXv8qvxRC4InIazwdyDseBDbzESUMKk1yU=
    75  github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
    76  github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
    77  github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
    78  github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
    79  github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
    80  github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
    81  github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
    82  github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
    83  github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
    84  github.com/fork/go-toml v0.0.0-20190116191733-b6c0e53d7304 h1:5+8j8FTpnFV4nEImW/ofkzEt8VoOiLXxdYIDsB73T38=
    85  github.com/fork/go-toml v0.0.0-20190116191733-b6c0e53d7304/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
    86  github.com/fork/go-toml v0.0.0-20190425002759-70bc0436ed16 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
    87  github.com/fork/go-toml v0.0.0-20190425002759-70bc0436ed16/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
    88  github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
    89  github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
    90  golang.org/x/sys v0.0.0-20190122071731-054c452bb702 h1:Lk4tbZFnlyPgV+sLgTw5yGfzrlOn9kx4vSombi2FFlY=
    91  golang.org/x/sys v0.0.0-20190122071731-054c452bb702/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
    92  gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
    93  gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
    94  gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
    95  gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
    96  gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
    97  gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
    98  `,
    99  				},
   100  			},
   101  			want: `
   102  go_repository(
   103      name = "com_github_bazelbuild_buildtools",
   104      importpath = "github.com/bazelbuild/buildtools",
   105      sum = "h1:zS8p6ZRbNVa7QfK3tpoIRDqGzCA2J0uJffaMTWoneac=",
   106      version = "v0.0.0-20190202002759-027686e28d67",
   107  )
   108  
   109  go_repository(
   110      name = "com_github_burntsushi_toml",
   111      importpath = "github.com/BurntSushi/toml",
   112      sum = "h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=",
   113      version = "v0.3.1",
   114  )
   115  
   116  go_repository(
   117      name = "com_github_davecgh_go_spew",
   118      importpath = "github.com/davecgh/go-spew",
   119      sum = "h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=",
   120      version = "v1.1.1",
   121  )
   122  
   123  go_repository(
   124      name = "com_github_fork_go_toml",
   125      importpath = "github.com/fork/go-toml",
   126      sum = "h1:5+8j8FTpnFV4nEImW/ofkzEt8VoOiLXxdYIDsB73T38=",
   127      version = "v0.0.0-20190116191733-b6c0e53d7304",
   128  )
   129  
   130  go_repository(
   131      name = "com_github_pelletier_go_toml",
   132      importpath = "github.com/pelletier/go-toml",
   133      replace = "github.com/fork/go-toml",
   134      sum = "h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=",
   135      version = "v0.0.0-20190425002759-70bc0436ed16",
   136  )
   137  
   138  go_repository(
   139      name = "in_gopkg_check_v1",
   140      importpath = "gopkg.in/check.v1",
   141      sum = "h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=",
   142      version = "v1.0.0-20180628173108-788fd7840127",
   143  )
   144  
   145  go_repository(
   146      name = "in_gopkg_yaml_v2",
   147      importpath = "gopkg.in/yaml.v2",
   148      sum = "h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=",
   149      version = "v2.2.2",
   150  )
   151  
   152  go_repository(
   153      name = "org_golang_x_tools",
   154      importpath = "golang.org/x/tools",
   155      sum = "h1:FkAkwuYWQw+IArrnmhGlisKHQF4MsZ2Nu/fX4ttW55o=",
   156      version = "v0.0.0-20190122202912-9c309ee22fab",
   157  )
   158  `,
   159  			wantErr:           "",
   160  			stubGoModDownload: nil,
   161  		},
   162  		{
   163  			desc: "modules-download-error",
   164  			files: []testtools.FileSpec{
   165  				{
   166  					Path: "go.mod",
   167  					Content: `
   168  module github.com/bazelbuild/bazel-gazelle
   169  
   170  require (
   171  	definitely.doesnotexist/ever v0.1.0
   172  )
   173  `,
   174  				}, {
   175  					Path: "go.sum",
   176  					Content: `
   177  definitely.doesnotexist/ever v0.1.0/go.mod h1:HI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuJ=
   178  `,
   179  				},
   180  			},
   181  			want:    "",
   182  			wantErr: "finding module sums: error from go mod download: failed to download\nError downloading definitely.doesnotexist/ever: Did not exist",
   183  			stubGoModDownload: func(dir string, args []string) ([]byte, error) {
   184  				return []byte(`{
   185  "Path": "definitely.doesnotexist/ever",
   186  "Version": "0.1.0",
   187  "Error": "Did not exist"
   188  }`), fmt.Errorf("failed to download")
   189  			},
   190  		},
   191  		{
   192  			desc: "modules-download-bad-json",
   193  			files: []testtools.FileSpec{
   194  				{
   195  					Path: "go.mod",
   196  					Content: `
   197  module github.com/bazelbuild/bazel-gazelle
   198  
   199  require (
   200  	definitely.doesnotexist/ever v0.1.0
   201  )
   202  `,
   203  				}, {
   204  					Path: "go.sum",
   205  					Content: `
   206  definitely.doesnotexist/ever v0.1.0/go.mod h1:HI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuJ=
   207  `,
   208  				},
   209  			},
   210  			want:    "",
   211  			wantErr: "finding module sums: error from go mod download: failed to download\nError parsing module for more error information: invalid character 'o' in literal null (expecting 'u')",
   212  			stubGoModDownload: func(dir string, args []string) ([]byte, error) {
   213  				return []byte(`{
   214  "Path": "definitely.doesnotexist/ever",
   215  "Version": "0.1.0",
   216  "Error": {
   217     "Err": not valid json
   218  }
   219  }`), fmt.Errorf("failed to download")
   220  			},
   221  		},
   222  		{
   223  			desc: "list-modules-error",
   224  			files: []testtools.FileSpec{
   225  				{
   226  					Path: "go.mod",
   227  					Content: `
   228  module github.com/bazelbuild/bazel-gazelle
   229  
   230  require (
   231  	definitely.doesnotexist/ever v0.1.0
   232  )
   233  `,
   234  				}, {
   235  					Path: "go.sum",
   236  					Content: `
   237  definitely.doesnotexist/ever v0.1.0/go.mod h1:HI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuJ=
   238  `,
   239  				},
   240  			},
   241  			want:    "",
   242  			wantErr: "error from go list: failed to download\nError listing definitely.doesnotexist/ever: Did not exist",
   243  			stubGoListModules: func(dir string) ([]byte, error) {
   244  				return []byte(`{
   245  "Path": "definitely.doesnotexist/ever",
   246  "Version": "0.1.0",
   247  "Error": {"Err": "Did not exist"}
   248  }`), fmt.Errorf("failed to download")
   249  			},
   250  		},
   251  		{
   252  			desc: "list-modules-bad-json",
   253  			files: []testtools.FileSpec{
   254  				{
   255  					Path: "go.mod",
   256  					Content: `
   257  module github.com/bazelbuild/bazel-gazelle
   258  
   259  require (
   260  	definitely.doesnotexist/ever v0.1.0
   261  )
   262  `,
   263  				}, {
   264  					Path: "go.sum",
   265  					Content: `
   266  definitely.doesnotexist/ever v0.1.0/go.mod h1:HI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuJ=
   267  `,
   268  				},
   269  			},
   270  			want:    "",
   271  			wantErr: "error from go list: failed to download\nError parsing module for more error information: invalid character 'n' after object key",
   272  			stubGoListModules: func(dir string) ([]byte, error) {
   273  				return []byte(`{
   274      "Path": "definitely.doesnotexist/ever",
   275      "Version": "0.1.0",
   276      "Error" not valid json
   277  }`), fmt.Errorf("failed to download")
   278  			},
   279  		},
   280  		{
   281  			desc: "work",
   282  			files: []testtools.FileSpec{
   283  				{
   284  					Path: "go.work",
   285  					Content: `
   286  go 1.18
   287  
   288  use (
   289  	./project1
   290  	./project2
   291  	./project3
   292  )
   293  `,
   294  				},
   295  				{
   296  					Path: "project1/go.mod",
   297  					Content: `
   298  module project1
   299  
   300  go 1.18
   301  
   302  require github.com/vmware/govmomi v0.21.0 // indirect
   303  `,
   304  				},
   305  				{
   306  					Path: "project1/go.sum",
   307  					Content: `
   308  github.com/davecgh/go-xdr v0.0.0-20161123171359-e6a2ba005892/go.mod h1:CTDl0pzVzE5DEzZhPfvhY/9sPFMQIxaJ9VAMs9AagrE=
   309  github.com/google/uuid v0.0.0-20170306145142-6a5e28554805/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
   310  github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
   311  github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
   312  github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
   313  github.com/vmware/govmomi v0.21.0 h1:jc8uMuxpcV2xMAA/cnEDlnsIjvqcMra5Y8onh/U3VuY=
   314  github.com/vmware/govmomi v0.21.0/go.mod h1:zbnFoBQ9GIjs2RVETy8CNEpb+L+Lwkjs3XZUL0B3/m0=
   315  github.com/vmware/vmw-guestinfo v0.0.0-20170707015358-25eff159a728/go.mod h1:x9oS4Wk2s2u4tS29nEaDLdzvuHdB19CvSGJjPgkZJNk=
   316  `,
   317  				},
   318  				{
   319  					Path: "project2/go.mod",
   320  					Content: `
   321  module project2
   322  
   323  go 1.18
   324  
   325  require github.com/vmware/govmomi v0.24.0 // indirect
   326  `,
   327  				},
   328  				{
   329  					Path: "project2/go.sum",
   330  					Content: `
   331  github.com/davecgh/go-xdr v0.0.0-20161123171359-e6a2ba005892/go.mod h1:CTDl0pzVzE5DEzZhPfvhY/9sPFMQIxaJ9VAMs9AagrE=
   332  github.com/google/uuid v0.0.0-20170306145142-6a5e28554805/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
   333  github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
   334  github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
   335  github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
   336  github.com/vmware/govmomi v0.24.0 h1:G7YFF6unMTG3OY25Dh278fsomVTKs46m2ENlEFSbmbs=
   337  github.com/vmware/govmomi v0.24.0/go.mod h1:Y+Wq4lst78L85Ge/F8+ORXIWiKYqaro1vhAulACy9Lc=
   338  github.com/vmware/vmw-guestinfo v0.0.0-20170707015358-25eff159a728/go.mod h1:x9oS4Wk2s2u4tS29nEaDLdzvuHdB19CvSGJjPgkZJNk=
   339  `,
   340  				},
   341  				{
   342  					Path: "project3/go.mod",
   343  					Content: `
   344  module project3
   345  
   346  go 1.18
   347  
   348  require github.com/vmware/govmomi v0.27.0 // indirect
   349  `,
   350  				},
   351  				{
   352  					Path: "project3/go.sum",
   353  					Content: `
   354  github.com/a8m/tree v0.0.0-20210115125333-10a5fd5b637d/go.mod h1:FSdwKX97koS5efgm8WevNf7XS3PqtyFkKDDXrz778cg=
   355  github.com/davecgh/go-xdr v0.0.0-20161123171359-e6a2ba005892/go.mod h1:CTDl0pzVzE5DEzZhPfvhY/9sPFMQIxaJ9VAMs9AagrE=
   356  github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
   357  github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
   358  github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
   359  github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
   360  github.com/vmware/govmomi v0.27.0 h1:KoQ8IsLAa7V78s5d7dgpZA8d039GBM83cVxgAq9uWuw=
   361  github.com/vmware/govmomi v0.27.0/go.mod h1:daTuJEcQosNMXYJOeku0qdBJP9SOLLWB3Mqz8THtv6o=
   362  github.com/vmware/vmw-guestinfo v0.0.0-20170707015358-25eff159a728/go.mod h1:x9oS4Wk2s2u4tS29nEaDLdzvuHdB19CvSGJjPgkZJNk=
   363  `,
   364  				},
   365  			},
   366  			want: `
   367  go_repository(
   368      name = "com_github_a8m_tree",
   369      importpath = "github.com/a8m/tree",
   370      sum = "h1:4E8RufAN3UQ/weB6AnQ4y5miZCO0Yco8ZdGId41WuQs=",
   371      version = "v0.0.0-20210115125333-10a5fd5b637d",
   372  )
   373  
   374  go_repository(
   375      name = "com_github_davecgh_go_xdr",
   376      importpath = "github.com/davecgh/go-xdr",
   377      sum = "h1:qg9VbHo1TlL0KDM0vYvBG9EY0X0Yku5WYIPoFWt8f6o=",
   378      version = "v0.0.0-20161123171359-e6a2ba005892",
   379  )
   380  
   381  go_repository(
   382      name = "com_github_google_uuid",
   383      importpath = "github.com/google/uuid",
   384      sum = "h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=",
   385      version = "v1.2.0",
   386  )
   387  
   388  go_repository(
   389      name = "com_github_kr_pretty",
   390      importpath = "github.com/kr/pretty",
   391      sum = "h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=",
   392      version = "v0.1.0",
   393  )
   394  
   395  go_repository(
   396      name = "com_github_kr_pty",
   397      importpath = "github.com/kr/pty",
   398      sum = "h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw=",
   399      version = "v1.1.1",
   400  )
   401  
   402  go_repository(
   403      name = "com_github_kr_text",
   404      importpath = "github.com/kr/text",
   405      sum = "h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=",
   406      version = "v0.1.0",
   407  )
   408  
   409  go_repository(
   410      name = "com_github_vmware_govmomi",
   411      importpath = "github.com/vmware/govmomi",
   412      sum = "h1:KoQ8IsLAa7V78s5d7dgpZA8d039GBM83cVxgAq9uWuw=",
   413      version = "v0.27.0",
   414  )
   415  
   416  go_repository(
   417      name = "com_github_vmware_vmw_guestinfo",
   418      importpath = "github.com/vmware/vmw-guestinfo",
   419      sum = "h1:sH9mEk+flyDxiUa5BuPiuhDETMbzrt9A20I2wktMvRQ=",
   420      version = "v0.0.0-20170707015358-25eff159a728",
   421  )
   422  `,
   423  			wantErr: "",
   424  			stubGoModDownload: func(s string, i []string) ([]byte, error) {
   425  				return []byte(`
   426  {
   427  	"Path": "github.com/a8m/tree",
   428  	"Version": "v0.0.0-20210115125333-10a5fd5b637d",
   429  	"Info": "/Users/hhalil/go/pkg/mod/cache/download/github.com/a8m/tree/@v/v0.0.0-20210115125333-10a5fd5b637d.info",
   430  	"GoMod": "/Users/hhalil/go/pkg/mod/cache/download/github.com/a8m/tree/@v/v0.0.0-20210115125333-10a5fd5b637d.mod",
   431  	"Zip": "/Users/hhalil/go/pkg/mod/cache/download/github.com/a8m/tree/@v/v0.0.0-20210115125333-10a5fd5b637d.zip",
   432  	"Dir": "/Users/hhalil/go/pkg/mod/github.com/a8m/tree@v0.0.0-20210115125333-10a5fd5b637d",
   433  	"Sum": "h1:4E8RufAN3UQ/weB6AnQ4y5miZCO0Yco8ZdGId41WuQs=",
   434  	"GoModSum": "h1:FSdwKX97koS5efgm8WevNf7XS3PqtyFkKDDXrz778cg="
   435  }
   436  {
   437  	"Path": "github.com/davecgh/go-xdr",
   438  	"Version": "v0.0.0-20161123171359-e6a2ba005892",
   439  	"Info": "/Users/hhalil/go/pkg/mod/cache/download/github.com/davecgh/go-xdr/@v/v0.0.0-20161123171359-e6a2ba005892.info",
   440  	"GoMod": "/Users/hhalil/go/pkg/mod/cache/download/github.com/davecgh/go-xdr/@v/v0.0.0-20161123171359-e6a2ba005892.mod",
   441  	"Zip": "/Users/hhalil/go/pkg/mod/cache/download/github.com/davecgh/go-xdr/@v/v0.0.0-20161123171359-e6a2ba005892.zip",
   442  	"Dir": "/Users/hhalil/go/pkg/mod/github.com/davecgh/go-xdr@v0.0.0-20161123171359-e6a2ba005892",
   443  	"Sum": "h1:qg9VbHo1TlL0KDM0vYvBG9EY0X0Yku5WYIPoFWt8f6o=",
   444  	"GoModSum": "h1:CTDl0pzVzE5DEzZhPfvhY/9sPFMQIxaJ9VAMs9AagrE="
   445  }
   446  {
   447  	"Path": "github.com/google/uuid",
   448  	"Version": "v1.2.0",
   449  	"Info": "/Users/hhalil/go/pkg/mod/cache/download/github.com/google/uuid/@v/v1.2.0.info",
   450  	"GoMod": "/Users/hhalil/go/pkg/mod/cache/download/github.com/google/uuid/@v/v1.2.0.mod",
   451  	"Zip": "/Users/hhalil/go/pkg/mod/cache/download/github.com/google/uuid/@v/v1.2.0.zip",
   452  	"Dir": "/Users/hhalil/go/pkg/mod/github.com/google/uuid@v1.2.0",
   453  	"Sum": "h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=",
   454  	"GoModSum": "h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo="
   455  }
   456  {
   457  	"Path": "github.com/kr/pretty",
   458  	"Version": "v0.1.0",
   459  	"Info": "/Users/hhalil/go/pkg/mod/cache/download/github.com/kr/pretty/@v/v0.1.0.info",
   460  	"GoMod": "/Users/hhalil/go/pkg/mod/cache/download/github.com/kr/pretty/@v/v0.1.0.mod",
   461  	"Zip": "/Users/hhalil/go/pkg/mod/cache/download/github.com/kr/pretty/@v/v0.1.0.zip",
   462  	"Dir": "/Users/hhalil/go/pkg/mod/github.com/kr/pretty@v0.1.0",
   463  	"Sum": "h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=",
   464  	"GoModSum": "h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo="
   465  }
   466  {
   467  	"Path": "github.com/kr/pty",
   468  	"Version": "v1.1.1",
   469  	"Info": "/Users/hhalil/go/pkg/mod/cache/download/github.com/kr/pty/@v/v1.1.1.info",
   470  	"GoMod": "/Users/hhalil/go/pkg/mod/cache/download/github.com/kr/pty/@v/v1.1.1.mod",
   471  	"Zip": "/Users/hhalil/go/pkg/mod/cache/download/github.com/kr/pty/@v/v1.1.1.zip",
   472  	"Dir": "/Users/hhalil/go/pkg/mod/github.com/kr/pty@v1.1.1",
   473  	"Sum": "h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw=",
   474  	"GoModSum": "h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ="
   475  }
   476  {
   477  	"Path": "github.com/kr/text",
   478  	"Version": "v0.1.0",
   479  	"Info": "/Users/hhalil/go/pkg/mod/cache/download/github.com/kr/text/@v/v0.1.0.info",
   480  	"GoMod": "/Users/hhalil/go/pkg/mod/cache/download/github.com/kr/text/@v/v0.1.0.mod",
   481  	"Zip": "/Users/hhalil/go/pkg/mod/cache/download/github.com/kr/text/@v/v0.1.0.zip",
   482  	"Dir": "/Users/hhalil/go/pkg/mod/github.com/kr/text@v0.1.0",
   483  	"Sum": "h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=",
   484  	"GoModSum": "h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI="
   485  }
   486  {
   487  	"Path": "github.com/vmware/govmomi",
   488  	"Version": "v0.27.0",
   489  	"Info": "/Users/hhalil/go/pkg/mod/cache/download/github.com/vmware/govmomi/@v/v0.27.0.info",
   490  	"GoMod": "/Users/hhalil/go/pkg/mod/cache/download/github.com/vmware/govmomi/@v/v0.27.0.mod",
   491  	"Zip": "/Users/hhalil/go/pkg/mod/cache/download/github.com/vmware/govmomi/@v/v0.27.0.zip",
   492  	"Dir": "/Users/hhalil/go/pkg/mod/github.com/vmware/govmomi@v0.27.0",
   493  	"Sum": "h1:KoQ8IsLAa7V78s5d7dgpZA8d039GBM83cVxgAq9uWuw=",
   494  	"GoModSum": "h1:daTuJEcQosNMXYJOeku0qdBJP9SOLLWB3Mqz8THtv6o="
   495  }
   496  {
   497  	"Path": "github.com/vmware/vmw-guestinfo",
   498  	"Version": "v0.0.0-20170707015358-25eff159a728",
   499  	"Info": "/Users/hhalil/go/pkg/mod/cache/download/github.com/vmware/vmw-guestinfo/@v/v0.0.0-20170707015358-25eff159a728.info",
   500  	"GoMod": "/Users/hhalil/go/pkg/mod/cache/download/github.com/vmware/vmw-guestinfo/@v/v0.0.0-20170707015358-25eff159a728.mod",
   501  	"Zip": "/Users/hhalil/go/pkg/mod/cache/download/github.com/vmware/vmw-guestinfo/@v/v0.0.0-20170707015358-25eff159a728.zip",
   502  	"Dir": "/Users/hhalil/go/pkg/mod/github.com/vmware/vmw-guestinfo@v0.0.0-20170707015358-25eff159a728",
   503  	"Sum": "h1:sH9mEk+flyDxiUa5BuPiuhDETMbzrt9A20I2wktMvRQ=",
   504  	"GoModSum": "h1:x9oS4Wk2s2u4tS29nEaDLdzvuHdB19CvSGJjPgkZJNk="
   505  }
   506  `), nil
   507  			},
   508  			stubGoListModules: func(dir string) ([]byte, error) {
   509  				return []byte(`
   510  {
   511          "Path": "project1",
   512          "Main": true,
   513          "Dir": "/Users/hhalil/Documents/VMware/mirrors_github_bazel-gazelle/hakan/project1",
   514          "GoMod": "/Users/hhalil/Documents/VMware/mirrors_github_bazel-gazelle/hakan/project1/go.mod",
   515          "GoVersion": "1.18"
   516  }
   517  {
   518          "Path": "project2",
   519          "Main": true,
   520          "Dir": "/Users/hhalil/Documents/VMware/mirrors_github_bazel-gazelle/hakan/project2",
   521          "GoMod": "/Users/hhalil/Documents/VMware/mirrors_github_bazel-gazelle/hakan/project2/go.mod",
   522          "GoVersion": "1.18"
   523  }
   524  {
   525          "Path": "project3",
   526          "Main": true,
   527          "Dir": "/Users/hhalil/Documents/VMware/mirrors_github_bazel-gazelle/hakan/project3",
   528          "GoMod": "/Users/hhalil/Documents/VMware/mirrors_github_bazel-gazelle/hakan/project3/go.mod",
   529          "GoVersion": "1.18"
   530  }
   531  {
   532          "Path": "github.com/a8m/tree",
   533          "Version": "v0.0.0-20210115125333-10a5fd5b637d",
   534          "Time": "2021-01-15T12:53:33Z",
   535          "Indirect": true,
   536          "GoMod": "/Users/hhalil/go/pkg/mod/cache/download/github.com/a8m/tree/@v/v0.0.0-20210115125333-10a5fd5b637d.mod"
   537  }
   538  {
   539          "Path": "github.com/davecgh/go-xdr",
   540          "Version": "v0.0.0-20161123171359-e6a2ba005892",
   541          "Time": "2016-11-23T17:13:59Z",
   542          "Indirect": true,
   543          "GoMod": "/Users/hhalil/go/pkg/mod/cache/download/github.com/davecgh/go-xdr/@v/v0.0.0-20161123171359-e6a2ba005892.mod"
   544  }
   545  {
   546          "Path": "github.com/google/uuid",
   547          "Version": "v1.2.0",
   548          "Time": "2021-01-22T18:20:15Z",
   549          "Indirect": true,
   550          "GoMod": "/Users/hhalil/go/pkg/mod/cache/download/github.com/google/uuid/@v/v1.2.0.mod"
   551  }
   552  {
   553          "Path": "github.com/kr/pretty",
   554          "Version": "v0.1.0",
   555          "Time": "2018-05-06T08:33:45Z",
   556          "Indirect": true,
   557          "GoMod": "/Users/hhalil/go/pkg/mod/cache/download/github.com/kr/pretty/@v/v0.1.0.mod"
   558  }
   559  {
   560          "Path": "github.com/kr/pty",
   561          "Version": "v1.1.1",
   562          "Time": "2018-01-13T18:08:13Z",
   563          "Indirect": true,
   564          "GoMod": "/Users/hhalil/go/pkg/mod/cache/download/github.com/kr/pty/@v/v1.1.1.mod"
   565  }
   566  {
   567          "Path": "github.com/kr/text",
   568          "Version": "v0.1.0",
   569          "Time": "2018-05-06T08:24:08Z",
   570          "Indirect": true,
   571          "GoMod": "/Users/hhalil/go/pkg/mod/cache/download/github.com/kr/text/@v/v0.1.0.mod"
   572  }
   573  {
   574          "Path": "github.com/vmware/govmomi",
   575          "Version": "v0.27.0",
   576          "Time": "2021-10-14T20:30:09Z",
   577          "Indirect": true,
   578          "Dir": "/Users/hhalil/go/pkg/mod/github.com/vmware/govmomi@v0.27.0",
   579          "GoMod": "/Users/hhalil/go/pkg/mod/cache/download/github.com/vmware/govmomi/@v/v0.27.0.mod",
   580          "GoVersion": "1.14"
   581  }
   582  {
   583          "Path": "github.com/vmware/vmw-guestinfo",
   584          "Version": "v0.0.0-20170707015358-25eff159a728",
   585          "Time": "2017-07-07T01:53:58Z",
   586          "Indirect": true,
   587          "GoMod": "/Users/hhalil/go/pkg/mod/cache/download/github.com/vmware/vmw-guestinfo/@v/v0.0.0-20170707015358-25eff159a728.mod"
   588  }
   589  `), nil
   590  			},
   591  		},
   592  	} {
   593  		t.Run(tc.desc, func(t *testing.T) {
   594  			if tc.stubGoModDownload != nil {
   595  				previousGoModDownload := goModDownload
   596  				goModDownload = tc.stubGoModDownload
   597  				defer func() { goModDownload = previousGoModDownload }()
   598  			}
   599  			if tc.stubGoListModules != nil {
   600  				previousGoListModules := goListModules
   601  				goListModules = tc.stubGoListModules
   602  				defer func() { goListModules = previousGoListModules }()
   603  			}
   604  			dir, cleanup := testtools.CreateFiles(t, tc.files)
   605  			defer cleanup()
   606  
   607  			filename := filepath.Join(dir, tc.files[0].Path)
   608  			c := &config.Config{Exts: map[string]interface{}{}}
   609  			rc, rcCleanup := repo.NewRemoteCache(nil)
   610  			defer func() {
   611  				if err := rcCleanup(); err != nil {
   612  					t.Fatal(err)
   613  				}
   614  			}()
   615  			gl := NewLanguage()
   616  			gl.Configure(c, "", nil)
   617  			importer := gl.(language.RepoImporter)
   618  			result := importer.ImportRepos(language.ImportReposArgs{
   619  				Config: c,
   620  				Path:   filename,
   621  				Cache:  rc,
   622  			})
   623  			if tc.wantErr != "" {
   624  				if result.Error == nil {
   625  					t.Fatalf("Want error %v but got %v", tc.wantErr, result)
   626  				}
   627  				if result.Error.Error() != tc.wantErr {
   628  					t.Fatalf("Want error %v but got %v", tc.wantErr, result.Error)
   629  				}
   630  				return
   631  			} else {
   632  				if result.Error != nil {
   633  					t.Fatal(result.Error)
   634  				}
   635  			}
   636  			f := rule.EmptyFile("test", "")
   637  			for _, r := range result.Gen {
   638  				r.Insert(f)
   639  			}
   640  			got := strings.TrimSpace(string(f.Format()))
   641  			want := strings.TrimSpace(tc.want)
   642  			if got != want {
   643  				t.Errorf("got:\n%s\n\nwant:\n%s\n", got, want)
   644  			}
   645  		})
   646  	}
   647  }