github.com/wolfd/bazel-gazelle@v0.14.0/internal/language/go/config_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 golang
    17  
    18  import (
    19  	"flag"
    20  	"path"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"github.com/bazelbuild/bazel-gazelle/internal/config"
    25  	"github.com/bazelbuild/bazel-gazelle/internal/language"
    26  	"github.com/bazelbuild/bazel-gazelle/internal/language/proto"
    27  	"github.com/bazelbuild/bazel-gazelle/internal/rule"
    28  )
    29  
    30  func testConfig() (*config.Config, *flag.FlagSet, []language.Language) {
    31  	c := config.New()
    32  	fs := flag.NewFlagSet("test", flag.ContinueOnError)
    33  	langs := []language.Language{proto.New(), New()}
    34  	for _, lang := range langs {
    35  		lang.RegisterFlags(fs, "update", c)
    36  	}
    37  	return c, fs, langs
    38  }
    39  
    40  func TestCommandLine(t *testing.T) {
    41  	c, fs, langs := testConfig()
    42  	args := []string{"-build_tags", "foo,bar", "-go_prefix", "example.com/repo", "-external", "vendored"}
    43  	if err := fs.Parse(args); err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	for _, lang := range langs {
    47  		if err := lang.CheckFlags(fs, c); err != nil {
    48  			t.Fatal(err)
    49  		}
    50  	}
    51  	gc := getGoConfig(c)
    52  	for _, tag := range []string{"foo", "bar", "gc"} {
    53  		if !gc.genericTags[tag] {
    54  			t.Errorf("expected tag %q to be set", tag)
    55  		}
    56  	}
    57  	if gc.prefix != "example.com/repo" {
    58  		t.Errorf(`got prefix %q; want "example.com/repo"`, gc.prefix)
    59  	}
    60  	if gc.depMode != vendorMode {
    61  		t.Errorf("got dep mode %v; want %v", gc.depMode, vendorMode)
    62  	}
    63  }
    64  
    65  func TestDirectives(t *testing.T) {
    66  	c, _, langs := testConfig()
    67  	content := []byte(`
    68  # gazelle:build_tags foo,bar
    69  # gazelle:importmap_prefix x
    70  # gazelle:prefix y
    71  `)
    72  	f, err := rule.LoadData(filepath.FromSlash("test/BUILD.bazel"), "test", content)
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	for _, lang := range langs {
    77  		lang.Configure(c, "test", f)
    78  	}
    79  	gc := getGoConfig(c)
    80  	for _, tag := range []string{"foo", "bar", "gc"} {
    81  		if !gc.genericTags[tag] {
    82  			t.Errorf("expected tag %q to be set", tag)
    83  		}
    84  	}
    85  	if gc.prefix != "y" {
    86  		t.Errorf(`got prefix %q; want "y"`, gc.prefix)
    87  	}
    88  	if gc.prefixRel != "test" {
    89  		t.Errorf(`got prefixRel %q; want "test"`, gc.prefixRel)
    90  	}
    91  	if gc.importMapPrefix != "x" {
    92  		t.Errorf(`got importmapPrefix %q; want "x"`, gc.importMapPrefix)
    93  	}
    94  	if gc.importMapPrefixRel != "test" {
    95  		t.Errorf(`got importmapPrefixRel %q; want "test"`, gc.importMapPrefixRel)
    96  	}
    97  }
    98  
    99  func TestVendorConfig(t *testing.T) {
   100  	c, _, langs := testConfig()
   101  	gc := getGoConfig(c)
   102  	gc.prefix = "example.com/repo"
   103  	gc.prefixRel = ""
   104  	gc.importMapPrefix = "bad-importmap-prefix"
   105  	gc.importMapPrefixRel = ""
   106  	for _, lang := range langs {
   107  		lang.Configure(c, "x/vendor", nil)
   108  	}
   109  	gc = getGoConfig(c)
   110  	if gc.prefix != "" {
   111  		t.Errorf(`prefix: got %q; want ""`, gc.prefix)
   112  	}
   113  	if gc.prefixRel != "x/vendor" {
   114  		t.Errorf(`prefixRel: got %q; want "x/vendor"`, gc.prefixRel)
   115  	}
   116  	if gc.importMapPrefix != "example.com/repo/x/vendor" {
   117  		t.Errorf(`importMapPrefix: got %q; want "example.com/repo/x/vendor"`, gc.importMapPrefix)
   118  	}
   119  	if gc.importMapPrefixRel != "x/vendor" {
   120  		t.Errorf(`importMapPrefixRel: got %q; want "x/vendor"`, gc.importMapPrefixRel)
   121  	}
   122  }
   123  
   124  func TestInferProtoMode(t *testing.T) {
   125  	c, _, langs := testConfig()
   126  	for _, tc := range []struct {
   127  		desc, rel, content string
   128  		old                proto.Mode
   129  		explicit           bool
   130  		want               proto.Mode
   131  	}{
   132  		{
   133  			desc: "default_empty",
   134  			old:  proto.DefaultMode,
   135  			want: proto.DefaultMode,
   136  		}, {
   137  			desc: "default_to_legacy",
   138  			old:  proto.DefaultMode,
   139  			content: `
   140  load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library")
   141  
   142  go_proto_library(
   143      name = "foo_proto",
   144  )
   145  `,
   146  			want: proto.LegacyMode,
   147  		}, {
   148  			desc: "default_to_disable",
   149  			old:  proto.DefaultMode,
   150  			content: `
   151  load("@some_repo//:custom.bzl", "go_proto_library")
   152  `,
   153  			want: proto.DisableMode,
   154  		}, {
   155  			desc: "vendor_disable",
   156  			old:  proto.DefaultMode,
   157  			rel:  "vendor",
   158  			want: proto.DisableMode,
   159  		}, {
   160  			desc:     "explicit_override_legacy",
   161  			old:      proto.DefaultMode,
   162  			explicit: true,
   163  			content: `
   164  load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library")
   165  `,
   166  			want: proto.DefaultMode,
   167  		}, {
   168  			desc:     "explicit_override_vendor",
   169  			old:      proto.DefaultMode,
   170  			explicit: true,
   171  			rel:      "vendor",
   172  			want:     proto.DefaultMode,
   173  		}, {
   174  			desc:     "disable_override_legacy",
   175  			old:      proto.DisableMode,
   176  			explicit: false,
   177  			content: `
   178  load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library")
   179  `,
   180  			want: proto.DisableMode,
   181  		},
   182  	} {
   183  		t.Run(tc.desc, func(t *testing.T) {
   184  			c := c.Clone()
   185  			pc := proto.GetProtoConfig(c)
   186  			pc.Mode = tc.old
   187  			pc.ModeExplicit = tc.explicit
   188  			var f *rule.File
   189  			if tc.content != "" {
   190  				var err error
   191  				f, err = rule.LoadData(path.Join(tc.rel, "BUILD.bazel"), tc.rel, []byte(tc.content))
   192  				if err != nil {
   193  					t.Fatal(err)
   194  				}
   195  			}
   196  			for _, lang := range langs {
   197  				lang.Configure(c, tc.rel, f)
   198  			}
   199  			pc = proto.GetProtoConfig(c)
   200  			if pc.Mode != tc.want {
   201  				t.Errorf("got %v; want %v", pc.Mode, tc.want)
   202  			}
   203  		})
   204  	}
   205  }
   206  
   207  func TestPreprocessTags(t *testing.T) {
   208  	gc := newGoConfig()
   209  	expectedTags := []string{"gc"}
   210  	for _, tag := range expectedTags {
   211  		if !gc.genericTags[tag] {
   212  			t.Errorf("tag %q not set", tag)
   213  		}
   214  	}
   215  	unexpectedTags := []string{"x", "cgo", "go1.8", "go1.7"}
   216  	for _, tag := range unexpectedTags {
   217  		if gc.genericTags[tag] {
   218  			t.Errorf("tag %q unexpectedly set", tag)
   219  		}
   220  	}
   221  }
   222  
   223  func TestPrefixFallback(t *testing.T) {
   224  	c, _, langs := testConfig()
   225  	for _, tc := range []struct {
   226  		desc, content, want string
   227  	}{
   228  		{
   229  			desc: "go_prefix",
   230  			content: `
   231  go_prefix("example.com/repo")
   232  `,
   233  			want: "example.com/repo",
   234  		}, {
   235  			desc: "gazelle",
   236  			content: `
   237  gazelle(
   238      name = "gazelle",
   239      prefix = "example.com/repo",
   240  )
   241  `,
   242  			want: "example.com/repo",
   243  		},
   244  	} {
   245  		t.Run(tc.desc, func(t *testing.T) {
   246  			f, err := rule.LoadData("BUILD.bazel", "", []byte(tc.content))
   247  			if err != nil {
   248  				t.Fatal(err)
   249  			}
   250  			for _, lang := range langs {
   251  				lang.Configure(c, "x", f)
   252  			}
   253  			gc := getGoConfig(c)
   254  			if !gc.prefixSet {
   255  				t.Fatalf("prefix not set")
   256  			}
   257  			if gc.prefix != tc.want {
   258  				t.Errorf("prefix: want %q; got %q", gc.prefix, tc.want)
   259  			}
   260  			if gc.prefixRel != "x" {
   261  				t.Errorf("rel: got %q; want %q", gc.prefixRel, "x")
   262  			}
   263  		})
   264  	}
   265  }