github.com/afking/bazel-gazelle@v0.0.0-20180301150245-c02bc0f529e8/cmd/move_labels/move_labels_test.go (about) 1 /* Copyright 2018 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 "io/ioutil" 20 "os" 21 "path/filepath" 22 "strings" 23 "testing" 24 ) 25 26 type fileSpec struct { 27 path, content string 28 } 29 30 func TestMoveLabels(t *testing.T) { 31 for _, tc := range []struct { 32 desc, from, to string 33 files, want []fileSpec 34 }{ 35 { 36 desc: "move", 37 from: "old", 38 to: "new", 39 files: []fileSpec{{ 40 path: "new/a/BUILD", 41 content: ` 42 load("//old:def.bzl", "x_binary") 43 44 x_binary( 45 name = "a", 46 deps = [ 47 ":a_lib", 48 "//old/b", 49 "//old/b:b_lib", 50 "@c//old:c_lib", 51 ], 52 locs = [ 53 "abc $(location //old/b) $(locations //old/b:b_lib) xyz", 54 ], 55 ) 56 `, 57 }}, 58 want: []fileSpec{{ 59 path: "new/a/BUILD", 60 content: ` 61 load("//new:def.bzl", "x_binary") 62 63 x_binary( 64 name = "a", 65 deps = [ 66 ":a_lib", 67 "//new/b", 68 "//new/b:b_lib", 69 "@c//old:c_lib", 70 ], 71 locs = [ 72 "abc $(location //new/b) $(locations //new/b:b_lib) xyz", 73 ], 74 ) 75 `, 76 }}, 77 }, { 78 desc: "vendor", 79 from: "", 80 to: "vendor/github.com/bazelbuild/buildtools", 81 files: []fileSpec{ 82 { 83 path: "vendor/github.com/bazelbuild/buildtools/BUILD.bazel", 84 content: ` 85 load("@io_bazel_rules_go//go:def.bzl", "go_prefix") 86 87 go_prefix("github.com/bazelbuild/buildtools") 88 89 config_setting( 90 name = "windows", 91 values = {"cpu": "x64_windows"}, 92 ) 93 94 test_suite( 95 name = "tests", 96 tests = [ 97 "//api_proto:api.gen.pb.go_checkshtest", 98 "//build:go_default_test", 99 "//build:parse.y.go_checkshtest", 100 "//build_proto:build.gen.pb.go_checkshtest", 101 "//deps_proto:deps.gen.pb.go_checkshtest", 102 "//edit:go_default_test", 103 "//extra_actions_base_proto:extra_actions_base.gen.pb.go_checkshtest", 104 "//lang:tables.gen.go_checkshtest", 105 "//tables:go_default_test", 106 "//wspace:go_default_test", 107 ], 108 ) 109 `, 110 }, { 111 path: `vendor/github.com/bazelbuild/buildtools/edit/BUILD.bazel`, 112 content: ` 113 load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 114 115 go_library( 116 name = "go_default_library", 117 srcs = [ 118 "buildozer.go", 119 "edit.go", 120 "fix.go", 121 "types.go", 122 ], 123 visibility = ["//visibility:public"], 124 deps = [ 125 "//api_proto:go_default_library", 126 "//build:go_default_library", 127 "//build_proto:go_default_library", 128 "//file:go_default_library", 129 "//lang:go_default_library", 130 "//tables:go_default_library", 131 "//wspace:go_default_library", 132 "@com_github_golang_protobuf//proto:go_default_library", 133 ], 134 ) 135 136 go_test( 137 name = "go_default_test", 138 srcs = ["edit_test.go"], 139 library = ":go_default_library", 140 deps = ["//build:go_default_library"], 141 ) 142 `, 143 }, 144 }, 145 want: []fileSpec{ 146 { 147 path: "vendor/github.com/bazelbuild/buildtools/BUILD.bazel", 148 content: ` 149 load("@io_bazel_rules_go//go:def.bzl", "go_prefix") 150 151 go_prefix("github.com/bazelbuild/buildtools") 152 153 config_setting( 154 name = "windows", 155 values = {"cpu": "x64_windows"}, 156 ) 157 158 test_suite( 159 name = "tests", 160 tests = [ 161 "//vendor/github.com/bazelbuild/buildtools/api_proto:api.gen.pb.go_checkshtest", 162 "//vendor/github.com/bazelbuild/buildtools/build:go_default_test", 163 "//vendor/github.com/bazelbuild/buildtools/build:parse.y.go_checkshtest", 164 "//vendor/github.com/bazelbuild/buildtools/build_proto:build.gen.pb.go_checkshtest", 165 "//vendor/github.com/bazelbuild/buildtools/deps_proto:deps.gen.pb.go_checkshtest", 166 "//vendor/github.com/bazelbuild/buildtools/edit:go_default_test", 167 "//vendor/github.com/bazelbuild/buildtools/extra_actions_base_proto:extra_actions_base.gen.pb.go_checkshtest", 168 "//vendor/github.com/bazelbuild/buildtools/lang:tables.gen.go_checkshtest", 169 "//vendor/github.com/bazelbuild/buildtools/tables:go_default_test", 170 "//vendor/github.com/bazelbuild/buildtools/wspace:go_default_test", 171 ], 172 ) 173 `, 174 }, { 175 path: `vendor/github.com/bazelbuild/buildtools/edit/BUILD.bazel`, 176 content: ` 177 load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 178 179 go_library( 180 name = "go_default_library", 181 srcs = [ 182 "buildozer.go", 183 "edit.go", 184 "fix.go", 185 "types.go", 186 ], 187 visibility = ["//visibility:public"], 188 deps = [ 189 "//vendor/github.com/bazelbuild/buildtools/api_proto:go_default_library", 190 "//vendor/github.com/bazelbuild/buildtools/build:go_default_library", 191 "//vendor/github.com/bazelbuild/buildtools/build_proto:go_default_library", 192 "//vendor/github.com/bazelbuild/buildtools/file:go_default_library", 193 "//vendor/github.com/bazelbuild/buildtools/lang:go_default_library", 194 "//vendor/github.com/bazelbuild/buildtools/tables:go_default_library", 195 "//vendor/github.com/bazelbuild/buildtools/wspace:go_default_library", 196 "@com_github_golang_protobuf//proto:go_default_library", 197 ], 198 ) 199 200 go_test( 201 name = "go_default_test", 202 srcs = ["edit_test.go"], 203 library = ":go_default_library", 204 deps = ["//vendor/github.com/bazelbuild/buildtools/build:go_default_library"], 205 ) 206 `, 207 }, 208 }, 209 }, 210 } { 211 t.Run(tc.desc, func(t *testing.T) { 212 dir, err := createFiles(tc.files) 213 if err != nil { 214 t.Fatal(err) 215 } 216 defer os.RemoveAll(dir) 217 218 args := []string{"-repo_root", dir, "-from", tc.from, "-to", filepath.Join(dir, filepath.FromSlash(tc.to))} 219 if err := run(args); err != nil { 220 t.Fatal(err) 221 } 222 223 checkFiles(t, dir, tc.want) 224 }) 225 } 226 } 227 228 func createFiles(files []fileSpec) (string, error) { 229 dir, err := ioutil.TempDir(os.Getenv("TEST_TEMPDIR"), "integration_test") 230 if err != nil { 231 return "", err 232 } 233 234 for _, f := range files { 235 path := filepath.Join(dir, filepath.FromSlash(f.path)) 236 if strings.HasSuffix(f.path, "/") { 237 if err := os.MkdirAll(path, 0700); err != nil { 238 os.RemoveAll(dir) 239 return "", err 240 } 241 continue 242 } 243 if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { 244 os.RemoveAll(dir) 245 return "", err 246 } 247 if err := ioutil.WriteFile(path, []byte(f.content), 0600); err != nil { 248 os.RemoveAll(dir) 249 return "", err 250 } 251 } 252 return dir, nil 253 } 254 255 func checkFiles(t *testing.T, dir string, files []fileSpec) { 256 for _, f := range files { 257 path := filepath.Join(dir, f.path) 258 if strings.HasSuffix(f.path, "/") { 259 if st, err := os.Stat(path); err != nil { 260 t.Errorf("could not stat %s: %v", f.path, err) 261 } else if !st.IsDir() { 262 t.Errorf("not a directory: %s", f.path) 263 } 264 } else { 265 want := f.content 266 if len(want) > 0 && want[0] == '\n' { 267 // Strip leading newline, added for readability. 268 want = want[1:] 269 } 270 gotBytes, err := ioutil.ReadFile(filepath.Join(dir, f.path)) 271 if err != nil { 272 t.Errorf("could not read %s: %v", f.path, err) 273 continue 274 } 275 got := string(gotBytes) 276 if got != want { 277 t.Errorf("%s: got %s ; want %s", f.path, got, f.content) 278 } 279 } 280 } 281 }