1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
// Copyright 2021 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package constraintutil
import (
"go/build/constraint"
"testing"
)
func TestFileParsing(t *testing.T) {
for _, test := range []struct {
name string
data string
expr string
}{
{
name: "Empty",
},
{
name: "NoConstraint",
data: "// copyright header\n\npackage main",
},
{
name: "ConstraintOnFirstLine",
data: "//go:build amd64\n#include \"textflag.h\"",
expr: "amd64",
},
{
name: "ConstraintAfterSlashSlashComment",
data: "// copyright header\n\n//go:build linux\n\npackage newlib",
expr: "linux",
},
{
name: "ConstraintAfterSlashStarComment",
data: "/*\ncopyright header\n*/\n\n//go:build !race\n\npackage oldlib",
expr: "!race",
},
{
name: "ConstraintInSlashSlashComment",
data: "// blah blah //go:build windows",
},
{
name: "ConstraintInSlashStarComment",
data: "/*\n//go:build windows\n*/",
},
{
name: "ConstraintAfterPackageClause",
data: "package oops\n//go:build race",
},
{
name: "ConstraintAfterCppInclude",
data: "#include \"textflag.h\"\n//go:build arm64",
},
} {
t.Run(test.name, func(t *testing.T) {
e, err := FromString(test.data)
if err != nil {
t.Fatalf("FromString(%q) failed: %v", test.data, err)
}
if e == nil {
if len(test.expr) != 0 {
t.Errorf("FromString(%q): got no constraint, wanted %q", test.data, test.expr)
}
} else {
got := e.String()
if len(test.expr) == 0 {
t.Errorf("FromString(%q): got %q, wanted no constraint", test.data, got)
} else if got != test.expr {
t.Errorf("FromString(%q): got %q, wanted %q", test.data, got, test.expr)
}
}
})
}
}
func TestCombine(t *testing.T) {
for _, test := range []struct {
name string
in []string
out string
}{
{
name: "0",
},
{
name: "1",
in: []string{"amd64 || arm64"},
out: "amd64 || arm64",
},
{
name: "2",
in: []string{"amd64", "amd64 && linux"},
out: "amd64 && amd64 && linux",
},
{
name: "3",
in: []string{"amd64", "amd64 || arm64", "amd64 || riscv64"},
out: "amd64 && (amd64 || arm64) && (amd64 || riscv64)",
},
} {
t.Run(test.name, func(t *testing.T) {
inexprs := make([]constraint.Expr, 0, len(test.in))
for _, estr := range test.in {
line := "//go:build " + estr
e, err := constraint.Parse(line)
if err != nil {
t.Fatalf("constraint.Parse(%q) failed: %v", line, err)
}
inexprs = append(inexprs, e)
}
outexpr := Combine(inexprs)
if outexpr == nil {
if len(test.out) != 0 {
t.Errorf("Combine(%v): got no constraint, wanted %q", test.in, test.out)
}
} else {
got := outexpr.String()
if len(test.out) == 0 {
t.Errorf("Combine(%v): got %q, wanted no constraint", test.in, got)
} else if got != test.out {
t.Errorf("Combine(%v): got %q, wanted %q", test.in, got, test.out)
}
}
})
}
}
|