Skip to content

Commit 6d5df5f

Browse files
rufoosfirecow
andauthored
fix: substitute variable in include:file if it's array of files (#1579)
* fix: substitute variable in include:file if it's array of files * Update .gitlab-ci-local-variables.yml --------- Co-authored-by: Mads Jon Nielsen <[email protected]>
1 parent c6ae765 commit 6d5df5f

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

src/parser-includes.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,11 @@ export class ParserIncludes {
219219

220220
for (const entry of include) {
221221
for (const [key, value] of Object.entries(entry)) {
222-
entry[key] = Utils.expandText(value, variables);
222+
if (Array.isArray(value)) {
223+
entry[key] = value.map((v) => Utils.expandText(v, variables));
224+
} else {
225+
entry[key] = Utils.expandText(value, variables);
226+
}
223227
}
224228
}
225229

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
FILE_NAME: "test-file"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
include:
3+
- project: test-group/gitlab-ci-local-test
4+
file:
5+
- $FILE_NAME.yml
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {WriteStreamsMock} from "../../../src/write-streams.js";
2+
import {handler} from "../../../src/handler.js";
3+
import chalk from "chalk";
4+
import {initBashSpy, initSpawnSpy} from "../../mocks/utils.mock.js";
5+
import {WhenStatics} from "../../mocks/when-statics.js";
6+
import fs from "fs-extra";
7+
8+
beforeAll(() => {
9+
initSpawnSpy(WhenStatics.all);
10+
});
11+
12+
test("include-project-file-with-variable test-job", async () => {
13+
const cwd = "tests/test-cases/include-project-file-with-variable";
14+
15+
await fs.rm(`${cwd}/.gitlab-ci-local/`, {recursive: true, force: true});
16+
17+
const spyGitRemote = {
18+
cmdArgs: ["git", "remote", "get-url", "origin"],
19+
returnValue: {stdout: "[email protected]:test-group/gitlab-ci-local-test.git"},
20+
};
21+
initSpawnSpy([...WhenStatics.all, spyGitRemote]);
22+
23+
const target = ".gitlab-ci-local/includes/gitlab.com/test-group/gitlab-ci-local-test/HEAD/";
24+
const spyGitArchive1 = {
25+
cmd: `git archive --remote=ssh://[email protected]:22/test-group/gitlab-ci-local-test.git HEAD test-file.yml | tar -f - -xC ${target}`,
26+
returnValue: {output: ""},
27+
};
28+
initBashSpy([spyGitArchive1]);
29+
30+
const mock = `${cwd}/mock-test-file.yml`;
31+
const mockTarget = `${cwd}/.gitlab-ci-local/includes/gitlab.com/test-group/gitlab-ci-local-test/HEAD/test-file.yml`;
32+
await fs.ensureFile(mockTarget);
33+
await fs.copyFile(mock, mockTarget);
34+
35+
const writeStreams = new WriteStreamsMock();
36+
await handler({
37+
cwd,
38+
job: ["test-job"],
39+
}, writeStreams);
40+
41+
const expected = [
42+
chalk`{blueBright test-job} {greenBright >} Hello from test file`,
43+
];
44+
expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining(expected));
45+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
test-job:
3+
stage: test
4+
script:
5+
- echo "Hello from test file"

0 commit comments

Comments
 (0)