github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/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  	"path/filepath"
    20  	"testing"
    21  
    22  	"github.com/bazelbuild/bazel-gazelle/testtools"
    23  )
    24  
    25  func TestMoveLabels(t *testing.T) {
    26  	for _, tc := range []struct {
    27  		desc, from, to string
    28  		files, want    []testtools.FileSpec
    29  	}{
    30  		{
    31  			desc: "move",
    32  			from: "old",
    33  			to:   "new",
    34  			files: []testtools.FileSpec{{
    35  				Path: "new/a/BUILD",
    36  				Content: `load("//old:def.bzl", "x_binary")
    37  
    38  x_binary(
    39      name = "a",
    40      locs = [
    41          "abc $(location  //old/b) $(locations //old/b:b_lib) xyz",
    42      ],
    43      deps = [
    44          ":a_lib",
    45          "//old/b",
    46          "//old/b:b_lib",
    47          "@c//old:c_lib",
    48      ],
    49  )
    50  `,
    51  			}},
    52  			want: []testtools.FileSpec{{
    53  				Path: "new/a/BUILD",
    54  				Content: `load("//new:def.bzl", "x_binary")
    55  
    56  x_binary(
    57      name = "a",
    58      locs = [
    59          "abc $(location  //new/b) $(locations //new/b:b_lib) xyz",
    60      ],
    61      deps = [
    62          ":a_lib",
    63          "//new/b",
    64          "//new/b:b_lib",
    65          "@c//old:c_lib",
    66      ],
    67  )
    68  `,
    69  			}},
    70  		}, {
    71  			desc: "vendor",
    72  			from: "",
    73  			to:   "vendor/github.com/bazelbuild/buildtools",
    74  			files: []testtools.FileSpec{
    75  				{
    76  					Path: "vendor/github.com/bazelbuild/buildtools/BUILD.bazel",
    77  					Content: `
    78  load("@io_bazel_rules_go//go:def.bzl", "go_prefix")
    79  
    80  go_prefix("github.com/bazelbuild/buildtools")
    81  
    82  config_setting(
    83      name = "windows",
    84      values = {"cpu": "x64_windows"},
    85  )
    86  
    87  test_suite(
    88      name = "tests",
    89      tests = [
    90          "//api_proto:api.gen.pb.go_checkshtest",
    91          "//build:go_default_test",
    92          "//build:parse.y.go_checkshtest",
    93          "//build_proto:build.gen.pb.go_checkshtest",
    94          "//deps_proto:deps.gen.pb.go_checkshtest",
    95          "//edit:go_default_test",
    96          "//extra_actions_base_proto:extra_actions_base.gen.pb.go_checkshtest",
    97          "//lang:tables.gen.go_checkshtest",
    98          "//tables:go_default_test",
    99          "//wspace:go_default_test",
   100      ],
   101  )
   102  `,
   103  				}, {
   104  					Path: `vendor/github.com/bazelbuild/buildtools/edit/BUILD.bazel`,
   105  					Content: `
   106  load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
   107  
   108  go_library(
   109      name = "go_default_library",
   110      srcs = [
   111          "buildozer.go",
   112          "edit.go",
   113          "fix.go",
   114          "types.go",
   115      ],
   116      visibility = ["//visibility:public"],
   117      deps = [
   118          "//api_proto:go_default_library",
   119          "//build:go_default_library",
   120          "//build_proto:go_default_library",
   121          "//file:go_default_library",
   122          "//lang:go_default_library",
   123          "//tables:go_default_library",
   124          "//wspace:go_default_library",
   125          "@com_github_golang_protobuf//proto:go_default_library",
   126      ],
   127  )
   128  
   129  go_test(
   130      name = "go_default_test",
   131      srcs = ["edit_test.go"],
   132      library = ":go_default_library",
   133      deps = ["//build:go_default_library"],
   134  )
   135  `,
   136  				},
   137  			},
   138  			want: []testtools.FileSpec{
   139  				{
   140  					Path: "vendor/github.com/bazelbuild/buildtools/BUILD.bazel",
   141  					Content: `
   142  load("@io_bazel_rules_go//go:def.bzl", "go_prefix")
   143  
   144  go_prefix("github.com/bazelbuild/buildtools")
   145  
   146  config_setting(
   147      name = "windows",
   148      values = {"cpu": "x64_windows"},
   149  )
   150  
   151  test_suite(
   152      name = "tests",
   153      tests = [
   154          "//vendor/github.com/bazelbuild/buildtools/api_proto:api.gen.pb.go_checkshtest",
   155          "//vendor/github.com/bazelbuild/buildtools/build:go_default_test",
   156          "//vendor/github.com/bazelbuild/buildtools/build:parse.y.go_checkshtest",
   157          "//vendor/github.com/bazelbuild/buildtools/build_proto:build.gen.pb.go_checkshtest",
   158          "//vendor/github.com/bazelbuild/buildtools/deps_proto:deps.gen.pb.go_checkshtest",
   159          "//vendor/github.com/bazelbuild/buildtools/edit:go_default_test",
   160          "//vendor/github.com/bazelbuild/buildtools/extra_actions_base_proto:extra_actions_base.gen.pb.go_checkshtest",
   161          "//vendor/github.com/bazelbuild/buildtools/lang:tables.gen.go_checkshtest",
   162          "//vendor/github.com/bazelbuild/buildtools/tables:go_default_test",
   163          "//vendor/github.com/bazelbuild/buildtools/wspace:go_default_test",
   164      ],
   165  )
   166  `,
   167  				}, {
   168  					Path: `vendor/github.com/bazelbuild/buildtools/edit/BUILD.bazel`,
   169  					Content: `
   170  load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
   171  
   172  go_library(
   173      name = "go_default_library",
   174      srcs = [
   175          "buildozer.go",
   176          "edit.go",
   177          "fix.go",
   178          "types.go",
   179      ],
   180      visibility = ["//visibility:public"],
   181      deps = [
   182          "//vendor/github.com/bazelbuild/buildtools/api_proto:go_default_library",
   183          "//vendor/github.com/bazelbuild/buildtools/build:go_default_library",
   184          "//vendor/github.com/bazelbuild/buildtools/build_proto:go_default_library",
   185          "//vendor/github.com/bazelbuild/buildtools/file:go_default_library",
   186          "//vendor/github.com/bazelbuild/buildtools/lang:go_default_library",
   187          "//vendor/github.com/bazelbuild/buildtools/tables:go_default_library",
   188          "//vendor/github.com/bazelbuild/buildtools/wspace:go_default_library",
   189          "@com_github_golang_protobuf//proto:go_default_library",
   190      ],
   191  )
   192  
   193  go_test(
   194      name = "go_default_test",
   195      srcs = ["edit_test.go"],
   196      library = ":go_default_library",
   197      deps = ["//vendor/github.com/bazelbuild/buildtools/build:go_default_library"],
   198  )
   199  `,
   200  				},
   201  			},
   202  		},
   203  	} {
   204  		t.Run(tc.desc, func(t *testing.T) {
   205  			dir, cleanup := testtools.CreateFiles(t, tc.files)
   206  			defer cleanup()
   207  
   208  			args := []string{"-repo_root", dir, "-from", tc.from, "-to", filepath.Join(dir, filepath.FromSlash(tc.to))}
   209  			if err := run(args); err != nil {
   210  				t.Fatal(err)
   211  			}
   212  
   213  			testtools.CheckFiles(t, dir, tc.want)
   214  		})
   215  	}
   216  }