github.com/afking/bazel-gazelle@v0.0.0-20180301150245-c02bc0f529e8/internal/config/directives_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 config
    17  
    18  import (
    19  	"reflect"
    20  	"testing"
    21  
    22  	bf "github.com/bazelbuild/buildtools/build"
    23  )
    24  
    25  func TestParseDirectives(t *testing.T) {
    26  	for _, tc := range []struct {
    27  		desc, content string
    28  		want          []Directive
    29  	}{
    30  		{
    31  			desc: "empty file",
    32  		}, {
    33  			desc: "locations",
    34  			content: `# gazelle:ignore top
    35  
    36  #gazelle:ignore before
    37  foo(
    38     "foo",  # gazelle:ignore inside
    39  ) # gazelle:ignore suffix
    40  #gazelle:ignore after
    41  
    42  # gazelle:ignore bottom`,
    43  			want: []Directive{
    44  				{"ignore", "top"},
    45  				{"ignore", "before"},
    46  				{"ignore", "after"},
    47  				{"ignore", "bottom"},
    48  			},
    49  		},
    50  	} {
    51  		t.Run(tc.desc, func(t *testing.T) {
    52  			f, err := bf.Parse("test.bazel", []byte(tc.content))
    53  			if err != nil {
    54  				t.Fatal(err)
    55  			}
    56  
    57  			got := ParseDirectives(f)
    58  			if !reflect.DeepEqual(got, tc.want) {
    59  				t.Errorf("got %#v ; want %#v", got, tc.want)
    60  			}
    61  		})
    62  	}
    63  }
    64  
    65  func TestApplyDirectives(t *testing.T) {
    66  	for _, tc := range []struct {
    67  		desc       string
    68  		directives []Directive
    69  		rel        string
    70  		want       Config
    71  	}{
    72  		{
    73  			desc:       "empty build_tags",
    74  			directives: []Directive{{"build_tags", ""}},
    75  			want:       Config{},
    76  		}, {
    77  			desc:       "build_tags",
    78  			directives: []Directive{{"build_tags", "foo,bar"}},
    79  			want:       Config{GenericTags: BuildTags{"foo": true, "bar": true}},
    80  		}, {
    81  			desc:       "build_file_name",
    82  			directives: []Directive{{"build_file_name", "foo,bar"}},
    83  			want:       Config{ValidBuildFileNames: []string{"foo", "bar"}},
    84  		}, {
    85  			desc:       "prefix",
    86  			directives: []Directive{{"prefix", "example.com/repo"}},
    87  			rel:        "sub",
    88  			want:       Config{GoPrefix: "example.com/repo", GoPrefixRel: "sub"},
    89  		},
    90  	} {
    91  		t.Run(tc.desc, func(t *testing.T) {
    92  			c := &Config{}
    93  			c.PreprocessTags()
    94  			got := ApplyDirectives(c, tc.directives, tc.rel)
    95  			tc.want.PreprocessTags()
    96  			if !reflect.DeepEqual(*got, tc.want) {
    97  				t.Errorf("got %#v ; want %#v", *got, tc.want)
    98  			}
    99  		})
   100  	}
   101  }
   102  
   103  func TestInferProtoMode(t *testing.T) {
   104  	for _, tc := range []struct {
   105  		desc, content string
   106  		c             Config
   107  		rel           string
   108  		want          ProtoMode
   109  	}{
   110  		{
   111  			desc: "default",
   112  		}, {
   113  			desc: "previous",
   114  			c:    Config{ProtoMode: LegacyProtoMode},
   115  			want: LegacyProtoMode,
   116  		}, {
   117  			desc: "explicit",
   118  			content: `# gazelle:proto default
   119  
   120  load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library")
   121  `,
   122  			want: DefaultProtoMode,
   123  		}, {
   124  			desc:    "explicit_no_override",
   125  			content: `load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library")`,
   126  			c: Config{
   127  				ProtoMode:         DefaultProtoMode,
   128  				ProtoModeExplicit: true,
   129  			},
   130  			want: DefaultProtoMode,
   131  		}, {
   132  			desc: "vendor",
   133  			rel:  "vendor",
   134  			want: DisableProtoMode,
   135  		}, {
   136  			desc:    "legacy",
   137  			content: `load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library")`,
   138  			want:    LegacyProtoMode,
   139  		}, {
   140  			desc:    "disable",
   141  			content: `load("@com_example_repo//proto:go_proto_library.bzl", go_proto_library = "x")`,
   142  			want:    DisableProtoMode,
   143  		}, {
   144  			desc:    "fix legacy",
   145  			content: `load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library")`,
   146  			c:       Config{ShouldFix: true},
   147  		}, {
   148  			desc:    "fix disabled",
   149  			content: `load("@com_example_repo//proto:go_proto_library.bzl", go_proto_library = "x")`,
   150  			c:       Config{ShouldFix: true},
   151  			want:    DisableProtoMode,
   152  		}, {
   153  			desc: "well known types",
   154  			c:    Config{GoPrefix: "github.com/golang/protobuf"},
   155  			want: LegacyProtoMode,
   156  		},
   157  	} {
   158  		t.Run(tc.desc, func(t *testing.T) {
   159  			var f *bf.File
   160  			var directives []Directive
   161  			if tc.content != "" {
   162  				var err error
   163  				f, err = bf.Parse("BUILD.bazel", []byte(tc.content))
   164  				if err != nil {
   165  					t.Fatalf("error parsing build file: %v", err)
   166  				}
   167  				directives = ParseDirectives(f)
   168  			}
   169  
   170  			got := InferProtoMode(&tc.c, tc.rel, f, directives)
   171  			if got.ProtoMode != tc.want {
   172  				t.Errorf("got proto mode %v ; want %v", got.ProtoMode, tc.want)
   173  			}
   174  		})
   175  	}
   176  }