Skip to content

Commit a52ad14

Browse files
author
Catherine Larson
committed
cubic-ai PR comment
1 parent f6e96b8 commit a52ad14

7 files changed

Lines changed: 232 additions & 5 deletions

src/utils.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,30 @@ export class Utils {
207207
when = rule.when ? rule.when : jobWhen;
208208
allowFailure = rule.allow_failure ?? allowFailure;
209209
ruleVariable = rule.variables;
210-
matchedRule = rule.if;
211210
ruleNeeds = rule.needs?.map((n: any) => needsComplex(n));
212211

212+
// Build matchedRule string to show which condition(s) matched
213+
const matchedConditions: string[] = [];
214+
if (rule.if) {
215+
matchedConditions.push(rule.if);
216+
}
217+
if (rule.exists) {
218+
const existsStr = Array.isArray(rule.exists)
219+
? `exists: [${rule.exists.join(', ')}]`
220+
: ``;
221+
matchedConditions.push(existsStr);
222+
}
223+
if (rule.changes) {
224+
const changesStr = Array.isArray(rule.changes)
225+
? `changes: [${rule.changes.join(', ')}]`
226+
: ``;
227+
matchedConditions.push(changesStr);
228+
}
229+
230+
// if rule only has 'when', then matchedConditions will have 0 items;
231+
// should not display anything as a matched rule, since 'when' is displayed in its own column
232+
matchedRule = matchedConditions.length > 0 ? matchedConditions.join(' && ') : '';
233+
213234
break; // Early return, will not evaluate the remaining rules
214235
}
215236

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
docker-build:
2+
stage: build
3+
rules:
4+
- if: 1 == 1
5+
changes:
6+
- example/*.js
7+
- other-file.txt
8+
- if: 2 == 2
9+
script:
10+
- docker build
11+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
docker-compose-up:
2+
stage: deploy
3+
environment:
4+
url: http://localhost:8891
5+
name: local
6+
script:
7+
- docker compose up -d
8+
9+
docker-compose-down:
10+
stage: .post
11+
rules:
12+
- if: 0 == 1
13+
- if: 1 == 1
14+
exists:
15+
- integration.test.ts
16+
script:
17+
- docker compose down
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
stages: [test, build]
3+
4+
# @Description Run Tests
5+
test-job:
6+
stage: test
7+
script:
8+
- echo ${GITLAB_USER_LOGIN}
9+
- '>&2 echo "Hello, error!"'
10+
11+
build-job:
12+
stage: build
13+
needs: [test-job]
14+
rules:
15+
- changes:
16+
- .gitlab-ci.yml
17+
- if: 1 == 1
18+
allow_failure: true
19+
script:
20+
- 'echo "Build something"'
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
stages: [test, build]
3+
4+
# @Description Run Tests
5+
test-job:
6+
stage: test
7+
script:
8+
- echo ${GITLAB_USER_LOGIN}
9+
- '>&2 echo "Hello, error!"'
10+
11+
build-job:
12+
stage: build
13+
needs: [test-job]
14+
rules:
15+
- exists:
16+
- integration.test.ts
17+
- .gitlab-ci.yml
18+
- if: 1 == 1
19+
allow_failure: true
20+
script:
21+
- 'echo "Build something"'
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# @Description Build
2+
build-job:
3+
stage: build
4+
script:
5+
- 'echo "Build something"'
6+
7+
test-job:
8+
stage: test
9+
needs: [build-job]
10+
rules:
11+
- when: on_failure
12+
script:
13+
- 'echo "Test something"'

tests/test-cases/list-rule-case/integration.test.ts

Lines changed: 128 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import {handler} from "../../../src/handler.js";
33
import chalk from "chalk-template";
44
import {initSpawnSpy} from "../../mocks/utils.mock.js";
55
import {WhenStatics} from "../../mocks/when-statics.js";
6+
import {vi} from "vitest";
7+
import {GitData} from "../../../src/git-data.js";
68

79
beforeAll(() => {
810
initSpawnSpy(WhenStatics.all);
911
});
1012

11-
test.concurrent("list-rule-case --list-rule", async () => {
13+
test.concurrent("list-rule-case --list-rule single rule", async () => {
1214
const writeStreams = new WriteStreamsMock();
1315
await handler({
1416
cwd: "tests/test-cases/list-rule-case/",
@@ -21,7 +23,6 @@ test.concurrent("list-rule-case --list-rule", async () => {
2123
chalk`{blueBright test-job } Run Tests {yellow test } on_success false `,
2224
chalk`{blueBright build-job} {yellow build} on_success true {yellow 1 == 1}`,
2325
];
24-
console.log(writeStreams.stdoutLines)
2526
expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining(expected));
2627
});
2728

@@ -39,6 +40,129 @@ test.concurrent("list-rule-case --list-rule verify that correct matching rule is
3940
chalk`{blueBright test-job } Run Tests {yellow test } on_success false `,
4041
chalk`{blueBright build-job} {yellow build} on_success true {yellow 1 == 1}`,
4142
];
42-
console.log(writeStreams.stdoutLines)
4343
expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining(expected));
44-
});
44+
});
45+
46+
test.concurrent("list-rule-case --list-rule verify that matching 'exists' rule is displayed", async () => {
47+
const writeStreams = new WriteStreamsMock();
48+
await handler({
49+
cwd: "tests/test-cases/list-rule-case/",
50+
listRule: true,
51+
file: ".gitlab-ci-match-exists-rule.yml",
52+
stateDir: ".gitlab-ci-match-exists-rule",
53+
}, writeStreams);
54+
55+
const expected = [
56+
chalk`{grey name description} {grey stage when } {grey allow_failure rule}`,
57+
chalk`{blueBright test-job } Run Tests {yellow test } on_success false `,
58+
chalk`{blueBright build-job} {yellow build} on_success true {yellow exists: [integration.test.ts, .gitlab-ci.yml]}`,
59+
];
60+
expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining(expected));
61+
});
62+
63+
test("list-rule-case --list-rule --evaluate-rule-changes verify that matching 'changes' rule is displayed", async () => {
64+
const writeStreams = new WriteStreamsMock();
65+
66+
// Mock GitData.changedFiles to return files that match test case
67+
const gitDataSpy = vi.spyOn(GitData, "changedFiles");
68+
gitDataSpy.mockReturnValue([".gitlab-ci.yml"]);
69+
70+
await handler({
71+
cwd: "tests/test-cases/list-rule-case/",
72+
listRule: true,
73+
evaluateRuleChanges: true,
74+
file: ".gitlab-ci-match-changes-rule.yml",
75+
stateDir: ".gitlab-ci-match-changes-rule",
76+
}, writeStreams);
77+
78+
const expected = [
79+
chalk`{grey name description} {grey stage when } {grey allow_failure rule}`,
80+
chalk`{blueBright test-job } Run Tests {yellow test } on_success false `,
81+
chalk`{blueBright build-job} {yellow build} on_success true {yellow changes: [.gitlab-ci.yml]}`,
82+
];
83+
expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining(expected));
84+
85+
gitDataSpy.mockRestore();
86+
});
87+
88+
test.concurrent("list-rule-case --list-rule verify that 'when' rule doesn't display", async () => {
89+
const writeStreams = new WriteStreamsMock();
90+
await handler({
91+
cwd: "tests/test-cases/list-rule-case/",
92+
listRule: true,
93+
file: ".gitlab-ci-when-rule.yml",
94+
stateDir: ".gitlab-ci-when-rule",
95+
}, writeStreams);
96+
97+
const expected = [
98+
chalk`{grey name description} {grey stage when } {grey allow_failure rule}`,
99+
chalk`{blueBright build-job} Build {yellow build } on_success false `,
100+
chalk`{blueBright test-job } {yellow test } on_failure false `,
101+
];
102+
expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining(expected));
103+
});
104+
105+
test.concurrent("list-rule-case --list-rule verify that job combining 'if' and 'exists' displays properly", async () => {
106+
const writeStreams = new WriteStreamsMock();
107+
await handler({
108+
cwd: "tests/test-cases/list-rule-case/",
109+
listRule: true,
110+
file: ".gitlab-ci-if-and-exists.yml",
111+
stateDir: ".gitlab-ci-if-and-exists",
112+
}, writeStreams);
113+
114+
const expected = [
115+
chalk`{grey name description} {grey stage when } {grey allow_failure rule}`,
116+
chalk`{blueBright docker-compose-up } {yellow deploy} on_success false `,
117+
chalk`{blueBright docker-compose-down} {yellow .post } on_success false {yellow 1 == 1 && exists: [integration.test.ts]}`,
118+
];
119+
expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining(expected));
120+
});
121+
122+
test.concurrent("list-rule-case --list-rule --evaluate-rule-changes verify that job combining 'if' and 'changes' displays properly", async () => {
123+
const writeStreams = new WriteStreamsMock();
124+
125+
const gitDataSpy = vi.spyOn(GitData, "changedFiles");
126+
gitDataSpy.mockReturnValue(["example/fake-file.js", "other-file.txt", "one-more-file.json"]);
127+
128+
await handler({
129+
cwd: "tests/test-cases/list-rule-case/",
130+
listRule: true,
131+
evaluateRuleChanges: true,
132+
file: ".gitlab-ci-if-and-changes.yml",
133+
stateDir: ".gitlab-ci-if-and-changes",
134+
}, writeStreams);
135+
136+
const expected = [
137+
chalk`{grey name description} {grey stage when } {grey allow_failure rule}`,
138+
chalk`{blueBright docker-build} {yellow build } on_success false {yellow 1 == 1 && changes: [example/*.js, other-file.txt]}`,
139+
];
140+
expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining(expected));
141+
142+
gitDataSpy.mockRestore();
143+
144+
});
145+
146+
test("list-rule-case --list-rule --evaluate-rule-changes verify that if changes weren't made, rule doesn't display", async () => {
147+
const writeStreams = new WriteStreamsMock();
148+
149+
const gitDataSpy = vi.spyOn(GitData, "changedFiles");
150+
gitDataSpy.mockReturnValue(["non-matching-file.js", "other-non-matching-file.txt"]);
151+
152+
await handler({
153+
cwd: "tests/test-cases/list-rule-case/",
154+
listRule: true,
155+
evaluateRuleChanges: true,
156+
file: ".gitlab-ci-if-and-changes.yml",
157+
stateDir: ".gitlab-ci-if-and-changes",
158+
}, writeStreams);
159+
160+
const expected = [
161+
chalk`{grey name description} {grey stage when } {grey allow_failure rule}`,
162+
chalk`{blueBright docker-build} {yellow build } on_success false {yellow 2 == 2}`,
163+
];
164+
expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining(expected));
165+
166+
gitDataSpy.mockRestore();
167+
168+
});

0 commit comments

Comments
 (0)