github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/cmd/generate_repo_config/generate_repo_config_test.go (about) 1 /* Copyright 2019 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 main 17 18 import ( 19 "path/filepath" 20 "reflect" 21 "testing" 22 23 "github.com/bazelbuild/bazel-gazelle/testtools" 24 ) 25 26 func TestGenerateRepoConfig(t *testing.T) { 27 tests := []struct { 28 name string 29 giveWorkspace string 30 giveReposContent string 31 wantContent string 32 }{ 33 { 34 name: "no duplicates", 35 giveWorkspace: ` 36 # gazelle:repo test 37 go_repository( 38 name = "com_github_pkg_errors", 39 build_file_generation = "off", 40 commit = "645ef00459ed84a119197bfb8d8205042c6df63d", 41 importpath = "github.com/pkg/errors", 42 ) 43 # gazelle:repository_macro repositories.bzl%go_repositories 44 `, 45 giveReposContent: ` 46 load("@bazel_gazelle//:deps.bzl", "go_repository") 47 def go_repositories(): 48 # gazelle:repo test2 49 go_repository( 50 name = "org_golang_x_net", 51 importpath = "golang.org/x/net", 52 tag = "1.2", 53 ) 54 # keep 55 go_repository( 56 name = "org_golang_x_sys", 57 importpath = "golang.org/x/sys", 58 remote = "https://github.com/golang/sys", 59 ) 60 `, 61 wantContent: ` 62 # Code generated by generate_repo_config.go; DO NOT EDIT. 63 64 go_repository( 65 name = "com_github_pkg_errors", 66 importpath = "github.com/pkg/errors", 67 ) 68 69 go_repository( 70 name = "org_golang_x_net", 71 importpath = "golang.org/x/net", 72 ) 73 74 go_repository( 75 name = "org_golang_x_sys", 76 importpath = "golang.org/x/sys", 77 ) 78 `, 79 }, { 80 name: "with duplicates", 81 giveWorkspace: ` 82 go_repository( 83 name = "com_github_pkg_errors", 84 build_file_generation = "off", 85 commit = "645ef00459ed84a119197bfb8d8205042c6df63d", 86 importpath = "github.com/pkg/errors", 87 ) 88 # gazelle:repository_macro repositories.bzl%go_repositories 89 # gazelle:repository go_repository name=org_golang_x_net importpath=golang.org2/x/net 90 `, 91 giveReposContent: ` 92 load("@bazel_gazelle//:deps.bzl", "go_repository") 93 def go_repositories(): 94 go_repository( 95 name = "com_github_pkg_errors", 96 importpath = "github.com2/pkg/errors", 97 ) 98 go_repository( 99 name = "org_golang_x_net", 100 importpath = "golang.org/x/net", 101 tag = "1.2", 102 ) 103 `, 104 wantContent: ` 105 # Code generated by generate_repo_config.go; DO NOT EDIT. 106 107 go_repository( 108 name = "com_github_pkg_errors", 109 importpath = "github.com2/pkg/errors", 110 ) 111 112 go_repository( 113 name = "org_golang_x_net", 114 importpath = "golang.org2/x/net", 115 ) 116 `, 117 }, 118 } 119 120 for _, tt := range tests { 121 t.Run(tt.name, func(t *testing.T) { 122 files := []testtools.FileSpec{ 123 { 124 Path: "WORKSPACE", 125 Content: tt.giveWorkspace, 126 }, { 127 Path: "repositories.bzl", 128 Content: tt.giveReposContent, 129 }, 130 } 131 132 dir, cleanup := testtools.CreateFiles(t, files) 133 defer cleanup() 134 135 tmp := t.TempDir() 136 137 got, err := generateRepoConfig(filepath.Join(tmp, "WORKSPACE"), filepath.Join(dir, "WORKSPACE")) 138 if err != nil { 139 t.Fatal(err) 140 } 141 142 want := []string{"WORKSPACE", "repositories.bzl"} 143 if !reflect.DeepEqual(got, want) { 144 t.Errorf("got %#v; want %#v", got, want) 145 } 146 147 testtools.CheckFiles(t, tmp, []testtools.FileSpec{ 148 { 149 Path: "WORKSPACE", 150 Content: tt.wantContent, 151 }, 152 }) 153 }) 154 } 155 }