github.com/afking/bazel-gazelle@v0.0.0-20180301150245-c02bc0f529e8/internal/merger/fix_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 merger
    17  
    18  import (
    19  	"testing"
    20  
    21  	"github.com/bazelbuild/bazel-gazelle/internal/config"
    22  	bf "github.com/bazelbuild/buildtools/build"
    23  )
    24  
    25  type fixTestCase struct {
    26  	desc, old, want string
    27  }
    28  
    29  func TestFixFile(t *testing.T) {
    30  	for _, tc := range []fixTestCase{
    31  		// squashCgoLibrary tests
    32  		{
    33  			desc: "no cgo_library",
    34  			old: `load("@io_bazel_rules_go//go:def.bzl", "go_library")
    35  
    36  go_library(
    37      name = "go_default_library",
    38  )
    39  `,
    40  			want: `load("@io_bazel_rules_go//go:def.bzl", "go_library")
    41  
    42  go_library(
    43      name = "go_default_library",
    44  )
    45  `,
    46  		},
    47  		{
    48  			desc: "non-default cgo_library not removed",
    49  			old: `load("@io_bazel_rules_go//go:def.bzl", "cgo_library")
    50  
    51  cgo_library(
    52      name = "something_else",
    53  )
    54  `,
    55  			want: `load("@io_bazel_rules_go//go:def.bzl", "cgo_library")
    56  
    57  cgo_library(
    58      name = "something_else",
    59  )
    60  `,
    61  		},
    62  		{
    63  			desc: "unlinked cgo_library removed",
    64  			old: `load("@io_bazel_rules_go//go:def.bzl", "cgo_library", "go_library")
    65  
    66  go_library(
    67      name = "go_default_library",
    68      library = ":something_else",
    69  )
    70  
    71  cgo_library(
    72      name = "cgo_default_library",
    73  )
    74  `,
    75  			want: `load("@io_bazel_rules_go//go:def.bzl", "cgo_library", "go_library")
    76  
    77  go_library(
    78      name = "go_default_library",
    79      cgo = True,
    80  )
    81  `,
    82  		},
    83  		{
    84  			desc: "cgo_library replaced with go_library",
    85  			old: `load("@io_bazel_rules_go//go:def.bzl", "cgo_library")
    86  
    87  # before comment
    88  cgo_library(
    89      name = "cgo_default_library",
    90      cdeps = ["cdeps"],
    91      clinkopts = ["clinkopts"],
    92      copts = ["copts"],
    93      data = ["data"],
    94      deps = ["deps"],
    95      gc_goopts = ["gc_goopts"],
    96      srcs = [
    97          "foo.go"  # keep
    98      ],
    99      visibility = ["//visibility:private"],    
   100  )
   101  # after comment
   102  `,
   103  			want: `load("@io_bazel_rules_go//go:def.bzl", "cgo_library")
   104  
   105  # before comment
   106  go_library(
   107      name = "go_default_library",
   108      visibility = ["//visibility:private"],
   109      cgo = True,
   110      cdeps = ["cdeps"],
   111      clinkopts = ["clinkopts"],
   112      copts = ["copts"],
   113      data = ["data"],
   114      deps = ["deps"],
   115      gc_goopts = ["gc_goopts"],
   116      srcs = [
   117          "foo.go",  # keep
   118      ],
   119  )
   120  # after comment
   121  `,
   122  		}, {
   123  			desc: "cgo_library merged with go_library",
   124  			old: `load("@io_bazel_rules_go//go:def.bzl", "go_library")
   125  
   126  # before go_library
   127  go_library(
   128      name = "go_default_library",
   129      srcs = ["pure.go"],
   130      deps = ["pure_deps"],
   131      data = ["pure_data"],
   132      gc_goopts = ["pure_gc_goopts"],
   133      library = ":cgo_default_library",
   134      cgo = False,
   135  )
   136  # after go_library
   137  
   138  # before cgo_library
   139  cgo_library(
   140      name = "cgo_default_library",
   141      srcs = ["cgo.go"],
   142      deps = ["cgo_deps"],
   143      data = ["cgo_data"],
   144      gc_goopts = ["cgo_gc_goopts"],
   145      copts = ["copts"],
   146      cdeps = ["cdeps"],
   147  )
   148  # after cgo_library
   149  `,
   150  			want: `load("@io_bazel_rules_go//go:def.bzl", "go_library")
   151  
   152  # before go_library
   153  # before cgo_library
   154  go_library(
   155      name = "go_default_library",
   156      srcs = [
   157          "pure.go",
   158          "cgo.go",
   159      ],
   160      deps = [
   161          "pure_deps",
   162          "cgo_deps",
   163      ],
   164      data = [
   165          "pure_data",
   166          "cgo_data",
   167      ],
   168      gc_goopts = [
   169          "pure_gc_goopts",
   170          "cgo_gc_goopts",
   171      ],
   172      cgo = True,
   173      cdeps = ["cdeps"],
   174      copts = ["copts"],
   175  )
   176  # after go_library
   177  # after cgo_library
   178  `,
   179  		},
   180  		// removeLegacyProto tests
   181  		{
   182  			desc: "current proto preserved",
   183  			old: `load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
   184  
   185  go_proto_library(
   186      name = "foo_go_proto",
   187      proto = ":foo_proto",
   188  )
   189  `,
   190  			want: `load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
   191  
   192  go_proto_library(
   193      name = "foo_go_proto",
   194      proto = ":foo_proto",
   195  )
   196  `,
   197  		},
   198  		{
   199  			desc: "load and proto removed",
   200  			old: `load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library")
   201  
   202  go_proto_library(
   203      name = "go_default_library_protos",
   204      srcs = ["foo.proto"],
   205      visibility = ["//visibility:private"],
   206  )
   207  `,
   208  			want: "",
   209  		},
   210  		{
   211  			desc: "proto filegroup removed",
   212  			old: `filegroup(
   213      name = "go_default_library_protos",
   214      srcs = ["foo.proto"],
   215  )
   216  
   217  go_proto_library(name = "foo_proto")
   218  `,
   219  			want: `go_proto_library(name = "foo_proto")
   220  `,
   221  		},
   222  		// migrateLibraryEmbed tests
   223  		{
   224  			desc: "library migrated to embed",
   225  			old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
   226  
   227  go_library(
   228      name = "go_default_library",
   229      srcs = ["foo.go"],
   230  )
   231  
   232  go_test(
   233      name = "go_default_test",
   234      srcs = ["foo_test.go"],
   235      library = ":go_default_library",
   236  )
   237  `,
   238  			want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
   239  
   240  go_library(
   241      name = "go_default_library",
   242      srcs = ["foo.go"],
   243  )
   244  
   245  go_test(
   246      name = "go_default_test",
   247      srcs = ["foo_test.go"],
   248      embed = [":go_default_library"],
   249  )
   250  `,
   251  		},
   252  		// migrateGrpcCompilers tests
   253  		{
   254  			desc: "go_grpc_library migrated to compilers",
   255  			old: `load("@io_bazel_rules_go//proto:def.bzl", "go_grpc_library")
   256  
   257  proto_library(
   258      name = "foo_proto",
   259      srcs = ["foo.proto"],
   260      visibility = ["//visibility:public"],
   261  )
   262  
   263  go_grpc_library(
   264      name = "foo_go_proto",
   265      importpath = "example.com/repo",
   266      proto = ":foo_proto",
   267      visibility = ["//visibility:public"],
   268  )
   269  `,
   270  			want: `load("@io_bazel_rules_go//proto:def.bzl", "go_grpc_library")
   271  
   272  proto_library(
   273      name = "foo_proto",
   274      srcs = ["foo.proto"],
   275      visibility = ["//visibility:public"],
   276  )
   277  
   278  go_proto_library(
   279      name = "foo_go_proto",
   280      importpath = "example.com/repo",
   281      proto = ":foo_proto",
   282      visibility = ["//visibility:public"],
   283      compilers = ["@io_bazel_rules_go//proto:go_grpc"],
   284  )
   285  `,
   286  		},
   287  		// removeBinaryImportPath tests
   288  		{
   289  			desc: "binary importpath removed",
   290  			old: `load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_test")
   291  
   292  go_binary(
   293      name = "cmd",
   294      srcs = ["main.go"],
   295      importpath = "example.com/repo",
   296  )
   297  
   298  go_test(
   299      name = "go_default_test",
   300      srcs = ["main_test.go"],
   301      importpath = "example.com/repo",
   302  )
   303  `,
   304  			want: `load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_test")
   305  
   306  go_binary(
   307      name = "cmd",
   308      srcs = ["main.go"],
   309  )
   310  
   311  go_test(
   312      name = "go_default_test",
   313      srcs = ["main_test.go"],
   314  )
   315  `,
   316  		},
   317  	} {
   318  		t.Run(tc.desc, func(t *testing.T) {
   319  			fix := func(f *bf.File) *bf.File {
   320  				c := &config.Config{}
   321  				return FixFile(c, FixFileMinor(c, f))
   322  			}
   323  			testFix(t, tc, fix)
   324  		})
   325  	}
   326  }
   327  
   328  func TestFixLoads(t *testing.T) {
   329  	for _, tc := range []fixTestCase{
   330  		{
   331  			desc: "empty file",
   332  			old:  "",
   333  			want: "",
   334  		}, {
   335  			desc: "non-Go file",
   336  			old: `load("@io_bazel_rules_intercal//intercal:def.bzl", "intercal_library")
   337  
   338  intercal_library(
   339      name = "intercal_default_library",
   340      srcs = ["foo.ic"],
   341  )
   342  `,
   343  			want: `load("@io_bazel_rules_intercal//intercal:def.bzl", "intercal_library")
   344  
   345  intercal_library(
   346      name = "intercal_default_library",
   347      srcs = ["foo.ic"],
   348  )
   349  `,
   350  		}, {
   351  			desc: "empty Go load",
   352  			old: `load("@io_bazel_rules_go//go:def.bzl")
   353  `,
   354  			want: "",
   355  		}, {
   356  			desc: "add and remove loaded symbols",
   357  			old: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
   358  
   359  go_library(name = "go_default_library")
   360  
   361  go_binary(name = "cmd")
   362  `,
   363  			want: `load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
   364  
   365  go_library(name = "go_default_library")
   366  
   367  go_binary(name = "cmd")
   368  `,
   369  		}, {
   370  			desc: "consolidate load statements",
   371  			old: `load("@io_bazel_rules_go//go:def.bzl", "go_library")
   372  load("@io_bazel_rules_go//go:def.bzl", "go_library")
   373  load("@io_bazel_rules_go//go:def.bzl", "go_test")
   374  
   375  go_library(name = "go_default_library")
   376  
   377  go_test(name = "go_default_test")
   378  `,
   379  			want: `load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
   380  
   381  go_library(name = "go_default_library")
   382  
   383  go_test(name = "go_default_test")
   384  `,
   385  		}, {
   386  			desc: "new load statement",
   387  			old: `go_library(
   388      name = "go_default_library",
   389  )
   390  
   391  go_embed_data(
   392      name = "data",
   393  )
   394  `,
   395  			want: `load("@io_bazel_rules_go//go:def.bzl", "go_library")
   396  
   397  go_library(
   398      name = "go_default_library",
   399  )
   400  
   401  go_embed_data(
   402      name = "data",
   403  )
   404  `,
   405  		}, {
   406  			desc: "proto symbols",
   407  			old: `go_proto_library(
   408      name = "foo_proto",
   409  )
   410  
   411  go_grpc_library(
   412      name = "bar_proto",
   413  )
   414  `,
   415  			want: `load("@io_bazel_rules_go//proto:def.bzl", "go_grpc_library", "go_proto_library")
   416  
   417  go_proto_library(
   418      name = "foo_proto",
   419  )
   420  
   421  go_grpc_library(
   422      name = "bar_proto",
   423  )
   424  `,
   425  		}, {
   426  			desc: "fixLoad doesn't touch other symbols or loads",
   427  			old: `load(
   428      "@io_bazel_rules_go//go:def.bzl",
   429      "go_embed_data",  # embed
   430      "go_test",
   431      foo = "go_binary",  # binary
   432  )
   433  load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library")
   434  
   435  go_library(
   436      name = "go_default_library",
   437  )
   438  `,
   439  			want: `load(
   440      "@io_bazel_rules_go//go:def.bzl",
   441      "go_embed_data",  # embed
   442      "go_library",
   443      foo = "go_binary",  # binary
   444  )
   445  load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library")
   446  
   447  go_library(
   448      name = "go_default_library",
   449  )
   450  `,
   451  		}, {
   452  			desc: "fixLoad doesn't touch loads from other files",
   453  			old: `load(
   454      "@com_github_pubref_rules_protobuf//go:rules.bzl",
   455      "go_proto_library",
   456      go_grpc_library = "go_proto_library",
   457  )
   458  
   459  go_proto_library(
   460      name = "foo_go_proto",
   461  )
   462  
   463  grpc_proto_library(
   464      name = "bar_go_proto",
   465  )
   466  `,
   467  			want: `load(
   468      "@com_github_pubref_rules_protobuf//go:rules.bzl",
   469      "go_proto_library",
   470      go_grpc_library = "go_proto_library",
   471  )
   472  
   473  go_proto_library(
   474      name = "foo_go_proto",
   475  )
   476  
   477  grpc_proto_library(
   478      name = "bar_go_proto",
   479  )
   480  `,
   481  		},
   482  	} {
   483  		t.Run(tc.desc, func(t *testing.T) {
   484  			testFix(t, tc, FixLoads)
   485  		})
   486  	}
   487  }
   488  
   489  func testFix(t *testing.T, tc fixTestCase, fix func(*bf.File) *bf.File) {
   490  	oldFile, err := bf.Parse("old", []byte(tc.old))
   491  	if err != nil {
   492  		t.Fatalf("%s: parse error: %v", tc.desc, err)
   493  	}
   494  	fixedFile := fix(oldFile)
   495  	if got := string(bf.Format(fixedFile)); got != tc.want {
   496  		t.Fatalf("%s: got %s; want %s", tc.desc, got, tc.want)
   497  	}
   498  }