github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/rule/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 rule
    17  
    18  import (
    19  	"reflect"
    20  	"testing"
    21  
    22  	bzl "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 := bzl.Parse("test.bazel", []byte(tc.content))
    53  			if err != nil {
    54  				t.Fatal(err)
    55  			}
    56  
    57  			got := parseDirectives(f.Stmt)
    58  			if !reflect.DeepEqual(got, tc.want) {
    59  				t.Errorf("got %#v ; want %#v", got, tc.want)
    60  			}
    61  		})
    62  	}
    63  }