gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/tools/constraintutil/constraintutil_test.go (about)

     1  // Copyright 2021 The gVisor Authors.
     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  package constraintutil
    16  
    17  import (
    18  	"go/build/constraint"
    19  	"testing"
    20  )
    21  
    22  func TestFileParsing(t *testing.T) {
    23  	for _, test := range []struct {
    24  		name string
    25  		data string
    26  		expr string
    27  	}{
    28  		{
    29  			name: "Empty",
    30  		},
    31  		{
    32  			name: "NoConstraint",
    33  			data: "// copyright header\n\npackage main",
    34  		},
    35  		{
    36  			name: "ConstraintOnFirstLine",
    37  			data: "//go:build amd64\n#include \"textflag.h\"",
    38  			expr: "amd64",
    39  		},
    40  		{
    41  			name: "ConstraintAfterSlashSlashComment",
    42  			data: "// copyright header\n\n//go:build linux\n\npackage newlib",
    43  			expr: "linux",
    44  		},
    45  		{
    46  			name: "ConstraintAfterSlashStarComment",
    47  			data: "/*\ncopyright header\n*/\n\n//go:build !race\n\npackage oldlib",
    48  			expr: "!race",
    49  		},
    50  		{
    51  			name: "ConstraintInSlashSlashComment",
    52  			data: "// blah blah //go:build windows",
    53  		},
    54  		{
    55  			name: "ConstraintInSlashStarComment",
    56  			data: "/*\n//go:build windows\n*/",
    57  		},
    58  		{
    59  			name: "ConstraintAfterPackageClause",
    60  			data: "package oops\n//go:build race",
    61  		},
    62  		{
    63  			name: "ConstraintAfterCppInclude",
    64  			data: "#include \"textflag.h\"\n//go:build arm64",
    65  		},
    66  	} {
    67  		t.Run(test.name, func(t *testing.T) {
    68  			e, err := FromString(test.data)
    69  			if err != nil {
    70  				t.Fatalf("FromString(%q) failed: %v", test.data, err)
    71  			}
    72  			if e == nil {
    73  				if len(test.expr) != 0 {
    74  					t.Errorf("FromString(%q): got no constraint, wanted %q", test.data, test.expr)
    75  				}
    76  			} else {
    77  				got := e.String()
    78  				if len(test.expr) == 0 {
    79  					t.Errorf("FromString(%q): got %q, wanted no constraint", test.data, got)
    80  				} else if got != test.expr {
    81  					t.Errorf("FromString(%q): got %q, wanted %q", test.data, got, test.expr)
    82  				}
    83  			}
    84  		})
    85  	}
    86  }
    87  
    88  func TestCombine(t *testing.T) {
    89  	for _, test := range []struct {
    90  		name string
    91  		in   []string
    92  		out  string
    93  	}{
    94  		{
    95  			name: "0",
    96  		},
    97  		{
    98  			name: "1",
    99  			in:   []string{"amd64 || arm64"},
   100  			out:  "amd64 || arm64",
   101  		},
   102  		{
   103  			name: "2",
   104  			in:   []string{"amd64", "amd64 && linux"},
   105  			out:  "amd64 && amd64 && linux",
   106  		},
   107  		{
   108  			name: "3",
   109  			in:   []string{"amd64", "amd64 || arm64", "amd64 || riscv64"},
   110  			out:  "amd64 && (amd64 || arm64) && (amd64 || riscv64)",
   111  		},
   112  	} {
   113  		t.Run(test.name, func(t *testing.T) {
   114  			inexprs := make([]constraint.Expr, 0, len(test.in))
   115  			for _, estr := range test.in {
   116  				line := "//go:build " + estr
   117  				e, err := constraint.Parse(line)
   118  				if err != nil {
   119  					t.Fatalf("constraint.Parse(%q) failed: %v", line, err)
   120  				}
   121  				inexprs = append(inexprs, e)
   122  			}
   123  			outexpr := Combine(inexprs)
   124  			if outexpr == nil {
   125  				if len(test.out) != 0 {
   126  					t.Errorf("Combine(%v): got no constraint, wanted %q", test.in, test.out)
   127  				}
   128  			} else {
   129  				got := outexpr.String()
   130  				if len(test.out) == 0 {
   131  					t.Errorf("Combine(%v): got %q, wanted no constraint", test.in, got)
   132  				} else if got != test.out {
   133  					t.Errorf("Combine(%v): got %q, wanted %q", test.in, got, test.out)
   134  				}
   135  			}
   136  		})
   137  	}
   138  }