Skip to content

Commit 6a88d56

Browse files
committed
[mix-messy-graph] Implement verification
1 parent 2f995c6 commit 6a88d56

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

mix_messy_graph/verify.py

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,94 @@
33
GitAutograderExercise,
44
GitAutograderStatus,
55
)
6+
from itertools import zip_longest
7+
8+
SQUASH_NOT_USED = (
9+
"You should be using squash merges for both 'feature-search' and 'feature-delete'"
10+
)
11+
WRONG_ORDER_OF_MERGING = "You need to merge 'feature-search' before 'feature-delete'"
12+
FEATURE_SEARCH_MERGE_MESSAGE = (
13+
"The message for merging 'feature-search' should be 'Add the search feature'"
14+
)
15+
FEATURE_DELETE_MERGE_MESSAGE = (
16+
"The message for merging 'feature-delete' should be 'Add the delete feature'"
17+
)
18+
MISMATCH_COMMIT_MESSAGE = (
19+
"Expected commit message of '{expected}', got '{given}' instead."
20+
)
21+
22+
FEATURE_SEARCH_BRANCH_STILL_EXISTS = "Branch 'feature-search' still exists."
23+
FEATURE_DELETE_BRANCH_STILL_EXISTS = "Branch 'feature-delete' still exists."
24+
LIST_BRANCH_STILL_EXISTS = "Branch 'list' still exists."
25+
26+
MISSING_FEATURES_FILE = "You are missing 'features.md'!"
27+
FEATURES_FILE_CONTENT_INVALID = "Contents of 'features.md' is not valid! Try again!"
28+
29+
EXPECTED_COMMIT_MESSAGES = [
30+
"Add features.md",
31+
"Mention feature for creating books",
32+
"Fix phrasing of heading",
33+
"Add the search feature",
34+
"Add the delete feature",
35+
]
36+
37+
EXPECTED_LINES = [
38+
"# Features",
39+
"## Create Book",
40+
"Allows creating one book at a time.",
41+
"## Searching for Books",
42+
"Allows searching for books by keywords.",
43+
"Works only for book titles.",
44+
"## Deleting Books",
45+
"Allows deleting books.",
46+
]
47+
48+
49+
def ensure_str(val) -> str:
50+
if isinstance(val, bytes):
51+
return val.decode("utf-8", errors="replace").strip()
52+
return str(val).strip()
653

754

855
def verify(exercise: GitAutograderExercise) -> GitAutograderOutput:
9-
# INSERT YOUR GRADING CODE HERE
56+
main_branch = exercise.repo.branches.branch("main")
57+
merge_commits = [c for c in main_branch.commits if len(c.parents) > 1]
58+
if len(merge_commits) > 0:
59+
raise exercise.wrong_answer([SQUASH_NOT_USED])
60+
61+
commit_messages = [ensure_str(c.commit.message) for c in main_branch.commits][::-1]
62+
for expected, given in zip_longest(EXPECTED_COMMIT_MESSAGES, commit_messages):
63+
if expected != given:
64+
raise exercise.wrong_answer(
65+
[
66+
MISMATCH_COMMIT_MESSAGE.format(
67+
expected=expected, given=(given or "<Missing commit>")
68+
)
69+
]
70+
)
71+
72+
feature_search_branch = exercise.repo.branches.branch_or_none("feature-search")
73+
feature_delete_branch = exercise.repo.branches.branch_or_none("feature-delete")
74+
list_branch = exercise.repo.branches.branch_or_none("list")
75+
branch_exists_messages = []
76+
if feature_search_branch is not None:
77+
branch_exists_messages.append(FEATURE_SEARCH_BRANCH_STILL_EXISTS)
78+
if feature_delete_branch is not None:
79+
branch_exists_messages.append(FEATURE_DELETE_BRANCH_STILL_EXISTS)
80+
if list_branch is not None:
81+
branch_exists_messages.append(LIST_BRANCH_STILL_EXISTS)
82+
83+
if branch_exists_messages:
84+
raise exercise.wrong_answer(branch_exists_messages)
85+
86+
with exercise.repo.files.file_or_none("features.md") as features_file:
87+
if features_file is None:
88+
raise exercise.wrong_answer([MISSING_FEATURES_FILE])
89+
90+
contents = [
91+
line.strip() for line in features_file.readlines() if line.strip() != ""
92+
]
93+
if contents != EXPECTED_LINES:
94+
raise exercise.wrong_answer([FEATURES_FILE_CONTENT_INVALID])
1095

1196
return exercise.to_output([], GitAutograderStatus.SUCCESSFUL)

0 commit comments

Comments
 (0)