diff --git a/src/parser.ts b/src/parser.ts index d02c2972a..02f67f8ac 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -134,6 +134,9 @@ export class Parser { // Check job variables for invalid hash of key value pairs, and cast numbers to strings Utils.forEachRealJob(gitlabData, (jobName, jobData) => { + assert(jobData.when !== "never", + chalk`This GitLab CI configuration is invalid: jobs:${jobName} when:never can only be used in a rules section or workflow:rules` + ); for (const [key, _value] of Object.entries(jobData.variables || {})) { let value = _value; if (value === null) value = ""; // variable's values are nullable diff --git a/tests/test-cases/when/.gitlab-ci.yml b/tests/test-cases/when/.gitlab-ci.yml new file mode 100644 index 000000000..ddd34da85 --- /dev/null +++ b/tests/test-cases/when/.gitlab-ci.yml @@ -0,0 +1,6 @@ +--- +test-job: + stage: deploy + when: never + script: + - echo "test" diff --git a/tests/test-cases/when/integration.when.test.ts b/tests/test-cases/when/integration.when.test.ts new file mode 100644 index 000000000..571848fd0 --- /dev/null +++ b/tests/test-cases/when/integration.when.test.ts @@ -0,0 +1,25 @@ +import {WriteStreamsMock} from "../../../src/write-streams"; +import {handler} from "../../../src/handler"; +import {initSpawnSpy} from "../../mocks/utils.mock"; +import {WhenStatics} from "../../mocks/when-statics"; +import {AssertionError} from "assert"; +import {assert} from "console"; + +beforeAll(() => { + initSpawnSpy(WhenStatics.all); +}); + +test("when", async () => { + try { + const writeStreams = new WriteStreamsMock(); + await handler({ + cwd: "tests/test-cases/when", + }, writeStreams); + } catch (e: any) { + assert(e instanceof AssertionError, "e is not instanceof AssertionError"); + expect(e.message).toContain("This GitLab CI configuration is invalid: jobs:test-job when:never can only be used in a rules section or workflow:rules"); + return; + } + + throw new Error("Error is expected but not thrown/caught"); +});