Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,14 @@ function validateInput (ctx: any) {

const regex = inputsSpecification.spec.inputs[interpolationKey]?.regex;
if (regex) {
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`);
let re: RegExp;
try {
re = new RegExp(regex);
} catch {
assert(false, chalk`This GitLab CI configuration is invalid: \`{blueBright ${configFilePath}}\`: \`{blueBright ${interpolationKey}}\` input: regex \`{blueBright ${regex}}\` is not a valid regular expression.`);
}
assert(re.test(String(inputValue)),
chalk`This GitLab CI configuration is invalid: \`{blueBright ${configFilePath}}\`: \`{blueBright ${interpolationKey}}\` input: \`{blueBright ${inputValue}}\` does not match required regex: {blueBright ${regex}}.`);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
spec:
inputs:
version:
regex: ^[unclosed
---
deploy:
script:
- echo $[[ inputs.version ]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
include:
- local: '/.gitlab-ci-input-template.yml'
inputs:
version: "v1.2.3"
stages:
- test
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
spec:
inputs:
version:
regex: ^v\d+\.\d+\.\d+$
---
deploy:
script:
- echo $[[ inputs.version ]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
include:
- local: '/.gitlab-ci-input-template.yml'
inputs:
version: "v1.2.3"
stages:
- test
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
spec:
inputs:
version:
regex: ^v\d+\.\d+\.\d+$
---
deploy:
script:
- echo $[[ inputs.version ]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
include:
- local: '/.gitlab-ci-input-template.yml'
inputs:
version: "invalid-version"
stages:
- test
57 changes: 57 additions & 0 deletions tests/test-cases/include-inputs/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,60 @@ scan-website:

expect(writeStreams.stdoutLines[0]).toEqual(expected);
});

test.concurrent("include-inputs regex validation (invalid)", async () => {
try {
const writeStreams = new WriteStreamsMock();
await handler({
cwd: "tests/test-cases/include-inputs/input-templates/regex-validation",
preview: true,
}, writeStreams);
} catch (e: any) {
assert(e instanceof AssertionError, "e is not instanceof AssertionError");
expect(e.message).toContain("This GitLab CI configuration is invalid:");
expect(e.message).toContain(
chalk`\`{blueBright version}\` input: \`{blueBright invalid-version}\` does not match required regex: {blueBright ^v\\d+\\.\\d+\\.\\d+$}.`,
);
return;
}

throw new Error("Error is expected but not thrown/caught");
});

test.concurrent("include-inputs regex validation (valid)", async () => {
const writeStreams = new WriteStreamsMock();
await handler({
cwd: "tests/test-cases/include-inputs/input-templates/regex-validation-pass",
preview: true,
}, writeStreams);

const expected = `---
stages:
- .pre
- test
- .post
deploy:
script:
- echo v1.2.3`;

expect(writeStreams.stdoutLines[0]).toEqual(expected);
});

test.concurrent("include-inputs regex validation (invalid pattern)", async () => {
try {
const writeStreams = new WriteStreamsMock();
await handler({
cwd: "tests/test-cases/include-inputs/input-templates/regex-validation-invalid-pattern",
preview: true,
}, writeStreams);
} catch (e: any) {
assert(e instanceof AssertionError, "e is not instanceof AssertionError");
expect(e.message).toContain("This GitLab CI configuration is invalid:");
expect(e.message).toContain(
chalk`\`{blueBright version}\` input: regex \`{blueBright ^[unclosed}\` is not a valid regular expression.`,
);
return;
}

throw new Error("Error is expected but not thrown/caught");
});