Skip to content

Commit cc3b886

Browse files
authored
fix: reject non-array needs with clear error (#1863)
1 parent 1206342 commit cc3b886

5 files changed

Lines changed: 57 additions & 0 deletions

File tree

src/data-expander.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,10 @@ export function needsComplex (data: any) {
129129
export function needsEach (jobName: string, gitlabData: any) {
130130
const jobData = gitlabData[jobName];
131131
if (!jobData.needs) return;
132+
assert(Array.isArray(jobData.needs), chalk`{blueBright ${jobName}} {yellow needs:} must be an array`);
132133

133134
for (const [i, n] of Object.entries<any>(jobData.needs)) {
135+
assert(n != null, chalk`{blueBright ${jobName}} {yellow needs:} entries cannot be empty`);
134136
jobData.needs[i] = needsComplex(n);
135137
}
136138
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
build-job:
3+
script:
4+
- echo "build"
5+
6+
dev-job:
7+
needs:
8+
- ~
9+
script:
10+
- echo "dev"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {WriteStreamsMock} from "../../../src/write-streams.js";
2+
import {handler} from "../../../src/handler.js";
3+
import {initSpawnSpy} from "../../mocks/utils.mock.js";
4+
import {WhenStatics} from "../../mocks/when-statics.js";
5+
import chalk from "chalk-template";
6+
7+
beforeAll(() => {
8+
initSpawnSpy(WhenStatics.all);
9+
});
10+
11+
test.concurrent("needs-empty-entry <dev-job>", async () => {
12+
const writeStreams = new WriteStreamsMock();
13+
14+
await expect(handler({
15+
cwd: "tests/test-cases/needs-empty-entry",
16+
stateDir: ".gitlab-ci-local-needs-empty-entry",
17+
}, writeStreams)).rejects.toThrow(chalk`{blueBright dev-job} {yellow needs:} entries cannot be empty`);
18+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
build-job:
3+
script:
4+
- echo "build"
5+
6+
dev-job:
7+
needs: build-job
8+
script:
9+
- echo "dev"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {WriteStreamsMock} from "../../../src/write-streams.js";
2+
import {handler} from "../../../src/handler.js";
3+
import {initSpawnSpy} from "../../mocks/utils.mock.js";
4+
import {WhenStatics} from "../../mocks/when-statics.js";
5+
import chalk from "chalk-template";
6+
7+
beforeAll(() => {
8+
initSpawnSpy(WhenStatics.all);
9+
});
10+
11+
test.concurrent("needs-not-array <dev-job>", async () => {
12+
const writeStreams = new WriteStreamsMock();
13+
14+
await expect(handler({
15+
cwd: "tests/test-cases/needs-not-array",
16+
stateDir: ".gitlab-ci-local-needs-not-array",
17+
}, writeStreams)).rejects.toThrow(chalk`{blueBright dev-job} {yellow needs:} must be an array`);
18+
});

0 commit comments

Comments
 (0)