Skip to content

Commit b08e503

Browse files
ApoorvaMohanemxxapo
andauthored
fix: validate spec:inputs:regex instead of logging a no-op warning (#1869)
Co-authored-by: emxxapo <apoorva.m@ericsson.com>
1 parent 221bdab commit b08e503

8 files changed

Lines changed: 113 additions & 1 deletion

File tree

src/parser.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,14 @@ function validateInput (ctx: any) {
443443

444444
const regex = inputsSpecification.spec.inputs[interpolationKey]?.regex;
445445
if (regex) {
446-
ctx.writeStreams?.stderr(chalk`{black.bgYellowBright WARN } spec:inputs:regex is currently not supported via gitlab-ci-local. This will just be a no-op.\n`);
446+
let re: RegExp;
447+
try {
448+
re = new RegExp(regex);
449+
} catch {
450+
assert(false, chalk`This GitLab CI configuration is invalid: \`{blueBright ${configFilePath}}\`: \`{blueBright ${interpolationKey}}\` input: regex \`{blueBright ${regex}}\` is not a valid regular expression.`);
451+
}
452+
assert(re.test(String(inputValue)),
453+
chalk`This GitLab CI configuration is invalid: \`{blueBright ${configFilePath}}\`: \`{blueBright ${interpolationKey}}\` input: \`{blueBright ${inputValue}}\` does not match required regex: {blueBright ${regex}}.`);
447454
}
448455
}
449456

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
spec:
3+
inputs:
4+
version:
5+
regex: ^[unclosed
6+
---
7+
deploy:
8+
script:
9+
- echo $[[ inputs.version ]]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
include:
3+
- local: '/.gitlab-ci-input-template.yml'
4+
inputs:
5+
version: "v1.2.3"
6+
stages:
7+
- test
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
spec:
3+
inputs:
4+
version:
5+
regex: ^v\d+\.\d+\.\d+$
6+
---
7+
deploy:
8+
script:
9+
- echo $[[ inputs.version ]]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
include:
3+
- local: '/.gitlab-ci-input-template.yml'
4+
inputs:
5+
version: "v1.2.3"
6+
stages:
7+
- test
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
spec:
3+
inputs:
4+
version:
5+
regex: ^v\d+\.\d+\.\d+$
6+
---
7+
deploy:
8+
script:
9+
- echo $[[ inputs.version ]]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
include:
3+
- local: '/.gitlab-ci-input-template.yml'
4+
inputs:
5+
version: "invalid-version"
6+
stages:
7+
- test

tests/test-cases/include-inputs/integration.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,60 @@ scan-website:
358358

359359
expect(writeStreams.stdoutLines[0]).toEqual(expected);
360360
});
361+
362+
test.concurrent("include-inputs regex validation (invalid)", async () => {
363+
try {
364+
const writeStreams = new WriteStreamsMock();
365+
await handler({
366+
cwd: "tests/test-cases/include-inputs/input-templates/regex-validation",
367+
preview: true,
368+
}, writeStreams);
369+
} catch (e: any) {
370+
assert(e instanceof AssertionError, "e is not instanceof AssertionError");
371+
expect(e.message).toContain("This GitLab CI configuration is invalid:");
372+
expect(e.message).toContain(
373+
chalk`\`{blueBright version}\` input: \`{blueBright invalid-version}\` does not match required regex: {blueBright ^v\\d+\\.\\d+\\.\\d+$}.`,
374+
);
375+
return;
376+
}
377+
378+
throw new Error("Error is expected but not thrown/caught");
379+
});
380+
381+
test.concurrent("include-inputs regex validation (valid)", async () => {
382+
const writeStreams = new WriteStreamsMock();
383+
await handler({
384+
cwd: "tests/test-cases/include-inputs/input-templates/regex-validation-pass",
385+
preview: true,
386+
}, writeStreams);
387+
388+
const expected = `---
389+
stages:
390+
- .pre
391+
- test
392+
- .post
393+
deploy:
394+
script:
395+
- echo v1.2.3`;
396+
397+
expect(writeStreams.stdoutLines[0]).toEqual(expected);
398+
});
399+
400+
test.concurrent("include-inputs regex validation (invalid pattern)", async () => {
401+
try {
402+
const writeStreams = new WriteStreamsMock();
403+
await handler({
404+
cwd: "tests/test-cases/include-inputs/input-templates/regex-validation-invalid-pattern",
405+
preview: true,
406+
}, writeStreams);
407+
} catch (e: any) {
408+
assert(e instanceof AssertionError, "e is not instanceof AssertionError");
409+
expect(e.message).toContain("This GitLab CI configuration is invalid:");
410+
expect(e.message).toContain(
411+
chalk`\`{blueBright version}\` input: regex \`{blueBright ^[unclosed}\` is not a valid regular expression.`,
412+
);
413+
return;
414+
}
415+
416+
throw new Error("Error is expected but not thrown/caught");
417+
});

0 commit comments

Comments
 (0)