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

     1  /* Copyright 2022 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 merger_test
    17  
    18  import (
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/bazelbuild/bazel-gazelle/merger"
    23  	"github.com/bazelbuild/bazel-gazelle/rule"
    24  	bzl "github.com/bazelbuild/buildtools/build"
    25  	"github.com/google/go-cmp/cmp"
    26  )
    27  
    28  func TestFixLoads(t *testing.T) {
    29  	knownLoads := []rule.LoadInfo{
    30  		{
    31  			Name: "@bar",
    32  			Symbols: []string{
    33  				"magic",
    34  			},
    35  		},
    36  		{
    37  			Name: "@foo",
    38  			Symbols: []string{
    39  				"foo_binary",
    40  				"foo_library",
    41  				"foo_test",
    42  			},
    43  		},
    44  		{
    45  			Name: "@bazel_tools//tools/build_defs/repo:utils.bzl",
    46  			Symbols: []string{
    47  				"maybe",
    48  			},
    49  		},
    50  		{
    51  			Name: "@bazel_skylib//lib:selects.bzl",
    52  			Symbols: []string{
    53  				"selects",
    54  			},
    55  		},
    56  	}
    57  
    58  	type testCase struct {
    59  		input string
    60  		want  string
    61  	}
    62  
    63  	for name, tc := range map[string]testCase{
    64  		"correct": {
    65  			input: `load("@foo", "foo_binary", "foo_library")
    66  
    67  foo_binary(name = "a")
    68  
    69  foo_library(name = "a_lib")
    70  `,
    71  			want: `load("@foo", "foo_binary", "foo_library")
    72  
    73  foo_binary(name = "a")
    74  
    75  foo_library(name = "a_lib")
    76  `,
    77  		},
    78  		"correct with macro": {
    79  			input: `load("@bar", "magic")
    80  load("@foo", "foo_binary", "foo_library")
    81  
    82  foo_binary(name = "a")
    83  
    84  foo_library(
    85  	name = "a_lib",
    86  	deps = [
    87  		"//a/b:c",
    88  		magic("baz"),
    89  	],
    90  )
    91  		`,
    92  			want: `load("@bar", "magic")
    93  load("@foo", "foo_binary", "foo_library")
    94  
    95  foo_binary(name = "a")
    96  
    97  foo_library(
    98      name = "a_lib",
    99      deps = [
   100          "//a/b:c",
   101          magic("baz"),
   102      ],
   103  )
   104  `,
   105  		},
   106  		"missing macro load": {
   107  			input: `load("@foo", "foo_binary", "foo_library")
   108  
   109  foo_binary(name = "a")
   110  
   111  foo_library(
   112      name = "a_lib",
   113      deps = [
   114          "//a/b:c",
   115          magic("baz"),
   116      ],
   117  )
   118  `,
   119  			want: `load("@bar", "magic")
   120  load("@foo", "foo_binary", "foo_library")
   121  
   122  foo_binary(name = "a")
   123  
   124  foo_library(
   125      name = "a_lib",
   126      deps = [
   127          "//a/b:c",
   128          magic("baz"),
   129      ],
   130  )
   131  `,
   132  		},
   133  		"unused macro": {
   134  			input: `load("@bar", "magic")
   135  			load("@foo", "foo_binary")
   136  
   137  foo_binary(name = "a")
   138  `,
   139  			want: `load("@foo", "foo_binary")
   140  
   141  foo_binary(name = "a")
   142  `,
   143  		},
   144  		"missing kind load symbol": {
   145  			input: `load("@foo", "foo_binary")
   146  
   147  foo_binary(name = "a")
   148  
   149  foo_library(name = "a_lib")
   150  `,
   151  			want: `load("@foo", "foo_binary", "foo_library")
   152  
   153  foo_binary(name = "a")
   154  
   155  foo_library(name = "a_lib")
   156  `,
   157  		},
   158  		"missing kind load": {
   159  			input: `foo_binary(name = "a")
   160  
   161  foo_library(name = "a_lib")
   162  `,
   163  			want: `load("@foo", "foo_binary", "foo_library")
   164  
   165  foo_binary(name = "a")
   166  
   167  foo_library(name = "a_lib")
   168  `,
   169  		},
   170  		"missing wrapper and wrapped kind load symbol": {
   171  			input: `maybe(
   172      foo_binary,
   173      name = "a",
   174  )
   175  `,
   176  			want: `load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
   177  load("@foo", "foo_binary")
   178  
   179  maybe(
   180      foo_binary,
   181      name = "a",
   182  )
   183  `,
   184  		},
   185  		"unused kind load symbol": {
   186  			input: `load("@foo", "foo_binary", "foo_library", "foo_test")
   187  
   188  foo_binary(name = "a")
   189  
   190  foo_library(name = "a_lib")
   191  `,
   192  			want: `load("@foo", "foo_binary", "foo_library")
   193  
   194  foo_binary(name = "a")
   195  
   196  foo_library(name = "a_lib")
   197  `,
   198  		},
   199  		"struct macro": {
   200  			input: `selects.config_setting_group(
   201      name = "a",
   202      match_any = [
   203          "//:config_a",
   204          "//:config_b",
   205      ],
   206  )
   207  `,
   208  			want: `load("@bazel_skylib//lib:selects.bzl", "selects")
   209  
   210  selects.config_setting_group(
   211      name = "a",
   212      match_any = [
   213          "//:config_a",
   214          "//:config_b",
   215      ],
   216  )
   217  `,
   218  		},
   219  	} {
   220  		t.Run(name, func(t *testing.T) {
   221  			f, err := rule.LoadData("", "", []byte(tc.input))
   222  			if err != nil {
   223  				t.Fatalf("unexpected error: %v", err)
   224  			}
   225  
   226  			merger.FixLoads(f, knownLoads)
   227  			f.Sync()
   228  
   229  			want := strings.TrimSpace(tc.want)
   230  			got := strings.TrimSpace(string(bzl.Format(f.File)))
   231  			if diff := cmp.Diff(want, got); diff != "" {
   232  				t.Errorf("FixLoads() mismatch (-want +got):\n%s", diff)
   233  			}
   234  		})
   235  	}
   236  }