-
Notifications
You must be signed in to change notification settings - Fork 210
Expand file tree
/
Copy pathrules-regex.test.ts
More file actions
196 lines (186 loc) · 7.12 KB
/
Copy pathrules-regex.test.ts
File metadata and controls
196 lines (186 loc) · 7.12 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
import {vi} from "vitest";
import "../src/global.js";
import {Argv} from "../src/argv";
import {Utils} from "../src/utils";
import {GitData} from "../src/git-data";
import {WriteStreamsMock} from "../src/write-streams";
let writeStreams;
let gitData: GitData;
let argv: Argv;
beforeEach(async () => {
writeStreams = new WriteStreamsMock();
gitData = await GitData.init("tests", writeStreams);
argv = await Argv.build({}, writeStreams);
vi.restoreAllMocks();
});
/* eslint-disable @stylistic/quotes */
const tests = [
{
rule: '"Hello World" =~ "/hello world/i"',
jsExpression: '"Hello World".matchRE2JS(RE2JS.compile("hello world", 1)) != null',
evalResult: true,
},
{
rule: '"Hello World" =~ /hello world/i',
jsExpression: '"Hello World".matchRE2JS(RE2JS.compile("hello world", 1)) != null',
evalResult: true,
},
{
rule: '"Hello World" =~ /Hello (?i)world/',
jsExpression: '"Hello World".matchRE2JS(RE2JS.compile("Hello (?i)world", 0)) != null',
evalResult: true,
},
{
rule: '"1.11" =~ /^([[:digit:]]+(.[[:digit:]]+)*|latest)$/',
jsExpression: '"1.11".matchRE2JS(RE2JS.compile("^([[:digit:]]+(.[[:digit:]]+)*|latest)$", 0)) != null',
evalResult: true,
},
{
rule: '"foo" !~ /foo/',
jsExpression: '"foo".matchRE2JS(RE2JS.compile("foo", 0)) == null',
evalResult: false,
},
{
rule: '"foo" =~ /foo/',
jsExpression: '"foo".matchRE2JS(RE2JS.compile("foo", 0)) != null',
evalResult: true,
},
{
rule: '"foo"=~ /foo/',
jsExpression: '"foo".matchRE2JS(RE2JS.compile("foo", 0)) != null',
evalResult: true,
},
{
rule: '"foo"=~/foo/',
jsExpression: '"foo".matchRE2JS(RE2JS.compile("foo", 0)) != null',
evalResult: true,
},
{
rule: '"foo"=~ /foo/',
jsExpression: '"foo".matchRE2JS(RE2JS.compile("foo", 0)) != null',
evalResult: true,
},
{
rule: '"foo" =~ "/foo/"',
jsExpression: '"foo".matchRE2JS(RE2JS.compile("foo", 0)) != null',
evalResult: true,
},
{
rule: '"test/url" =~ "/test/ur/"',
jsExpression: '"test/url".matchRE2JS(RE2JS.compile("test/ur", 0)) != null',
evalResult: true,
},
{
rule: '"test/url" =~ "/test\\/ur/"',
jsExpression: '"test/url".matchRE2JS(RE2JS.compile("test\\/ur", 0)) != null',
evalResult: true,
},
{
rule: '"test/url" =~ /test/ur/',
expectedErrSubStr: "Error attempting to evaluate the following rules:",
},
{
rule: '"master" =~ /master$/',
jsExpression: '"master".matchRE2JS(RE2JS.compile("master$", 0)) != null',
evalResult: true,
},
{
rule: '"23" =~ "1234"',
expectedErrSubStr: "must be a regex pattern. Do not rely on this behavior!",
},
{
rule: '"23" =~ \'1234\'',
expectedErrSubStr: "must be a regex pattern. Do not rely on this behavior!",
},
{
rule: '"23" =~ /1234/',
jsExpression: '"23".matchRE2JS(RE2JS.compile("1234", 0)) != null',
evalResult: false,
},
{
rule: '$CI_COMMIT_BRANCH && $GITLAB_FEATURES =~ /\bdependency_scanning\b/ && $CI_GITLAB_FIPS_MODE == "true"',
jsExpression: 'null && false && null == "true"',
evalResult: false,
},
{
rule: '($CI_MERGE_REQUEST_SOURCE_BRANCH_NAME =~ /^perf_.*$/)',
jsExpression: '(false)', // (null.matchRE2JS(RE2JS.compile("^perf_.*$", 0)) != null => (false)
evalResult: false,
},
{
rule: '("qwerty" =~ /^perf_.*$/)',
jsExpression: '("qwerty".matchRE2JS(RE2JS.compile("^perf_.*$", 0)) != null)',
evalResult: false,
},
{
rule: `"product-name/v0.0.0+build.0" =~ /^(?:product-name\\/)?v\\d+\\.\\d+\\.\\d+.*/`,
jsExpression: "\"product-name/v0.0.0+build.0\".matchRE2JS(RE2JS.compile(\"^(?:product-name\\\\/)?v\\\\d+\\\\.\\\\d+\\\\.\\\\d+.*\", 0)) != null",
evalResult: true,
},
];
/* eslint-enable @stylistic/quotes */
describe("gitlab rules regex", () => {
tests.filter(t => !t.expectedErrSubStr)
.forEach((t) => {
test(`- if: '${t.rule}'\n\t => ${t.evalResult}`, async () => {
const rules = [ {if: t.rule} ];
const evalSpy = vi.spyOn(global, "eval");
const evaluateRuleIfSpy = vi.spyOn(Utils, "evaluateRuleIf");
Utils.getRulesResult({argv, cwd: "", rules, variables: {}}, gitData);
expect(evaluateRuleIfSpy).toHaveReturnedWith(t.evalResult);
expect(evalSpy).toHaveBeenCalledWith(t.jsExpression);
});
});
});
/* eslint-disable @stylistic/quotes */
// A `$VAR` referenced *inside* a regex literal on the RHS of `=~`/`!~` must be
// substituted with its raw value, not a quoted JS string literal.
const variableInRegexTests: {rule: string; envs: {[key: string]: string}; jsExpression: string; evalResult: boolean}[] = [
{
// Drupal gitlab_templates: include.drupalci.main.yml
rule: "$CORE_PHP_MIN == $CORE_PHP_MAX && ($PHP_VERSION =~ '/^\\$_TARGET_PHP$/' || $PHP_VERSION =~ '/^\\$CORE_PHP_MAX$/')",
envs: {CORE_PHP_MIN: "8.3", CORE_PHP_MAX: "8.3", _TARGET_PHP: "8.3", PHP_VERSION: "8.3"},
jsExpression: '"8.3" == "8.3" && ("8.3".matchRE2JS(RE2JS.compile("^8.3$", 0)) != null || "8.3".matchRE2JS(RE2JS.compile("^8.3$", 0)) != null)',
evalResult: true,
},
{
// bare $VAR inside a bare regex literal
rule: "$CI_COMMIT_BRANCH =~ /$BRANCHNAME/",
envs: {CI_COMMIT_BRANCH: "master", BRANCHNAME: "master"},
jsExpression: '"master".matchRE2JS(RE2JS.compile("master", 0)) != null',
evalResult: true,
},
{
// RHS is a variable that *holds* a regex literal — must still work
rule: "$TAG =~ $TAG_REGEX",
envs: {TAG: "prefix/1.0.0", TAG_REGEX: "/^prefix\\/.+/"},
jsExpression: '"prefix/1.0.0".matchRE2JS(RE2JS.compile("^prefix\\/.+", 0)) != null',
evalResult: true,
},
];
/* eslint-enable @stylistic/quotes */
describe("gitlab rules regex with variable interpolation", () => {
variableInRegexTests.forEach((t) => {
test(`- if: '${t.rule}'\n\t => ${t.evalResult}`, () => {
const evalSpy = vi.spyOn(global, "eval");
const res = Utils.evaluateRuleIf(t.rule, t.envs);
expect(res).toBe(t.evalResult);
expect(evalSpy).toHaveBeenCalledWith(t.jsExpression);
});
});
});
describe("gitlab rules regex [invalid]", () => {
tests.filter(t => t.expectedErrSubStr)
.forEach((t) => {
test(`- if: '${t.rule}'\n\t to throws error that contains \`${t.expectedErrSubStr}\``, async () => {
const rules = [ {if: t.rule} ];
try {
Utils.getRulesResult({argv, cwd: "", rules, variables: {}}, gitData);
} catch (e: any) {
expect(e.message).toContain(t.expectedErrSubStr);
return;
}
throw new Error("Error is expected but not thrown/caught");
});
});
});