Skip to content

Commit d5a09c7

Browse files
[glossary-branch-rename] Improve verify logic and update tests (#263)
Fixes #262
1 parent 34e4d90 commit d5a09c7

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

glossary_branch_rename/test_verify.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from repo_smith.repo_smith import RepoSmith
1111

1212
from .verify import (
13+
NO_RENAME_EVIDENCE,
14+
RESET_MESSAGE,
1315
verify,
1416
STU_LOCAL_PRESENT,
1517
STU_REMOTE_PRESENT,
@@ -126,3 +128,20 @@ def test_remote_old_branch_still_exists():
126128

127129
output = test.run()
128130
assert_output(output, GitAutograderStatus.UNSUCCESSFUL, [STU_REMOTE_PRESENT])
131+
132+
def test_no_rename_evidence():
133+
with base_setup() as (test, rs):
134+
rs.git.checkout(EXPECTED_NEW_BRANCH_NAME, branch=True)
135+
rs.git.push("origin", EXPECTED_NEW_BRANCH_NAME)
136+
rs.git.branch(BRANCH_TO_RENAME, delete=True)
137+
rs.git.push("origin", f":{BRANCH_TO_RENAME}")
138+
139+
output = test.run()
140+
assert_output(
141+
output,
142+
GitAutograderStatus.UNSUCCESSFUL,
143+
[
144+
NO_RENAME_EVIDENCE,
145+
RESET_MESSAGE,
146+
],
147+
)

glossary_branch_rename/verify.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
from git_autograder import (
23
GitAutograderOutput,
34
GitAutograderExercise,
@@ -11,6 +12,32 @@
1112
RENAMED_LOCAL_MISSING = f"Local branch {RENAMED_BRANCH} is missing."
1213
STU_REMOTE_PRESENT = f"Remote branch {STU_BRANCH} still exists."
1314
RENAMED_REMOTE_MISSING = f"Remote branch {RENAMED_BRANCH} is missing."
15+
NO_RENAME_EVIDENCE = f"Local branch '{STU_BRANCH}' was not renamed to '{RENAMED_BRANCH}'!"
16+
RESET_MESSAGE = 'If needed, reset the repository using "gitmastery progress reset" and start again.'
17+
18+
19+
def branch_has_rename_evidence(
20+
exercise: GitAutograderExercise, new_branch: str, old_branch: str
21+
) -> bool:
22+
"""Performs a DFS on the branch renames starting with STU till S-to-Z.
23+
24+
This is necessary since the renames could be performed in parts:
25+
26+
STU -> S-to-U -> S-to-Z
27+
"""
28+
branch = exercise.repo.branches.branch(new_branch)
29+
30+
rename_regex = re.compile("^renamed refs/heads/(.+) to refs/heads/(.+)$")
31+
for entry in branch.reflog[::-1]:
32+
match_group = rename_regex.match(entry.message)
33+
if match_group is None:
34+
continue
35+
original = match_group.group(1)
36+
new = match_group.group(2)
37+
if original == old_branch:
38+
old_branch = new
39+
40+
return old_branch == new_branch
1441

1542

1643
def verify(exercise: GitAutograderExercise) -> GitAutograderOutput:
@@ -42,6 +69,9 @@ def verify(exercise: GitAutograderExercise) -> GitAutograderOutput:
4269

4370
if comments:
4471
raise exercise.wrong_answer(comments)
72+
73+
if not branch_has_rename_evidence(exercise, RENAMED_BRANCH, STU_BRANCH):
74+
raise exercise.wrong_answer([NO_RENAME_EVIDENCE, RESET_MESSAGE])
4575

4676
return exercise.to_output(
4777
["Nice work renaming the branch locally and on the remote!"],

0 commit comments

Comments
 (0)