Skip to content

Commit c92bf7f

Browse files
authored
feat: config nested array up to 10 level deep (#1386)
1 parent d0a0884 commit c92bf7f

8 files changed

+162
-16
lines changed

src/data-expander.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,10 @@ export function globalVariables (gitlabData: any) {
227227
}
228228

229229
export function flattenLists (gitlabData: any) {
230-
traverse(gitlabData, ({parent, key, value}) => {
230+
traverse(gitlabData, ({parent, key, value, meta}) => {
231231
if (parent != null && key != null && Array.isArray(value)) {
232-
parent[key] = value.flat(5);
232+
parent[key] = value.flat(9);
233+
assert(!parent[key].some(Array.isArray), chalk`This Gitlab CI configuration is invalid: {blueBright ${meta.nodePath}} config should be string or a nested array of strings up to 10 level deep`);
233234
}
234-
}, {cycleHandling: false});
235+
}, {cycleHandling: true});
235236
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
.script1:
3+
- echo test
4+
.script2:
5+
- !reference [.script1]
6+
.script3:
7+
- !reference [.script2]
8+
.script4:
9+
- !reference [.script3]
10+
.script5:
11+
- !reference [.script4]
12+
.script6:
13+
- !reference [.script5]
14+
.script7:
15+
- !reference [.script6]
16+
.script8:
17+
- !reference [.script7]
18+
.script9:
19+
- !reference [.script8]
20+
test:
21+
image: alpine
22+
script:
23+
- !reference [.script9]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
.script1:
3+
- echo test
4+
.script2:
5+
- !reference [.script1]
6+
.script3:
7+
- !reference [.script2]
8+
.script4:
9+
- !reference [.script3]
10+
.script5:
11+
- !reference [.script4]
12+
.script6:
13+
- !reference [.script5]
14+
.script7:
15+
- !reference [.script6]
16+
.script8:
17+
- !reference [.script7]
18+
.script9:
19+
- !reference [.script8]
20+
.script10:
21+
- !reference [.script9]
22+
test:
23+
image: alpine
24+
script:
25+
- !reference [.script10]

tests/test-cases/reference/integration.reference.test.ts

+42
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,45 @@ normal_job:
110110
- echo Hello from \${CI_JOB_NAME}`;
111111
expect(writeStreams.stdoutLines[0]).toEqual(expected);
112112
});
113+
114+
115+
test("should support 10 level deep", async () => {
116+
const writeStreams = new WriteStreamsMock();
117+
await handler({
118+
preview: true,
119+
file: ".gitlab-ci-10-level-deep.yml",
120+
cwd: "tests/test-cases/reference",
121+
}, writeStreams);
122+
123+
const expected = `
124+
---
125+
stages:
126+
- .pre
127+
- build
128+
- test
129+
- deploy
130+
- .post
131+
test:
132+
image:
133+
name: alpine
134+
script:
135+
- echo test
136+
`;
137+
138+
expect(writeStreams.stdoutLines.join("\n")).toEqual(expected.trim());
139+
});
140+
141+
test("should not support 11 level deep", async () => {
142+
try {
143+
const writeStreams = new WriteStreamsMock();
144+
await handler({
145+
noColor: true,
146+
file: ".gitlab-ci-11-level-deep.yml",
147+
cwd: "tests/test-cases/reference",
148+
}, writeStreams);
149+
} catch (e: any) {
150+
expect(e.message).toEqual("This Gitlab CI configuration is invalid: test.script config should be string or a nested array of strings up to 10 level deep");
151+
return;
152+
}
153+
throw new Error("Error is expected but not thrown/caught");
154+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
test-job:
3+
script:
4+
- echo 1
5+
- - echo 2
6+
- - - echo 3
7+
- - - - echo 4
8+
- - - - - echo 5
9+
- - - - - - echo 6
10+
- - - - - - - echo 7
11+
- - - - - - - - echo 8
12+
- - - - - - - - - echo 9
13+
- - - - - - - - - - echo 10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
test-job:
3+
script:
4+
- echo 1
5+
- - echo 2
6+
- - - echo 3
7+
- - - - echo 4
8+
- - - - - echo 5
9+
- - - - - - echo 6
10+
- - - - - - - echo 7
11+
- - - - - - - - echo 8
12+
- - - - - - - - - echo 9
13+
- - - - - - - - - - echo 10
14+
- - - - - - - - - - - echo 11

tests/test-cases/script-multidimension/.gitlab-ci.yml

-5
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,56 @@
11
import {WriteStreamsMock} from "../../../src/write-streams.js";
22
import {handler} from "../../../src/handler.js";
3-
import chalk from "chalk";
43
import {initSpawnSpy} from "../../mocks/utils.mock.js";
54
import {WhenStatics} from "../../mocks/when-statics.js";
65

76
beforeAll(() => {
87
initSpawnSpy(WhenStatics.all);
98
});
109

11-
test("script-multidimension <test-job>", async () => {
10+
test("should support 10 level deep", async () => {
1211
const writeStreams = new WriteStreamsMock();
1312
await handler({
13+
preview: true,
14+
file: ".gitlab-ci-10-level-deep.yml",
1415
cwd: "tests/test-cases/script-multidimension",
15-
job: ["test-job"],
1616
}, writeStreams);
1717

18-
const expected = [
19-
chalk`{blueBright test-job} {greenBright >} Test something`,
20-
chalk`{blueBright test-job} {greenBright >} Test something else`,
21-
];
22-
expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining(expected));
18+
const expected = `
19+
---
20+
stages:
21+
- .pre
22+
- build
23+
- test
24+
- deploy
25+
- .post
26+
test-job:
27+
script:
28+
- echo 1
29+
- echo 2
30+
- echo 3
31+
- echo 4
32+
- echo 5
33+
- echo 6
34+
- echo 7
35+
- echo 8
36+
- echo 9
37+
- echo 10
38+
`;
39+
40+
expect(writeStreams.stdoutLines.join("\n")).toEqual(expected.trim());
41+
});
42+
43+
test("should not support 11 level deep", async () => {
44+
try {
45+
const writeStreams = new WriteStreamsMock();
46+
await handler({
47+
noColor: true,
48+
file: ".gitlab-ci-11-level-deep.yml",
49+
cwd: "tests/test-cases/script-multidimension",
50+
}, writeStreams);
51+
} catch (e: any) {
52+
expect(e.message).toEqual("This Gitlab CI configuration is invalid: test-job.script config should be string or a nested array of strings up to 10 level deep");
53+
return;
54+
}
55+
throw new Error("Error is expected but not thrown/caught");
2356
});

0 commit comments

Comments
 (0)