|
| 1 | +from os import EX_TEMPFAIL |
1 | 2 | from git_autograder import ( |
2 | 3 | GitAutograderOutput, |
3 | 4 | GitAutograderExercise, |
4 | 5 | GitAutograderStatus, |
5 | 6 | ) |
6 | 7 |
|
| 8 | +MISSING_DEVELOPMENT_BRANCH = "You are missing the 'development' branch!" |
| 9 | +WRONG_BRANCH_POINT = "You did not branch from the commit with tag v1.0!" |
| 10 | +FEATURE_SEARCH_BRANCH_STILL_EXISTS = ( |
| 11 | + "Branch 'feature-search' still exists! Remember to delete it after merging!" |
| 12 | +) |
| 13 | +FEATURE_DELETE_BRANCH_STILL_EXISTS = ( |
| 14 | + "Branch 'feature-delete' still exists! Remember to delete it after merging!" |
| 15 | +) |
| 16 | +LIST_BRANCH_STILL_EXISTS = ( |
| 17 | + "Branch 'list' still exists! Remember to rename it to 'feature-list'!" |
| 18 | +) |
| 19 | +FEATURE_LIST_BRANCH_MISSING = "Branch 'feature-list' is missing. Did you misspell it?" |
| 20 | +MERGE_FEATURE_SEARCH_FIRST = "You need to merge 'feature-search' first!" |
| 21 | +MERGE_FEATURE_DELETE_SECOND = "You need to merge 'feature-delete' second!" |
| 22 | +MISSING_FEATURES_FILE = "You are missing 'features.md'!" |
| 23 | +FEATURES_FILE_CONTENT_INVALID = "Contents of 'features.md' is not valid! Try again!" |
| 24 | + |
| 25 | +RESET_MESSAGE = 'Reset the repository using "gitmastery progress reset" and start again' |
| 26 | + |
| 27 | + |
| 28 | +EXPECTED_LINES = [ |
| 29 | + "# Features", |
| 30 | + "## Create Book", |
| 31 | + "Allows creating one book at a time.", |
| 32 | + "## Searching for Books", |
| 33 | + "Allows searching for books by keywords.", |
| 34 | + "Works only for book titles.", |
| 35 | + "## Deleting Books", |
| 36 | + "Allows deleting books.", |
| 37 | +] |
| 38 | + |
7 | 39 |
|
8 | 40 | def verify(exercise: GitAutograderExercise) -> GitAutograderOutput: |
9 | | - # INSERT YOUR GRADING CODE HERE |
| 41 | + development_branch = exercise.repo.branches.branch_or_none("development") |
| 42 | + if development_branch is None: |
| 43 | + raise exercise.wrong_answer([MISSING_DEVELOPMENT_BRANCH]) |
| 44 | + |
| 45 | + tag_commit = exercise.repo.repo.tags["v1.0"].commit |
| 46 | + development_commit = development_branch.latest_commit |
| 47 | + branched_from_tag_hexsha = exercise.repo.repo.git.merge_base( |
| 48 | + tag_commit.hexsha, development_commit.hexsha |
| 49 | + ) |
| 50 | + # Alternative is to use reflog which states where the branch is created from |
| 51 | + if branched_from_tag_hexsha != tag_commit.hexsha: |
| 52 | + # Not branched from this but maybe somewhere earlier |
| 53 | + raise exercise.wrong_answer([WRONG_BRANCH_POINT]) |
| 54 | + |
| 55 | + reflog = development_branch.reflog |
| 56 | + merge_logs = [log for log in reflog if "merge" in log.action] |
| 57 | + # reflog returns it in reverse order |
| 58 | + has_feature_delete_commit_merge = ( |
| 59 | + len(merge_logs) >= 1 |
| 60 | + and merge_logs[0].action == "commit (merge)" |
| 61 | + and "feature-delete" in merge_logs[0].message |
| 62 | + ) |
| 63 | + has_feature_search_merge = ( |
| 64 | + len(merge_logs) >= 2 and merge_logs[1].action == "merge feature-search" |
| 65 | + ) |
| 66 | + if not has_feature_search_merge: |
| 67 | + raise exercise.wrong_answer([MERGE_FEATURE_SEARCH_FIRST, RESET_MESSAGE]) |
| 68 | + |
| 69 | + if not has_feature_delete_commit_merge: |
| 70 | + raise exercise.wrong_answer([MERGE_FEATURE_DELETE_SECOND, RESET_MESSAGE]) |
| 71 | + |
| 72 | + feature_list_branch = exercise.repo.branches.branch_or_none("feature-list") |
| 73 | + list_branch = exercise.repo.branches.branch_or_none("list") |
| 74 | + if list_branch is not None and feature_list_branch is None: |
| 75 | + raise exercise.wrong_answer([LIST_BRANCH_STILL_EXISTS]) |
| 76 | + elif list_branch is None and feature_list_branch is None: |
| 77 | + raise exercise.wrong_answer([FEATURE_LIST_BRANCH_MISSING]) |
| 78 | + |
| 79 | + with exercise.repo.files.file_or_none("features.md") as features_file: |
| 80 | + if features_file is None: |
| 81 | + raise exercise.wrong_answer([MISSING_FEATURES_FILE]) |
| 82 | + |
| 83 | + contents = [ |
| 84 | + line.strip() for line in features_file.readlines() if line.strip() != "" |
| 85 | + ] |
| 86 | + if contents != EXPECTED_LINES: |
| 87 | + raise exercise.wrong_answer([FEATURES_FILE_CONTENT_INVALID]) |
10 | 88 |
|
11 | | - return exercise.to_output([], GitAutograderStatus.SUCCESSFUL) |
| 89 | + return exercise.to_output( |
| 90 | + [ |
| 91 | + "Great work using all of the concepts you've learnt about branching to mix the messy documentation!" |
| 92 | + ], |
| 93 | + GitAutograderStatus.SUCCESSFUL, |
| 94 | + ) |
0 commit comments