-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntax_test.go
More file actions
249 lines (242 loc) · 6.92 KB
/
syntax_test.go
File metadata and controls
249 lines (242 loc) · 6.92 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package conditional
import "testing"
func TestConditionalSyntaxReference(t *testing.T) {
tests := []evaluateCase{
{
name: "equality comparator",
source: docsConditionalsSource,
expression: `12345 == 12345`,
want: true,
},
{
name: "inequality comparator",
source: docsConditionalsSource,
expression: `"feature-branch" != "main"`,
want: true,
},
{
name: "regex match comparator",
source: docsConditionalsSource,
expression: `"v1.0" =~ /^v[0-9]+\.0$/`,
want: true,
},
{
name: "regex not match comparator",
source: docsConditionalsSource,
expression: `"build all" !~ /skip tests/`,
want: true,
},
{
name: "logical operators respect precedence",
source: docsConditionalsSource,
expression: `true || false && false`,
want: true,
},
{
name: "array includes operator",
source: docsConditionalsSource,
expression: `["main", "production"] includes "production"`,
want: true,
},
{
name: "single and double quoted strings compare equally",
source: docsConditionalsSource,
expression: `'feature-branch' == "feature-branch"`,
want: true,
},
{
name: "literals include true false and null",
source: docsConditionalsSource,
expression: `true == true && false == false && null == null`,
want: true,
},
{
name: "parentheses change logical grouping",
source: docsConditionalsSource,
expression: `(true || false) && false`,
want: false,
},
{
name: "prefix bang negates booleans",
source: docsConditionalsSource,
expression: `!false`,
want: true,
},
{
name: "comments are ignored",
source: docsConditionalsSource,
expression: "// ignored\ntrue && true\n// also ignored",
want: true,
},
{
name: "inline comments terminate expression text",
source: upstreamParserSpec,
expression: "true // ignores comments on the same line",
want: true,
},
}
runEvaluateCases(t, tests)
}
func TestConditionalSyntaxErrors(t *testing.T) {
tests := []validateCase{
{
name: "parse error for malformed comparator",
source: upstreamParserSpec,
expression: `nope != == one`,
wantError: ErrorKindParse,
},
{
name: "parse error for unsupported regexp flag",
source: upstreamParserSpec,
expression: `"main" =~ /main/x`,
wantError: ErrorKindParse,
},
{
name: "parse error for malformed dotted identifier",
source: upstreamParserSpec,
expression: `build..branch == "main"`,
wantError: ErrorKindParse,
},
{
name: "parse error for out of range octal string escape",
source: upstreamConditionalGrammar,
expression: `"\400" == ""`,
wantError: ErrorKindParse,
},
{
name: "parser rejects local-only at greater operator",
source: upstreamParserSpec,
expression: `["main"] @> build.branch`,
ctx: Context{
EntryPoint: EntryPointBuildCondition,
Build: Build{Branch: str("main")},
},
wantError: ErrorKindParse,
},
{
name: "validation rejects env without an argument",
source: upstreamBuildConditionSpec,
expression: `env() == ""`,
wantError: ErrorKindValidation,
},
{
name: "validation rejects build env with too many arguments",
source: upstreamBuildConditionSpec,
expression: `build.env("FOO", "BAR") == null`,
wantError: ErrorKindValidation,
},
{
name: "validation rejects env non string argument",
source: upstreamParserSpec,
expression: `env(1) == ""`,
wantError: ErrorKindValidation,
},
{
name: "validation rejects env escaped dollar name",
source: upstreamBuildConditionSpec,
expression: `env("$$FOO") == ""`,
wantError: ErrorKindValidation,
},
{
name: "validation rejects env backslash escaped dollar name",
source: upstreamBuildConditionSpec,
expression: `env("\$FOO") == ""`,
wantError: ErrorKindValidation,
},
{
name: "validation rejects unknown variable",
source: upstreamParserSpec,
expression: `not_a_variable == "x"`,
wantError: ErrorKindValidation,
},
{
name: "validation rejects unknown function",
source: upstreamParserSpec,
expression: `not_a_function() == "x"`,
wantError: ErrorKindValidation,
},
{
name: "validation rejects regex with non regexp rhs",
source: upstreamParserSpec,
expression: `"d" =~ "hello"`,
wantError: ErrorKindValidation,
},
{
name: "validation rejects regex with boolean lhs",
source: upstreamParserSpec,
expression: `true =~ /true/`,
wantError: ErrorKindValidation,
},
{
name: "validation rejects mismatched equality types",
source: upstreamParserSpec,
expression: `1 == "one"`,
wantError: ErrorKindValidation,
},
{
name: "validation rejects boolean string comparison",
source: upstreamParserSpec,
expression: `true == "true"`,
wantError: ErrorKindValidation,
},
{
name: "validation rejects includes on non array",
source: upstreamParserSpec,
expression: `"not-an-array" includes "one"`,
wantError: ErrorKindValidation,
},
{
name: "validation rejects invalid enum literal",
source: upstreamBuildConditionSpec,
expression: `build.state == "faillled"`,
wantError: ErrorKindValidation,
},
{
name: "validation rejects escaped static dollar enum literal",
source: upstreamConditionalGrammar,
expression: `build.state == "\\\$STATE"`,
wantError: ErrorKindValidation,
},
{
name: "validation rejects reversed enum comparison",
source: upstreamParserSpec,
expression: `"faillled" == build.state`,
wantError: ErrorKindValidation,
},
{
name: "validation rejects enum in string regex position",
source: upstreamParserSpec,
expression: `build.state =~ /pass/`,
wantError: ErrorKindValidation,
},
{
name: "validation rejects ternary branch type mismatch",
source: upstreamParserSpec,
expression: `(true ? "string" : 123) == null`,
wantError: ErrorKindValidation,
},
}
runValidateCases(t, tests)
}
func TestConditionalSyntaxEvaluationErrors(t *testing.T) {
tests := []evaluateCase{
{
name: "evaluation rejects local-only at greater operator at parse time",
source: upstreamParserSpec,
expression: `["main"] @> build.branch`,
ctx: Context{
EntryPoint: EntryPointBuildCondition,
Build: Build{Branch: str("main")},
},
wantError: ErrorKindParse,
},
{
name: "evaluation rejects literal unsupported Buildkite env",
source: upstreamBuildConditionSpec,
expression: `build.env("BUILDKITE_AGENT_ACCESS_TOKEN") == null`,
ctx: Context{EntryPoint: EntryPointBuildCondition},
wantError: ErrorKindValidation,
},
}
runEvaluateCases(t, tests)
}