Skip to content

Commit 0765174

Browse files
committed
validate step has tests && tests before solution
Signed-off-by: shmck <[email protected]>
1 parent f6159c4 commit 0765174

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/utils/validateCommits.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
export function validateCommitOrder(positions: string[]): boolean {
44
// loop over positions
55
const errors: number[] = [];
6-
let previous = { level: 0, step: 0 };
7-
let current = { level: 0, step: 0 };
6+
let previous = { level: 0, step: 0, type: "" };
7+
let current = { level: 0, step: 0, type: "" };
88
positions.forEach((position: string, index: number) => {
99
if (position === "INIT") {
1010
if (previous.level !== 0 && previous.step !== 0) {
1111
errors.push(index);
1212
}
13-
current = { level: 0, step: 0 };
13+
current = { level: 0, step: 0, type: "" };
1414
return;
1515
} else {
1616
// @deprecate - remove L|Q
1717
const levelMatch = position.match(/^(?<level>[0-9]+)$/);
1818
// @deprecate - remove S|Q|A
1919
const stepMatch = position.match(
20-
/^(?<level>[0-9]+)\.(?<step>[0-9]+):[T|S]$/
20+
/^(?<level>[0-9]+)\.(?<step>[0-9]+):(?<stepType>[T|S])$/
2121
);
2222
if (levelMatch) {
2323
// allows next level or step
@@ -27,7 +27,7 @@ export function validateCommitOrder(positions: string[]): boolean {
2727
return;
2828
}
2929
const level = Number(levelString);
30-
current = { level, step: 0 };
30+
current = { level, step: 0, type: "" };
3131
} else if (stepMatch) {
3232
// allows next level or step
3333
if (!stepMatch?.groups?.level || !stepMatch?.groups.step) {
@@ -38,13 +38,26 @@ export function validateCommitOrder(positions: string[]): boolean {
3838

3939
const level = Number(levelString);
4040
const step = Number(stepString);
41-
current = { level, step };
41+
const type = stepMatch?.groups.stepType;
42+
43+
const sameStep = previous.level === level && previous.step === step;
44+
45+
if (
46+
// tests should come before the solution
47+
(sameStep && type === "T" && previous.type === "S") ||
48+
// step should have tests
49+
(!sameStep && type === "S")
50+
) {
51+
errors.push(index);
52+
}
53+
current = { level, step, type };
4254
} else {
4355
// error
4456
console.warn(`Invalid commit position: ${position}`);
4557
return;
4658
}
4759
if (
60+
// levels or steps are out of order
4861
current.level < previous.level ||
4962
(current.level === previous.level && current.step < previous.step)
5063
) {

tests/commitOrder.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ describe("commitOrder", () => {
1414
"1",
1515
"1",
1616
"1.1:T",
17+
"1.1:T",
18+
"1.1:S",
1719
"1.1:S",
1820
"1.2:T",
1921
"1.2:S",
@@ -45,5 +47,15 @@ describe("commitOrder", () => {
4547
const result = validateCommitOrder(positions);
4648
expect(result).toBe(false);
4749
});
50+
it("should return false if solution is before step", () => {
51+
const positions = ["INIT", "1", "1.1:S", "1.1:T", "1.2:T"];
52+
const result = validateCommitOrder(positions);
53+
expect(result).toBe(false);
54+
});
55+
it("should return false if solution but no test step", () => {
56+
const positions = ["INIT", "1", "1.1:S", "1.2:T"];
57+
const result = validateCommitOrder(positions);
58+
expect(result).toBe(false);
59+
});
4860
});
4961
});

0 commit comments

Comments
 (0)