Skip to content

Commit fc33502

Browse files
authored
fix: handle edge cases of jobs:when where the value never is considered invalid (#1329)
1 parent 82b8f88 commit fc33502

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/parser.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ export class Parser {
134134

135135
// Check job variables for invalid hash of key value pairs, and cast numbers to strings
136136
Utils.forEachRealJob(gitlabData, (jobName, jobData) => {
137+
assert(jobData.when !== "never",
138+
chalk`This GitLab CI configuration is invalid: jobs:${jobName} when:never can only be used in a rules section or workflow:rules`
139+
);
137140
for (const [key, _value] of Object.entries(jobData.variables || {})) {
138141
let value = _value;
139142
if (value === null) value = ""; // variable's values are nullable
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
test-job:
3+
stage: deploy
4+
when: never
5+
script:
6+
- echo "test"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import {WriteStreamsMock} from "../../../src/write-streams";
2+
import {handler} from "../../../src/handler";
3+
import {initSpawnSpy} from "../../mocks/utils.mock";
4+
import {WhenStatics} from "../../mocks/when-statics";
5+
import {AssertionError} from "assert";
6+
import {assert} from "console";
7+
8+
beforeAll(() => {
9+
initSpawnSpy(WhenStatics.all);
10+
});
11+
12+
test("when", async () => {
13+
try {
14+
const writeStreams = new WriteStreamsMock();
15+
await handler({
16+
cwd: "tests/test-cases/when",
17+
}, writeStreams);
18+
} catch (e: any) {
19+
assert(e instanceof AssertionError, "e is not instanceof AssertionError");
20+
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");
21+
return;
22+
}
23+
24+
throw new Error("Error is expected but not thrown/caught");
25+
});

0 commit comments

Comments
 (0)