Skip to content

Commit 76f6eba

Browse files
committed
[log-and-order] Setup verification to pick up on dynamic SHA
1 parent 3cf8378 commit 76f6eba

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

log_and_order/tests/test_verify.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@
1212
def test_base():
1313
with (
1414
patch("log_and_order.verify.get_head_sha", return_value="a" * 15),
15-
patch("log_and_order.verify.get_head_message", return_value="Hello world"),
15+
patch(
16+
"log_and_order.verify.get_target_commit_message", return_value="Hello world"
17+
),
1618
patch("log_and_order.verify.get_target_commit_sha", return_value="b" * 15),
1719
loader.load(
1820
"specs/base.yml",
1921
"start",
2022
mock_answers={
2123
QUESTION_ONE: "a" * 7,
22-
QUESTION_TWO: "Hello world",
24+
QUESTION_TWO.format(SHA="hi"): "Hello world",
2325
QUESTION_THREE: "b" * 7,
2426
},
2527
) as output,
@@ -30,14 +32,16 @@ def test_base():
3032
def test_wrong_head_sha():
3133
with (
3234
patch("log_and_order.verify.get_head_sha", return_value="a" * 15),
33-
patch("log_and_order.verify.get_head_message", return_value="Hello world"),
35+
patch(
36+
"log_and_order.verify.get_target_commit_message", return_value="Hello world"
37+
),
3438
patch("log_and_order.verify.get_target_commit_sha", return_value="b" * 15),
3539
loader.load(
3640
"specs/base.yml",
3741
"start",
3842
mock_answers={
3943
QUESTION_ONE: "b" * 7,
40-
QUESTION_TWO: "Hello world",
44+
QUESTION_TWO.format(SHA="hi"): "Hello world",
4145
QUESTION_THREE: "b" * 7,
4246
},
4347
) as output,
@@ -52,36 +56,44 @@ def test_wrong_head_sha():
5256
def test_wrong_head_message():
5357
with (
5458
patch("log_and_order.verify.get_head_sha", return_value="a" * 15),
55-
patch("log_and_order.verify.get_head_message", return_value="Hello world"),
59+
patch(
60+
"log_and_order.verify.get_target_commit_message", return_value="Hello world"
61+
),
5662
patch("log_and_order.verify.get_target_commit_sha", return_value="b" * 15),
5763
loader.load(
5864
"specs/base.yml",
5965
"start",
6066
mock_answers={
6167
QUESTION_ONE: "a" * 7,
62-
QUESTION_TWO: "Bye world",
68+
QUESTION_TWO.format(SHA="hi"): "Bye world",
6369
QUESTION_THREE: "b" * 7,
6470
},
6571
) as output,
6672
):
6773
assert_output(
6874
output,
6975
GitAutograderStatus.UNSUCCESSFUL,
70-
[HasExactValueRule.NOT_EXACT.format(question=QUESTION_TWO)],
76+
[
77+
HasExactValueRule.NOT_EXACT.format(
78+
question=QUESTION_TWO.format(SHA="hi")
79+
)
80+
],
7181
)
7282

7383

7484
def test_wrong_target_sha():
7585
with (
7686
patch("log_and_order.verify.get_head_sha", return_value="a" * 15),
77-
patch("log_and_order.verify.get_head_message", return_value="Hello world"),
87+
patch(
88+
"log_and_order.verify.get_target_commit_message", return_value="Hello world"
89+
),
7890
patch("log_and_order.verify.get_target_commit_sha", return_value="b" * 15),
7991
loader.load(
8092
"specs/base.yml",
8193
"start",
8294
mock_answers={
8395
QUESTION_ONE: "a" * 7,
84-
QUESTION_TWO: "Hello world",
96+
QUESTION_TWO.format(SHA="hi"): "Hello world",
8597
QUESTION_THREE: "a" * 7,
8698
},
8799
) as output,

log_and_order/verify.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
from typing import Tuple
23

34
from git_autograder import (
@@ -11,6 +12,9 @@
1112

1213
QUESTION_ONE = "What is the SHA of the commit HEAD points to?"
1314
QUESTION_TWO = "What is the commit message of the commit {SHA}?"
15+
QUESTION_TWO_REGEX = re.compile(
16+
"^What is the commit message of the commit ([\\d\\w]+)\\?$"
17+
)
1418
QUESTION_THREE = (
1519
'What is the SHA of the commit with the commit message "Rewrite the comments"?'
1620
)
@@ -46,6 +50,20 @@ def get_head_message(exercise: GitAutograderExercise) -> str:
4650
return ensure_str(exercise.repo.repo.head.commit.message).strip()
4751

4852

53+
def get_target_commit_message(exercise: GitAutograderExercise, sha: str) -> str:
54+
target_commit = next(
55+
(
56+
c
57+
for c in exercise.repo.repo.iter_commits(all=True)
58+
if c.hexsha.strip() == sha
59+
),
60+
None,
61+
)
62+
if target_commit is None:
63+
raise Exception(f"Could not find commit with SHA '{sha}'")
64+
return ensure_str(target_commit.message).strip()
65+
66+
4967
def get_target_commit_sha(exercise: GitAutograderExercise) -> str:
5068
target_msg = "Rewrite the comments"
5169
target_commit = next(
@@ -65,7 +83,13 @@ def verify(exercise: GitAutograderExercise) -> GitAutograderOutput:
6583
head_sha = get_head_sha(exercise)
6684
head_sha_short = head_sha[:7]
6785

68-
head_message = get_head_message(exercise)
86+
# Get second question and find the SHA hash to pick
87+
second_question = exercise.answers.questions[1]
88+
sha_match = QUESTION_TWO_REGEX.match(second_question)
89+
assert sha_match is not None
90+
sha = sha_match.group(1)
91+
92+
target_message = get_target_commit_message(exercise, sha)
6993

7094
target_sha = get_target_commit_sha(exercise)
7195
target_sha_short = target_sha[:7]
@@ -75,9 +99,9 @@ def verify(exercise: GitAutograderExercise) -> GitAutograderOutput:
7599
NotEmptyRule(),
76100
OneOfValueRule(head_sha, head_sha_short),
77101
).add_validation(
78-
QUESTION_TWO,
102+
QUESTION_TWO.format(SHA=sha),
79103
NotEmptyRule(),
80-
HasExactValueRule(head_message),
104+
HasExactValueRule(target_message),
81105
).add_validation(
82106
QUESTION_THREE, NotEmptyRule(), OneOfValueRule(target_sha, target_sha_short)
83107
).validate()

0 commit comments

Comments
 (0)