github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/language/go/build_constraints_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 golang
    17  
    18  import (
    19  	"go/build/constraint"
    20  	"testing"
    21  
    22  	"github.com/google/go-cmp/cmp"
    23  )
    24  
    25  func TestFilterBuildTags(t *testing.T) {
    26  	for _, tc := range []struct {
    27  		desc  string
    28  		input constraint.Expr
    29  		want  constraint.Expr
    30  	}{
    31  		{
    32  			desc:  "should remain",
    33  			input: mustParseBuildTag(t, "go1.8 || go1.9"),
    34  			want:  mustParseBuildTag(t, "go1.8 || go1.9"),
    35  		},
    36  		{
    37  			desc:  "simple 1",
    38  			input: mustParseBuildTag(t, "!(go1.8 || go1.9)"),
    39  			want:  mustParseBuildTag(t, "go1.8 && go1.9"),
    40  		},
    41  		{
    42  			desc:  "simple 2",
    43  			input: mustParseBuildTag(t, "!(foobar || go1.8 || go1.9)"),
    44  			want:  mustParseBuildTag(t, "!foobar && go1.8 && go1.9"),
    45  		},
    46  		{
    47  			desc:  "complex 1",
    48  			input: mustParseBuildTag(t, "!(cgo && (go1.8 || go1.9) || race || msan)"),
    49  			want:  mustParseBuildTag(t, "(cgo || (go1.8 && go1.9)) && race && msan"),
    50  		},
    51  		{
    52  			desc:  "complex 2",
    53  			input: mustParseBuildTag(t, "!(cgo && (go1.8 || go1.9 && (race && foobar)))"),
    54  			want:  mustParseBuildTag(t, "cgo || go1.8 && (go1.9 || (race || !foobar))"),
    55  		},
    56  		{
    57  			desc:  "complex 3",
    58  			input: mustParseBuildTag(t, "!(cgo && (go1.8 || go1.9 && (race && foobar) || baz))"),
    59  			want:  mustParseBuildTag(t, "cgo || (go1.8 && (go1.9 || (race || !foobar)) && !baz)"),
    60  		},
    61  	} {
    62  		t.Run(tc.desc, func(t *testing.T) {
    63  			bt, err := newBuildTags(tc.input)
    64  			if err != nil {
    65  				t.Fatal(err)
    66  			}
    67  			if diff := cmp.Diff(tc.want, bt.expr); diff != "" {
    68  				t.Errorf("(-want, +got): %s", diff)
    69  			}
    70  		})
    71  	}
    72  }