|
| 1 | +import json |
| 2 | +from pathlib import Path |
| 3 | +from typing import Optional |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +import pytest |
| 7 | +from git.repo import Repo |
| 8 | +from git_autograder import ( |
| 9 | + GitAutograderExercise, |
| 10 | + GitAutograderTestLoader, |
| 11 | + GitAutograderWrongAnswerException, |
| 12 | + assert_output, |
| 13 | +) |
| 14 | +from git_autograder.status import GitAutograderStatus |
| 15 | + |
| 16 | +from ..verify import IMPROPER_GH_CLI_SETUP, NO_FORK_FOUND, NOT_GIT_MASTERY_FORK, verify |
| 17 | + |
| 18 | +REPOSITORY_NAME = "fork-repo" |
| 19 | + |
| 20 | +loader = GitAutograderTestLoader(__file__, REPOSITORY_NAME, verify) |
| 21 | + |
| 22 | +# NOTE: This exercise is a special case where we do not require repo-smith. Instead, |
| 23 | +# we directly mock function calls to verify that all branches are covered for us. |
| 24 | + |
| 25 | + |
| 26 | +# TODO: The current tooling isn't mature enough to handle mock GitAutograderExercise in |
| 27 | +# cases like these. We would ideally need some abstraction rather than creating our own. |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def exercise(tmp_path: Path) -> GitAutograderExercise: |
| 32 | + repo_dir = tmp_path / "ignore-me" |
| 33 | + repo_dir.mkdir() |
| 34 | + |
| 35 | + Repo.init(repo_dir) |
| 36 | + with open(tmp_path / ".gitmastery-exercise.json", "a") as config_file: |
| 37 | + config_file.write( |
| 38 | + json.dumps( |
| 39 | + { |
| 40 | + "exercise_name": "remote-control", |
| 41 | + "tags": [], |
| 42 | + "requires_git": True, |
| 43 | + "requires_github": True, |
| 44 | + "base_files": {}, |
| 45 | + "exercise_repo": { |
| 46 | + "repo_type": "local", |
| 47 | + "repo_name": "ignore-me", |
| 48 | + "init": True, |
| 49 | + "create_fork": None, |
| 50 | + "repo_title": None, |
| 51 | + }, |
| 52 | + "downloaded_at": None, |
| 53 | + } |
| 54 | + ) |
| 55 | + ) |
| 56 | + |
| 57 | + exercise = GitAutograderExercise(exercise_path=tmp_path) |
| 58 | + return exercise |
| 59 | + |
| 60 | + |
| 61 | +def test_pass(exercise: GitAutograderExercise): |
| 62 | + with ( |
| 63 | + patch("fork_repo.verify.get_username", return_value="dummy"), |
| 64 | + patch("fork_repo.verify.has_fork", return_value=True), |
| 65 | + patch("fork_repo.verify.is_parent_git_mastery", return_value=True), |
| 66 | + ): |
| 67 | + output = verify(exercise) |
| 68 | + assert_output(output, GitAutograderStatus.SUCCESSFUL) |
| 69 | + |
| 70 | + |
| 71 | +def test_improper_gh_setup(exercise: GitAutograderExercise): |
| 72 | + with ( |
| 73 | + patch("fork_repo.verify.get_username", return_value=None), |
| 74 | + patch("fork_repo.verify.has_fork", return_value=True), |
| 75 | + patch("fork_repo.verify.is_parent_git_mastery", return_value=True), |
| 76 | + pytest.raises(GitAutograderWrongAnswerException) as exception, |
| 77 | + ): |
| 78 | + verify(exercise) |
| 79 | + |
| 80 | + assert exception.value.message == [IMPROPER_GH_CLI_SETUP] |
| 81 | + |
| 82 | + |
| 83 | +def test_no_fork(exercise: GitAutograderExercise): |
| 84 | + with ( |
| 85 | + patch("fork_repo.verify.get_username", return_value="dummy"), |
| 86 | + patch("fork_repo.verify.has_fork", return_value=False), |
| 87 | + patch("fork_repo.verify.is_parent_git_mastery", return_value=True), |
| 88 | + pytest.raises(GitAutograderWrongAnswerException) as exception, |
| 89 | + ): |
| 90 | + verify(exercise) |
| 91 | + |
| 92 | + assert exception.value.message == [NO_FORK_FOUND] |
| 93 | + |
| 94 | + |
| 95 | +def test_not_right_parent(exercise: GitAutograderExercise): |
| 96 | + with ( |
| 97 | + patch("fork_repo.verify.get_username", return_value="dummy"), |
| 98 | + patch("fork_repo.verify.has_fork", return_value=True), |
| 99 | + patch("fork_repo.verify.is_parent_git_mastery", return_value=False), |
| 100 | + pytest.raises(GitAutograderWrongAnswerException) as exception, |
| 101 | + ): |
| 102 | + verify(exercise) |
| 103 | + |
| 104 | + assert exception.value.message == [NOT_GIT_MASTERY_FORK] |
0 commit comments